Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is it a bug in connectedComponentsWithStats?

As some demand, I want to get all small rectangle in this image

Of course, If you are old version. we can use findcontour and boundingRect to do such thing. But since we have connectedComponentsWithStats now, I try to use it to ease my life. This is my current try:

//OpenCV 3.3.1 project
#include<opencv.hpp>
#include<string>
using namespace std;
using namespace cv;

Mat highlight(Mat, Mat);
int main() {
    Mat mask(5, 5, CV_8UC1, Scalar(0));
    for (int i = 0; i < mask.rows; i++) {
        uchar* data = mask.ptr<uchar>(i);
        for (int j = 0; j < mask.cols; j++)
            if ((i + j) % 2 == 0)
                data[j] = 255;
    }

    resize(mask, mask, Size(500, 400), 0,0, INTER_NEAREST);
    Mat element = getStructuringElement(MORPH_RECT, Size(2,2));
    erode(mask, mask, element);
    Mat labels, stats, centroids;
    int nmask = connectedComponentsWithStats(mask, labels, stats, centroids, 4, CV_16U);
    stats.convertTo(stats, CV_8U);
    Mat temImg = Mat(mask.size(), CV_8UC1, Scalar(0));

    vector<Mat> rects;
    for (int i = 1; i < nmask; i++) {
        uchar* data = stats.ptr<uchar>(i);
        rectangle(temImg, Rect(data[0], data[1], data[2], data[3]), Scalar(255));
    }
    imshow("temImg", temImg);

    waitKey();
    return 0;
}

I'm very confused, why I will get these rectangle??

Is it a bug in my code or a bug in the new function connectedComponentsWithStats?