2013-06-26 33 views
1

我實際上正在使用cv :: Mat和B & W像素。 我正在尋找一種方法來獲得我的黑點在這個墊子的清單。獲取cv的黑色像素列表:: Mat

有人知道如何做這種事嗎?

我想這樣做,因爲我想檢測這個點的邊界矩形。 (最好是讓他們回到在載體中)

somekind的的:

cv::Mat blackAndWhite; 
std::vector<cv::Point> blackPixels = MAGIC_FUNCTION(blackAndWhite); 

感謝您的幫助。

編輯:我想要確切地說我想要最佳實踐,越是遵循Opencv越好。

回答

2

可以遍歷cv::Mat檢查是0的像素,並獲得來自線性指數的x和y座標如果基質是在存儲器中連續:

// assuming your matrix is CV_8U, and black is 0 
std::vector<cv::Point> blackPixels; 
unsigned int const *p = blackAndWhite.ptr<unsigned char>(); 
for(int i = 0; i < blackAndWhite.rows * blackAndWhite.cols; ++i, ++p) 
{ 
    if(*p == 0) 
    { 
    int x = i % blackAndWhite.cols; 
    int y = i/blackAndWhite.cols; 
    blackPixels.push_back(cv::Point(x, y)); 
    } 
} 
+0

我想過,但我正在尋找你知道的最好的工具。更符合Opencv的標準。 – Poko

+0

我的第一個想法是cv :: compare函數,但它返回另一個cv :: Mat,所以問題仍然保持不變。 – ChronoTrigger

+0

我可能會採用你的詭計,我會讓你知道的。 – Poko

2

這個來自OpenCV的例子展示瞭如何做到你想要的:Creating Bounding boxes and circles for contours。基本上,它是:

// ... 
    /// Find contours 
    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    /// Approximate contours to polygons + get bounding rects and circles 
    vector<vector<Point> > contours_poly(contours.size()); 
    vector<Rect> boundRect(contours.size()); 
    vector<Point2f>center(contours.size()); 
    vector<float>radius(contours.size()); 

    for(int i = 0; i < contours.size(); i++) 
    { 
    approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); 
    boundRect[i] = boundingRect(Mat(contours_poly[i])); 
    minEnclosingCircle((Mat)contours_poly[i], center[i], radius[i]); 
    }