[英]Smoothing 2D contour with rough boundary in numpy array
I have a contour that is represented in a numpy array in such a way that the boundary points have 1 and the rest are 0. An example image is shown below.我有一个在 numpy 数组中表示的轮廓,其边界点为 1,rest 为 0。示例图像如下所示。 How could I smooth this contour?我怎样才能平滑这个轮廓?
I am trying the get a contour that is smoother than the one in the image right now我正在尝试获得一个比现在图像中的轮廓更平滑的轮廓
You can use active_contour
to match a spline with a set number of points to your contour.您可以使用active_contour
将具有一定数量点的样条与轮廓匹配。 If you lower the number of points you get a smoother contour.如果降低点数,您将获得更平滑的轮廓。
You can use morphological transformations such as erosion and dilation.您可以使用形态学变换,例如腐蚀和膨胀。 OpenCV has a nice tutorial here: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html OpenCV has a nice tutorial here: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
As @Mad Physicist says you can use morphological techniques I recommend using Opening (Dilation and erosion) followed by a dilation as follows:正如@Mad Physicist 所说,您可以使用形态学技术,我建议使用打开(膨胀和腐蚀),然后使用膨胀,如下所示:
import cv2
import numpy as np
img = cv2.imread('img.png',0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11))
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel,iterations= 5)
opening = cv2.dilate(opening,kernel,iterations = 1)
img_window = np.hstack((img,opening))
cv2.imshow("image",img_window)
cv2.waitKey(0)
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.