2016-03-08 65 views
0

我幾周前開始使用opencv。我想知道是否有從輪廓列表中找出最亮輪廓並繪製最亮的輪廓的功能。到目前爲止,我設法轉換灰度,閾值圖像和使用findContour函數來查找圖像中的所有輪廓。如何通過亮度過濾輪廓

嘗試使用minMax函數,但無法找到它在java中的使用方式。

public void process(Mat rgbaImage) {  
     Imgproc.threshold(rgbaImage,rgbaImage,230,255,Imgproc.THRESH_BINARY);  
     Imgproc.findContours(rgbaImage,contours,mHierarchy,Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); 

     /* for(int id = 0; id < contours.size();id++) { 

      double area = Imgproc.contourArea(contours.get(id)); 
      if (area > 8000){ 
       Log.i(TAG1, "contents founds at id" + id); 
      }  

     } */ 

    }` 

回答

0

如果您的「最亮」表示最亮的平均顏色,則可以使用cv :: mean(Mat src,Mat mask)。
可悲的是我只知道C++ OpenCV實現,但我認爲Java版本幾乎與C++一樣。

C++實施例:

Mat src; // This is your src image 
vector<vector<Point>> contours; // This is your array of contours 

findContours(src.clone(), contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image 

int brightestIdx = -1; 
int brightestColor = -1; 
for(int i=0; i<contours.size(); i++) 
{ 
    // First, make a mask image of each contour 
    Mat mask(src.cols, src.rows, CV_8U, Scalar(0)); 
    drawContours(mask, contours, i, Scalar(255), CV_FILLED); 

    // Second, calculate average brightness with mask 
    Scalar m = mean(src, mask); 

    // Finally, compare current average with previous one 
    if(m[0] > brightestColor) 
    { 
     brightestColor = m[0]; 
     brightestIdx = i; 
    } 
} 

// Now you've found the brightest index. 
// Do whatever you want. 

Mat brightest_only(src.cols, src.rows, CV_8U, Scalar(0)); 
drawContours(brightest_only, contours, brightestIdx, Scalar(255), 1);