简体   繁体   English

检测图像中的矩形会产生不需要的结果(opencv,java)

[英]Detecting rectangle in image gives unwanted result (opencv, java)

I have this picture that I have done thresholding on to convert it to binary image and black and white. 我有这张图片,我已经进行了阈值处理,将其转换为二进制图像和黑白图像。 See the picture below: 见下图:

例1

I want to extract each box with a letter, this was done by the following code: 我想用一个字母提取每个框,这是通过以下代码完成的:

 List<MatOfPoint> contours = new ArrayList<MatOfPoint>();  
  Imgproc.findContours(destination3, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);

         MatOfPoint2f approxCurve = new MatOfPoint2f();
         int x = 1;
         //For each contour found
         for (int i=0; i<contours.size(); i++)
         {
             //Convert contours(i) from MatOfPoint to MatOfPoint2f
             MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );
             //Processing on mMOP2f1 which is in type MatOfPoint2f
             double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
             Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

             //Convert back to MatOfPoint
             MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

             // Get bounding rect of contour
             Rect rect = Imgproc.boundingRect(points);
             if(rect.height > 50 && rect.height < 100) {
              // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
             //Core.rectangle(destination, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(255, 0, 255), 3); 
             Rect roi = new Rect(rect.x, rect.y, rect.width, rect.height);
             Mat cropped = new Mat(destination3, roi);
             Highgui.imwrite("letter"+x+".jpg", cropped);
             x++;
             }
         }

But a letter extracted is totally black, it seems like it fills in the white letters as well as seen in the picture below. 但提取的字母完全是黑色的,似乎它填写了白色字母以及下图所示。 How do I fix this? 我该如何解决? What is wrong with my code? 我的代码出了什么问题?

电流输出

From the documentation of findContours() : (emphasis mine) findContours()的文档:(强调我的)

Note: Source image is modified by this function. 注意: 此图像功能会修改源图像。 Also, the function does not take into account 1-pixel border of the image (it's filled with 0's and used for neighbor analysis in the algorithm), therefore the contours touching the image border will be clipped. 此外,该功能不考虑图像的1像素边界(它填充0并用于算法中的邻域分析),因此将剪切触摸图像边界的轮廓。

The strange image you see is a result of the modifications findContours() makes to destination3 . 您看到的奇怪图像是findContours()destination3进行修改的结果。 You should be able to get correct results by passing a deep copy of your data to findContours() , by calling clone() . 您应该能够通过调用clone()将数据的深层副本传递给findContours()来获得正确的结果。 The line where you invoke findContours() would then look like this: 您调用findContours()行将如下所示:

Imgproc.findContours(destination3.clone(), contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
                             //   ^^ Vive la difference!

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

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