Ask Your Question

Getting full objects on a binary image (C++)

asked 2016-09-04 05:14:04 -0500

bivalvo gravatar image

Hi there,

I have a binary image like this:

image description

When I apply findContours function to the image I get one vector<vector<point> > which contains a set of points for each object detected. So, when I try to draw the first element of the vector<vector<point> > variable I get the next output image:

image description

If I draw the same image using the parameter CV_FILLED, I get the next result:

image description

My answer is: Can I get the whole pixels of each object, instead the pixels of the contours? So when I paint all the pixels of the vector I get the image #3. I want to process all the pixels of an object, not only the contours.

Thank you.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-09-04 05:20:08 -0500

berak gravatar image

connectedComponents returns a "labels" image, where each blob has its own id.

using that, Mat firstBlob = (labels==1); would paint your example image.

edit flag offensive delete link more


Excuse me, I didn't explained well myself.

What I want to get is not a image with the object painted. What I want is to get are the whole points of an object, for processing the X's and Y's values. Similar information to what findContours returns, but with the whole pixels of the objects, in this case.

bivalvo gravatar imagebivalvo ( 2016-09-04 06:03:33 -0500 )edit

imho, it's still the same answer: look at connectedComponents().

berak gravatar imageberak ( 2016-09-04 06:30:21 -0500 )edit

also you can take a look at the sample connected_components.cpp

sturkmen gravatar imagesturkmen ( 2016-09-04 07:07:14 -0500 )edit

Sorry, berak, but I'm working on a project with OpenCV 2.4.8. and connectedComponents() function is not available. Any other way of getting a vector<vector <point=""> >?

bivalvo gravatar imagebivalvo ( 2016-09-05 08:26:28 -0500 )edit

oh, that's unfortunate. (but , since 2.4.8 is rather old, what about an update ?)

if you have to stick with your current version, probably going back to your original idea (drawing filled regions) and then enumerating the "on" pixels with findNonZero() might be a dumb fallback.

berak gravatar imageberak ( 2016-09-05 08:34:25 -0500 )edit

Question Tools

1 follower


Asked: 2016-09-04 05:14:04 -0500

Seen: 246 times

Last updated: Sep 04 '16