如果我正确地理解了你的要求,那么你已经非常接近你的目标了。
以下是我的想法(使用Python 3.6和OpenCV 3.2):
edged = cv2.Canny(input_img.copy(), 50, 200)
_, contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # note that this function returns 3 values
def show_each_contour(original_image):
margin = 2 # you can set the margin to 0 to better understand its effect
for i,c in enumerate(contours):
area = cv2.contourArea(c)
if area > 0:
rect = cv2.boundingRect(c)
x,y,w,h = rect
cv2.rectangle(original_image, (x-margin,y-margin), (x+w+margin,y+h+margin), (0,0,255), 2)
cropped = original_image[y-margin:y+h+margin, x-margin:x+w+margin]
if area < 2000:
kernel = np.ones((5,5), np.uint8)
numberOfIterations = area / 200
else:
kernel = np.ones((5,5), np.uint8)
numberOfIterations = 7
eroded_shape = cv2.erode(cropped.copy(), kernel, iterations = int(numberOfIterations))
original_image[y-margin:y+h+margin, x-margin:x+w+margin] = eroded_shape # we copy the eroded_shape back into the original_image
除了最后一行,我将腐蚀的形状复制到原始图像的正确位置,我没有对您的代码做太多更改。
输入图像在左侧,输出在右侧。
希望这有帮助,如果这不是你想要的,请告诉我。