当我试图获得凸性缺陷时,我不断收到此错误:contours.cpp:1969:error:( - 1515)ptnum> 3在函数凸性缺陷中。我尝试过很多不同的东西,但似乎没什么用。我觉得我做凸性缺陷的方式是错误的。如果有人能用适当的方式向我展示非常有用的代码。
贝娄是我的代码:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of green color in HSV
lower_green = np.array([50,100,100], dtype=np.uint8)
upper_green = np.array([70,255,255], dtype=np.uint8)
# Threshold the HSV image to get only green colors
mask = cv2.inRange(hsv, lower_green, upper_green)
contours,hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cnt = contours[0]
cv2.drawContours(frame, contours, -1, (255,0,0), 3)
#cnt = 0
for cnt in contours:
hull = cv2.convexHull(cnt,returnPoints=False)
if (len(hull)>=3):
defects = cv2.convexityDefects(cnt,hull)
if (defects != None):
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
cv2.line(frame,start,end,[0,255,0],2)
cv2.circle(frame,far,5,[0,0,255],-1)
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()