简体   繁体   中英

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)

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.

The strange image you see is a result of the modifications findContours() makes to destination3 . You should be able to get correct results by passing a deep copy of your data to findContours() , by calling clone() . The line where you invoke findContours() would then look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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