日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python椭圆拟合_opencv python 轮廓特征/凸包/外接矩形/外接圆/拟合矩形/拟合直线/拟合圆...

發(fā)布時間:2023/12/3 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python椭圆拟合_opencv python 轮廓特征/凸包/外接矩形/外接圆/拟合矩形/拟合直线/拟合圆... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Contour Features

1 圖像的矩

cv2.moments()

圖像的矩可以幫助計算物體的某些特征,如對象的質心,對象的區(qū)域等.

代碼:

import cv2

import numpy as np

img = cv2.imread('img7.png',0)

ret,thresh = cv2.threshold(img,127,255,0)

im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]

M = cv2.moments(cnt)

print( M )

輸出:{'m00': 283.0, 'm10': 8260.666666666666, 'm01': 34747.666666666664, 'm20': 251349.8333333333, 'm11': 1008063.0, 'm02': 4274513.166666666, 'm30': 7941981.4, 'm21': 30484543.9, 'm12': 123258620.46666667, 'm03': 526819846.70000005, 'mu20': 10223.989595602674, 'mu11': -6208.702394974302, 'mu02': 8080.874165684916, 'mu30': 8302.495426246896, 'mu21': -14552.154961312423, 'mu12': 11791.528133469663, 'mu03': -3268.923251092434, 'nu20': 0.12765785058625623, 'nu11': -0.07752253611575, 'nu02': 0.10089867729257346, 'nu30': 0.006162296011483629, 'nu21': -0.010800931752771139, 'nu12': 0.008751933371317017, 'nu03': -0.0024262672459139235}

此刻,可以提取有用的數(shù)據(jù),如面積,質心等.

質心由關系給出:

cx = int(M['m10']/M['m00'])

cy = int(M['m01']/M['m00'])

2輪廓面積

cv2.contourArea(contour[, oriented])

3輪廓周長

cv2.arcLength(curve, closed)

第二個參數(shù)指定形狀是否為閉合輪廓

4輪廓近似

它根據(jù)我們指定的精度將輪廓形狀近似為具有較少頂點數(shù)的另一個形狀.它是Douglas-Peucker算法的一種實現(xiàn)方式.

cv2.approxPolyDP(curve, epsilon, closed[, approxCurve])

第二個參數(shù)epsilon,它是從輪廓到近似輪廓的最大距離.第三個參數(shù)指定曲線是否閉合.

下面,在第二幅圖像中,綠線表示epsilon =弧長的10%的近似曲線. 第三幅圖像顯示相同的epsilon =弧長的1%.

代碼:

import cv2

import numpy as np

img = cv2.imread('img8.png')

cv2.imshow('src',img)

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cnt = contours[1]

epsilon = 0.1*cv2.arcLength(cnt,True)

approx = cv2.approxPolyDP(cnt,epsilon,True)

cv2.polylines(img, [approx], True, (0, 0, 255), 2)

cv2.imshow('show',img)

cv2.waitKey()

5凸包

凸包看起來類似輪廓近似,但是它不是(兩者在某些情況下可能提供相同的結果).

convexHull(points[, hull[, clockwise[, returnPoints]]]):檢查曲線的凸性缺陷并進行修正.

points:傳入的輪廓

hull:輸出

clockwise:方向標志,如果為True,則順時針方向輸出凸包.

returnPoints:默認情況下為True,然后它返回hull points的坐標; 如果為False,則返回與hull points對應的輪廓點的索引

下面的手形圖像. 紅線表示手的凸包, 雙面箭頭標記顯示凸起缺陷.

代碼:

import cv2

import numpy as np

img = cv2.imread('img8.png')

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cnt = contours[1]

hull = cv2.convexHull(cnt)

returnPoints = True,得到以下值:

array([[[192, 135]],

[[ 9, 135]],

[[ 9, 12]],

[[192, 12]]], dtype=int32)

如果想找到凸性缺陷,需要傳遞returnPoints = False,得到以下結果:

array([[129],

[ 67],

[ 0],

[142]], dtype=int32)

這些是輪廓中相應點的索引,檢查第一個值:

cnt[129]

Out[3]: array([[192, 135]], dtype=int32)

與第一個結果相同.

6 檢查凸性

cv2.isContourConvex(contour):檢查曲線是否凸起

7 外接矩形

7.1 直邊外接矩形

它是一個直的矩形,它不考慮對象的旋轉。因此,邊界矩形的面積不會最小.

cv.boundingRect()

設(x,y)為矩形的左上角坐標,(w,h)為寬度和高度

代碼:

import cv2

import numpy as np

img = cv2.imread('img7.png')

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cnt = contours[0]

x,y,w,h = cv2.boundingRect(cnt)

cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

cv2.imshow('show',img)

cv2.waitKey()

7.2 最小外接矩形

cv.minAreaRect返回一個Box2D結構,其中包含以下detals - (center(x,y),(width,height),rotation of rotation)

cv.boxPoints畫上述矩形.

代碼:

rect = cv2.minAreaRect(cnt)

box = cv2.boxPoints(rect)

box = np.int0(box)

cv2.drawContours(img,[box],0,(0,0,255),2)

8 最小封閉圈

(x,y),radius = cv2.minEnclosingCircle(cnt)

center = (int(x),int(y))

radius = int(radius)

cv2.circle(img,center,radius,(0,255,0),2)

9 擬合橢圓

ellipse = cv2.fitEllipse(cnt)

cv2.ellipse(img,ellipse,(0,255,0),2)

10 擬合直線

rows,cols = img.shape[:2]

[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01)

lefty = int((-x*vy/vx) + y)

righty = int(((cols-x)*vy/vx)+y)

cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)

總結

以上是生活随笔為你收集整理的python椭圆拟合_opencv python 轮廓特征/凸包/外接矩形/外接圆/拟合矩形/拟合直线/拟合圆...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。