环境:Python3.8 和 OpenCV
内容:图像质心、周长、面积和近似轮廓的计算
import cv2 as cv
import matplotlib.pyplot as plt
# 封装图片显示函数
def image_show(image):
if image.ndim == 2:
plt.imshow(image, cmap='gray')
else:
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()
if __name__ == '__main__':
# 读取原图
img_desk = cv.imread('desk.png')
# 转换为灰度图
img_gray = cv.cvtColor(img_desk, cv.COLOR_RGB2GRAY)
# 二值化
[thresh, img_bin] = cv.threshold(img_gray, -1, 255, cv.THRESH_OTSU)
# 获取轮廓
[cnt, hir] = cv.findContours(img_bin, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE)
# 绘制轮廓
cv.drawContours(img_desk, cnt, 12, (0, 0, 255), 2)
# 计算一阶矩
Moment = cv.moments(cnt[12])
# 计算质心
Moment_X = int(Moment['m10'] / Moment['m00'])
Moment_Y = int(Moment['m01'] / Moment['m00'])
print("显示图像的质心为:", Moment_X, Moment_Y)
# 计算弧长
arc = cv.arcLength(cnt[12], True)
print("显示图像的弧长为:", int(arc))
# 计算面积
area = cv.contourArea(cnt[12])
print("显示图像的面积为:", int(area))
# 近似轮廓
epsilon = 0.1 * arc
approx = cv.approxPolyDP(cnt[12], epsilon, True)
# 显示图像
image_show(img_desk)
# 绘制图像的近似轮廓
cv.drawContours(img_desk, [approx], 0, (255, 0, 0), 3)
# 显示图像
image_show(img_desk)