我使用了名为cv2.findContours()的函数绘制轮廓,但是无法识别出一些使轮廓不连续的位置。这是我的代码和结果。 非常感谢有人可以教我如何合并同一对象的轮廓?
import numpy as np
import os
import cv2
image = cv2.imread("/home/rafael/Desktop/2.jpg")
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([50, 10, 10])
upper = np.array([120, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
res = cv2.bitwise_and(image, image, mask = mask)
kernel = np.ones((5, 5), np.uint8)
d_im = cv2.dilate(mask, kernel, iterations = 1)
e_im = cv2.erode(d_im, kernel, iterations = 1)
src, contours, hierarchy = cv2.findContours(e_im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
temp = []
tmp = []
num = 0
for i in range(len(contours)):
if len(contours[i]) < 35:
temp.append(i)
for i in temp:
del contours[i - num]
num = num + 1
cv2.drawContours(image, contours, -1, (0, 0, 255), 2)
cv2.imwrite('/home/rafael/Desktop/13.jpg', image)
结果
全图