我在使用 erode 和 dilate 时遇到问题,我不确定我是否理解如何正确使用这些参数。
我有这张照片:
使用这段代码:
def dilate_and_erode(self):
img = self.img
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
res = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))
dilated = cv2.dilate(res, kernel)
eroded=cv2.erode(dilated,kernel)
img_contours = cv2.findContours(eroded, cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE)[-2]
img_contours = sorted(img_contours, key=cv2.contourArea)
ctr = []
for i in img_contours:
if cv2.contourArea(i) > 100:
ctr.append(i)
mask = np.zeros(img.shape[:2], np.uint8)
cv2.drawContours(mask, ctr,-1, 255, -1)
new_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("Original Image", img)
cv2.imshow("New image", new_img)
cv2.imshow("Mask", mask)
cv2.waitKey(0)
我明白了:
这不是我想要的,我想去除图片顶部的黑噪声
任何建议将不胜感激!