[英]Drawing convexHull in openCV2 Python
所以我試圖從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
我使用以下代碼獲取您給出的圖像的凸包:
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()
由於輪廓基於圖像中的白色區域,因此我可以通過更改代碼中的第5行來獲得兩種類型的輪廓。
情況1 :
情況2:現在,當我更改代碼段中的第五行時,我得到了: 當我反轉二進制圖像ret, thresh = cv2.threshold(img_gray, 127, 255, 1)
如您所見,基於二進制圖像中的白色區域找到輪廓。
希望這可以幫助。
我使用此鏈接獲取代碼並供參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.