当前位置: 首页 > news >正文

OpenCV(三十四):轮廓外接最大、最小矩形和多边形拟合

目录

1.轮廓外接最大矩形boundingRect()

2.轮廓外接最小矩形minAreaRect()

3.轮廓外接多边形approxPolyDP()


1.轮廓外接最大矩形boundingRect()

Rect cv::boundingRect ( InputArray array )

  • array:输入的灰度图像或者2D点集,数据类型为vector<Point>或者Mat。

示例代码:

//轮廓最大外接矩阵
void Contour_external_maxmatrix(Mat image) {Mat gray,binary;cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化GaussianBlur(gray,gray,Size(9,9),2,2);//滤波threshold(gray,binary,170,255,THRESH_BINARY|THRESH_OTSU);//自适应二值化//轮廓检测vector<vector<Point>> contours;//轮廓vector<Vec4i> hierarchy;//存放轮廓结构变量findContours(binary,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());//寻找轮廓的外接矩阵for(int n=0;n<contours.size();n++) {//最大外接矩阵Rect rect = boundingRect(contours[n]);rectangle(image, rect, Scalar(7, 67, 255), 2, 8, 0);}imwrite("/sdcard/DCIM/max.png",image);
}

 轮廓外接最大矩形的结果:

2.轮廓外接最小矩形minAreaRect()

RotatedRect cv::minAreaRect ( InputArray points )

  • array:输入的灰度图像或者2D点集,数据类型为vector<Point>或者Mat。

示例代码:


//轮廓最小外接矩阵
void Contour_external_minmatrix(Mat image){Mat gray,binary;cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化GaussianBlur(gray,gray,Size(9,9),2,2);//滤波threshold(gray,binary,170,255,THRESH_BINARY|THRESH_OTSU);//自适应二值化//轮廓检测vector<vector<Point>> contours;//轮廓vector<Vec4i> hierarchy;//存放轮廓结构变量findContours(binary,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());//寻找轮廓的外接矩阵for(int n=0;n<contours.size();n++) {//最小外接矩阵RotatedRect rrect= minAreaRect(contours[n]);Point2f points[4];rrect.points(points);//读取最小外接矩阵的四个顶点Point2f cpt=rrect.center;//最小外接矩阵的中心//绘制旋转矩阵与中心位置for(int i=0;i<4;i++){if(i==3){line(image,points[i],points[0],Scalar(0,0,255,255),2,8,0);break;}line(image,points[i],points[i+1],Scalar(0,0,255,255),2,8,0);}//绘制矩阵中心circle(image,cpt,4,Scalar(0,0,255,255),-1,8,0);}imwrite("/sdcard/DCIM/min.png",image);}

 轮廓外接最小矩形的结果:

 

3.轮廓外接多边形approxPolyDP()

void cv::approxPolyDP (InputArray curve,

OutputArray approxCurve,

double    epsilon,

bool      closed

  • curve:输入轮廓像素点。
  • approxCurve:多边形逼近结果,以多边形顶点坐标的形式给出
  • epsilon:逼近的精度,即原始曲线和逼近曲线之间的最大距离。
  • closed:逼近曲线是否为封闭曲线的标志, true表示曲线封闭,即最后一个顶点与第一个顶点相连

示例代码:


void drawapp(Mat result,Mat img2){for(int i=0;i<result.rows;i++){//最后一个坐标点与第一个坐标点连接if(i==result.rows-1){Vec2i point1=result.at<Vec2i>(i);Vec2i point2=result.at<Vec2i>(0);line(img2,point1,point2,Scalar(0,0,255,255),4,8,0);break;}Vec2i point1=result.at<Vec2i>(i);Vec2i point2=result.at<Vec2i>(i+1);line(img2,point1,point2,Scalar(0,0,255,255),4,8,0);}}//轮廓多边形拟合
void Contour_external_matrix(Mat image){Mat gray,binary;cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化// GaussianBlur(gray,gray,Size(9,9),2,2);//滤波// threshold(gray,binary,170,255,THRESH_BINARY|THRESH_OTSU);//自适应二值化//轮廓的发现与绘制vector<vector<Point>> contours;//轮廓vector<Vec4i> hierarchy;//存放轮廓结构变量findContours(gray,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());//绘制多边形for(int n=0;n<contours.size();n++) {//用最小外接矩阵求取轮廓中心RotatedRect rrect= minAreaRect(contours[n]);Point2f center=rrect.center;//最小外接矩阵的中心circle(image,center,2,Scalar(0,0,255,255),2,8,0);Mat result;approxPolyDP(contours[n],result,4,true);//多边形拟合drawapp(result,image);}imwrite("/sdcard/DCIM/matrix.png",image);}

多边形拟合的结果:

http://www.lryc.cn/news/162655.html

相关文章:

  • Kafka3.0.0版本——消费者(offset的默认维护位置)
  • Wireshark技巧[监听串口包]
  • 安全运营中心即服务提供商评估
  • 算法通关村第十三关——幂运算问题解析
  • Python 之使用Numpy库来加载Numpy(.npy)文件并检查其内容
  • C#学习系列之UDP同端口收发问题
  • SpringMVC之文件上传下载以及jrebel的使用
  • 基于Fomantic UI Web构建 个人导航站点网站源码 网站技术导航源码
  • DRF02-请求响应与路由
  • http直接调用paddlepaddle实现文字转语音,语音转文字
  • 9. xaml ComboBox控件
  • 【后量子密码】CRYSTALS-KYBER 算法(二):密钥封装 KEM(附源码分析)
  • 什么是原⼦操作?在 JUC 中有哪些原⼦类?
  • 2022年12月 C/C++(八级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • Hadoop的HDFS的集群安装部署
  • uniapp 在 onLoad 事件中 this.$refs 娶不到的问题
  • 常見算法時間複雜度分析
  • 自学Python05-学会Python中的函数定义
  • 设计模式-组合模式(Composite)
  • 架构核心技术之微服务架构
  • SQL Server2022版+SSMS安装教程(保姆级)
  • go语言基础---8
  • Oracle的 dblink 学习笔记
  • 任意文件上传
  • 【Unity3D】UI Toolkit自定义元素
  • layui手机端使用laydate时间选择器被输入法遮挡的解决方案
  • MVSNet CVPR-2018 学习总结笔记 译文 深度学习三维重建
  • Kafka/Spark-01消费topic到写出到topic
  • 【算法与数据结构】98、LeetCode验证二叉搜索树
  • 关于GitHub Desktop中的“Open in Git Bash”无法使用的问题