예제 #1
0
        public void Analyze(System.Drawing.Bitmap bitmap, ColorConfig currentConfig, bool useMorphologic = true, bool detectBox = false)
        {
            System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
            bitmaps.UpdateImages(bitmap);
            using (Image <Bgr, byte> img = new Image <Bgr, byte>(bitmaps.Bitmap))
            {
                IImage uimage = createMat();
                CvInvoke.CvtColor(img.Clone(), uimage, ColorConversion.Bgr2Hsv, 3);

                double cannyThreshold        = 180.0;
                double cannyThresholdLinking = 10.0;
                IImage cannyEdges            = createMat();
                CvInvoke.Canny(img.Clone(), cannyEdges, cannyThreshold, cannyThresholdLinking);

                if (detectBox)
                {
                    var boxList = BoxDetection(cannyEdges);
                    foreach (var box in boxList)
                    {
                        for (int i = 0; i < box.Item1.Length; i++)
                        {
                            var pointA = box.Item1[i];
                            var pointB = box.Item1[(i + 1) % box.Item1.Length];
                            CvInvoke.Line(img, pointA, pointB, new Bgr(Color.DarkOrange).MCvScalar, 2);
                        }
                    }
                }


                //use image pyr to remove noise
                IImage pyrDown = createMat();
                CvInvoke.PyrDown(uimage, pyrDown);
                CvInvoke.PyrUp(pyrDown, uimage);

                IImage    imgThresholded = createMat();
                MCvScalar lower          = new MCvScalar(currentConfig.LowH, currentConfig.LowS, currentConfig.LowV);
                MCvScalar upper          = new MCvScalar(currentConfig.HighH, currentConfig.HighS, currentConfig.HighV);

                CvInvoke.InRange(uimage, new ScalarArray(lower), new ScalarArray(upper), imgThresholded);

                if (useMorphologic)
                {
                    morphOps(imgThresholded);
                }

                var contours   = ContourDetection(imgThresholded);
                var maxContour = contours.OrderByDescending(t => t.Item2).FirstOrDefault();
                foreach (var box in contours)
                {
                    MCvScalar contourColor;
                    if (box == maxContour)
                    {
                        contourColor = new Bgr(Color.DarkOrange).MCvScalar;
                        var vertices = box.Item4.GetVertices();
                        for (int i = 0; i < 4; i++)
                        {
                            CvInvoke.Line(img, new Point((int)vertices[i].X, (int)vertices[i].Y),
                                          new Point((int)vertices[(i + 1) % 4].X, (int)vertices[(i + 1) % 4].Y), contourColor);
                        }
                    }
                    else
                    {
                        contourColor = new Bgr(Color.Green).MCvScalar;
                    }
                    for (int i = 0; i < box.Item1.Length; i++)
                    {
                        var pointA = box.Item1[i];
                        var pointB = box.Item1[(i + 1) % box.Item1.Length];
                        CvInvoke.Line(img, pointA, pointB, contourColor, 4);
                    }
                    CvInvoke.Circle(img, box.Item3, 5, contourColor, 2);
                }
                //Add Hour
                CvInvoke.PutText(img, DateTime.Now.ToString(), new Point(5, 30), FontFace.HersheySimplex, 0.8, new Bgr(Color.LightBlue).MCvScalar);

                bitmaps.Calculations = sw.ElapsedMilliseconds;
                sw.Restart();
                bitmaps.UpdateImages(null, img.ToBitmap(),
                                     cannyEdges.Bitmap,
                                     imgThresholded.Bitmap);

                bitmaps.ImageSet = sw.ElapsedMilliseconds;
                bitmaps.FPS      = bitmaps.Calculations + bitmaps.ImageSet;

                if (this.analyzerOutput != null)
                {
                    analyzerOutput.FovSize = bitmaps.Bitmap.Size;
                    if (maxContour != null)
                    {
                        analyzerOutput.Detected = true;
                        analyzerOutput.Center   = maxContour.Item3;
                        analyzerOutput.Width    = (int)maxContour.Item4.Size.Width;
                        analyzerOutput.Height   = (int)maxContour.Item4.Size.Height;
                    }
                    else
                    {
                        analyzerOutput.Detected = false;
                        analyzerOutput.Center   = Point.Empty;
                        analyzerOutput.Distance = 0;
                    }
                }
            }
        }