2013-07-12 138 views
3

我能夠檢測圖像中最大的方形/矩形(綠色)。但是,我想將圖像中檢測到的最大方形/矩形轉換爲新圖像(存儲在新的Mat中)。OpenCV Android使用最大輪廓的邊緣創建新圖像

這裏的這個功能上有最大的矩形/平方米的返回圖像:http://img153.imageshack.us/img153/9308/nn4w.png

這裏是我到目前爲止的代碼:

private Mat findLargestRectangle(Mat original_image) { 
    Mat imgSource = original_image; 

    //convert the image to black and white 
    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY); 

    //convert the image to black and white does (8 bit) 
    Imgproc.Canny(imgSource, imgSource, 50, 50); 

    //apply gaussian blur to smoothen lines of dots 
    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5); 

    //find the contours 
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); 

    double maxArea = -1; 
    int maxAreaIdx = -1; 
    MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point 
    MatOfPoint2f approxCurve = new MatOfPoint2f(); 
    Mat largest_contour = contours.get(0); 
    List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>(); 
    for (int idx = 0; idx < contours.size(); idx++) { 
     temp_contour = contours.get(idx); 
     double contourarea = Imgproc.contourArea(temp_contour); 
     //compare this contour to the previous largest contour found 
     if (contourarea > maxArea) { 
      //check if this contour is a square 
      MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray()); 
      int contourSize = (int)temp_contour.total(); 
      Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true); 
      if (approxCurve.total() == 4) { 
       maxArea = contourarea; 
       maxAreaIdx = idx; 
       largest_contours.add(temp_contour); 
       largest_contour = temp_contour; 
      } 
     } 
    } 
    MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1); 
    largest_contours = new ArrayList<MatOfPoint>(); 
    largest_contours.add(temp_largest); 

    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB); 
    Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1); 

    //create the new image here using the largest detected square 

    Toast.makeText(getApplicationContext(), "Largest Contour: ", Toast.LENGTH_LONG).show(); 

    return imgSource; 
} 

變量largest_contours是MatOfPoint列表但只包含最大輪廓,還存儲在largest_contour變量中。我怎樣才能從最大的輪廓創造一個新的形象?

我正在使用的Android OpenCV的和有用於檢測圖像的幾個教程,但不完全是關於如何使用Imgproc.warpPerspective()

謝謝!

+0

當你說「做一個新的形象」時,你是什麼意思?這個「新形象」包含什麼? – Aurelius

+0

你好@Aurelius,這是我試圖實現http://stackoverflow.com/questions/17512234/android-opencv-find-largest-square-or-rectangle。我幾個星期前發佈了這個,現在我可以檢測圖像中最大的方形輪廓,我想裁剪該圖像並創建一個新的。 –

+0

我成功使用此代碼找出最大的矩形。我也找到了一種方法來保存最終的圖像。由此產生的圖像黑白。我怎樣才能防止呢?我希望它被着色。 – TharakaNirmana

回答

1

所有你需要的是找到這個輪廓的角。您可以使用extreme points的方法。

您應該簡單地找出具有最小x和最小y(這是您的最小值),最小x最大y(這是您的左下角)的點,依此類推。

在C++中,有一個名爲algoritm的庫,它有min/max方法。例如min_element將幫助您找到具有最小x或y的點。不要忘記包含標題。

獲得4分後,可以使用透視變換。

先輸入您的積分到this method。目的地應該是

Point2f dest[4] = {(0,0),(image.width,0),(0,image.height),(image.height,image.width)} 

您的情況。使用此方法獲得的矩陣(M)將使用another method將您的點轉換爲目標。

祝你好運。

編輯:關於第二個想法;因爲你的輪廓不是平行四邊形,所以你的極端點爲min x = bottomleft,min y = topleft,等等。然後找到最小元素更容易。

+0

你好巴西,謝謝你的迴應。感謝確定極值點的算法。四邊形可以是平行四邊形,但圖像來自移動相機。再次感謝! –

+0

你好Canberk Ba​​ci,這是我迄今取得的進展:http://stackoverflow.com/questions/17637730/android-opencv-getperspectivetransform-and-warpperspective你是這個項目的一大幫助。謝謝! –

+0

我不認爲極端的方法適用於角度旋轉的矩形(其邊不平行於座標軸)。 –

0

這不是在新Mat上使用drawContour的方式。 This document will help you.

正確的方法是

Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); 

    ... 

    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB); 
    Imgproc.drawContours(imgSource, contours, maxAreaIdx, new Scalar(0, 255, 0), 1); 
+0

謝謝@Thomas,這確實幫助了很多處理,因爲我不需要最大的數組列表。但我仍然非常需要裁剪最大的輪廓,並從中創建一個新的圖像。再次感謝! –