Reputation:
I have a black and white image made up of lines, like the following:
I am trying to get the points that can describe the curve, so essentially the contour. However, contours trace the boundary, and so in the case of lines, it traces from start to end, and then back to start.
I am getting the contour as follows:
cv::findContours(src, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_NONE);
To try and get rid of points that are represented by backtracking i am running
approxPolyDP(contour, contour, epsilon, true);
However this still leaves points on the way back.
Is there a better way of solving this problem? My line drawings can be more complicated that this one.
Upvotes: 2
Views: 511
Reputation: 4342
The reason of this issue is that you are using the cv::RETR_TREE
as contour retrieval mode. This mode as the documentations says:
CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours.
So in your line you are finding inner and outer line of the main line. In this case it can be the solution to use (for only the outer line) CV_RETR_EXTERNAL instead of CV_RETR_TREE.
CV_RETR_EXTERNAL retrieves only the extreme outer contours.
Upvotes: 1