凸缺陷分析仪中的缺失点

时间:2019-02-23 08:33:43

标签: python opencv

我有这个简单的python代码(python 3.6.7 cv 4.0.0)来识别图像中的凸度缺陷。该图像只是在cv BW图像上写入文本而生成的。凸度缺陷少了一点。为什么?

import cv2
import numpy as np

img = np.zeros((800,800), np.uint8)     
cv2.putText(img,"W W",(100,500),cv2.FONT_HERSHEY_SIMPLEX,10,255,30,2)
contours,hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
img1=img.copy()
img1.fill(0)
cv2.drawContours(img1,contours,-1,255,1)

for cnt in contours:
    hull = cv2.convexHull(cnt,returnPoints = False)
    defects = cv2.convexityDefects(cnt,hull)
    if isinstance(defects,np.ndarray):
        try:
            for i in range(len(defects)):
                s,e,f,d = defects[i,0]
                print (s,e,f,d)
                start = tuple(cnt[s][0])
                end = tuple(cnt[e][0])
                far = tuple(cnt[f][0])
                ##cv2.line(img1,start,end,[255],1)
                cv2.circle(img1,far,3,[255],-1)
        finally:
            pass
    else:
        print(type(defects))
cv2.imshow('img',img1)
cv2.waitKey(0)
cv2.imwrite("image.png",img1)
cv2.destroyAllWindows()

结果是此图像: a defect is not identifyed

0 个答案:

没有答案