我的工作流程的开头与您的类似。第一步:模糊图像..
blurred = cv2.GaussianBlur(gray, (5, 5), 0) # Blur
第二步:获取精彩图片......
canny = cv2.Canny(blurred, 30, 150) # Canny
第三步:在canny图像上绘制轮廓。这会关闭撕裂的碎片。
# Find contours _, contours, _ = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # Draw contours on canny (this connects the contours cv2.drawContours(canny, contours, -1, 255, 2) canny = 255 - canny
第四步:填埋(洪水填充区域为灰色)
# Get mask for floodfill h, w = thresh.shape[:2] mask = np.zeros((h+2, w+2), np.uint8) # Floodfill from point (0, 0) cv2.floodFill(thresh, mask, (0,0), 123);
第五步:摆脱真正的小而非常大的轮廓
# Create a blank image to draw on res = np.zeros_like(src_img) # Create a list for unconnected contours unconnectedContours = [] for contour in contours: area = cv2.contourArea(contour) # If the contour is not really small, or really big if area > 123 and area < 760000: cv2.drawContours(res, [contour], 0, (255,255,255), cv2.FILLED) unconnectedContours.append(contour)
最后,一旦你分割了碎片,它们就可以嵌套了。