import cv2 as cv
import math
def area_perimeter_roundness(image_path):
image = cv.imread(image_path)
blur = cv.blur(image,(3,3))
gray = cv.cvtColor(blur,cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(gray,30,255,cv.THRESH_BINARY)
contours,hierarchy = cv.findContours(thresh,cv.RETR_TREE,cv.CHAIN_APPROX_NONE)
contoursArea = 0 # 面积
arcLength = 0 # 周长
for i in range(len(contours)):
cnt = contours[i]
if(contoursArea <= cv.contourArea(cnt)):
contoursArea = cv.contourArea(cnt)
if(arcLength <= cv.arcLength(cnt , True)):
arcLength = cv.arcLength(cnt , True)
roundness = (contoursArea ** 2) / ( 4 * math.pi * arcLength ) # 圆度
return contoursArea, arcLength, roundness
长度、面积、圆度特征
于 2023-03-17 14:06:18 首次发布