import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread("../SampleImages/stars.png")
plt.imshow(img[:,:,::-1])
img_contour = img.copy()
#得到灰度圖做Canny邊緣檢測(cè)
img_gray = cv.cvtColor(img_contour, cv.COLOR_BGR2GRAY)
edges = cv.Canny(img_gray, 120, 255, 0)
#提取并繪制輪廓
contours,hierarchy = cv.findContours(edges, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
img_contour = cv.drawContours(img_contour, contours, -1, (0,255,0), 2)
plt.imshow(img_contour, cmap=plt.cm.gray)
#凸包檢測(cè)
#hull = cv.convexHull(points, clockwise, returnpoints)
#hull: 輸出凸包結(jié)果,n*1*2數(shù)據(jù)結(jié)構(gòu),n為外包圍圈的點(diǎn)的個(gè)數(shù)
#points: 輸入的坐標(biāo)點(diǎn),通常為1* n * 2 結(jié)構(gòu),n為所有的坐標(biāo)點(diǎn)的數(shù)目
#clockwise:轉(zhuǎn)動(dòng)方向,TRUE為順時(shí)針,否則為逆時(shí)針;
#returnPoints:默認(rèn)為TRUE,返回凸包上點(diǎn)的坐標(biāo),如果設(shè)置為FALSE,會(huì)返回與凸包點(diǎn)對(duì)應(yīng)的輪廓上的點(diǎn)。
#參考資料:https://blog.csdn.net/lovetaozibaby/article/details/103214672
hulls = []
for contour in contours:
hull = cv.convexHull(contour)
hulls.append(hull)
img_convex_hull = cv.drawContours(img, hulls, -1, (0,255,0), 2)
plt.imshow(img_convex_hull[:,:,::-1])
?
?
文章來源:http://www.zghlxwxcb.cn/news/detail-692302.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-692302.html
到了這里,關(guān)于Python Opencv實(shí)踐 - 凸包檢測(cè)(ConvexHull)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!