目前我在opencv-python
工作,有凸性缺陷。
从手册中我得到了以下代码:
import cv2
import numpy as np
img = cv2.imread('star.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours,hierarchy = cv2.findContours(thresh,2,1)
cnt = contours[0]
hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)
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(img,start,end,[0,255,0],2)
cv2.circle(img,far,5,[0,0,255],-1)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
根据opencv pyhthon manualcv2.convexityDefects()
返回四个值,它们是[起点、终点、最远点、近似值
到最远点的距离]。在
我不明白for循环的五行是如何工作的?在
看看official docs。 s、 这些点就是等高线的索引。d是距离最远的轮廓点和船体之间的距离。在
相关问题 更多 >
编程相关推荐