concatenate contour vector [closed]

asked 2014-02-28 13:58:59 -0600

Lupin gravatar image

updated 2014-02-28 14:08:23 -0600

berak gravatar image

Hi

Firstly, I am trying to recognize the shape of an object, compare it with the database and give out the result.

I successfully extract the contour of the test object as well as training database by function

cv::findcontour

As I care only about the outer shape, the contour output is taken as contour[0] for comparision

cv::findContours(input,contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,cv::Point(0,0));

I would like to use cv::matchshape function with Hu moment, but before that, I will combine contour of all database image into on vector like below

std::vector<std::vector<cv::Point> > trainingDatabase;
//trainingDatabase contain all the outer contours of database image

then when making comparision, I only have to do like this

for (int i=0;i<trainingDatabase.size();i++){
double result=cv::matchShapes(contour[0],trainingData[i],CV_CONTOURS_MATCH_I1,0);}

But could you please suggest me how to concatenate all the contour into one single trainingDatabase vector?

Thank you very much

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-02 08:16:19.329961

Comments

imho, you can't concatenate them. each of them is an ordered pointlist, joining them will destroy the order (or , at least, won't establish the same kind of order for the concateneted one).

berak gravatar imageberak ( 2014-02-28 14:06:40 -0600 )edit

Are you trying to group multiple contours that are associated with a single object? In other words, is your target composed of multiple contours that when combined tend to show the outline of the object

Will Stewart gravatar imageWill Stewart ( 2014-02-28 14:13:01 -0600 )edit
3

let me put it this way: ofc you can throw all points into one list, but then you loose the 'closed line segment' property, and end up with an unordered pointcloud. applying matchShapes/humoments to this does not make much sense then. ( hausdorff distance, maybe )

humoments require a single blob.

if you got 2 of them, whether you join them into a single array or not, their outlines remain separated.

imho, instead of joining all your contours into one, better find a smarter distance measure for a list of contours

berak gravatar imageberak ( 2014-02-28 14:24:52 -0600 )edit

Thank you very much for your reply.

You are quite right, it is not possible to do it in such a primitive way. As matchShape function seems to focus only on 2 contour-comparision, I decided to implement K-Nearest neighbour matching. The result is much nicer, and most importantly the calculation time is shorter than matchShapes function

Looks like knn will be my choice.

@berak: you are right, I should find out a smarter distance mesurement from beginning

Lupin gravatar imageLupin ( 2014-03-02 16:15:16 -0600 )edit

with knn in OpenCV, the task is done only by push contour vector into a "super vector", which holds the training data to train classifier. I should think of it from beginning.........

Lupin gravatar imageLupin ( 2014-03-02 16:18:46 -0600 )edit