欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV-Python圖像輪廓之輪廓特征詳解

 更新時(shí)間:2021年12月21日 10:52:03   作者:機(jī)器視覺小學(xué)徒  
圖像輪廓是指由位于邊緣、連續(xù)的、具有相同顏色和強(qiáng)度的點(diǎn)構(gòu)成的曲線,它可以用于形狀分析以及對(duì)象檢測(cè)和識(shí)別。本文將帶大家詳細(xì)了解一下圖像的輪廓特征,感興趣的可以學(xué)習(xí)一下

前言

圖像輪廓是指由位于邊緣、連續(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)文章

  • pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理)

    pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理)

    這篇文章主要介紹了pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理),pandas對(duì)大數(shù)據(jù)有很多便捷的清洗用法,尤其針對(duì)缺失值和重復(fù)值,詳細(xì)介紹感興趣的小伙伴可以參考下面文章內(nèi)容
    2022-08-08
  • Pytorch中的數(shù)據(jù)集劃分&正則化方法

    Pytorch中的數(shù)據(jù)集劃分&正則化方法

    這篇文章主要介紹了Pytorch中的數(shù)據(jù)集劃分&正則化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python用戶自定義異常的實(shí)例講解

    python用戶自定義異常的實(shí)例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python用戶自定義異常的實(shí)例講解,以后需要的朋友們可以跟著學(xué)習(xí)參考下。
    2021-07-07
  • Pandas?DataFrame.drop()刪除數(shù)據(jù)的方法實(shí)例

    Pandas?DataFrame.drop()刪除數(shù)據(jù)的方法實(shí)例

    pandas作為數(shù)據(jù)分析強(qiáng)大的庫(kù),是基于numpy數(shù)組構(gòu)建的,專門用來(lái)處理表格和混雜的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame.drop()刪除數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Python實(shí)現(xiàn)釘釘訂閱消息功能

    Python實(shí)現(xiàn)釘釘訂閱消息功能

    這篇文章主要介紹了Python實(shí)現(xiàn)釘釘訂閱消息,本文通過(guò)實(shí)例代碼截圖的形式給大家展示的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • python多次繪制條形圖的方法

    python多次繪制條形圖的方法

    這篇文章主要為大家詳細(xì)介紹了python多次繪制條形圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Python線程之同步機(jī)制實(shí)際應(yīng)用場(chǎng)景舉例說(shuō)明

    Python線程之同步機(jī)制實(shí)際應(yīng)用場(chǎng)景舉例說(shuō)明

    這篇文章主要給大家分享的是Python線程之同步機(jī)制實(shí)際應(yīng)用場(chǎng)景舉例說(shuō)明,銀行轉(zhuǎn)賬小栗子供大家參考學(xué)習(xí),希望對(duì)你有一定的幫助
    2022-02-02
  • Python在OpenCV里實(shí)現(xiàn)極坐標(biāo)變換功能

    Python在OpenCV里實(shí)現(xiàn)極坐標(biāo)變換功能

    這篇文章主要介紹了在OpenCV里實(shí)現(xiàn)極坐標(biāo)變換功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • python文件拆分與重組實(shí)例

    python文件拆分與重組實(shí)例

    今天小編就為大家分享一篇python文件拆分與重組實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • 用opencv給圖片換背景色的示例代碼

    用opencv給圖片換背景色的示例代碼

    這篇文章主要介紹了用opencv給圖片換背景色的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論