簡體   English   中英

檢測圖像中的矩形會產生不需要的結果(opencv,java)

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

我有這張圖片,我已經進行了閾值處理,將其轉換為二進制圖像和黑白圖像。 見下圖:

例1

我想用一個字母提取每個框,這是通過以下代碼完成的:

 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++;
             }
         }

但提取的字母完全是黑色的,似乎它填寫了白色字母以及下圖所示。 我該如何解決? 我的代碼出了什么問題?

電流輸出

findContours()的文檔:(強調我的)

注意: 此圖像功能會修改源圖像。 此外,該功能不考慮圖像的1像素邊界(它填充0並用於算法中的鄰域分析),因此將剪切觸摸圖像邊界的輪廓。

您看到的奇怪圖像是findContours()destination3進行修改的結果。 您應該能夠通過調用clone()將數據的深層副本傳遞給findContours()來獲得正確的結果。 您調用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