我已经研究了一整天了,我们如何去除红色圆圈中的小背景噪音?我需要它在像这样的其他图片样本上工作。
我使用的想法是findContours,然后添加一个带有小于特定区域的所有小黑色噪声的掩模(试错法)。
image = cv2.imread("11_Image_after_noise_removal.png")
# copy image
img = image.copy()
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY)
thresh = 255 - thresh
# Use cv2.CCOMP for two level hierarchy
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_SIMPLE) # Use cv2.CCOMP for two level hierarchy
cv2.drawContours(img, contours, -1, (0, 255, 0), 1)
cv2.imshow("First detection", img)
# loop through the contours
for i, cnt in enumerate(contours):
# if the contour has no other contours inside of it
if hierarchy[0][i][3] != -1: # basically look for holes
# if the size of the contour is less than a threshold (noise)
if cv2.contourArea(cnt) < 70:
# Fill the holes in the original image
cv2.drawContours(img, [cnt], 0, (0, 0, 0), -1)
# display result
# Visualize the image after the Otsu's method application
cv2.imshow("Image after noise removal", img)
cv2.waitKey(0)
cv2.destroyAllWindows().destroyAllWindows()发布于 2020-11-22 22:47:24
您可以使用area = cv.contourArea(cnt)检查轮廓区域,如果它低于某个阈值,则忽略它。
下面是OpenCV文档:https://docs.opencv.org/4.3.0/dd/d49/tutorial_py_contour_features.html
https://stackoverflow.com/questions/64951935
复制相似问题