示例#1
0
        public void ProcessImage(Image <Gray, byte> grayFrame)
        {
            if (equalizeHist)
            {
                grayFrame._EqualizeHist();//autocontrast
            }
            //smoothed
            Image <Gray, byte> smoothedGrayFrame = grayFrame.PyrDown();

            smoothedGrayFrame = smoothedGrayFrame.PyrUp();
            //canny
            Image <Gray, byte> cannyFrame = null;

            if (noiseFilter)
            {
                cannyFrame = smoothedGrayFrame.Canny(new Gray(cannyThreshold), new Gray(cannyThreshold));
            }
            //smoothing
            if (blur)
            {
                grayFrame = smoothedGrayFrame;
            }
            //binarize
            CvInvoke.cvAdaptiveThreshold(grayFrame, grayFrame, 255, Emgu.CV.CvEnum.ADAPTIVE_THRESHOLD_TYPE.CV_ADAPTIVE_THRESH_MEAN_C, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY, adaptiveThresholdBlockSize + adaptiveThresholdBlockSize % 2 + 1, adaptiveThresholdParameter);
            //
            grayFrame._Not();
            //
            if (addCanny)
            {
                if (cannyFrame != null)
                {
                    grayFrame._Or(cannyFrame);
                }
            }
            //
            this.binarizedFrame = grayFrame;

            //dilate canny contours for filtering
            if (cannyFrame != null)
            {
                cannyFrame = cannyFrame.Dilate(3);
            }

            //find contours
            var sourceContours = grayFrame.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);

            //filter contours
            contours = FilterContours(sourceContours, cannyFrame, grayFrame.Width, grayFrame.Height);
            //find templates
            lock (foundTemplates)
                foundTemplates.Clear();
            samples.Clear();

            lock (templates)
                Parallel.ForEach <Contour <Point> >(contours, (contour) =>
                {
                    var arr         = contour.ToArray();
                    Template sample = new Template(arr, contour.Area, samples.templateSize);
                    lock (samples)
                        samples.Add(sample);

                    if (!onlyFindContours)
                    {
                        FoundTemplateDesc desc = finder.FindTemplate(templates, sample);

                        if (desc != null)
                        {
                            lock (foundTemplates)
                                foundTemplates.Add(desc);
                        }
                    }
                }
                                                    );
            //
            FilterByIntersection(ref foundTemplates);
        }
示例#2
0
        public void ProcessImage(Image <Gray, byte> grayFrame)
        {
            if (equalizeHist)
            {
                grayFrame._EqualizeHist();//autocontrast
            }
            //高斯平滑
            Image <Gray, byte> smoothedGrayFrame = grayFrame.PyrDown();

            smoothedGrayFrame = smoothedGrayFrame.PyrUp();

            //canny
            Image <Gray, byte> cannyFrame = null;

            if (noiseFilter)
            {
                cannyFrame = smoothedGrayFrame.Canny(cannyThreshold, cannyThreshold);
            }

            //smoothing
            if (blur)
            {
                grayFrame = smoothedGrayFrame;
            }

            //局部自适应阈值二值化,阈值本身作为了一个变量,检测更有效
            //CvInvoke.cvAdaptiveThreshold(grayFrame, grayFrame, 255, Emgu.CV.CvEnum.ADAPTIVE_THRESHOLD_TYPE.CV_ADAPTIVE_THRESH_MEAN_C, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY, adaptiveThresholdBlockSize + adaptiveThresholdBlockSize % 2 + 1, adaptiveThresholdParameter);
            CvInvoke.AdaptiveThreshold(grayFrame, grayFrame, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.MeanC, Emgu.CV.CvEnum.ThresholdType.Binary, adaptiveThresholdBlockSize + adaptiveThresholdBlockSize % 2 + 1, adaptiveThresholdParameter);

            //
            grayFrame._Not();
            //
            if (addCanny)
            {
                if (cannyFrame != null)
                {
                    grayFrame._Or(cannyFrame);    //试验了一下,这样轮廓会更加明显
                }
            }
            //
            this.binarizedFrame = grayFrame;

            //dilate canny contours for filtering
            //膨胀操作,使白色区域的像素增加一圈,看起来轮廓更细了
            if (cannyFrame != null)
            {
                cannyFrame = cannyFrame.Dilate(3);
            }

            //find contours
            VectorOfVectorOfPoint sourceContours = new VectorOfVectorOfPoint();
            CvInvoke.FindContours(grayFrame, sourceContours, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone);
            //var sourceContours = grayFrame.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);

            //filter contours
            contours = FilterContours(sourceContours, cannyFrame, grayFrame.Width, grayFrame.Height);


            //find templates
            lock (foundTemplates)
                foundTemplates.Clear();
            samples.Clear();

            lock (templates)   //多线程,异步执行
                Parallel.ForEach <VectorOfPoint>(contours, (contour) =>
                {
                    var arr         = contour.ToArray();
                    Template sample = new Template(arr, CvInvoke.ContourArea(contour), samples.templateSize);
                    lock (samples)
                        samples.Add(sample);

                    if (!onlyFindContours)
                    {
                        FoundTemplateDesc desc = finder.FindTemplate(templates, sample);

                        if (desc != null)
                        {
                            lock (foundTemplates)
                                foundTemplates.Add(desc);
                        }
                    }
                }
                                                 );
            //
            FilterByIntersection(ref foundTemplates);
        }