日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

图像凸性检测函数convexityDefects在Python2.7下使用opencv3.0的问题

發布時間:2025/3/15 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图像凸性检测函数convexityDefects在Python2.7下使用opencv3.0的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在學習Python下的OpenCV,在圖像的凸性檢測中,發現opencv3.0下的convexityDefects函數對圖像的凸性缺陷處理有錯誤。不知道是opencv3.0的版本問題還是我個人的錯誤代碼。

例如使用的Python版本是2.7.6,使用的OpenCV版本是3.0,以下是圖像凸性檢測代碼:

import cv2
import numpy as np

img = cv2.imread('star2.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img_gray,127,255,0)
img_c,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
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])
print start,end,far,d
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()


結果顯示,某處缺陷最大值定位錯誤,如圖:



而如果使用OpenCV2.4.13版本,以下是圖像凸性檢測代碼:

import cv2
import numpy as np

img = cv2.imread('star2.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img_gray,127,255,0)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
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])
print start,end,far,d
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()


結果顯示,圖像的凸性檢測是正確的,如圖所示:



總結:

出現這樣的問題是因為OpenCV3.0版本還不夠穩定還是我的編程錯誤呢?不知道各位有沒有遇到類似的問題,特此提出來,希望大家討論一下!

總結

以上是生活随笔為你收集整理的图像凸性检测函数convexityDefects在Python2.7下使用opencv3.0的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。