Esempio n. 1
0
        private void SetVisualizerPositions()
        {
            vizR = new FormVisualizer(FormVisualizer.DisplayMode.Emgu, "Red");
            vizG = new FormVisualizer(FormVisualizer.DisplayMode.Emgu, "Green");
            vizB = new FormVisualizer(FormVisualizer.DisplayMode.Emgu, "Blue");
            vizY = new FormVisualizer(FormVisualizer.DisplayMode.Emgu, "Yellow");

            RegistryKey rk = null;

            try
            {
                rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\" + this.ProductName);
                if ((rk != null) && ((string)rk.GetValue("R_K", "-1") != "-1"))
                {
                    FormWebCamEmgu.ReadWindowState(rk, vizR, "Red");
                    FormWebCamEmgu.ReadWindowState(rk, vizG, "Green");
                    FormWebCamEmgu.ReadWindowState(rk, vizB, "Blue");
                    FormWebCamEmgu.ReadWindowState(rk, vizY, "Yellow");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Problem occurred while reading registry saved properties");
            }
            finally
            {
                if (rk != null)
                {
                    rk.Dispose();
                }
            }

            vizR.Show();
            vizG.Show();
            vizB.Show();
            vizY.Show();
        }
Esempio n. 2
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);
        }