Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to get a connected component in one Mat

I have such image

I hope to get one connected component in one Mat with an efficient method. So I have write such code to implement it.

 #include<opencv.hpp>
using namespace std;
using namespace cv;

int main() {

    Mat bin_img = Mat::zeros(10, 10, CV_8UC1);
    rectangle(bin_img, Point(1, 2), Point(3, 4), Scalar(255), -1);
    circle(bin_img, Point(5, 7), 2, Scalar(255), -1);

    int img_height = bin_img.rows, img_width = bin_img.cols;
    Mat labels;
    int n = connectedComponents(bin_img, labels);
    vector<Mat> masks(n - 1, Mat::zeros(bin_img.size(), CV_8UC1));

    Mat mask1 = masks[0];
    Mat mask2 = masks[1];
    for (int i = 0; i < img_height; i++)
    {
        int*plabels = labels.ptr<int>(i);
        for (int j = 0; j < img_width; j++)
        {
            cout << plabels[j] << " ";
            if (plabels[j] != 0)
            {
                uchar* pmask = masks[plabels[j] - 1].ptr<uchar>(i);
                pmask[j] = 255;
            }
        }
        cout << endl;

    }
    for (int i = 0; i < n - 1; i++)
        imshow(to_string(i), masks[i]);

    waitKey(0);
    return 0;
}

But I don't know why all my element in vector masks is same totally. Could anybody can give me a hand?

How to get a connected component in one Mat

I have such image

I hope to get one connected component in one Mat with an efficient method. So I have write such code to implement it.

 #include<opencv.hpp>
using namespace std;
using namespace cv;

int main() {

    Mat bin_img = Mat::zeros(10, 10, CV_8UC1);
    rectangle(bin_img, Point(1, 2), Point(3, 4), Scalar(255), -1);
    circle(bin_img, Point(5, 7), 2, Scalar(255), -1);

    int img_height = bin_img.rows, img_width = bin_img.cols;
    Mat labels;
    int n = connectedComponents(bin_img, labels);
    vector<Mat> masks(n - 1, Mat::zeros(bin_img.size(), CV_8UC1));

    Mat mask1 = masks[0];
    Mat mask2 = masks[1];
    for (int i = 0; i < img_height; i++)
    {
        int*plabels = labels.ptr<int>(i);
        for (int j = 0; j < img_width; j++)
        {
            cout << plabels[j] << " ";
            if (plabels[j] != 0)
            {
                uchar* pmask = masks[plabels[j] - 1].ptr<uchar>(i);
                pmask[j] = 255;
            }
        }
        cout << endl;

    }
    for (int i = 0; i < n - 1; i++)
        imshow(to_string(i), masks[i]);

    waitKey(0);
    return 0;
}

But I don't know why all my element in vector masks is same totally. Could anybody can give me a hand?

How to get a connected component in one Mat

I have such image

I hope to get one connected component in one Mat with an efficient method. So I have write such code to implement it.

 #include<opencv.hpp>
using namespace std;
using namespace cv;

int main() {

    Mat bin_img = Mat::zeros(10, 10, CV_8UC1);
    rectangle(bin_img, Point(1, 2), Point(3, 4), Scalar(255), -1);
    circle(bin_img, Point(5, 7), 2, Scalar(255), -1);

    int img_height = bin_img.rows, img_width = bin_img.cols;
    Mat labels;
    int n = connectedComponents(bin_img, labels);
    vector<Mat> masks(n - 1, Mat::zeros(bin_img.size(), CV_8UC1));

    Mat mask1 = masks[0];
    Mat mask2 = masks[1];
    for (int i = 0; i < img_height; i++)
    {
        int*plabels = labels.ptr<int>(i);
        for (int j = 0; j < img_width; j++)
        {
            if (plabels[j] != 0)
            {
                uchar* pmask = masks[plabels[j] - 1].ptr<uchar>(i);
                pmask[j] = 255;
            }
        }
        cout << endl;

    }
    for (int i = 0; i < n - 1; i++)
        imshow(to_string(i), masks[i]);

    waitKey(0);
    return 0;
}

But I don't know why all my element in vector masks is same totally.


update

Of course, I know there are some method can do this, such as compare in fllowing anser or inRange or operator ==. But I hope to use .ptr method here. Could anybody can give me a hand?