private static void GenerateTemplate(ImageProcessor processor, Bitmap bmp, string name)
 {
     processor.ProcessImage(new Image<Bgr, byte>(bmp));
     //find max contour
     if (processor.samples.Count > 0)
     {
         processor.samples.Sort((t1, t2) => -t1.sourceArea.CompareTo(t2.sourceArea));
         processor.samples[0].name = name;
         processor.templates.Add(processor.samples[0]);
     }
 }
Пример #2
0
        public void MergeContours(double owerflowContoursRatio, double adaptiveThresholdParameter, int adaptiveThresholdBlockSize)
        {
            int secondBiggestContourIndex;
            ImageProcessor processor = new ImageProcessor();
            processor.equalizeHist = false;
            processor.finder.maxRotateAngle = Math.PI / 4;
            processor.minContourArea = 10;
            processor.minContourLength = 15;
            processor.finder.maxACFDescriptorDeviation = 2;
            processor.finder.minACF = 0.96;
            processor.finder.minICF = 0.85;
            processor.blur = false;
            processor.noiseFilter = false;
            processor.cannyThreshold = 50;
            processor.adaptiveThresholdBlockSize = 1;
            processor.adaptiveThresholdParameter = 0.5;

            processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(_digit)), true);
            if (processor.contours.Count < 3)
            {
                FindBestContour(processor, owerflowContoursRatio);
                FillContour();
                _digit = _filledContour;
                return;
            }

            while (processor.contours.Count > 2)
            {
                AdaptiveThreshold(adaptiveThresholdParameter, adaptiveThresholdBlockSize);
                FillAllContours(processor, owerflowContoursRatio);
                _digit = _filledContour;
                processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(_digit)), true);
                SortContour(processor, out secondBiggestContourIndex);
                processor.minContourArea = (int)(processor.contours[secondBiggestContourIndex].Area / 4);
                processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(_digit)), true);
            }
        }
Пример #3
0
 public void FindMaxContour(ImageProcessor processor)
 {
     int secondLongestContourIndex;
     processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(_digit)), true);
     _contour = processor.contours[SortContour(processor, out secondLongestContourIndex)];
 }
Пример #4
0
        public void FindBestContour(ImageProcessor processor, double owerflowContoursRatio)
        {
            int secondBiggestContourIndex;
            processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(_digit)), true);
            var biggestContourIndex = SortContour(processor, out secondBiggestContourIndex);

            if (((biggestContourIndex != secondBiggestContourIndex) &&
                (processor.contours[biggestContourIndex].Area / processor.contours[secondBiggestContourIndex].Area <
                 owerflowContoursRatio)) || (processor.contours.Count < 3))
            {
                _contour = processor.contours[secondBiggestContourIndex];
            }
            else
            {
                _contour = processor.contours[biggestContourIndex];
            }
        }
Пример #5
0
        public void FillAllContours(ImageProcessor processor, double owerflowContoursRatio)
        {
            Bitmap newFill = new Bitmap(_digit.Width, _digit.Height);
            using (Graphics contourGraph = Graphics.FromImage(newFill))
            {
                contourGraph.Clear(Color.Black);
            }

            processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(_digit)), true);

            for (int contourCounter = 0; contourCounter < processor.contours.Count; contourCounter++)
            {
                using (Graphics contourGraph = Graphics.FromImage(newFill))
                {
                    contourGraph.FillPolygon(new SolidBrush(Color.White), processor.contours[contourCounter].ToArray());
                }
            }

            _filledContour = newFill;
        }
Пример #6
0
        public Image DisplayAllContours(ImageProcessor processor, Color maxContourColor, Color secondMaxContourColor, Color baseContourColor)
        {
            Image resultDigit = new Bitmap(_digit);
            processor.ProcessImage(new Image<Bgr, Byte>(new Bitmap(resultDigit)), true);
            int secondLongestContourIndex;
            var longestContourIndex = SortContour(processor, out secondLongestContourIndex);

            for (int contourIndex = 0; contourIndex < processor.contours.Count; contourIndex++)
            {
                if (processor.contours[contourIndex].Total > 1)
                {
                    using (Graphics digitGraph = Graphics.FromImage(resultDigit))
                    {
                        if (contourIndex == longestContourIndex)
                        {
                            digitGraph.DrawLines(new Pen(maxContourColor), processor.contours[contourIndex].ToArray());
                        }
                        else if (contourIndex == secondLongestContourIndex)
                        {
                            digitGraph.DrawLines(new Pen(secondMaxContourColor), processor.contours[contourIndex].ToArray());
                        }
                        else
                        {
                            digitGraph.DrawLines(new Pen(baseContourColor), processor.contours[contourIndex].ToArray());
                        }
                    }
                }
            }

            return resultDigit;
        }