我试图让OpenCV认识到,红色区域是一个轮廓,而不是将其分割成两个单独的轮廓。我尝试过扰乱等级制度,但没有成功。有什么想法吗
下面提供了图像和代码
import cv2
import numpy as np
black = (0,0,0)
# All of these are in hsv format
lb=np.array([0,181,0])
ub=np.array([6,255,255])
# Load img
img = cv2.imread('test.jpg')
img=cv2.resize(img,(400,400))
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))
#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask = cv2.inRange(imgHSV, lb, ub)
maskso=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
masksc=cv2.morphologyEx(maskso,cv2.MORPH_CLOSE,kernelClose)
conts,hiers=cv2.findContours(masksc.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# Create convex hull
hull = []
for i in range(len(conts)):
# creating convex hull object for each contour
hull.append(cv2.convexHull(conts[i], False))
# Draw contour
cv2.drawContours(img,hull,-1,black,2)
cv2.imshow("image", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
替换此代码片段
与
我得到以下输出,我假设这是您想要的:
我没有收集每个轮廓的单个凸包,而是收集所有轮廓,然后得到所有轮廓点的凸包
希望有帮助
相关问题 更多 >
编程相关推荐