目錄
- 前言
- 寬高比
- Extend
- Solidity
- 等效直徑
- 方向
- 掩摸和像素點(diǎn)
- 最大值,最小值以及它們的位置
- 平均顏色及平均灰度
- 極點(diǎn)
前言
輪廓自身的一些屬性特征及輪廓所包圍對(duì)象的特征對(duì)于描述圖像具有重要意義。本篇博文將介紹幾個(gè)輪廓自身的屬性特征及輪廓包圍對(duì)象的特征。
寬高比
在輪廓中,我們可以通過(guò)寬高比來(lái)描述輪廓,例如矩形的輪廓寬高比為:
寬高比=寬度/高度
下面,我們來(lái)計(jì)算矩形輪廓的寬高比,代碼如下:
import cv2
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)
cv2.imshow("img1", img)
aspectRatio=float(w)/h
print(aspectRatio)
cv2.waitKey()
cv2.destroyAllWindows()
運(yùn)行之后,我們可以得到輪廓的寬高比約為3:
![](/d/20211017/43b793ffee36a1b5d98c54f826ce42ff.gif)
Extend
我們還可以使用輪廓面積與矩形邊界面積之比Extend來(lái)描述圖像及其輪廓特征,數(shù)學(xué)計(jì)算公式圖下:
Extend=輪廓面積/矩形邊界面積
下面,我們來(lái)計(jì)算Extend,代碼如下:
import cv2
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
rectArea=w*h#矩形邊界面積
cntArea=cv2.contourArea(contours[0])#輪廓面積
extend=float(cntArea)/rectArea
print(extend)
本例中,輪廓面積與矩形邊界面積的比值Extend大約為0.8:
![](/d/20211017/051ac18623f1b57309a54461e1f0afbc.gif)
Solidity
我們還可以使用輪廓面積與凸包面積之比Solidity來(lái)衡量圖像,輪廓以及凸包的特征。其數(shù)學(xué)計(jì)算公式為:
Slidity=輪廓面積/凸包面積
下面,我們來(lái)計(jì)算Slidity,代碼如下:
import cv2
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
cntArea=cv2.contourArea(contours[0])#輪廓面積
hull=cv2.convexHull(contours[0])
hullArea=cv2.contourArea(hull)#凸包面積
solidity=float(cntArea)/hullArea
print(solidity)
運(yùn)行之后,本例輪廓面積與凸包面積的比值solidity約為1:
![](/d/20211017/e0141d9b08ec3fed71338ae39b479af2.gif)
等效直徑
在OpenCV中,我們還可以使用等效直徑來(lái)衡量輪廓的特征值,該值是與輪廓面積相等的圓形的直徑。其數(shù)學(xué)計(jì)算公式為:
![](/d/20211017/f4e409cdb4ebfe147e8ea56e53988061.gif)
下面,我們來(lái)計(jì)算與輪廓面積相等的圓形直徑,代碼如下:
import cv2
import numpy as np
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
cntArea=cv2.contourArea(contours[0])#輪廓面積
equiDiameter=np.sqrt(4*cntArea/np.pi)
print(equiDiameter)
cv2.circle(img,(100,100),int(equiDiameter/2),(0,255,0),3)
cv2.imshow("img1",img)
cv2.waitKey()
cv2.destroyAllWindows()
運(yùn)行之后,我們得到其等效直徑約為145:
![](/d/20211017/f9b085877c8af97870db9ad93947ada1.gif)
方向
在OpenCV中,函數(shù)cv2.fitEllipse()可以用來(lái)構(gòu)建最優(yōu)擬合橢圓,還可以在返回值內(nèi)分別返回橢圓的中心點(diǎn),軸長(zhǎng),旋轉(zhuǎn)角度信息。使用這種形式,能夠直觀地獲取橢圓的方向等信息。
函數(shù)cv2.fitEllipse()返回值為:
(x,y):橢圓的中心點(diǎn)
(MA,ma):橢圓水平方向軸與垂直方向軸的長(zhǎng)度
angle:橢圓的旋轉(zhuǎn)角度
import cv2
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ellipsis=cv2.fitEllipse(contours[0])
(x, y), (MA, ma), angle = cv2.fitEllipse(contours[0])
print((x, y), (MA, ma), angle)
cv2.ellipse(img, ellipsis, (0, 255, 0), 2)
cv2.imshow("img1", img)
cv2.waitKey()
cv2.destroyAllWindows()
本來(lái)就是橢圓圖,下面擬合后正好也是橢圓:
![](/d/20211017/bddbf8a9d81ee04d4ac3b0260c16427d.gif)
掩摸和像素點(diǎn)
有時(shí)候,我們還像獲取某對(duì)象的掩摸圖像及其對(duì)應(yīng)的點(diǎn)。在OpenCV中,它還提供了cv2.findNonZero()函數(shù)用于獲取一個(gè)圖像內(nèi)的輪廓點(diǎn)位置,其完整定義如下:
def findNonZero(src, idx=None):
src:要查找非零元素的圖像
idx:返回值,表示非0元素的索引位置。具體格式為(行號(hào),列號(hào))
下面,我們實(shí)測(cè)該函數(shù),代碼如下:
import cv2
import numpy as np
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask=np.zeros(gray.shape,np.uint8)
cv2.drawContours(mask,[contours[0]],0,255,2)
pixelpoints=cv2.findNonZero(mask)
print(pixelpoints)
cv2.imshow("img1", mask)
cv2.waitKey()
cv2.destroyAllWindows()
運(yùn)行之后,我們會(huì)得到輪廓點(diǎn)位置:
![](/d/20211017/92e597c362e3e6ff4ce6e046f3a9d5e2.gif)
最大值,最小值以及它們的位置
在OpenCV中,它提供cv2.minMaxLoc()函數(shù)獲取指定對(duì)象內(nèi)最大值,最小值以及位置等信息,其完整定義如下:
def minMaxLoc(src, mask=None):
src:?jiǎn)瓮ǖ缊D像
mask:掩摸,通過(guò)使用掩摸圖像,得到掩膜指定區(qū)域內(nèi)的最值信息
該函數(shù)返回4個(gè)值:最小值,最大值,最小值位置,最大值位置。
下面,我們來(lái)獲取這些值,代碼如下:
import cv2
import numpy as np
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(gray.shape, np.uint8)
cv2.drawContours(mask, [contours[0]], 0, 255, 2)
min, max, min_loc, max_loc = cv2.minMaxLoc(gray, mask)
print(min, max, min_loc, max_loc)
運(yùn)行之后,控制臺(tái)輸出4個(gè)值:
![](/d/20211017/70d36f85b469b1b4eacd75fc92fc625d.gif)
平均顏色及平均灰度
在OpenCV中,它給我們提供cv2.mean()函數(shù)計(jì)算一個(gè)對(duì)象的平均顏色與平均灰度。其完整定義如下:
def mean(src, mask=None):
參數(shù)與上面兩個(gè)小節(jié)一樣,這里不在贅述。下面,我們來(lái)使用這個(gè)函數(shù),代碼如下:
import cv2
import numpy as np
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask=np.zeros(gray.shape,np.uint8)
cv2.drawContours(mask,[contours[0]],0,255,2)
mean=cv2.mean(img,mask)
運(yùn)行之后,輸出4個(gè)值:RGB以及A通道的均值:
![](/d/20211017/5c38bc690512f1c4fd2224f7996b8439.gif)
極點(diǎn)
有時(shí)候,我們希望獲取某個(gè)對(duì)象內(nèi)的極點(diǎn),比如最左,最右,最上,最下等。在OpenCV中,它給我們提供了以下方法進(jìn)行獲?。?/p>
left=tuple(cnt[cnt[:,:,0].argmin()][0])
right=tuple(cnt[cnt[:,:,0].argmax()][0])
top=tuple(cnt[cnt[:,:,1].argmin()][0])
bottom=tuple(cnt[cnt[:,:,1].argmax()][0])
下面,我們來(lái)通過(guò)這些方法獲取,代碼如下:
import cv2
import numpy as np
img = cv2.imread("26_1.jpg")
cv2.imshow("img", img)
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(img.shape, np.uint8)
cnt = contours[0]
left = tuple(cnt[cnt[:, :, 0].argmin()][0])
right = tuple(cnt[cnt[:, :, 0].argmax()][0])
top = tuple(cnt[cnt[:, :, 1].argmin()][0])
bottom = tuple(cnt[cnt[:, :, 1].argmax()][0])
print(left, right, top, bottom)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, "left", left, font, 1, (0, 255, 0), 2)
cv2.putText(img, "right", right, font, 1, (0, 255, 0), 2)
cv2.putText(img, "top", top, font, 1, (0, 255, 0), 2)
cv2.putText(img, "bottom", bottom, font, 1, (0, 255, 0), 2)
cv2.imshow("result",img)
cv2.waitKey()
cv2.destroyAllWindows()
運(yùn)行之后,值與效果如下:
![](/d/20211017/bd81d2ab9f098c8197716c508dd574fd.gif)
到此這篇關(guān)于OpenCV-Python實(shí)現(xiàn)輪廓的特征值的文章就介紹到這了,更多相關(guān)OpenCV 輪廓的特征值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python通過(guò)OpenCV的findContours獲取輪廓并切割實(shí)例
- Python Opencv實(shí)現(xiàn)圖像輪廓識(shí)別功能
- OpenCV-Python實(shí)現(xiàn)輪廓檢測(cè)實(shí)例分析
- opencv python 圖像輪廓/檢測(cè)輪廓/繪制輪廓的方法
- OpenCV 輪廓檢測(cè)的實(shí)現(xiàn)方法
- Opencv提取連通區(qū)域輪廓的方法
- python+opencv輪廓檢測(cè)代碼解析
- Opencv處理圖像之輪廓提取
- OpenCV實(shí)現(xiàn)圖像輪廓檢測(cè)以及外接矩形
- opencv3/C++輪廓的提取與篩選方式
- Opencv實(shí)現(xiàn)輪廓提取功能