OpenCV之轮廓的特征2:面积、周长

2023-07-09  本文已影响0人  大龙10

一、轮廓的面积

cv2.contourArea ( InputArray contour,bool oriented = false )

二、例1

import cv2
import numpy as np
 
img = cv2.imread("pie.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
contour = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(img,contour,-1,(0,0,255),2)
cv2.imshow("res",img)
cnt = contour[0]
M = cv2.moments(cnt)
area1=cv2.contourArea(cnt)
print(area1)
area2 = int(M['m00'])
print(area2)
cv2.waitKey(0)
cv2.destroyAllWindows()

三、轮廓的周长

retval = cv.arcLength( curve, closed )

import cv2
import numpy as np
 
img = cv2.imread("pie.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
contour = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(img,contour,-1,(0,0,255),2)
cv2.imshow("res",img)
cnt = contour[0]
C= cv2.arcLength(cnt,True)
print(C)
cv2.waitKey(0)
cv2.destroyAllWindows()

四、资料

qiaokuankuan的博客:
https://www.cnblogs.com/wuyuan2011woaini/p/15656445.html
上一篇 下一篇

猜你喜欢

热点阅读