OpenCV-Python实战(14)——轮廓拟合

1、最小包围三角形 cv2.minEnclosingTriangle()

python 复制代码
area,triangle = cv2.minEnclosingTriangle(contours)

**area:**最小包围三角形面积。

**triangle:**最小包围三角形的坐标,\[\[230 60],\[ 39 123],\[185 269]]。

**contours:**轮廓。

python 复制代码
import cv2
import numpy as np

# 图像前处理
img = cv2.imread('m.png') # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 灰度图
ret,img_binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY) # 二值图
# 找轮廓
contours,hierarchy = cv2.findContours(img_binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# 找最小包围三角形
area,triangle = cv2.minEnclosingTriangle(contours[0])
# 定位点坐标
triangle = np.int32(triangle)  # 这一步非常重要
print(triangle)
# 画轮廓
img1 = cv2.polylines(img.copy(),[triangle],True,(255,0,0),2)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

2、包围矩形 cv2.boundingRect()

python 复制代码
x,y,w,h  = cv2.boundingRect(contours)

**x,y,w,h:**函数返回值(x,y,w,h)元组,x,y代表矩形的左上角点坐标;w,h代表矩形宽高。

**contours:**轮廓。

python 复制代码
import cv2
import numpy as np

# 图像前处理
img = cv2.imread('m.png') # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 灰度图
ret,img_binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY) # 二值图
# 找轮廓
contours,hierarchy = cv2.findContours(img_binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# 找包围***
x,y,w,h  = cv2.boundingRect(contours[0])
# 画轮廓
img1 = cv2.rectangle(img.copy(),(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

3、最小包围矩形 cv2.minAreaRect()

复制代码
retval = cv2.minAreaRect(contours)

**retval:**函数返回值,((x,y),(w,h),angle),(x,y)矩形中心点坐标,(w,h)矩形的宽高,angle:旋转角度,正值为顺时针,负值为逆时针。

**contours:**轮廓。

复制代码
points = cv2.boxPoints(retval)

**points:**矩形四个顶点坐标。

retval:cv2.minAreaRect() 函数返回值。

python 复制代码
import cv2
import numpy as np

# 图像前处理
img = cv2.imread('m.png') # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 灰度图
ret,img_binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY) # 二值图
# 找轮廓
contours,hierarchy = cv2.findContours(img_binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# 找最小包围矩形
box = cv2.minAreaRect(contours[0])
# 定位点坐标
points = cv2.boxPoints(box)
points = np.int32(points)
# 画轮廓
img1 = cv2.drawContours(img.copy(),[points],0,(255,0,0),2)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

4、最小包围圆 cv2.minAreaRect()

python 复制代码
center,radius = cv2.minEnclosingCircle(contours)

center:圆心的下(x,y)坐标。

radins:圆的半径。

contours:轮廓。

python 复制代码
import cv2
import numpy as np

# 图像前处理
img = cv2.imread('fang.png') # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 灰度图
ret,img_binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY) # 二值图
# 找轮廓
contours,hierarchy = cv2.findContours(img_binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# 找包围***
(x,y),radius = cv2.minEnclosingCircle(contours[0])
# 数据处理
center = (int(x),int(y))  # 这一步非常重要
radius = int(radius)
# 画轮廓
img1 = cv2.circle(img.copy(),center,radius,(255,0,0),2)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

5、最优拟合椭圆cv2.minAreaRect()

python 复制代码
ellipse = cv2.fitEllipse(contours)

**ellipse:**函数返回值,((x,y),(a,b),angle)。(x,y)中心点坐标;(a,b)长短轴直径;angle旋转角度,正值为顺时针,负值为逆时针。

**contours:**轮廓。

python 复制代码
import cv2
import numpy as np

# 图像前处理
img = cv2.imread('juxing.png') # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 灰度图
ret,img_binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY) # 二值图
# 找轮廓
contours,hierarchy = cv2.findContours(img_binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
print(len(contours[0]))  # len(contours[0]) 必须大于 5 ,否则 ellipse = cv2.fitEllipse(contours[0])会报错
# 找包围***
ellipse = cv2.fitEllipse(contours[0])
# 画轮廓
img1 = cv2.ellipse(img.copy(),ellipse,(255,0,0),2)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

6、近视多边形 cv2.approxPolyDP()

相关推荐
sell-cloud1 小时前
Prompt Engineering要凉了?Fable 5.5能“猜“意图,但银行正在封杀AI Agent——企业AI的真正护城河是什么
人工智能·prompt
Navicat中国3 小时前
Navicat 18 内置 AI 助手让数据库开发从繁琐操作迈向智能协作
大数据·人工智能·数据库开发
4SAPI3 小时前
多模型请求路由怎么设计?成本、速度和能力如何平衡
人工智能·python·gateway·php
魔乐社区3 小时前
翼芯・智算测试与适配优化专区,让国产算力“选得准、跑得通、用得好“
人工智能·开源·大模型
XCDoris5 小时前
郴州logo设计机构
大数据·人工智能·python
Goboy5 小时前
一个人带三个Agent,我用全新TRAE从0到1搭了个资讯周刊站
人工智能·trae
俊哥V5 小时前
每日 AI 研究简报 · 2026-10-19
人工智能·ai
AI_AGENT_DEV_AI5 小时前
AI英语教培智能体的开发
人工智能
物联网软硬件开发-轨物科技6 小时前
【技术白皮书】光伏清检一体化机器人系统解决方案技术白皮书
大数据·运维·网络·人工智能·机器人
gCode Teacher 格码致知6 小时前
Pandas教学-17:Series与Dataframe的关系
开发语言·python·pandas