OpenCV-Python圖像輪廓之輪廓特征詳解
前言
圖像輪廓是指由位于邊緣、連續(xù)的、具有相同顏色和強(qiáng)度的點(diǎn)構(gòu)成的曲線,它可以用于形狀分析以及對(duì)象檢測(cè)和識(shí)別。
一、輪廓的矩
輪廓的矩包含了輪廓的各種幾何特征,如面積、位置、角度、形狀等。cv2.moments()函數(shù)用于返回輪廓的矩,其基本格式如下:
ret = cv2.moments(array[, binaryImage]) ret為返回的輪廓的矩,是一個(gè)字典對(duì)象, 大多數(shù)矩的含義比較抽象, 但其中的零階矩(m00)表示輪廓的面積 array為表示輪廓的數(shù)組 binaryImage值為True時(shí),會(huì)將array對(duì)象中的所有非0值設(shè)置為1
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('shape2.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
img1 = cv2.drawContours(img1, contours, -1,(0,255,0),2)
cv2.imshow('Contours',img1)
m0 = cv2.moments(contours[0])
m1 = cv2.moments(contours[1])
print('輪廓0的矩:', m0)
print('輪廓1的矩:', m1)
print('輪廓0的面積:', m0['m00'])
print('輪廓1的面積:', m1['m00'])
cv2.waitKey(0)
cv2.destroyAllWindows()


二、輪廓的面積
cv2.contourArea()函數(shù)用于返回輪廓的面積,其基本格式如下:
ret = cv2.contourArea(contour[, oriented]) ret為返回的面積 contour為輪廓 oriented為可選參數(shù), 其參數(shù)值為True時(shí), 返回值的正與負(fù)表示表示輪廓是順時(shí)針還是逆時(shí)針, 參數(shù)值為False(默認(rèn)值)時(shí), 函數(shù)返回值為絕對(duì)值
img = cv2.imread('shape2.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
m0 = cv2.contourArea(contours[0])
m1 = cv2.contourArea(contours[1])
print('輪廓0的面積:', m0)
print('輪廓1的面積:', m1)

三、輪廓的長(zhǎng)度
cv2.arcLength()函數(shù)用于返回輪廓的長(zhǎng)度,其基本格式如下:
ret = cv2.cv2.arcLength(contour, closed) ret為返回的長(zhǎng)度 contour為輪廓 closed為布爾值, 為True時(shí)表示輪廓是封閉的
img = cv2.imread('shape2.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
m0 = cv2.arcLength(contours[0], True)
m1 = cv2.arcLength(contours[1], True)
print('輪廓0的長(zhǎng)度:', m0)
print('輪廓1的長(zhǎng)度:', m1)

四、輪廓的近似多邊形
cv2.approxPolyDP()函數(shù)用于返回輪廓的近似多邊形,其基本格式如下:
ret = cv2.cv2.arcLength(contour, epsilon, closed) ret為返回的近似多邊形 contour為輪廓 epsilon為精度, 表示近似多邊形接近輪廓的最大距離 closed為布爾值, 為True時(shí)表示輪廓是封閉的
img = cv2.imread('shape3.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
img1 = cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
arcl = cv2.arcLength(contours[0], True)
img2 = img1.copy()
app = cv2.approxPolyDP(contours[0], arcl*0.05, True)
img2 = cv2.drawContours(img2, [app], -1, (255,0,0), 2)
cv2.imshow('contours',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

五、輪廓的凸包
cv2.convexHull()函數(shù)用于返回輪廓的凸包,其基本格式如下:
hull = cv2.convexHull(contours[, clockwise[, returnPointss]]) hull為返回的凸包, 是一個(gè)numpy.ndarray對(duì)象, 包含了凸包的關(guān)鍵點(diǎn) contours為輪廓 clockwise為方向標(biāo)記, 為True時(shí), 凸包為順時(shí)針方向, 為False(默認(rèn)值)時(shí), 凸包為逆時(shí)針方向 returnPointss為True時(shí)(默認(rèn)值)時(shí), 返回的hull中包含的是凸包關(guān)鍵點(diǎn)的坐標(biāo), 為False時(shí), 返回的是凸包關(guān)鍵點(diǎn)在輪廓中的索引
img = cv2.imread('shape3.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
img1 = cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
hull = cv2.convexHull(contours[0])
print('returnPoints = Treu 時(shí)返回的凸包;\n',hull)
hull2 = cv2.convexHull(contours[0], returnPoints=False)
print('returnPoints = False時(shí)返回的凸包;\n',hull2)
cv2.polylines(img1, [hull], True, (255,0,0),2)
cv2.imshow('ConvecHull',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


六、輪廓的直邊界矩形
輪廓的直邊界矩形是指可容納輪廓的矩形,且矩形的兩條邊必須是平行的,直邊界矩形不一定是面積最小的邊界矩形。
cv2.boundingRect()函數(shù)用于返回輪廓的直邊界矩形,其基本格式如下:
ret = cv2.boundingRect(contours) ret為返回的直邊界矩形, 它是一個(gè)四元組, 其格式為(矩形左上角x坐標(biāo), 矩形左上角y坐標(biāo), 矩形的寬度, 矩形的高度) contours為用于計(jì)算直邊界矩形的輪廓
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
img1 = cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
ret = cv2.boundingRect(contours[0])
print('直邊界矩形:\n', ret)
pt1 = (ret[0], ret[1])
pt2 = (ret[0] + ret[2], ret[1] + ret[3])
img2 = img1.copy()
img2 = cv2.rectangle(img2, pt1, pt2, (255,0,0), 1)
cv2.imshow('Rectangle', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()


七、輪廓的旋轉(zhuǎn)矩形
輪廓的旋轉(zhuǎn)矩形是指可容納輪廓的面積最小的矩形。
cv2.minAreaRect()函數(shù)用于返回輪廓的旋轉(zhuǎn)矩形,其基本格式如下:
box = cv2.minAreaRect(contour) box為返回的旋轉(zhuǎn)矩陣, 它是一個(gè)三元組, 其格式為((矩形中心點(diǎn)x坐標(biāo), 矩形中心點(diǎn)y坐標(biāo)), (矩形的寬度, 矩形的高度), 矩形的旋轉(zhuǎn)角度) contour為用于計(jì)算矩形的輪廓
cv2.minAreaRect()函數(shù)返回的結(jié)果不能直接用于繪制旋轉(zhuǎn)矩形,可以使用cv2.boxPoints()函數(shù)將其轉(zhuǎn)換為矩形的頂點(diǎn)坐標(biāo),其基本格式如下:
points = cv2.boxPoints(box) points為返回的矩形頂點(diǎn)坐標(biāo), 坐標(biāo)數(shù)據(jù)為浮點(diǎn)數(shù) box為cv2.minAreaRect()函數(shù)返回的矩形數(shù)據(jù)
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
# 計(jì)算最小旋轉(zhuǎn)矩形
ret = cv2.minAreaRect(contours[0])
rect = cv2.boxPoints(ret)
rect = np.int0(rect)
img2 = img1.copy()
cv2.drawContours(img2, [rect], 0, (255,0,0), 2)
cv2.imshow('Rectangle', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

八、輪廓的最小外包圓
cv2.minEnclosingCircle()函數(shù)用于返回可容納輪廓的最小外包圓,其基本格式如下:
center, radius = cv2.minEnclosingCircle(contours) center為圓心 radius為半徑 contours為用于計(jì)算最小外包圓的輪廓
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
# 計(jì)算最小外包圓
(x, y), radius = cv2.minEnclosingCircle(contours[0])
center = (int(x),int(y))
radius = int(radius)
img2 = img1.copy()
cv2.circle(img2, center, radius, (255,0,0),2)
cv2.imshow('Circle',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

九、輪廓的擬合橢圓
cv2.fitEllipse()函數(shù)用于返回輪廓的擬合橢圓,其基本格式如下:
ellipse = cv2.fitEllipse(contours) ellipse為返回的橢圓 contours為用于計(jì)算擬合橢圓的輪廓
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
# 計(jì)算擬合橢圓
ellipse = cv2.fitEllipse(contours[0])
img2 = img1.copy()
cv2.ellipse(img2, ellipse, (255,0,0),2)
cv2.imshow('Circle',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

十、輪廓的擬合直線
cv2.fitLine()函數(shù)用于返回輪廓的擬合直線,其基本格式如下:
line = cv2.fitLine(contours, distType, param, reps, aeps) line為返回的擬合直線 contours為用于計(jì)算擬合直線的輪廓 distType為距離參數(shù)類型, 決定如何計(jì)算擬合直線 param為距離參數(shù), 與距離參數(shù)類型有關(guān), 其設(shè)置為0時(shí), 函數(shù)將自動(dòng)選擇最優(yōu)值 reps為計(jì)算擬合直線需要的徑向精度, 通常設(shè)置為0.01 aeps為計(jì)算擬合直線需要的軸向精度, 通常設(shè)置為0.01
param距離參數(shù)類型:

img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255), 2)
cv2.imshow('Contours',img1)
#計(jì)算擬合直線
img2 = img1.copy()
rows, cols = img.shape[:2]
[vx, vy, x, y] = cv2.fitLine(contours[0], cv2.DIST_L1, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((cols - x) * vy / vx) + y)
cv2.line(img2, (0, lefty), (cols-1, righty), (255,0,0), 2)
cv2.imshow('FitLine',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

十一、輪廓的最小外包三角形
cv2.minEnclosingTriangle()函數(shù)用于返回可容納輪廓的最小外包三角形,其基本格式如下:
retval, triangle = cv2.minEnclosingTriangle(contours) retval為最小外包三角形的面積 triangle為最小外包三角形 contours為用于計(jì)算最小外包三角形的輪廓
img = cv2.imread('shape4.jpg')
cv2.imshow('original', img)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img1 = np.zeros(img.shape, np.uint8) + 255
cv2.drawContours(img1, contours, -1, (0,0,255) ,2)
cv2.imshow('Contours',img1)
# 計(jì)算最小外包三角形
retval, triangle = cv2.minEnclosingTriangle(contours[0])
triangle = np.int0(triangle)
img2 = img1.copy()
cv2.polylines(img2, [triangle], True, (255,0,0),2)
cv2.imshow('Triangle',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

以上就是OpenCV-Python圖像輪廓之輪廓特征詳解的詳細(xì)內(nèi)容,更多關(guān)于OpenCV-Python輪廓特征的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何將python中的List轉(zhuǎn)化成dictionary
這篇文章主要介紹在python中如何將list轉(zhuǎn)化成dictionary,通過提出兩個(gè)問題來告訴大家如何解決,有需要的可以參考借鑒。2016-08-08
Django JSonResponse對(duì)象的實(shí)現(xiàn)
本文主要介紹了Django JSonResponse對(duì)象的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python中函數(shù)調(diào)用9大方法小結(jié)
在Python中,函數(shù)是一種非常重要的編程概念,它們使得代碼模塊化、可重用,并且能夠提高代碼的可讀性,本文將深入探討Python函數(shù)調(diào)用的9種方法,需要的可以參考下2024-01-01
Python設(shè)計(jì)模式之狀態(tài)模式原理與用法詳解
這篇文章主要介紹了Python設(shè)計(jì)模式之狀態(tài)模式原理與用法,簡(jiǎn)單描述了狀態(tài)模式的概念、原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與使用狀態(tài)模式的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
Python3.4學(xué)習(xí)筆記之列表、數(shù)組操作示例
這篇文章主要介紹了Python3.4列表、數(shù)組操作,結(jié)合實(shí)例形式分析了Python3.4列表的創(chuàng)建、元素追加、刪除、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解
這篇文章主要介紹了python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
淺談Python描述數(shù)據(jù)結(jié)構(gòu)之KMP篇
這篇文章主要介紹了Python描述數(shù)據(jù)結(jié)構(gòu)之KMP篇,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
服務(wù)器端jupyter notebook映射到本地瀏覽器的操作
這篇文章主要介紹了服務(wù)器端jupyter notebook映射到本地瀏覽器的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
CentOS 6.5下安裝Python 3.5.2(與Python2并存)
這篇文章主要給大家介紹了在CentOS 6.5下安裝Python 3.5.2的方法教程,安裝后的python3與Python2并存,文中分享了詳細(xì)的方法步驟,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,下面來一起看看吧。2017-06-06

