Ask Your Question
0

drawning labeling components in a image opencv c++

asked 2016-12-30 19:11:11 -0600

alexander33 gravatar image

I have the next image.

original image

I used the function connectedComponentsWithStats for find the components of the image. now i want draw only the components with an area > 3000, in this case there are 3 of the 5 components that the function found, i used the function "compare" for draw the 3 components but the result is not expected (only draw 1 component), Some idea for get the final result? thank you. my result is:

final result

int main(int argc, const char** argv)

{

img = imread("blob.png", 0);

if (img.empty())
{
    cout << "Could not read input image file: " << endl;
    return -1;
}

namedWindow("Image", 1);
imshow("Image", img);

Mat labelImage(img.size(), CV_32S);
Mat stats, centroids;
int nLabels = connectedComponentsWithStats(img, labelImage, stats, centroids, 8, CV_32S);
std::vector<Vec3b> colors(nLabels);
std::vector<int> labels_finals;
colors[0] = Vec3b(0, 0, 0);//background


for (int label = 1; label < nLabels; ++label){ //label  0 is the background
    if ((stats.at<int>(label, CC_STAT_AREA)) > 3000){
        labels_finals.push_back(label);
        //cout << "hola" << endl;
    }

    cout << "area del component: " << label << "-> " << stats.at<int>(label, CC_STAT_AREA) << endl;
    //colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
    colors[label] = Vec3b(0, 255, 0);
}


Mat dst(img.size(), CV_8UC3);
for (int r = 0; r < dst.rows; ++r){
    for (int c = 0; c < dst.cols; ++c){

        int label = labelImage.at<int>(r, c);
        //cout << "label:  " << label << endl; 
        Vec3b &pixel = dst.at<Vec3b>(r, c);//accesa al elemento 
        pixel = colors[label];
    }
}

Mat dst2(img.size(), CV_8UC3);

for (int i = 0; i < labels_finals.size(); ++i){
    std::cout << "path i:  " << labels_finals[i] << ' ' << endl;
    compare(labelImage, labels_finals[i], dst2, CMP_EQ);
}

imshow("compare imagem ", dst2);

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-12-31 03:14:59 -0600

LBerger gravatar image

updated 2016-12-31 03:22:07 -0600

Something like this should work :

    Mat img=imread("g:/lib/opencv/samples/data/lena.jpg",IMREAD_GRAYSCALE);
    Mat imBin;
    threshold(img,imBin,180,255,THRESH_BINARY);
    imshow("Result", imBin);
    waitKey();
    Mat stats, centroids, labelImage;
    int nLabels = connectedComponentsWithStats(imBin, labelImage, stats, centroids, 8, CV_32S);
    Mat mask(labelImage.size(), CV_8UC1, Scalar(0));
    Mat surfSup=stats.col(4)>2000;

    for (int i = 1; i < nLabels; i++)
    {
        if (surfSup.at<uchar>(i, 0))
        {
            mask = mask | (labelImage==i);
        }
    }
    Mat r(img.size(), CV_8UC1, Scalar(0));
    img.copyTo(r,mask);
    imshow("Result", r);
    waitKey();
edit flag offensive delete link more

Comments

2

Thank you, @LBerger, works very fine.....

alexander33 gravatar imagealexander33 ( 2016-12-31 04:14:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-12-30 19:11:11 -0600

Seen: 13,045 times

Last updated: Dec 31 '16