public static void GenerateAntipatterns(ImageProcessor processor)
        {
            Bitmap bmp = new Bitmap(200, 200);
            Graphics gr = Graphics.FromImage(bmp);
            //
            processor.onlyFindContours = true;
            //square
            gr.Clear(Color.White);
            gr.FillRectangle(Brushes.Black, new Rectangle(10, 10, 80, 80));
            GenerateTemplate(processor, bmp, "antipattern");
            //rect1
            gr.Clear(Color.White);
            gr.FillRectangle(Brushes.Black, new Rectangle(10, 10, 50, 100));
            GenerateTemplate(processor, bmp, "antipattern");
            //rect2
            gr.Clear(Color.White);
            gr.FillRectangle(Brushes.Black, new Rectangle(10, 10, 20, 100));
            GenerateTemplate(processor, bmp, "antipattern");
            //circle
            gr.Clear(Color.White);
            gr.FillEllipse(Brushes.Black, new Rectangle(10, 10, 100, 100));
            GenerateTemplate(processor, bmp, "antipattern");

            processor.onlyFindContours = false;
        }
Exemplo n.º 2
0
        public MainForm()
        {
            InitializeComponent();

            _processor = new ImageProcessor();

            LoadTemplateFile(_currentFilename);
        }
 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]);
     }
 }
Exemplo n.º 4
0
 public MainForm()
 {
     InitializeComponent();
     //create image preocessor
     processor = new ImageProcessor();
     //load default templates
     templateFile = AppDomain.CurrentDomain.BaseDirectory + "\\Tahoma.bin";
     LoadTemplates(templateFile);
     //apply settings
     ApplySettings();
     //
     Application.Idle += new EventHandler(Application_Idle);
 }
 public static void GenerateChars(ImageProcessor processor, char[] chars, Font font)
 {
     Bitmap bmp = new Bitmap(400, 400);
     font = new Font(font.FontFamily, 140, font.Style);
     Graphics gr = Graphics.FromImage(bmp);
     //
     processor.onlyFindContours = true;
     foreach (char c in chars)
     {
         gr.Clear(Color.White);
         gr.DrawString(c.ToString(), font, Brushes.Black, 5, 5);
         GenerateTemplate(processor, bmp, c.ToString());
     }
     processor.onlyFindContours = false;
 }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
0
 public void FindTemplate(ImageProcessor processor)
 {
     var contours = new List<Contour<Point>>();
     contours.Add(_contour);
     processor.FindTemplatesNonParalel(contours, true);
     _contourTemplate = processor.samples[0];
 }
Exemplo n.º 8
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)];
 }
Exemplo n.º 9
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];
            }
        }
Exemplo n.º 10
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;
        }
Exemplo n.º 11
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;
        }
Exemplo n.º 12
0
 private void LoadTemplates(string fileName, ImageProcessor proc)
 {
     try
     {
         using (FileStream fs = new FileStream(fileName, FileMode.Open))
             proc.templates = (Templates)new BinaryFormatter().Deserialize(fs);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 13
0
 private void SaveTemplates(string fileName, ImageProcessor proc)
 {
     try
     {
         using (FileStream fs = new FileStream(fileName, FileMode.Create))
             new BinaryFormatter().Serialize(fs, proc.templates);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 14
0
 private void LoadTemplates(string fileName, ImageProcessor proc)
 {
     using (FileStream fs = new FileStream(fileName, FileMode.Open))
         proc.templates = (Templates) new BinaryFormatter().Deserialize(fs);
 }
 public AutoGenerateForm(ImageProcessor processor)
 {
     InitializeComponent();
     tbFont.Text = new FontConverter().ConvertToString(tbChars.Font);
     this.processor = processor;
 }
Exemplo n.º 16
0
 public void Recognise(ImageProcessor processor)
 {
     var contours = new List<Contour<Point>>();
     contours.Add(_contour);
     List<FoundTemplateDesc> recognisedDigits = processor.FindTemplatesNonParalel(contours, true, _rather6, _rather8, _rather9, _rather0);
     if (recognisedDigits[0] == null)
     {
         _recognised = false;
     }
     else
     {
         _contourTemplate = recognisedDigits[0].template;
         _cifre = recognisedDigits[0].template.name.SafeToInt(-1);
         _recognised = true;
     }
 }
Exemplo n.º 17
0
 private int SortContour(ImageProcessor processor, out int secondLongestContourIndex)
 {
     int longestContourIndex = 0;
     secondLongestContourIndex = 0;
     double contourArea = 0;
     for (int contourIndex = 0; contourIndex < processor.contours.Count; contourIndex++)
     {
         if (contourArea < processor.contours[contourIndex].Area)
         {
             contourArea = processor.contours[contourIndex].Area;
             secondLongestContourIndex = longestContourIndex;
             longestContourIndex = contourIndex;
         }
     }
     return longestContourIndex;
 }
Exemplo n.º 18
0
        private void InitProcessors()
        {
            _processor = new ImageProcessor();
            _processor.equalizeHist = false;
            //_processor.finder.maxRotateAngle = differences69.Checked ? Math.PI/49 : Math.PI;
            _processor.finder.maxRotateAngle = differences69.Checked ? _appSettings.MaxAngle : Math.PI;
            _processor.finder.maxRotateAngle = differences0689.Checked ? _appSettings.MaxAngleForSeven : Math.PI;
            _processor.minContourArea = _appSettings.MinContourArea;
            _processor.minContourLength = _appSettings.MinContourLength;
            _processor.finder.maxACFDescriptorDeviation = _appSettings.MaxACFDescriptorDeviation; //Auto correlation deviation
            _processor.finder.maxACFDescriptorDeviation0 = _appSettings.MaxACFDescriptorDeviation0;
            _processor.finder.maxACFDescriptorDeviation1 = _appSettings.MaxACFDescriptorDeviation1;
            _processor.finder.maxACFDescriptorDeviation2 = _appSettings.MaxACFDescriptorDeviation2;
            _processor.finder.maxACFDescriptorDeviation3 = _appSettings.MaxACFDescriptorDeviation3;
            _processor.finder.maxACFDescriptorDeviation4 = _appSettings.MaxACFDescriptorDeviation4;
            _processor.finder.maxACFDescriptorDeviation5 = _appSettings.MaxACFDescriptorDeviation5;
            _processor.finder.maxACFDescriptorDeviation6 = _appSettings.MaxACFDescriptorDeviation6;
            _processor.finder.maxACFDescriptorDeviation7 = _appSettings.MaxACFDescriptorDeviation7;
            _processor.finder.maxACFDescriptorDeviation8 = _appSettings.MaxACFDescriptorDeviation8;
            _processor.finder.maxACFDescriptorDeviation9 = _appSettings.MaxACFDescriptorDeviation9;
            _processor.finder.maxACFNormaDeviation = _appSettings.MaxACFNormaDeviation0; //Auto correlation norma deviation
            _processor.finder.maxACFNormaDeviation0 = _appSettings.MaxACFNormaDeviation0;
            _processor.finder.maxACFNormaDeviation1 = _appSettings.MaxACFNormaDeviation1;
            _processor.finder.maxACFNormaDeviation2 = _appSettings.MaxACFNormaDeviation2;
            _processor.finder.maxACFNormaDeviation3 = _appSettings.MaxACFNormaDeviation3;
            _processor.finder.maxACFNormaDeviation4 = _appSettings.MaxACFNormaDeviation4;
            _processor.finder.maxACFNormaDeviation5 = _appSettings.MaxACFNormaDeviation5;
            _processor.finder.maxACFNormaDeviation6 = _appSettings.MaxACFNormaDeviation6;
            _processor.finder.maxACFNormaDeviation7 = _appSettings.MaxACFNormaDeviation7;
            _processor.finder.maxACFNormaDeviation8 = _appSettings.MaxACFNormaDeviation8;
            _processor.finder.maxACFNormaDeviation9 = _appSettings.MaxACFNormaDeviation9;
            _processor.finder.minACF = _appSettings.MinACF;
            _processor.finder.minICF = _appSettings.MinICF;
            _processor.blur = false;
            _processor.noiseFilter = false;
            _processor.cannyThreshold = _appSettings.СannyThreshold;
            _processor.adaptiveThresholdBlockSize = _appSettings.AdaptiveThresholdBlockSize;
            _processor.adaptiveThresholdParameter = _appSettings.AdaptiveThresholdParameter; //false ? 1.5 : 0.5;

            LoadTemplates(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +
                          Path.DirectorySeparatorChar + _appSettings.TemplatesFile, _processor);
        }