Tre*_*ice 3 python ocr image-processing convex-hull opencv-contour
所以我试图从python中的轮廓绘制convexHull,但是当我打印图像时它没有改变.
roi=mask[y:y+h,x:x+w]
roi = cv2.fastNlMeansDenoisingColored(roi,None,15,15,7,21)
hull = cv2.convexHull(cnt)
cv2.drawContours(roi,[hull],0,(147,0,255),2)
cv2.imshow(str(i),roi)
blank_image[y:y+h,x:x+w] = roi
Run Code Online (Sandbox Code Playgroud)
我使用以下代码获取您给出的图像的凸包:
import cv2
import numpy as np
img = cv2.imread('2.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255, 0)
contours,hierarchy = cv2.findContours(thresh,2,1)
print len(contours)
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()
Run Code Online (Sandbox Code Playgroud)
由于轮廓基于图像中的白色区域,因此我可以通过更改代码中的第5行来获得两种类型的轮廓.
情况1 :
情况2:
现在,当我更改代码段中的第五行时,我得到了:
当我反转二进制图像 ret, thresh = cv2.threshold(img_gray, 127, 255, 1)
如您所见,基于二进制图像中的白色区域找到轮廓.
希望这可以帮助.
我使用此链接获取代码并供参考.
| 归档时间: |
|
| 查看次数: |
13873 次 |
| 最近记录: |