美文网首页
OpenCV之轮廓的特征2:面积、周长

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

作者: 大龙10 | 来源:发表于2023-07-09 06:37 被阅读0次

一、轮廓的面积

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

  • contour:
    是一个向量,二维点、

  • oriented:
    有默认值false,面向区域标识符,如果为true,该函数返回一个带符号的面积,其正负取决于轮廓的方向(顺时针还是逆时针)。
    根据这个特性可以根据面积的符号来确定轮廓的位置。如果是默认值false,则面积以绝对值的形式返回。

  • 图像的面积等于它的0阶矩:M['m00']

二、例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

相关文章

网友评论

      本文标题:OpenCV之轮廓的特征2:面积、周长

      本文链接:https://www.haomeiwen.com/subject/oxorudtx.html