使用OpenCV for ,我正在尝试获取图像中的噪声元素的掩码,以便稍后用作cv.inpaint()函数的输入。
给我一个灰度图像(二维矩阵,值从0到255)在input_mtx_8u变量中,带有噪声(非常低值的孤立多边形)。
到目前为止我所做的是:
的边
laplacian = cv2.Laplacian(input_mtx_8u, cv2.CV_8UC1)
lapl_bin, lapl_bin_val = cv2.threshold(laplacian, 25, 255, cv2.THRESH_BINARY)的轮廓
contours, _ = cv2.findContours(lapl_bin_val, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)filled_mtx = input_mtx_8u.copy()
cv2.fillPoly(filled_mtx, contours, (255, 255, 0), 4)由于某些原因,我的“填充多边形”没有完全填充(见图)。我做错什么了?

发布于 2020-09-09 20:53:18
正如@fmw42 42所指出的,填充等高线的解决方案是使用drawContours()而不是fillPoly()。
我得到的最后的工作代码是:
# input_mtx_8u = 2D matrix with uint8 values from 0 to 255
laplacian = cv2.Laplacian(input_mtx_8u, cv2.CV_8UC1)
lapl_bin, lapl_bin_val = cv2.threshold(laplacian, 25, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(lapl_bin_val, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
inpaint_mask = np.zeros(input_mtx_8u.shape, dtype=np.uint8)
for contour in contours:
cv2.drawContours(inpaint_mask, [contour], -1, (255, 0, 0), thickness=-1)
# inpaint_mask = can be used as the mask for cv2.inpaint()请注意,由于某种原因:
cv2.drawContours(input_mtx_cont, contours, -1, (255, 0, 0), thickness=-1)不管用。一个人必须循环绘制等高线.
https://stackoverflow.com/questions/63816633
复制相似问题