最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

圖像輪廓是指由位于邊緣、連續(xù)的、具有相同顏色和強度的點構成的曲線,它可以用于形狀分析以及對象檢測和識別。

一、輪廓的矩

輪廓的矩包含了輪廓的各種幾何特征,如面積、位置、角度、形狀等。cv2.moments()函數(shù)用于返回輪廓的矩,其基本格式如下:

ret = cv2.moments(array[, binaryImage])

ret為返回的輪廓的矩,是一個字典對象, 大多數(shù)矩的含義比較抽象, 但其中的零階矩(m00)表示輪廓的面積
array為表示輪廓的數(shù)組
binaryImage值為True時,會將array對象中的所有非0值設置為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ù)值為False(默認值)時, 函數(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.contourArea(contours[0])
m1 = cv2.contourArea(contours[1])

print('輪廓0的面積:', m0)
print('輪廓1的面積:', m1)

在這里插入圖片描述

三、輪廓的長度

cv2.arcLength()函數(shù)用于返回輪廓的長度,其基本格式如下:

ret = cv2.cv2.arcLength(contour, closed)

ret為返回的長度
contour為輪廓
closed為布爾值, 為True時表示輪廓是封閉的
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的長度:', m0)
print('輪廓1的長度:', m1)

在這里插入圖片描述

四、輪廓的近似多邊形

cv2.approxPolyDP()函數(shù)用于返回輪廓的近似多邊形,其基本格式如下:

ret = cv2.cv2.arcLength(contour, epsilon, closed)

ret為返回的近似多邊形
contour為輪廓
epsilon為精度, 表示近似多邊形接近輪廓的最大距離
closed為布爾值, 為True時表示輪廓是封閉的
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為返回的凸包, 是一個numpy.ndarray對象, 包含了凸包的關鍵點
contours為輪廓
clockwise為方向標記, 為True時, 凸包為順時針方向, 為False(默認值)時, 凸包為逆時針方向
returnPointss為True時(默認值)時, 返回的hull中包含的是凸包關鍵點的坐標, 為False時, 返回的是凸包關鍵點在輪廓中的索引
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 時返回的凸包;\n',hull)

hull2 = cv2.convexHull(contours[0], returnPoints=False)
print('returnPoints = False時返回的凸包;\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為返回的直邊界矩形, 它是一個四元組, 其格式為(矩形左上角x坐標, 矩形左上角y坐標, 矩形的寬度, 矩形的高度)
contours為用于計算直邊界矩形的輪廓
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()

在這里插入圖片描述

在這里插入圖片描述

七、輪廓的旋轉矩形

輪廓的旋轉矩形是指可容納輪廓的面積最小的矩形。

cv2.minAreaRect()函數(shù)用于返回輪廓的旋轉矩形,其基本格式如下:

box = cv2.minAreaRect(contour)

box為返回的旋轉矩陣, 它是一個三元組, 其格式為((矩形中心點x坐標, 矩形中心點y坐標), (矩形的寬度, 矩形的高度), 矩形的旋轉角度)
contour為用于計算矩形的輪廓

cv2.minAreaRect()函數(shù)返回的結果不能直接用于繪制旋轉矩形,可以使用cv2.boxPoints()函數(shù)將其轉換為矩形的頂點坐標,其基本格式如下:

points = cv2.boxPoints(box)

points為返回的矩形頂點坐標, 坐標數(shù)據為浮點數(shù)
box為cv2.minAreaRect()函數(shù)返回的矩形數(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)

# 計算最小旋轉矩形
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為用于計算最小外包圓的輪廓
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)

# 計算最小外包圓
(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為用于計算擬合橢圓的輪廓
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)

# 計算擬合橢圓
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為用于計算擬合直線的輪廓
distType為距離參數(shù)類型, 決定如何計算擬合直線
param為距離參數(shù), 與距離參數(shù)類型有關, 其設置為0時, 函數(shù)將自動選擇最優(yōu)值
reps為計算擬合直線需要的徑向精度, 通常設置為0.01
aeps為計算擬合直線需要的軸向精度, 通常設置為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)

#計算擬合直線
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為用于計算最小外包三角形的輪廓
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)

# 計算最小外包三角形
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圖像輪廓之輪廓特征詳解的詳細內容,更多關于OpenCV-Python輪廓特征的資料請關注腳本之家其它相關文章!

相關文章

  • 如何將python中的List轉化成dictionary

    如何將python中的List轉化成dictionary

    這篇文章主要介紹在python中如何將list轉化成dictionary,通過提出兩個問題來告訴大家如何解決,有需要的可以參考借鑒。
    2016-08-08
  • Django JSonResponse對象的實現(xiàn)

    Django JSonResponse對象的實現(xiàn)

    本文主要介紹了Django JSonResponse對象的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • Python中函數(shù)調用9大方法小結

    Python中函數(shù)調用9大方法小結

    在Python中,函數(shù)是一種非常重要的編程概念,它們使得代碼模塊化、可重用,并且能夠提高代碼的可讀性,本文將深入探討Python函數(shù)調用的9種方法,需要的可以參考下
    2024-01-01
  • Python設計模式之狀態(tài)模式原理與用法詳解

    Python設計模式之狀態(tài)模式原理與用法詳解

    這篇文章主要介紹了Python設計模式之狀態(tài)模式原理與用法,簡單描述了狀態(tài)模式的概念、原理并結合實例形式分析了Python實現(xiàn)與使用狀態(tài)模式的相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • Python3.4學習筆記之列表、數(shù)組操作示例

    Python3.4學習筆記之列表、數(shù)組操作示例

    這篇文章主要介紹了Python3.4列表、數(shù)組操作,結合實例形式分析了Python3.4列表的創(chuàng)建、元素追加、刪除、排序等相關操作技巧,需要的朋友可以參考下
    2019-03-03
  • python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解

    python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解

    這篇文章主要介紹了python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • Python中new方法的詳解

    Python中new方法的詳解

    今天小編就為大家分享一篇關于Python中new方法的詳解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 淺談Python描述數(shù)據結構之KMP篇

    淺談Python描述數(shù)據結構之KMP篇

    這篇文章主要介紹了Python描述數(shù)據結構之KMP篇,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 服務器端jupyter notebook映射到本地瀏覽器的操作

    服務器端jupyter notebook映射到本地瀏覽器的操作

    這篇文章主要介紹了服務器端jupyter notebook映射到本地瀏覽器的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • CentOS 6.5下安裝Python 3.5.2(與Python2并存)

    CentOS 6.5下安裝Python 3.5.2(與Python2并存)

    這篇文章主要給大家介紹了在CentOS 6.5下安裝Python 3.5.2的方法教程,安裝后的python3與Python2并存,文中分享了詳細的方法步驟,對大家具有一定的參考學習價值,下面來一起看看吧。
    2017-06-06

最新評論

辉县市| 内乡县| 永昌县| 林西县| 陆丰市| 阿勒泰市| 镇赉县| 隆安县| 丽江市| 肇东市| 浦县| 会宁县| 鞍山市| 太原市| 佛坪县| 奇台县| 花莲县| 潼关县| 奎屯市| 婺源县| 彩票| 天全县| 莱西市| 浦城县| 赣榆县| 弥渡县| 灵川县| 新乐市| 湾仔区| 增城市| 博乐市| 二连浩特市| 扶余县| 库车县| 神农架林区| 顺平县| 内乡县| 会理县| 德惠市| 许昌县| 石泉县|