我尝试了阈值处理,凸包,轮廓来做蒙版,但是它们都不能很好地工作,我的数据集是一堆球拍,徽标和绘画是较亮的颜色,因此对二进制文件使用阈值不会“行不通,并且Canny和轮廓没有很好地抓住边缘,很难闭合,我只想将球拍的形状遮盖起来,是否有更好的方法来解决此问题?谢谢
import cv2
import numpy as np
import random as rng
img =cv2.imread(r"D:\Hu_20200117\20200117160808\51-20200117160823868.bmp",cv2.IMREAD_GRAYSCALE)
img = cv2.medianBlur(img,5)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
dilated = cv2.dilate(img, kernel)
canny = cv2.Canny(dilated, 20, 150)
th3 = cv2.adaptiveThreshold(dilated,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,5)
_,contours, _ = cv2.findContours(th3, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hull_list = []
for i in range(len(contours)):
hull = cv2.convexHull(contours[i])
hull_list.append(hull)
# Draw contours + hull results
drawing = np.zeros((th3.shape[0], th3.shape[1], 3), dtype=np.uint8)
for i in range(len(contours)):
# cv2.drawContours(drawing, contours, i, (255,255,255),cv2.FILLED)
cv2.drawContours(drawing, hull_list, i, (255,255,255),cv2.FILLED)
# Show in a window
foreground = cv2.morphologyEx(drawing, cv2.MORPH_OPEN, kernel)
foreground = cv2.morphologyEx(foreground, cv2.MORPH_CLOSE, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
background = cv2.dilate(foreground, kernel, iterations=3)
cv2.imshow('drawing', drawing)
cv2.imshow('Background', background)
cv2.waitKey(0)