OpenCV学习(32) 求轮廓的包围盒
在OpenCV中,能够很方便的求轮廓包围盒。包括矩形,圆形,椭圆形以及倾斜的矩形(包围面积最小)集中包围盒。用到的四个函数是:
Rect boundingRect(InputArray points)
void minEnclosingCircle(InputArray points, Point2f& center, float& radius)
RotatedRect minAreaRect(InputArray points)
RotatedRect fitEllipse(InputArray points)
输入的参数都是轮廓,下面是程序代码:
1. Rect和原型包围盒代码:
nt main( int argc, char** argv )
{
//装入图像
src = imread("../ballon.jpg", 1 ); //转化为灰度图并进行blur操作
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) ); namedWindow( "source", CV_WINDOW_AUTOSIZE );
imshow( "source", src ); Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy; //得到二值图像
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
//查找轮廓
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); //对轮廓进行多边形近似处理求得圆形和四边形包围盒
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() ); //得到每个轮廓的包围盒RECT以及园,园用中心和半径表示
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
} //画轮廓以及它的四边形和原型包围盒
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
//tl是左上角坐标, br是右下角坐标
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
} namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing ); while(1)
waitKey(0);
return(0);
}
程序运行效果:
2. 椭圆形和倾斜的矩形包围盒代码:
int main( int argc, char** argv )
{
//装入图像
src = imread("../ballon.jpg", 1 ); //转化为灰度图并进行blur操作
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) ); namedWindow( "source", CV_WINDOW_AUTOSIZE );
imshow( "source", src ); Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy; //得到二值图像
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
//查找轮廓
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); //查找轮廓的最小旋转rect和椭圆包围盒
vector<RotatedRect> minRect( contours.size() );
vector<RotatedRect> minEllipse( contours.size() ); for( int i = 0; i < contours.size(); i++ )
{ minRect[i] = minAreaRect( Mat(contours[i]) );
if( contours[i].size() > 5 )
{ minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
} //画轮廓和包围盒
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// 轮廓
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
// 椭圆
ellipse( drawing, minEllipse[i], color, 2, 8 );
// 旋转rect
Point2f rect_points[4]; minRect[i].points( rect_points );
for( int j = 0; j < 4; j++ )
line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
} namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing ); while(1)
waitKey(0);
return(0);
}
程序运行效果:
OpenCV学习(32) 求轮廓的包围盒的更多相关文章
- OpenCV学习代码记录——轮廓(contour)检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习(34) 点到轮廓的距离
在OpenCV中,可以很方便的计算一个像素点到轮廓的距离,计算距离的函数为: double pointPolygonTest(InputArray contour, Point2f pt, bool ...
- OpenCV学习笔记(14)——轮廓的性质
提取一些经常使用的对象特征 1.长宽比 边界矩形的宽高比 x,y,w,h = cv2.boundingRect(cnt) a ...
- opencv学习笔记(二)寻找轮廓
opencv学习笔记(二)寻找轮廓 opencv中使用findContours函数来查找轮廓,这个函数的原型为: void findContours(InputOutputArray image, O ...
- OpenCV 学习笔记03 边界框、最小矩形区域和最小闭圆的轮廓
本节代码使用的opencv-python 4.0.1,numpy 1.15.4 + mkl 使用图片为 Mjolnir_Round_Car_Magnet_300x300.jpg 代码如下: impor ...
- OpenCV学习(30) 轮廓defects
上一篇教程中,我们学习了如何计算轮廓的凸包,其实对一个轮廓而言,可能它的凸包和它本身是重合的,也有可能不是重合的.比如下面左边图像的轮廓本身就是凸包,而右边图像的轮廓则不是.我们可以通过函数bool ...
- OpenCV学习(28) 轮廓
OpenCV中可以方便的在一副图像中检测到轮廓,并把这些轮廓画出来.主要用到两个函数:一个是findContours( img, contours0, hierarchy, RETR_TREE, CH ...
- OpenCV学习笔记(12)——OpenCV中的轮廓
什么是轮廓 找轮廓.绘制轮廓等 1.什么是轮廓 轮廓可看做将连续的点(连着边界)连在一起的曲线,具有相同的颜色和灰度.轮廓在形态分析和物体的检测和识别中很有用. 为了更加准确,要使用二值化图像.在寻找 ...
- OpenCV 学习笔记03 findContours函数
opencv-python 4.0.1 1 函数释义 词义:发现轮廓! 从二进制图像中查找轮廓(Finds contours in a binary image):轮廓是形状分析和物体检测和识别的 ...
随机推荐
- The file will have its original line endings in your working directory.
在空仓库的情况下,add,出现一下问题 The file will have its original line endings in your working directory. 当报这个警告时是 ...
- Stringbuilder & Stringbuffer
StringBuilder和StringBuffer的父类都是继承了 AbstractStringBuilder, 他们各自的append方法都是调用了 super.append(str), 但是一个 ...
- 虚拟机hadoop集群搭建
hadoop tar -xvf hadoop-2.7.3.tar.gz mv hadoop-2.7.3 hadoop 在hadoop根目录创建目录 hadoop/hdfs hadoop/hdfs/tm ...
- 详谈 Spring 中的 IOC 和 AOP
这篇文章主要讲 Spring 中的几个点,Spring 中的 IOC,AOP,下一篇说说 Spring 中的事务操作,注解和 XML 配置. Spring 简介 Spring 是一个开源的轻量级的企业 ...
- python io 模块之 open() 方法(好久没写博客了)
io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True),打开file ...
- Python 面向对象编程——继承和多态
<基本定义> 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超 ...
- PHP 二维数组根据某个字段排序 复制代码 array_multisort
//二维数组,按照里面的age从大到小降序,代码如下 <?php header('Content-Type:text/html;Charset=utf-8'); $arrUsers = arra ...
- BZOJ 2653: middle 主席树 二分
https://www.lydsy.com/JudgeOnline/problem.php?id=2653 因为是两个方向向外延伸所以不能对编号取前缀和(这里只有前缀和向后传递的性质,不是实际意义的和 ...
- 用C读取系统明文(附源码)
从一好朋友那得到一个好东西 可以读取系统明文 请用vc++ 6.0编译 #include <windows.h> #include <stdio.h> // // Vsbat[ ...
- hdu 4163 Stock Prices 花式排序
Stock Prices Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...



