concatenate contour vector [closed]
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
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).
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
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
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
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.........