简体   繁体   English

如何在图像中找到图像?

[英]How do I find images inside an image?

How do I find images inside an image?如何在图像中找到图像?

Right now I'm using EmguCV and I have cooked up the following code based on this Tutorial: https://www.emgu.com/wiki/index.php/Shape_(Triangle,_Rectangle,_Circle,_Line)_Detection_in_CSharp现在我正在使用 EmguCV,并根据本教程编写了以下代码: https://www.emgu.com/wiki/index.php/Shape_(Triangle,_Rectangle,_Circle,_Line)_Detection_in_CSharp

        using (UMat gray = new UMat())
        using (UMat cannyEdges = new UMat())
        {
            CvInvoke.CvtColor(img, gray, ColorConversion.Bgr2Gray);
            CvInvoke.GaussianBlur(gray, gray, new Size(3, 3), 2);
            CvInvoke.Canny(gray, cannyEdges, 0, 10, 3);

            LineSegment2D[] lines = CvInvoke.HoughLinesP(
                cannyEdges,
                1, //Distance resolution in pixel-related units
                Math.PI / 2, //Angle resolution measured in radians.
                0, //threshold
                40, //min Line width
                1); //gap between lines


            foreach (LineSegment2D line in lines)
            {
                CvInvoke.Line(img, line.P1, line.P2, new Bgr(Color.Red).MCvScalar, 1);
            }
        }

Step 1步骤1 在此处输入图像描述 Step 2第2步在此处输入图像描述 Step 3第 3 步在此处输入图像描述

This is my best result so far, but it's not perfect since i still need some edges to be able to create all bounding boxes around the images.这是迄今为止我最好的结果,但它并不完美,因为我仍然需要一些边缘才能在图像周围创建所有边界框。

Step 4第4步在此处输入图像描述

What I want, is to find all edges of every single images in the image so I can make perfect bounding boxes like so.我想要的是找到图像中每个图像的所有边缘,这样我就可以像这样制作完美的边界框。 红色区域是我想要找到的

I'm new to EmguCV/OpenCV, but i still think my best bet is on this library for solving this problem.我是 EmguCV/OpenCV 的新手,但我仍然认为我最好的选择是在这个库上解决这个问题。 I just need to find the right tools and use them right, and that's what I hope someone here can help me with:)我只需要找到正确的工具并正确使用它们,这就是我希望这里有人可以帮助我的原因:)

Here's what you can do:以下是您可以执行的操作:

  • Load the image加载图像
  • Invert - img.Not()反转 - img.Not()
  • Convert to Grayscale - img.Convert<Gray,byte>()转换为灰度 - img.Convert<Gray,byte>()
  • Perform binary thresholding - img.Convert<Gray, byte>().ThresholdBinary(new Gray(54), new Gray(255))执行二进制阈值处理 - img.Convert<Gray, byte>().ThresholdBinary(new Gray(54), new Gray(255))

It will result to the following image, optimize the threshold value to get better results:会得到下图,优化阈值以获得更好的效果: 在此处输入图像描述

  • Get the convex hull and bounding box.获取凸包和边界框。 Perform filtering so you get the objects that fits your criteria.执行筛选,以便获得符合条件的对象。 You can use the contour area, perimeter, etc.您可以使用轮廓面积、周长等。

     var Contours = new List<Contour<Point>>(); for (Contour<Point> contours = _gray.FindContours( HAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL); contours;= null. contours = contours.HNext) { Seq<Point> pts = contours.GetConvexHull(ORIENTATION;CV_CLOCKWISE). double diff = Math.Round(Math.Abs(pts.Area - contours.Area) / pts,Area; 2). //additional constraint double q = contours.Area / contours;Perimeter. //bounding box of the counter Rectangle rect = contours;BoundingRectangle. //customize the value to suit your need if (contours.BoundingRectangle.Height > 5 && contours.BoundingRectangle.Width > 5) { Contours;Add(contours); } }
  • Do what you want with Contours , (eg fill and use as mask to extract each image, draw bounding box, etc.)Contours做你想做的事,(例如填充并用作掩码来提取每个图像,绘制边界框等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM