#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 27 19:14:12 2017
@author: fs
"""
import cv2
import numpy as np
img = cv2.imread('lunkuo.png')
ret,thresh = cv2.threshold(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY),127,255,0)
_,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)#得到轮廓信息
cnt = contours[0]#取第一条轮廓
M = cv2.moments(cnt)#计算第一条轮廓的矩
imgnew = cv2.drawContours(img, contours, -1, (0,255,0), 3)#把所有轮廓画出来
print (M)
#这两行是计算中心点坐标
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
#计算轮廓所包含的面积
area = cv2.contourArea(cnt)
#计算轮廓的周长
perimeter = cv2.arcLength(cnt,True)
#轮廓的近似
epsilon = 0.02*perimeter
approx = cv2.approxPolyDP(cnt,epsilon,True)
imgnew1 = cv2.drawContours(img, approx, -1, (0,0,255), 3)
cv2.imshow('lunkuo',imgnew)
cv2.imshow('approx_lunkuo',imgnew1)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(0)
cv2.waitKey(0)
cv2.waitKey(0)
cv2.waitKey(0)
opencv——计算轮廓的周长、所包含面积、中心点
最新推荐文章于 2025-10-28 17:50:32 发布
本文介绍了一个使用Python和OpenCV进行图像处理的实例,通过读取图片文件并转换为灰度图像,利用阈值分割获取二值图像,进一步寻找并绘制轮廓,计算轮廓的面积、周长及近似形状。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.11
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
1456





