OpenCV, how to use arrays of points for smoothing and sampling contours?
Another possibility is to use the algorithm openFrameworks uses:
https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/graphics/ofPolyline.cpp#L416-459
It traverses the contour and essentially applies a low-pass filter using the points around it. Should do exactly what you want with low overhead and (there's no reason to do a big filter on an image that's essentially just a contour).
Your Gaussian blurring doesn't work because you're blurring in column direction, but there is only one column. Using GaussianBlur()
leads to a "feature not implemented" error in OpenCV when trying to copy the vector back to a cv::Mat
(that's probably why you have this strange resize()
in your code), but everything works fine using cv::blur()
, no need to resize()
. Try Size(0,41) for example. Using cv::BORDER_WRAP
for the border issue doesn't seem to work either, but here is another thread of someone who found a workaround for that.
Oh... one more thing: you said that your contours are likely to be much smaller. Smoothing your contour that way will shrink it. The extreme case is k = size_of_contour
, which results in a single point. So don't choose your k too big.
How about approxPolyDP()?
It uses this algorithm to 'smooth' a contour (basically gettig rid of most of the contour's points and leave the ones that represent a good approximation of your contour)