公式
圆度计算公式:
4*PI*A/P^2
其中PI表示Π,A表示区域面积,P表示区域周长。
Opencv
def compute_roundness(label_image):
contours, hierarchy = cv2.findContours(np.array(label_image, dtype=np.uint8), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
a = cv2.contourArea(contours[0]) * 4 * math.pi
b = math.pow(cv2.arcLength(contours[0], True), 2)
if b == 0:
return 0
return a / b
skimage
from scipy import misc
import numpy as np
from skimage.measure import label, regionprops
import cv2
import math
mask = misc.imread(mask_path)
img = misc.imread(img_path, mode="L")
mask = label(mask)
mask_props = regionprops(mask, intensity_image=img)
for mask_prop in mask_props:
roundness = (4 * math.pi * mask_prop['area']) / (mask_prop['perimeter']**2)