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()

相关推荐
胖虎喜欢静香2 分钟前
从零到一快速实现 Mini DeepResearch
人工智能·python·开源
MoonBit月兔5 分钟前
「Why MoonBit 」第一期——Singularity Note AI 学习助手
开发语言·人工智能·moonbit
qq_392690668 分钟前
Redis怎样应对Redis集群整体宕机带来的雪崩
jvm·数据库·python
β添砖java11 分钟前
深度学习(11)数值稳定+模型初始化、激活函数
人工智能·深度学习
珠海西格电力18 分钟前
零碳园区产业园管理系统的全场景源网荷储氢协同调度功能是如何实现的
大数据·运维·人工智能·物联网·能源
smallyoung19 分钟前
具有反思能力的 Agentic RAG 实战:用 LangChain4j 实现 CRAG 纠错检索
人工智能·后端
wenzhangli722 分钟前
Harness Engineering:AICode 的灵魂——Ooder A2UI 从难产到重生的深度实践
人工智能·ai编程
Deepoch26 分钟前
Deepoc 具身模型开发板在田间除草机器人自主作业中的技术应用
人工智能·机器人·具身模型·deepoc·除草机器人
Muyuan199828 分钟前
22.让 RAG Agent 更像真实产品:聊天页面优化、PDF 上传、知识库重建与检索片段展示
python·django·pdf·fastapi
ai大模型中转api测评33 分钟前
解密 GPT-5.5:原生多模态架构如何重定义 AI 逻辑推理与精准制图
大数据·人工智能·gpt·架构·api