import cv2
o=cv2.imread('C:/Users/xpp/Desktop/Finger.png')#原始图像
cv2.imshow('original',o)
gray=cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)#将彩色图片转换为灰度图
ret,thresh=cv2.threshold(gray,235,255,cv2.THRESH_BINARY)#将灰度图片转换为二值图片
contours,hierarchy=cv2.findContours(thresh,2,1)#计算图像轮廓
for cnt in contours:
hull=cv2.convexHull(cnt)#计算凸包
length=len(hull)
if length>5:
for i in range(length):
cv2.line(gray,tuple(hull[i][0]),tuple(hull[(i 1)%length][0]),(0,0,255),2)#绘制凸包
hull=cv2.convexHull(cnt,returnPoints=False)#计算凸包
defects=cv2.convexityDefects(cnt,hull)#计算凸缺陷
for j in range(defects.shape[0]):#构造凸缺陷
s,e,f,d=defects[j,0]
start=tuple(cnt[s][0])
end=tuple(cnt[e][0])
far=tuple(cnt[f][0])
cv2.line(gray,start,end,[0,0,0],2)#绘制凸缺陷
cv2.circle(gray,far,6,[0,0,0],-1)#绘制凸缺陷
cv2.imshow('result',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()