我想检测图像中的轮廓,并用任意值填充它们。 不幸的是,如果两个贴片彼此靠近,我很难找到轮廓。
例如在下图中,我希望找到三个轮廓,但是仅一个对角线pixel
就足以检测三个单独的轮廓。有没有办法使contour
的检测更可靠?
这是当前代码
# find contours
cv_img = cv2.imread(f_name+".png")
imgray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
inverted = cv2.bitwise_not(imgray)
#cv2.imwrite(f_name+"_inverted.png", inverted)
im2, contours, hierarchy = cv2.findContours(inverted, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print ("Found ", len(contours), " contours")
contours = sorted(contours, key=cv2.contourArea)
if len(contours) == 3:
blank_image = np.zeros((int(model_size[1]/4), int(model_size[0]/4), 1), np.uint8)
cv2.drawContours(blank_image, contours, 0, 255, thickness=cv2.FILLED)
cv2.drawContours(blank_image, contours, 1, 255, thickness=cv2.FILLED)
cv2.imwrite(f_name+"_contours.png", blank_image)