Exemplo n.º 1
0
        /* Match templates to given image, if defects found then return 1, else return 0: */
        public int Match_templates(Mat image, List <Mat> templates)
        {
            Image <Bgr, Byte>  img      = image.ToImage <Bgr, Byte>();
            Image <Gray, Byte> img_mask = new Image <Gray, Byte>(image.Size.Width, image.Size.Height);

            Mat res = new Mat();
            List <MinMax_info> found   = new List <MinMax_info>();
            double             min_val = 0;
            double             max_val = 0;
            Point min_loc = new Point();
            Point max_loc = new Point();

            foreach (Mat temp in templates)
            {
                CvInvoke.MatchTemplate(image, temp, res, TemplateMatchingType.CcoeffNormed);
                CvInvoke.Threshold(res, res, threshold, 255, ThresholdType.ToZero);
                while (true)
                {
                    CvInvoke.MinMaxLoc(res, ref min_val, ref max_val, ref min_loc, ref max_loc, null);
                    if (max_val >= threshold)
                    {
                        var MinMax_to_add = new MinMax_info
                        {
                            max_val   = max_val,
                            temp_size = temp.Size,
                            max_loc   = max_loc
                        };
                        found.Add(MinMax_to_add);
                        var box_size = new Size
                        {
                            Width  = Convert.ToInt32(Math.Round(temp.Size.Width / 1.5)),
                            Height = Convert.ToInt32(Math.Round(temp.Size.Height / 1.5))
                        };

                        Rectangle img_box = new Rectangle(max_loc, box_size);
                        CvInvoke.Rectangle(img, img_box, new MCvScalar(0, 0, 255), 1, LineType.EightConnected, 0);
                        CvInvoke.Rectangle(res, img_box, new MCvScalar(0, 0, 255), -1, LineType.EightConnected, 0);
                        CvInvoke.Rectangle(img_mask, img_box, new MCvScalar(255, 255, 255), -1, LineType.EightConnected, 0);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            CvInvoke.NamedWindow("Image", NamedWindowType.Normal);
            CvInvoke.Imshow("Image", img);
            CvInvoke.WaitKey();
            CvInvoke.DestroyAllWindows();
            CvInvoke.NamedWindow("Image", NamedWindowType.Normal);
            CvInvoke.Imshow("Image", img_mask);
            CvInvoke.WaitKey();
            CvInvoke.DestroyAllWindows();
            Console.WriteLine(found.Count);



            return(0);
        }
Exemplo n.º 2
0
        /* Finds barcode in a given Image:
         * Returns Mat containing barcode only. */
        public Mat Find_barcode(Mat img)
        {
            //var img = CvInvoke.Imread(img_path, ImreadModes.Color);
            /*  Load in templates: */
            List <Mat> template = new List <Mat>();

            Image <Bgr, Byte> imgCV = new Image <Bgr, byte>(Properties.Resources.barcode4);
            Mat imgMAT = imgCV.Mat;

            template.Add(imgMAT);

            var resized = new Mat();
            var res     = new Mat();
            /* Barcode location Information initilization: */
            MinMax_info found;

            found.is_none      = true;
            found.max_val      = 0;
            found.bottom_right = new Point(0, 0);
            found.top_left     = new Point(0, 0);
            /* set up linspace loop for templates scaling: */
            double start_val = 1;
            double stop_val = 4;
            double step_val = Linspace_step(start_val, stop_val, 10);
            double min_val = 0; double max_val = 0; Point min_loc = new Point(); Point max_loc = new Point();

            /* Loop through scale increments to find barcode location: */
            for (int i = 0; i < template.Count; i++)
            {
                for (double scale = start_val; scale < stop_val; scale += step_val)
                {
                    CvInvoke.Resize(template[i], resized, new System.Drawing.Size(Convert.ToInt32(scale * template[i].Size.Width), Convert.ToInt32(scale * template[i].Size.Height)));
                    if (template[i].Size.Height < img.Size.Height && template[i].Size.Width < img.Size.Width)
                    {
                        CvInvoke.MatchTemplate(img, resized, res, TemplateMatchingType.CcoeffNormed);
                        CvInvoke.MinMaxLoc(res, ref min_val, ref max_val, ref min_loc, ref max_loc, null);
                        if (found.is_none == true || max_val > found.max_val)
                        {
                            found.is_none        = false;
                            found.max_val        = max_val;
                            found.top_left       = max_loc;
                            found.bottom_right.X = max_loc.X + resized.Size.Width;
                            found.bottom_right.Y = max_loc.Y + resized.Size.Height;
                        }
                    }
                }
            }
            int       offset  = -10;
            Rectangle img_box = new Rectangle(found.top_left.X - offset, found.top_left.Y - offset, found.bottom_right.X - found.top_left.X + offset, found.bottom_right.Y - found.top_left.Y + offset);

            barcode_info = found;
            return(Crop_image(img, img_box));
        }