OpenCV, how to use arrays of points for smoothing and sampling contours?

寵の児 提交于 2019-11-30 03:52:40
Ben

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.

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).

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)

weidongxu

From 2.1 OpenCV doc section Basic Structures:

template<typename T>
explicit Mat::Mat(const vector<T>& vec, bool copyData=false)

You probably want to set 2nd param to true in:

smoothCont = cv::Mat(contours[0]);

and try again (this way cv::GaussianBlur should be able to modify the data).

I know this was written a long time ago, but did you tried a big erode followed by a big dilate (opening), and then find the countours? It looks like a simple and fast solution, but I think it could work, at least to some degree.

user3540823

Basically the sudden changes in contour corresponds to high frequency content. An easy way to smooth your contour would be to find the fourier coefficients assuming the coordinates form a complex plane x + iy and then by eliminating the high frequency coefficients.

My take ... many years later ...!

Maybe two easy ways to do it:

  • loop a few times with dilate,blur,erode. And find the contours on that updated shape. I found 6-7 times gives good results.
  • create a bounding box of the contour, and draw an ellipse inside the bounded rectangle.

Adding the visual results below:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!