public bool isCellIlluminated(IPixelReadable image, int x, int y, float blackBrightnessLowerBound, float blackBrightnessUpperBound, int plusMinusSpread, int brightnessErrorCount)
    {
        var cellColor        = image.getPixel(x, y);
        var cellColorPlus    = image.getPixel(x + plusMinusSpread, y);
        var cellColorMinus   = image.getPixel(x - plusMinusSpread, y);
        var upCellColorPlus  = image.getPixel(x, y + plusMinusSpread);
        var upCellColorMinus = image.getPixel(x, y - plusMinusSpread);
        var brightness       = cellBrightness(cellColor);
        var brightness2      = cellBrightness(cellColorPlus);
        var brightness3      = cellBrightness(cellColorMinus);
        var brightness4      = cellBrightness(upCellColorMinus);
        var brightness5      = cellBrightness(upCellColorMinus);
        int brightnessCount  = 0;

        brightnessCount += (brightness <blackBrightnessLowerBound || brightness> blackBrightnessUpperBound) ? 0 : 1;
        brightnessCount += (brightness2 <blackBrightnessLowerBound || brightness2> blackBrightnessUpperBound) ? 0 : 1;
        brightnessCount += (brightness3 <blackBrightnessLowerBound || brightness3> blackBrightnessUpperBound) ? 0 : 1;
        brightnessCount += (brightness4 <blackBrightnessLowerBound || brightness4> blackBrightnessUpperBound) ? 0 : 1;
        brightnessCount += (brightness5 <blackBrightnessLowerBound || brightness5> blackBrightnessUpperBound) ? 0 : 1;
        if (brightnessCount < brightnessErrorCount || y > 971)//allow 2 errors, 971 is height of top row
        {
            return(false);
        }
        else
        {
            return(true);
        }
    }
 public void updateGridWithImage(
     IPixelReadable image,
     Grid grid,
     int startingX,
     int startingY,
     int cellWidth,
     int cellHeight,
     int columnCount,
     int rowCount,
     float blackBrightnessLowerBound,
     float blackBrightnessUpperBound,
     int plusMinusSpread,
     int brightnessErrorCount,
     bool areCellsInited = false
     )
 {
     //initing cells if not already correctly allocated
     if (!areCellsInited)
     {
         initCells(rowCount, columnCount);
     }
     for (int x = 0; x < columnCount; x++)
     {
         for (int y = 0; y < rowCount; y++)
         {
             int xPixelPos = startingX + x * cellWidth;
             int yPixelPos = startingY + y * cellHeight;
             cells[rowCount - y - 1][x] = isCellIlluminated(image, xPixelPos, yPixelPos, blackBrightnessLowerBound, blackBrightnessUpperBound, plusMinusSpread, brightnessErrorCount);
         }
     }
     grid.cells = cells;
 }
    public List <int> getUpNextColors(IPixelReadable image, int topX, int topY, int topX2, int topY2, int cellHeight, int rowCount, int secondSquareHeightOffset, int secondSquareHeightOffset2)
    {
        var retr = new List <int>();
        //get top one
        int     yPixelPos1 = (topY);
        int     yPixelPos2 = (yPixelPos1 + secondSquareHeightOffset);
        Color32 cell1Color = image.getPixel(topX, 1080 - yPixelPos1);
        Color32 cell2Color = image.getPixel(topX, 1080 - yPixelPos2);
        int     color1Name = getClosestColor(cell1Color);
        int     color2Name = getClosestColor(cell2Color);

        if (color1Name == -1 && color2Name == -1)
        {
            retr.Add(-1);
        }
        else if (color1Name == -1)
        {
            retr.Add(color2Name);
        }
        else if (color2Name == -1)
        {
            retr.Add(color1Name);
        }
        else
        {
            retr.Add(color1Name);
        }
        //get rest
        for (int y = 0; y < rowCount; y++)
        {
            yPixelPos1 = (topY2 + cellHeight * y);
            yPixelPos2 = (yPixelPos1 + secondSquareHeightOffset2);
            cell1Color = image.getPixel(topX2, 1080 - yPixelPos1);
            cell2Color = image.getPixel(topX2, 1080 - yPixelPos2);
            color1Name = getClosestColor(cell1Color);
            color2Name = getClosestColor(cell2Color);
            if (color1Name == -1 && color2Name == -1)
            {
                retr.Add(-1);
            }
            else if (color1Name == -1)
            {
                retr.Add(color2Name);
            }
            else if (color2Name == -1)
            {
                retr.Add(color1Name);
            }
            else
            {
                retr.Add(color1Name);
            }
        }
        return(retr);
    }
    public void updateGridIncomingDangerousPieces(Grid grid,
                                                  IPixelReadable image,
                                                  int incomingDangerousPiecesX,
                                                  int incomingDangerousPiecesStartY,
                                                  int incomingDangerousPiecesEndY,
                                                  float blackBrightnessLowerBound,
                                                  int boxHeightDivisor,
                                                  int plusMinusSpread)
    {
        int numberOfColoredPieces = 0;

        for (int i = incomingDangerousPiecesStartY; i < incomingDangerousPiecesEndY; i++)
        {
            numberOfColoredPieces += isCellIlluminated(image, incomingDangerousPiecesX, i, blackBrightnessLowerBound, 1.0f, plusMinusSpread, 4)?1:0;
        }
        grid.incomingDangerousPieces = numberOfColoredPieces / boxHeightDivisor;
    }
    void Start()
    {
        currentState = ProgramState.ManualControl;

        WebCamTexture webTex = new WebCamTexture(WebCamTexture.devices[1].name);

        webTex.Play();
        texReader = new WebCamTextureReader(webTex);
        //texReader = new ScreenShotTextureReader();


        parser = new ImageParser();

        if (!isTraining)
        {
            serialPort = new SerialPort("COM4");
            serialPort.Open();
        }
        if (isTraining)
        {
            threader.messageQueue = new ConcurrentQueue <string>();
            ThreadStart start = new ThreadStart(threader.runTuner);
            Thread      t     = new Thread(start);
            t.Start();
        }

        //var hidden = new float[][]{new float[]{-0.06903733f,-0.1204211f,-0.1170338f,0.03330595f,-0.0929533f,0.1004087f,0.0253228f,-0.07049648f,0.1561288f,-0.2057665f},new float[]{-0.1833734f,0.2184264f,0.04389104f,0.03153691f,0.1989222f,-0.004495893f,-0.1988735f,-0.05761508f,-0.03419961f,-0.1252105f},new float[]{-0.1174469f,0.06082164f,0.1511514f,-0.07508145f,0.1869553f,0.2032531f,0.08316454f,-0.07734036f,0.160016f,-0.2196597f},new float[]{-0.04042799f,0.12335f,-0.2520556f,-0.04117061f,0.1304957f,-0.01909676f,0.07300739f,0.05919087f,0.02102444f,-0.09814784f},new float[]{-0.1872339f,-0.1362969f,0.1683453f,-0.01297639f,0.07160946f,0.04766374f,0.1250072f,0.1642009f,0.09859828f,-0.07657336f},new float[]{0.0426106f,0.1403711f,-0.1820064f,-0.06172101f,0.1371788f,0.03265697f,-0.0272984f,-0.09486318f,0.08233191f,-0.02668947f}};
        //var output = new float[]{0.1153422f,-0.08480756f,-0.03375334f,0.1751222f,0.1993017f,0.1691856f};
        //var hidden = new float[][]{new float[]{0.1062265f,0.07877557f,0.1296688f,0.1801857f,0.155361f,0.08026402f,5.229413E-05f,0.0513033f,-0.04129005f,0.1580591f},new float[]{0.07237879f,-0.08428323f,-0.1284456f,0.1626194f,0.1416797f,0.2010917f,-0.04338667f,0.1170366f,-0.05595298f,0.04115711f},new float[]{-0.0725678f,0.0803528f,-0.1890458f,-0.01033701f,-0.1815529f,0.0845101f,0.04547426f,0.1759871f,0.1553514f,0.05655741f},new float[]{-0.03995487f,0.186867f,-0.1791575f,-0.05288832f,0.009598507f,0.09033263f,0.008156282f,0.09895851f,-0.02606005f,0.0123509f},new float[]{-0.1431304f,0.03786864f,-0.1379245f,-0.1383549f,-0.2064656f,0.05489919f,-0.1603064f,-0.1823927f,0.1994664f,0.0003644727f},new float[]{-0.1313158f,-0.01504898f,-0.07030262f,-0.186277f,0.1034396f,-0.1876001f,0.06108657f,0.1755974f,0.2439985f,-0.1074236f}};
        //var output = new float[]{-0.1634287f,0.1086006f,0.07287102f,0.1285644f,0.06330933f,-0.145795f};
        //var hidden = new float[][]{new float[]{0.1493298f,0.01999732f,0.1369107f,0.1883943f,-0.1532951f,0.1624803f,0.1441682f,0.08189679f,-0.1258692f,0.1400606f},new float[]{0.1411773f,0.1465782f,-0.1582646f,0.09787317f,-0.1013228f,0.1742776f,-0.09175301f,-0.04335905f,-0.1535288f,-0.1726231f},new float[]{0.1423891f,0.1568068f,-0.1309288f,-0.16624f,-0.179458f,-0.04458414f,-0.07536326f,0.1750511f,0.06456359f,0.1614914f},new float[]{-0.06910742f,0.1849477f,-0.1562149f,-0.02061264f,0.09389784f,0.1115483f,0.1832086f,0.1280793f,0.06631156f,0.007651481f},new float[]{0.1185598f,0.1117894f,-0.1006476f,0.01884162f,0.04223451f,-0.06615675f,-0.03255002f,-0.1742624f,0.06808273f,-0.08383059f},new float[]{0.1309216f,-0.005340355f,0.1106042f,0.07571947f,-0.07535377f,0.204163f,-0.02816167f,-0.1554123f,0.1706816f,-0.04058816f}};
        //var output = new float[]{-0.07528235f,0.04620438f,0.04189136f,0.08600978f,0.1298999f,-0.2040185f};
        var hidden = new float[][] { new float[] { 0.194341f, 0.1434416f, 0.0713096f, 0.1653562f, 0.1112117f, 0.1232969f, -0.01119053f, 0.03627405f, -0.1254188f, 0.01899251f, -0.05036948f }, new float[] { 0.05425321f, -0.1496129f, 0.0816373f, 0.1543122f, -0.1064251f, 0.1125381f, 0.08268484f, 0.1927576f, 0.2012903f, 0.2134653f, 0.05713469f }, new float[] { 0.03931943f, 0.1033663f, -0.3848397f, -0.07148883f, -0.03369519f, 0.06521149f, 0.01828165f, -0.1288408f, -0.01749198f, -0.07911009f, 0.06041071f }, new float[] { -0.08144363f, 0.101594f, -0.175469f, -0.1271559f, 0.1225737f, -0.08820423f, -0.1741386f, 0.1383524f, 0.07709136f, -0.08639109f, -0.06383685f }, new float[] { -0.04410164f, 0.1367995f, 0.05679908f, 0.1721974f, -0.1239951f, -0.2085233f, 0.02573769f, -0.02817038f, -0.07766398f, -0.1140089f, -0.08351984f }, new float[] { -0.1507141f, 0.008166309f, 0.1841051f, -0.0453295f, 0.03497292f, -0.08636673f, -0.02734959f, -0.1324452f, -0.00981016f, -0.167251f, -0.07732157f } };
        var output = new float[] { -0.0354594f, 0.09784601f, 0.1386255f, 0.06091305f, 0.01426123f, -0.05693473f };

        ai = new AI(hidden, output);

        InitGrid();
        addNewPiece(0);

        provider = new ImageProvider();
        drawGrid(grid1);
    }