
【OpenCV】 外接矩形、最小外接矩形、多边形拟合、外接圆
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
·
任务:给定这样一张图片求图片中白色区域的外接矩形、最小外接矩形、拟合多边形以及外接圆

1、外接矩形
x, y, w, h = cv2.boundingRect(points)
输入:点集
返回值:左上角点坐标以及宽高
实现代码:
import cv2
imgpath = '1.jpg'
# 读取图片
image = cv2.imread(imgpath)
# 转换为灰度图
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# 将图片二值化
_, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# 在二值图上寻找轮廓
_, contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# or
# contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cont in contours:
# 外接矩形
x, y, w, h = cv2.boundingRect(cont)
# 在原图上画出预测的矩形
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 10)
输出结果:

2、最小外接矩形
(cx,cy), (l,w), theta=cv2.minAreaRect(points)
输入:点集
返回值:中心点坐标->(cx,cy);长宽->(l,w);从x轴逆时针旋转到宽(w)的角度->theta
cv2.boxPoints()可以将minAreaRect的返回值转换为四个角点坐标
实现代码:
import cv2
imgpath = '1.jpg'
image = cv2.imread(imgpath)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cont in contours:
# 对每个轮廓点求最小外接矩形
rect = cv2.minAreaRect(cont)
# cv2.boxPoints可以将轮廓点转换为四个角点坐标
box = cv2.boxPoints(rect)
# 这一步不影响后面的画图,但是可以保证四个角点坐标为顺时针
startidx = box.sum(axis=1).argmin()
box = np.roll(box,4-startidx,0)
# 在原图上画出预测的外接矩形
box = box.reshape((-1,1,2)).astype(np.int32)
cv2.polylines(image,[box],True,(0,255,0),10)
输出结果:

3、外接多边形
box = cv2.approxPolyDP(curve, epsilon, closed)
输入:
curve:点集(折线图)
epsilon:点到相对应的切线的距离的阈值。(大于阈值舍弃,小于阈值保留,epsilon越小,折线的形状越“接近”曲线。)
closed:曲线是否闭合(True/False)
返回值:
多边形角点坐标
实现代码:
import cv2
imgpath = '1.jpg'
image = cv2.imread(imgpath)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cont in contours:
# 取轮廓长度的1%为epsilon
epsilon = 0.01*cv2.arcLength(cont,True)
# 预测多边形
box = cv2.approxPolyDP(cont,epsilon,True)
img = cv2.polylines(image,[box],True,(0,0,255),10)
输出结果:

4、外接圆
(x, y), radius = cv2.minEnclosingCircle(cont)
输入:点集
返回值:圆心 --> (x, y), 半径 --> radius
import cv2
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cont in contours:
# 外接圆
(x, y), radius = cv2.minEnclosingCircle(cont)
cv2.circle(image,(int(x),int(y)),int(radius), (0, 0, 255), 10)
输出结果:

原文链接:https://blog.csdn.net/weixin_37763340/article/details/114339621
OpenCV: 开源计算机视觉库
最近提交(Master分支:6 个月前 )
42a13208
Fix heap buffer overflow and use after free in imgcodecs #27138
This fixes:
- https://g-issues.oss-fuzz.com/issues/405243132
- https://g-issues.oss-fuzz.com/issues/405456349
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
8 小时前
8e2826dd
Set C++ standard for all CUDA configurations 16 小时前
更多推荐




所有评论(0)