예제 #1
0
        } // End FindSpots

        private WeightCenter CalculateWeightCenter(byte[, ,] source, int x, int y, int width,
                                                   int height, FilterColors color)
        {
            int sum_x = 0, sum_y = 0, n = 0;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (source[i + y, j + x, (byte)color] == 255)
                    {
                        sum_x += j;
                        sum_y += i;
                        n++;
                    }
                }
            }

            WeightCenter w;

            if (n > 0)
            {
                w = new WeightCenter(
                    x + (int)sum_x / n,
                    y + (int)sum_y / n, y);
            }
            else
            {
                w = WeightCenter.Empty;
            }
            return(w);
        }
예제 #2
0
        private void RetrieveColorInformation(FilterColors color, byte[,,] original,
                                              WeightCenter colorSpotLocation, Point3D whiteSpotLocation, Point3D blackSpotLocation)
        {
            if (WhiteSpot == Color.Empty)
            {
                WhiteSpot = ImageProcessing.GetColor(
                    original,
                    new WeightCenter((int)whiteSpotLocation.X, (int)whiteSpotLocation.Y, 0), 0);
            }

            if (BlackSpot == Color.Empty)
            {
                BlackSpot = ImageProcessing.GetColor(
                    original,
                    new WeightCenter((int)blackSpotLocation.X, (int)blackSpotLocation.Y, 0), 0);
            }

            Color colorSpot = ImageProcessing.GetColor(original, colorSpotLocation, 0);

            switch (color)
            {
            case FilterColors.B:
                if (BlueSpot == Color.Empty)
                {
                    BlueSpot = colorSpot;
                }
                break;

            case FilterColors.G:
                if (GreenSpot == Color.Empty)
                {
                    GreenSpot = colorSpot;
                }
                break;

            case FilterColors.R:
                if (RedSpot == Color.Empty)
                {
                    RedSpot = colorSpot;
                }
                break;

            case FilterColors.Y:
                if (YellowSpot == Color.Empty)
                {
                    YellowSpot = colorSpot;
                }
                break;

            default:
                break;
            }

            RecalcVectors();
            RotateColorVectors();
        }
예제 #3
0
        /// <summary>
        /// Check if side number corresponds to the color.
        /// </summary>
        /// <param name="sideNumber">Side Number</param>
        /// <param name="color">Color</param>
        /// <returns>True if pass</returns>
        private bool CheckSideNumberColor(int sideNumber, FilterColors color)
        {
            // Check the number of side
            if (color == FilterColors.R) // Check for Red side
            {
                if ((sideNumber != 6) && (sideNumber != 7) && (sideNumber != 10))
                {
                    return(false);
                }
            }

            if (color == FilterColors.Y) // Check for Yellow side
            {
                if ((sideNumber != 1) && (sideNumber != 5) && (sideNumber != 11))
                {
                    return(false);
                }
            }

            if (color == FilterColors.G)
            {
                if ((sideNumber != 8) && (sideNumber != 9) && (sideNumber != 12))
                {
                    //MessageBox.Show("sidenumber = " + sideNumber.ToString() + " color " + color.ToString());
                    return(false);
                }
            }

            if (color == FilterColors.B)
            {
                if ((sideNumber != 2) && (sideNumber != 3) && (sideNumber != 4))
                {
                    //MessageBox.Show("sidenumber = " + sideNumber.ToString() + " color " + color.ToString());
                    return(false);
                }
            }

            return(true);
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <param name="elements"></param>
        /// <param name="i"></param>
        /// <param name="color"></param>
        /// <param name="isRows"></param>
        /// <returns></returns>
        private static int ScanPixels(byte[, ,] source, int elements, int i, FilterColors color, bool isRows)
        {
            int j = 0;

            switch (isRows)
            {
            case true:
                // Find pixels and stop until first founded on the current row
                while ((j < elements) && (source[i, j, (int)color] != 255))
                {
                    j++;
                }
                break;

            case false:
                // Find pixels and stop until first founded on the current col
                while ((j < elements) && (source[j, i, (int)color] != 255))
                {
                    j++;
                }
                break;
            }
            return(j);
        }
예제 #5
0
        // Returns source parameters - descriptor, destination parameters - projection
        private Point3D[][] DetectFace(byte[, ,] data, byte[, ,] original, List <WeightCenter> weights, int rows, int cols, FilterColors color)
        {
            // Five points from the sides descriptor - destination for homography
            Point3D[] destP            = dataMouse.ColorSpotsSide01;
            Point3D[] destPars_fourthP = Geometry.homography_calc_pars(
                new Point3D[] { destP[0], destP[1], destP[2], destP[4] });
            Point3D[] destPars_fifthP = Geometry.homography_calc_pars(
                new Point3D[] { destP[0], destP[1], destP[2], destP[3] });
            Point3D[][] destPars = new Point3D[][] { destPars_fourthP, destPars_fifthP };
            double      distance;
            double      distanceMin = 10000;
            //int jMin = 0;
            //int[] indexes = new int[5];
            List <Point3D> homographyRes, weightListCurrent;
            List <Point3D> homographyMin = null, weightListMin = null;

            Point3D[] hmMinSourcePars = null, hmMinDestPars = null;

            // Convert the WeightCenter struct to List of Point3D
            List <Point3D> weightsList = new List <Point3D>(15);

            foreach (WeightCenter item in weights)
            {
                weightsList.Add(new Point3D(item.x, item.y, 0));
            }

            for (int i = 0; i < 6; i++)
            {
                weightListCurrent = new List <Point3D>(10);
                for (int j = 0; j < 4; j++)
                {
                    weightListCurrent.Add(weightsList[dataMouse.indexWeightsDetect[i, j]]);
                }
                Point3D[] sourcePars = Geometry.homography_calc_pars(weightListCurrent.ToArray());
                weightListCurrent.Add(weightsList[4]);

                for (int j = 0; j <= 1; j++)
                {
                    homographyRes = Geometry.homography_calc(weightListCurrent, sourcePars, destPars[j]);
                    distance      = Geometry.Distance(homographyRes[4], destP[j + 3]);
                    if (distance < distanceMin)
                    {
                        distanceMin     = distance;
                        homographyMin   = homographyRes;
                        weightListMin   = weightListCurrent;
                        hmMinSourcePars = sourcePars;
                        hmMinDestPars   = destPars[j];
                        //jMin = j;
                        //indexes[0] = indexWeightsDetect[i, 0];
                        //indexes[1] = indexWeightsDetect[i, 1];
                        //indexes[2] = indexWeightsDetect[i, 2];
                        // For correct order of points
                        if (j == 0) // This should be fixed in future
                        {
                            Point3D pTemp = homographyMin[3];
                            homographyMin[3] = homographyMin[4];
                            homographyMin[4] = pTemp;

                            pTemp            = weightListMin[3];
                            weightListMin[3] = weightListMin[4];
                            weightListMin[4] = pTemp;

                            //indexes[3] = 4;
                            //indexes[4] = indexWeightsDetect[i, 3];
                        }
                        else
                        {
                            //indexes[4] = 4;
                            //indexes[3] = indexWeightsDetect[i, 3];
                        }
                    }
                }
            }

            if (distanceMin < 1)
            {
                // If true is returned, recalc parameters
                Point3D[] sourceBinPars, destBinPars;
                if (CheckOrder(weightListMin, rows))
                {
                    //int[] indReordered = new int[5];
                    //indReordered[0] = indexes[0];
                    //indReordered[1] = indexes[4];
                    //indReordered[2] = indexes[3];
                    //indReordered[3] = indexes[2];
                    //indReordered[4] = indexes[1];
                    //binarPlacesHomography = Geometry.homography_calc(binarPlaces, hmMinDestPars, hmMinSourcePars);
                    //sourceBinPars = Geometry.homography_calc_pars(new Point3D[] { destP[0], destP[1], destP[2], destP[3] });
                    sourceBinPars = destPars_fifthP;
                    //destBinPars = Geometry.homography_calc_pars(new Point3D[] { weightsList[indReordered[0]],
                    //    weightsList[indReordered[1]], weightsList[indReordered[2]], weightsList[indReordered[3]] });
                    destBinPars = Geometry.homography_calc_pars(
                        new Point3D[] { weightListMin[0], weightListMin[1], weightListMin[2], weightListMin[3] });
                }
                else
                {
                    sourceBinPars = hmMinDestPars;
                    destBinPars   = hmMinSourcePars;
                }

                Point3D[][] orientationParameters = new Point3D[][] { sourceBinPars, destBinPars };
                return(orientationParameters);
            }
            else
            {
                return(null);
            }
        }
예제 #6
0
        private List <WeightCenter> FindSpots(byte[, ,] source, byte[, ,] original, int rows, int cols, FilterColors color)
        {
            int x1, y1, x2, y3;
            List <WeightCenter> weights = new List <WeightCenter>(20);
            WeightCenter        w;
            List <Strip>        RowStrips = FindStrips(source, rows, cols, color, true);  //True for rows
            List <Strip>        ColStrips = FindStrips(source, cols, rows, color, false); //False for cols

            foreach (Strip row in RowStrips)
            {
                foreach (Strip col in ColStrips)
                {
                    x1 = col.start;
                    y1 = row.start;
                    x2 = col.end;
                    y3 = row.end;

                    w = CalculateWeightCenter(source, x1, y1, x2 - x1 + 1, y3 - y1 + 1, color);
                    if ((w.x != 0) && (w.y != 0))
                    {
                        weights.Add(w);
                    }
                }
            }

            if (weights.Count == 5)
            {
                return(weights);
            }
            else
            {
                return(null);
            }

            //VisualizeStrips(source, RowStrips, ColStrips, rows, cols);
            //VisualizeWeightCenter(weights, rows, cols, source);
        } // End FindSpots
예제 #7
0
        private List <Strip> FindStrips(byte[,,] source, int strip_direction, int elements, FilterColors color, bool isRows) //Check columns or rows for strips with pixels
        {
            List <Strip> strip = new List <Strip>(20);
            int          j;
            int          start, end;

            for (int i = 0; i < strip_direction; i++)
            {
                j = 0;
                switch (isRows)
                {
                case true:
                    while ((j < elements) && (source[i, j, (int)color] != 255))     // Find pixels and stop until first founded on the current row
                    {
                        j++;
                    }
                    break;

                case false:
                    while ((j < elements) && (source[j, i, (int)color] != 255))     // Find pixels and stop until first founded on the current col
                    {
                        j++;
                    }
                    break;
                }
                if (j < elements) //If a pixel is found
                {
                    start = i;    //Mark its start // strip[currentStrip].
                    do            //Find the last row with pixels
                    {
                        j = 0;
                        i++;
                        if (i == strip_direction)
                        {
                            break;
                        }
                        switch (isRows)
                        {
                        case true:
                            while ((j < elements) && (source[i, j, (int)color] != 255))     // Find pixels and stop until first founded on the current row
                            {
                                j++;
                            }
                            break;

                        case false:
                            while ((j < elements) && (source[j, i, (int)color] != 255))     // Find pixels and stop until first founded on the current col
                            {
                                j++;
                            }
                            break;
                        }
                    } while (j < elements); // if j == cols -> therefore we've found an empty row
                    end = i - 1;            //Mark the end of a strip with last row with pixels // strip[currentStrip].
                    if ((end - start) > 3)  //If the strip is with normal width > 5 rows jump to next strip
                    {
                        strip.Add(new Strip(start, end));
                    }
                }
            }

            return(strip);
        }
예제 #8
0
        private void Detection(byte[, ,] filtered, byte[, ,] original, int rows, int cols, FilterColors color)
        {
            List <WeightCenter> sp = FindSpots(filtered, original, rows, cols, color);

            Point3D[][] orientation = DetectFace(filtered, original, sp, rows, cols, color);

            FindBinaryCode(original, orientation[0], orientation[1]);
        }
예제 #9
0
 private void RatioSeparation(byte[,,] sourceData, byte[, ,] destData, int row, int col, float RatioRG, float RatioRB, float RatioGB, FilterColors color)
 {
     if ((CompareRatios(RatioRG, Ratios[(int)color].RatioRG, Ratios[(int)color].LimitRG)) &&
         (CompareRatios(RatioRB, Ratios[(int)color].RatioRB, Ratios[(int)color].LimitRB)) &&
         (CompareRatios(RatioGB, Ratios[(int)color].RatioGB, Ratios[(int)color].LimitGB)))
     {
         destData[row, col, 2] = sourceData[row, col, 2];
         destData[row, col, 1] = sourceData[row, col, 1];
         destData[row, col, 0] = sourceData[row, col, 0];
     }
     else
     {
         destData[row, col, 2] = 0;
         destData[row, col, 1] = 0;
         destData[row, col, 0] = 0;
     }
 }
예제 #10
0
        // Returns the correct order of weight centers if exactly five spots are detected
        private List <Point3D> DetectFace(byte[, ,] data, byte[, ,] original, List <WeightCenter> weights, int rows, int cols, FilterColors color)
        {
            // Five points from the sides descriptor - destination for homography
            Point3D[] destP = new Point3D[5];
            // Side 6
            //destP[0] = new Point3D(-11.8513, -16.3120, -31.4164);
            //destP[1] = new Point3D(-19.1759, 6.2306, -31.4164);
            //destP[2] = new Point3D(0.0000, 20.1627, -31.4164);
            //destP[3] = new Point3D(19.1759, 6.2306, -31.4164);
            //destP[4] = new Point3D(11.8514, -16.3120, -31.4164);

            // Side 1
            destP[4] = new Point3D(11.8514, 16.3119, 31.4164);
            destP[3] = new Point3D(19.1759, -6.2307, 31.4164);
            destP[2] = new Point3D(0.0000, -20.1627, 31.4164);
            destP[1] = new Point3D(-19.1759, -6.2306, 31.4164);
            destP[0] = new Point3D(-11.8513, 16.3120, 31.4164);

            int[,] indexWeightsDetect =
            {
                { 0, 1, 2, 3, 4 },
                { 0, 1, 2, 4, 3 },
                { 0, 1, 3, 2, 4 },
                { 0, 1, 3, 4, 2 },
                { 0, 2, 1, 3, 4 },
                { 0, 2, 1, 4, 3 },
                { 0, 2, 3, 1, 4 },
                { 0, 2, 3, 4, 1 },
                { 0, 3, 1, 2, 4 },
                { 0, 3, 1, 4, 2 },
                { 0, 3, 2, 1, 4 },
                { 0, 3, 2, 4, 1 }
            };
            int[] indexCamera    = { 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3 };
            int[] indexDescrPars = { 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4 };

            //Point3D[] destPars_fourthP = Geometry.homography_calc_pars(
            //    new Point3D[] { destP[0], destP[1], destP[2], destP[4] });
            //Point3D[] destPars_fifthP = Geometry.homography_calc_pars(
            //    new Point3D[] { destP[0], destP[1], destP[2], destP[3] });
            //Point3D[][] destPars = new Point3D[][] { destPars_fourthP, destPars_fifthP };

            double distance;
            double distanceMin = 10000;
            int    jMin        = 0;

            int[]          weightIndexes = new int[5];
            List <Point3D> homographyRes;//, weightListCurrent;
            List <Point3D> homographyMin = null, weightListMin = null;

            Point3D[] hmMinSourcePars = null, hmMinDestPars = null;

            List <Point3D> weightsList = new List <Point3D>(15);

            foreach (WeightCenter item in weights)
            {
                weightsList.Add(new Point3D(item.x, item.y, 0));
            }

            //if ((weights[0].rowStart == weights[1].rowStart) &&
            //    (weights[1].rowStart != weights[2].rowStart) ||
            //    (weights[3].rowStart != weights[4].rowStart))
            //{
            //    weightsList.Reverse();
            //}

            //ConvertXY(weightsList, rows);

            for (int i = 0; i < 12; i++)
            {
                Point3D[] sourcePars = Geometry.homography_calc_pars(new Point3D[] {
                    weightsList[indexWeightsDetect[i, 0]],
                    weightsList[indexWeightsDetect[i, 1]],
                    weightsList[indexWeightsDetect[i, 2]],
                    weightsList[indexDescrPars[i]]
                });

                Point3D[] destPars = Geometry.homography_calc_pars(
                    new Point3D[] { destP[0], destP[1], destP[2], destP[indexDescrPars[i]] });

                List <Point3D> currList = new List <Point3D>(1);
                currList.Add(weightsList[indexCamera[i]]);
                homographyRes = Geometry.homography_calc(currList, sourcePars, destPars);
                distance      = Geometry.Distance(homographyRes[0], destP[indexCamera[i]]);

                if (distance < distanceMin)
                {
                    distanceMin      = distance;
                    homographyMin    = homographyRes;
                    hmMinSourcePars  = sourcePars;
                    hmMinDestPars    = destPars;
                    weightIndexes[0] = indexWeightsDetect[i, 0];
                    weightIndexes[1] = indexWeightsDetect[i, 1];
                    weightIndexes[2] = indexWeightsDetect[i, 2];
                    weightIndexes[3] = indexWeightsDetect[i, 3];
                    weightIndexes[4] = indexWeightsDetect[i, 4];

                    //// For correct order of points
                    //if (j == 0) // This should be fixed in future
                    //{
                    //    Point3D pTemp = homographyMin[3];
                    //    homographyMin[3] = homographyMin[4];
                    //    homographyMin[4] = pTemp;

                    //    pTemp = weightListMin[3];
                    //    weightListMin[3] = weightListMin[4];
                    //    weightListMin[4] = pTemp;
                    //}
                }
            }

            MessageBox.Show("DistMin = " + distanceMin.ToString());

            if (distanceMin < 1)
            {
                List <Point3D> binarPlaces      = new List <Point3D>(5);
                List <Point3D> weightsReordered = new List <Point3D>(5);
                for (int i = 0; i < 5; i++)
                {
                    weightsReordered.Add(weightsList[weightIndexes[i]]);
                }
                // If true is returned, recalc parameters
                if (CheckOrder(weightsReordered, rows))
                {
                    // Mirrored binary places coordinates
                    binarPlaces.Add(new Point3D(3.7171, 16.8942, 0.0000));
                    binarPlaces.Add(new Point3D(-14.9187, 8.7557, 0.0000));
                    binarPlaces.Add(new Point3D(-12.9373, -11.4829, 0.0000));
                    binarPlaces.Add(new Point3D(6.9230, -15.8525, 0.0000));
                    binarPlaces.Add(new Point3D(17.2160, 1.6855, 0.0000));
                }
                else
                {
                    binarPlaces.Add(new Point3D(-3.6793, 16.9995, 0.0000));
                    binarPlaces.Add(new Point3D(-17.3045, 1.7539, 0.0000));
                    binarPlaces.Add(new Point3D(-7.0154, -15.9155, 0.0000));
                    binarPlaces.Add(new Point3D(12.9687, -11.5903, 0.0000));
                    binarPlaces.Add(new Point3D(15.0305, 8.7524, 0.0000));
                }
                List <Point3D> binarPlacesHomography = Geometry.homography_calc(binarPlaces, hmMinDestPars, hmMinSourcePars);
                WeightCenter   binFound = FindBinaryPlace(binarPlacesHomography, original);

                if (checkBoxWriteDetected.Checked)
                {
                    using (StreamWriter str = new StreamWriter("homography.txt", true))
                    {
                        //List<Point3D> weightListConverted = Geometry.CloneList(weightsList);
                        //ConvertXY(weightListConverted, rows);
                        //foreach (Point3D pt in weightListConverted)
                        //{
                        //    str.WriteLine(pt.X.ToString() + "," + pt.Y.ToString() + "," + pt.Z.ToString());
                        //}
                        //str.WriteLine();

                        //foreach (Point3D pt in homographyMin)
                        //{
                        //    str.WriteLine(pt.X.ToString() + "," + pt.Y.ToString() + "," + pt.Z.ToString());
                        //}
                        //str.WriteLine();

                        List <Point3D> weightListConverted = Geometry.CloneList(weightListMin);
                        ConvertXY(weightListConverted, rows);
                        foreach (Point3D pt in weightListConverted)
                        {
                            str.WriteLine(pt.X.ToString() + "," + pt.Y.ToString() + "," + pt.Z.ToString());
                        }
                        str.WriteLine();
                    }
                }

                if (checkBoxVizDetectedSpots.Checked)
                {
                    //DrawPoints(data, weightListMin, rows, cols);
                    //ConvertYX(binarPlacesHomography, rows);
                    //DrawPoints(original, binarPlacesHomography, rows, cols);
                    VisualizeWeightCenter(binFound, rows, cols, original);
                }
                //List<List<Point3D>> result = new List<List<Point3D>>(3);
                //result.Add(weightListMin);
                return(binarPlacesHomography);
            }
            else
            {
                return(null);
            }
        }
예제 #11
0
        /// <summary>
        /// Detect the orientation of camera toward descriptor
        /// </summary>
        /// <param name="filtered">Filtered image array</param>
        /// <param name="original">Original image array</param>
        /// <param name="colorIndex">Define which color filter is used</param>
        /// <returns>Four points. Camera place, center
        /// of projection and two orientations(X and Y)</returns>
        public List <Point3D> Detection(
            Image <Bgr, byte> imgContainer,
            byte[, ,] original, FilterColors colorIndex,
            int FrameRows, int FrameCols,
            List <Point3D> CenterProjXY)
        {
            FilterColors frameColor = colorIndex;

            // Correction is made for yellow color, which value is 4,
            // but a image array consists only of three color channels.
            // The color is used as index in that array
            if (colorIndex == FilterColors.Y)
            {
                colorIndex = FilterColors.R;
            }

            List <WeightCenter> sp = SpotDetection.FindSpots(imgContainer.Data, original, colorIndex, FrameRows, FrameCols);

            if (sp == null)
            {
                return(null);
            }

            Point3D[][] orientation = DetectFace(sp, FrameRows);

            // In case five color spots are detected the processing of current frame continues
            if (orientation == null)
            {
                return(null);
            }

            // Draw the corrected points on image
            Visualization.DrawPoints(original, new List <Point3D>(orientation[2]), FrameRows, FrameCols, 128);

            WeightCenter   binFound;
            List <Point3D> bin8Location = CalcBinaryPlaces(original, orientation[0], orientation[1], out binFound);

            // Draw the exact 8 spots of binary code
            //Visualization.DrawPoints(original, bin8Location, FrameRows, FrameCols, 150);

            int sideNumber = ReadBinaryCode(original, bin8Location);

            if (sideNumber < 0)
            {
                return(null);
            }

            #region Visualization of binary code location
            //if (imgVisualizePoints != null)
            //{
            //    int rows = original.GetLength(0);
            //    int cols = original.GetLength(1);
            //    Visualization.DrawPoints(imgVisualizePoints.Data, binar8Codes, rows, cols);
            //}
            //Number 3 has problem
            //if (color == FilterColors.Blue)
            //    mainTextViz.SetText(sideNumber.ToString());
            #endregion

            // Compare side number and color if they match correctly
            if (!CheckSideNumberColor(sideNumber, frameColor))
            {
                return(null);
            }

            List <Point3D> fourPdescriptor = new List <Point3D>(5);
            fourPdescriptor.Add(dataMouse.ColorSpotsDescriptorSmall[0]);
            fourPdescriptor.Add(dataMouse.ColorSpotsDescriptorSmall[1]);
            fourPdescriptor.Add(dataMouse.ColorSpotsDescriptorSmall[2]);
            fourPdescriptor.Add(dataMouse.ColorSpotsDescriptorSmall[3]);
            fourPdescriptor.Add(dataMouse.ColorSpotsDescriptorSmall[4]);
            Point3D[] cameraProjectionPoints = orientation[2];

            int binPosition = binFound.rowStart;

            List <Point3D> fourPprojection = new List <Point3D>(4);
            // Order points in counter clock wise from binary code
            fourPprojection.Add(cameraProjectionPoints[dataMouse.indexBinaryPositionReorder[binPosition, 0]]);
            fourPprojection.Add(cameraProjectionPoints[dataMouse.indexBinaryPositionReorder[binPosition, 1]]);
            fourPprojection.Add(cameraProjectionPoints[dataMouse.indexBinaryPositionReorder[binPosition, 2]]);
            fourPprojection.Add(cameraProjectionPoints[dataMouse.indexBinaryPositionReorder[binPosition, 3]]);
            fourPprojection.Add(cameraProjectionPoints[dataMouse.indexBinaryPositionReorder[binPosition, 4]]);

            // For distance check
            List <Point3D> FoundSpots = Geometry.CloneList(fourPprojection);

            ImageProcessing.ConvertXY(fourPprojection, FrameRows);

            //FourP.ToDraw = true;
            //FourP.FourPointCalibrationMain(fourPdescriptor, CenterProjXY, fourPprojection);

            //FiveVariants(fourPprojection, CenterProjXY, fourPdescriptor);

            Point3D[] sourceViewPars = Geometry.align_calc_pars(new Point3D[] {
                dataMouse.ColorSpotsDescriptorSmall[0],
                dataMouse.ColorSpotsDescriptorSmall[1],
                dataMouse.ColorSpotsDescriptorSmall[2]
            });

            // sideNumber-1 is the index of the detected side
            Point3D[] destViewPars = Geometry.align_calc_pars(new Point3D[] {
                dataMouse.ColorSpotSides[sideNumber - 1][0],
                dataMouse.ColorSpotSides[sideNumber - 1][1],
                dataMouse.ColorSpotSides[sideNumber - 1][2]
            });

            // Averaging five result from 4-point transformation
            List <Point3D> fourP = new List <Point3D>();

            int n = 0;
            List <List <Point3D> > fourPoint = new List <List <Point3D> >();
            //Rotate elements in list and calculate four point transform
            for (int i = 0; i < fourPprojection.Count; i++)
            {
                List <List <Point3D> > pl1 = Geometry.cal_4p_trans(fourPdescriptor.GetRange(0, 4), CenterProjXY, fourPprojection.GetRange(0, 4));

                if (pl1 != null)
                {
                    fourPoint.Add(pl1[1]);

                    // Align camera toward descriptor
                    Geometry.align((fourPoint[n]), sourceViewPars, destViewPars);

                    //Add weight in fifth point in X value
                    fourPoint[n].Add(WeightDistance(fourPoint[n], FoundSpots, sideNumber, original, CenterProjXY, FrameRows, FrameCols, true));
                    n++;
                }
                //Add first in last position. Remove firs.
                fourPdescriptor.Add(fourPdescriptor[0]);
                fourPdescriptor.RemoveAt(0);

                fourPprojection.Add(fourPprojection[0]);
                fourPprojection.RemoveAt(0);
            }

            //Remove worst result
            double maxDist  = -1;
            double currDist = 0;
            n = 0;
            for (int i = 0; i < fourPoint.Count; i++)
            {
                currDist = fourPoint[i][4].X;
                if (currDist > maxDist)
                {
                    maxDist = currDist;
                    n       = i;
                }
            }
            if ((fourPoint.Count != 0) && (fourPoint.Count >= 2) && (fourPoint[n][4].X > 10))
            {
                fourPoint.RemoveAt(n);
            }

            fourP = FormWebCamEmgu.AverageViewPointsWeight(false, fourPoint);

            if (fourP == null)
            {
                return(null);
            }

            fourP.Add(WeightDistance(fourP, FoundSpots, sideNumber, original, CenterProjXY, FrameRows, FrameCols, false));

            return(fourP);
        }
예제 #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <param name="original"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        public static List <WeightCenter> FindSpots(byte[, ,] source, byte[, ,] original, FilterColors color, int FrameRows, int FrameCols)
        {
            int x1, y1, x2, y3;
            List <WeightCenter> weights = new List <WeightCenter>(20);
            WeightCenter        w;
            List <Strip>        RowStrips = FindStrips(source, FrameRows, FrameCols, color, true);  //True for rows
            List <Strip>        ColStrips = FindStrips(source, FrameCols, FrameRows, color, false); //False for cols

            foreach (Strip row in RowStrips)
            {
                foreach (Strip col in ColStrips)
                {
                    x1 = col.start;
                    y1 = row.start;
                    x2 = col.end;
                    y3 = row.end;

                    w = CalculateWeightCenter(source, x1, y1, x2 - x1 + 1, y3 - y1 + 1, color);
                    if ((w.x != 0) && (w.y != 0))
                    {
                        weights.Add(w);
                    }
                }
            }

            //Visualization.VisualizeStrips(source, RowStrips, ColStrips, FrameRows, FrameCols);

            if (weights.Count == 5)
            {
                Visualization.VisualizeWeightCenter(weights, original, FrameRows, FrameCols); // Dobri visualization

                //if (FoundSpotsCheck.CheckState == CheckState.Checked)
                //Visualization.VisualizeWeightCenter(weights, imgOriginal.Data, FrameRows, FrameCols);
                return(weights);
            }
            else
            {
                return(null);
            }

            //VisualizeStrips(source, RowStrips, ColStrips, rows, cols);
            //VisualizeWeightCenter(weights, rows, cols, source);
        } // End FindSpots
예제 #13
0
        /// <summary>
        /// Mark strips with valid pixels by rows or columns
        /// </summary>
        /// <param name="source">Image, which is checked for valid pixels</param>
        /// <param name="StripDirection">Count of pixels in the current direction (rows or cols)</param>
        /// <param name="elements">Count of pixels in the opposite direction (cols or rows)</param>
        /// <param name="color">Indicates which color is being processed</param>
        /// <param name="isRows">For processing in direction of rows, needs to be set true</param>
        /// <returns>List of founded strips</returns>
        public static List <Strip> FindStrips(byte[, ,] source, int StripDirection, int elements, FilterColors color, bool isRows)
        {
            List <Strip> strip = new List <Strip>(20);
            int          j;
            int          start, end;

            for (int i = 0; i < StripDirection; i++)
            {
                j = ScanPixels(source, elements, i, color, isRows);
                if (j < elements) //If a pixel is found
                {
                    start = i;    //Mark its start
                    do            //Find the last row with pixels
                    {
                        i++;
                        if (i == StripDirection)
                        {
                            break;
                        }
                        j = ScanPixels(source, elements, i, color, isRows);
                    } while (j < elements); // if j == cols -> therefore we've found an empty row
                    end = i - 1;            //Mark the end of a strip with last row with pixels // strip[currentStrip].
                    if ((end - start) > 5)  //If the strip is with normal width > 5 rows jump to next strip
                    {
                        strip.Add(new Strip(start, end));
                    }
                }
            }
            return(strip);
        }