Ask Your Question

Revision history [back]

Detection of multiple objects for further processing.

I am new to this, but have written code to detect an object from an image and then apply background and foreground masks, It works great if there is only one object. I have some images where there are up to 4 different objects. If there are multiple objects my code just selects one and processes that. My code is as follows:

string imgPath = defTempPath + "\" + "picTemp.jpg";// main image to process sketchImg = Cv2.ImRead(imgPath, ImreadModes.AnyColor); if (sketchImg != null) { Mat img = new Mat(sketchImg.Rows, sketchImg.Cols, MatType.CV_8UC1); Cv2.CvtColor(sketchImg, img, ColorConversionCodes.BGR2GRAY); Mat I = new Mat(); Cv2.Threshold(img, I, 50, 255, ThresholdTypes.BinaryInv); //Cv2.Dilate(I, I, Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3))); OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchyIndexes; Cv2.FindContours(I, out contours, out hierarchyIndexes, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, null);

                int mind = -1;
                double marea = 0;
                int i = 0;
                while (i >= 0)
                {
                    double area = Cv2.ContourArea(contours[i]);
                    if (area > marea)
                    {
                        marea = area;
                        mind = i;
                    }
                    i = hierarchyIndexes[i].Next;
                }

                if (mind != -1)
                {
                    Mat mask1 = new Mat(img.Rows, img.Cols, sketchImg.Type());
                    mask1.SetTo(new Scalar(0, 0, 0));
                    Mat mask2 = new Mat(img.Rows, img.Cols, sketchImg.Type());
                    mask2.SetTo(new Scalar(0, 0, 0));
                    var contour = contours[mind];
                    List<OpenCvSharp.Point> points = contour.ToList();
                    List<List<OpenCvSharp.Point>> cnt = new List<List<OpenCvSharp.Point>>();
                    cnt.Add(points);

                    Cv2.FillPoly(mask1, cnt, new Scalar(255, 255, 255));

                    i = 0;
                    while (i >= 0)
                    {
                        cnt.Clear();
                        if (i == mind)
                        {
                            i = hierarchyIndexes[i].Next;
                        }
                        else
                        {
                            contour = contours[i];
                            points = contour.ToList();
                            cnt.Add(points);
                            Cv2.FillPoly(mask2, cnt, new Scalar(255, 255, 255));

                            i = hierarchyIndexes[i].Next;
                        }
                    }

                    Mat mask = new Mat();
                    Cv2.Subtract(mask1, mask2, mask);

                    if (bgImg == null)
                    {
                        bgImg = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                        bgImg.SetTo(new Scalar(0));
                    }
                    if (fgImg == null)
                    {
                        fgImg = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                        fgImg.SetTo(new Scalar(255));
                    }

                    bgImg = bgImg.Resize(sketchImg.Size());
                    fgImg = fgImg.Resize(sketchImg.Size());
                    Mat res1 = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                    Mat res2 = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                    res = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());


                    Cv2.BitwiseAnd(fgImg, mask, res1);
                    Cv2.BitwiseNot(mask, mask);
                    Cv2.BitwiseAnd(bgImg, mask, res2);
                    Cv2.Add(res1, res2, res);

               //     pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                    pictureBox2.BackgroundImage = new Bitmap(res.ToMemoryStream(), false);
                    Cv2.ImWrite(defaultOutputPath + "\\op.jpg", res);

here is a sample picture of an image with multiple objects - can not figure out how to add image to post. These are basically line drawings Vector drawings converted to an image.

Detection of multiple objects for further processing.

I am new to this, but have written code to detect an object from an image and then apply background and foreground masks, It works great if there is only one object. I have some images where there are up to 4 different objects. If there are multiple objects my code just selects one and processes that. My code is as follows:

 string imgPath = defTempPath + "\" "\\" + "picTemp.jpg";// main image to process
                sketchImg = Cv2.ImRead(imgPath, ImreadModes.AnyColor);
                if (sketchImg != null)
                {
                    Mat img = new Mat(sketchImg.Rows, sketchImg.Cols, MatType.CV_8UC1);
                    Cv2.CvtColor(sketchImg, img, ColorConversionCodes.BGR2GRAY);
                    Mat I = new Mat();
                    Cv2.Threshold(img, I, 50, 255, ThresholdTypes.BinaryInv);
                    //Cv2.Dilate(I, I, Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3)));
                    OpenCvSharp.Point[][] contours;
                    HierarchyIndex[] hierarchyIndexes;
                    Cv2.FindContours(I, out contours, out hierarchyIndexes, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, null);

null);
                 int mind = -1;
                 double marea = 0;
                 int i = 0;
                 while (i >= 0)
                 {
                     double area = Cv2.ContourArea(contours[i]);
                     if (area > marea)
                     {
                         marea = area;
                         mind = i;
                     }
                     i = hierarchyIndexes[i].Next;
                 }

                 if (mind != -1)
                 {
                     Mat mask1 = new Mat(img.Rows, img.Cols, sketchImg.Type());
                     mask1.SetTo(new Scalar(0, 0, 0));
                     Mat mask2 = new Mat(img.Rows, img.Cols, sketchImg.Type());
                     mask2.SetTo(new Scalar(0, 0, 0));
                     var contour = contours[mind];
                     List<OpenCvSharp.Point> points = contour.ToList();
                     List<List<OpenCvSharp.Point>> cnt = new List<List<OpenCvSharp.Point>>();
                     cnt.Add(points);

                     Cv2.FillPoly(mask1, cnt, new Scalar(255, 255, 255));

                     i = 0;
                     while (i >= 0)
                     {
                         cnt.Clear();
                         if (i == mind)
                         {
                             i = hierarchyIndexes[i].Next;
                         }
                         else
                         {
                             contour = contours[i];
                             points = contour.ToList();
                             cnt.Add(points);
                             Cv2.FillPoly(mask2, cnt, new Scalar(255, 255, 255));

                             i = hierarchyIndexes[i].Next;
                         }
                     }

                     Mat mask = new Mat();
                     Cv2.Subtract(mask1, mask2, mask);

                     if (bgImg == null)
                     {
                         bgImg = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                         bgImg.SetTo(new Scalar(0));
                     }
                     if (fgImg == null)
                     {
                         fgImg = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                         fgImg.SetTo(new Scalar(255));
                     }

                     bgImg = bgImg.Resize(sketchImg.Size());
                     fgImg = fgImg.Resize(sketchImg.Size());
                     Mat res1 = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                     Mat res2 = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());
                     res = new Mat(sketchImg.Rows, sketchImg.Cols, sketchImg.Type());


                     Cv2.BitwiseAnd(fgImg, mask, res1);
                     Cv2.BitwiseNot(mask, mask);
                     Cv2.BitwiseAnd(bgImg, mask, res2);
                     Cv2.Add(res1, res2, res);

                //     pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                     pictureBox2.BackgroundImage = new Bitmap(res.ToMemoryStream(), false);
                     Cv2.ImWrite(defaultOutputPath + "\\op.jpg", res);

here is a sample picture of an image with multiple objects - can not figure out how to add image to post. These are basically line drawings Vector drawings converted to an image.