Ask Your Question
0

How to retrieve a std::vector of points from cv::Mat?

asked 2014-12-01 11:44:14 -0500

wal-o-mat gravatar image

I have a 'cv::Mat' with labeled blobs. So I am able to retrieve a mask for each component. Now what I am interested in are the indices of all points of a mask. The reason is that I want to use 'cv::fitLine' with these points, or any other minimizer to match data with my model. The only idea I have is iterating over the image and manually collect the indices, but maybe there is a smarter way? Maybe converting a (dense) matrix to a sparse one or some such?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2014-12-01 18:26:57 -0500

You could implement some kind of tree search. Start with a point in your mask and add its neighbours to a stack if they are also in the masked region and remove (set to zero) your starting point. Do the same until your stack is empty. This gives you a running time of O(N) if N is the number of pixels in your mask instead of O(colsrows). This of course only works well if your mask is basically one region. If it consists of many small regions, you will have to look at your whole picture to find each region so that you could again end up in something like O(colsrows).

edit flag offensive delete link more
0

answered 2014-12-02 05:29:32 -0500

If I understood it correctly, the task you are looking for is performed by the findCountours function. The findContours method outputs a vector<vector<cv::Point> >. If you apply findCountours in your Mat you will have as many vector<cv::Point> as blobs in your binary image, and each of these vectors contains all the cv::Points that make the blob.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-12-01 11:44:14 -0500

Seen: 1,017 times

Last updated: Dec 02 '14