OpenCV3.0 Examples学习笔记(16)-minarea.cpp-minAreaRect,minEnclosingTriangle,minEnclosingCircle获取点集外接形状
来源:互联网 发布:美即面膜怎么样知乎 编辑:程序博客网 时间:2024/06/05 03:41
这个系列的目的是通过对OpenCV示例,进一步了解OpenCV函数的使用,不涉及具体原理。
目录
简介
Example运行截图
Example分析
Example代码
简介
本文记录了对OpenCV示例minarea.cpp的分析。这个示例主要演示了如何使用minAreaRect,minEnclosingTriangle,minEnclosingCircle函数。
示例涉及minAreaRect。
minAreaRect
主要求得包含点集最小面积的矩形,这个矩形是可以有偏转角度的,可以与图像的边界不平行。
函数原型:
CV_EXPORTS_W RotatedRect minAreaRect( InputArray points );
参数说明:
points:表示输入的点集。(vector<Point> points,或Mat(points))。
返回值说明:
RotatedRect,包含了外接矩形的中心center、大小size以及角度angle。
注意:示例使用了RotatedRect的函数points,void points(Point2f pts[]) const;,函数用于返回RotatedRect的4个顶点,具体使用如下:
Point2f vtx[4];
box.points(vtx);
// Draw the bounding box
for( i = 0; i < 4; i++ )
line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, LINE_AA);
minEnclosingTriangle
获取点集最小外接三角形
函数原型:
CV_EXPORTS_W double minEnclosingTriangle( InputArray points, CV_OUT OutputArray triangle );
参数说明:
points:表示输入的点集.(vector<Point> points,或Mat(points));
triangle :表示三角形三个顶点的坐标(vector<Point2f> triangle;)。
注意:绘制三角形代码如下:
// Draw the triangle
for( i = 0; i < 3; i++ )
line(img, triangle[i], triangle[(i+1)%3], Scalar(255, 255, 0), 1, LINE_AA);
minEnclosingCircle
获取点集最小外接圆形
函数原型:
CV_EXPORTS_W void minEnclosingCircle( InputArray points,
CV_OUT Point2f& center, CV_OUT float& radius );
参数说明:
points:表示输入的点集(vector<Point> points,或Mat(points));
center:表示圆心;
radius:表示原的半径。
注意:绘制圆形代码如下:
// Draw the circle
circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA);
Example运行截图
Example分析
1.创建预览图
Mat img(500, 500, CV_8UC3);
2.创建随机
RNG& rng = theRNG();
注意:
(1)theRNG()函数返回一个默认的随机数生成器。
3.在for循环中反复观察,直到按键盘ESC,q,Q任意一键,则退出
for(;;)
{
...
char key = (char)waitKey();
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
return 0;
4.详细分析循环中处理过程
4.1.获取1-100随机数count(作为测试样本点数量),并声明i作为遍历count的计数器
int i, count = rng.uniform(1, 101);
4.2.存储测试样本点
vector<Point> points;
4.3.在图像中间位置随机生成测试样本点
// Generate a random set of points
for( i = 0; i < count; i++ )
{
Point pt;
pt.x = rng.uniform(img.cols/4, img.cols*3/4);
pt.y = rng.uniform(img.rows/4, img.rows*3/4);
points.push_back(pt);
}
4.4.调用minAreaRect获取点集最小外接矩形
// Find the minimum area enclosing bounding box
RotatedRect box = minAreaRect(Mat(points));
4.5.调用minEnclosingTriangle获取点集最小外接三角形
// Find the minimum area enclosing triangle
vector<Point2f> triangle;
minEnclosingTriangle(points, triangle);
4.6.调用minEnclosingCircle获取点集最小外接圆形
// Find the minimum area enclosing circle
Point2f center, vtx[4];
float radius = 0;
minEnclosingCircle(Mat(points), center, radius);
4.7.使用RotatedRect函数points,返回RotatedRect4个顶点
box.points(vtx);
4.8.设置预览图背景为黑色
img = Scalar::all(0);
4.9.绘制测试样本点
// Draw the points
for( i = 0; i < count; i++ )
circle( img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA );
4.10.绘制矩形
// Draw the bounding box
for( i = 0; i < 4; i++ )
line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, LINE_AA);
4.11.绘制三角形
// Draw the triangle
for( i = 0; i < 3; i++ )
line(img, triangle[i], triangle[(i+1)%3], Scalar(255, 255, 0), 1, LINE_AA);
4.12.绘制圆
// Draw the circle
circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA);
注意:
函数 cvRound, cvFloor, cvCeil 用一种舍入方法将输入浮点数转换成整数。
(1)cvRound 返回和参数最接近的整数值。
(2)cvFloor 返回不大于参数的最大整数值。
(3)cvCeil 返回不小于参数的最小整数值。
4.13.显示预览图
imshow( "Rectangle, triangle & circle", img );
Example代码
#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>using namespace cv;using namespace std;static void help(){ cout << "This program demonstrates finding the minimum enclosing box, triangle or circle of a set\n" << "of points using functions: minAreaRect() minEnclosingTriangle() minEnclosingCircle().\n" << "Random points are generated and then enclosed.\n\n" << "Press ESC, 'q' or 'Q' to exit and any other key to regenerate the set of points.\n\n" << "Call:\n" << "./minarea\n" << "Using OpenCV v" << CV_VERSION << "\n" << endl;}int main( int /*argc*/, char** /*argv*/ ){ help(); Mat img(500, 500, CV_8UC3); RNG& rng = theRNG(); for(;;) { int i, count = rng.uniform(1, 101); vector<Point> points; // Generate a random set of points for( i = 0; i < count; i++ ) { Point pt; pt.x = rng.uniform(img.cols/4, img.cols*3/4); pt.y = rng.uniform(img.rows/4, img.rows*3/4); points.push_back(pt); } // Find the minimum area enclosing bounding box RotatedRect box = minAreaRect(Mat(points)); // Find the minimum area enclosing triangle vector<Point2f> triangle; minEnclosingTriangle(points, triangle); // Find the minimum area enclosing circle Point2f center, vtx[4]; float radius = 0; minEnclosingCircle(Mat(points), center, radius); box.points(vtx); img = Scalar::all(0); // Draw the points for( i = 0; i < count; i++ ) circle( img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA ); // Draw the bounding box for( i = 0; i < 4; i++ ) line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, LINE_AA); // Draw the triangle for( i = 0; i < 3; i++ ) line(img, triangle[i], triangle[(i+1)%3], Scalar(255, 255, 0), 1, LINE_AA); // Draw the circle circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA); imshow( "Rectangle, triangle & circle", img ); char key = (char)waitKey(); if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC' break; } return 0;}
参考资料:
1.《minAreaRect函数》2.《OpenCV API Reference》
3.《cvRound》
1 0
- OpenCV3.0 Examples学习笔记(16)-minarea.cpp-minAreaRect,minEnclosingTriangle,minEnclosingCircle获取点集外接形状
- OpenCV3.0 Examples学习笔记(2)-convexhull.cpp
- OpenCV3.0 Examples学习笔记(3)-cout_mat.cpp
- OpenCV3.0 Examples学习笔记(4)-demhist.cpp
- OpenCV3.0 Examples学习笔记(5)-distrans.cpp
- OpenCV3.0 Examples学习笔记(6)-edge.cpp
- OpenCV3.0 Examples学习笔记(7)-ffilldemo.cpp
- OpenCV3.0 Examples学习笔记(8)-filestorage.cpp
- OpenCV3.0 Examples学习笔记(1)-contours2.cpp-通过findContours 函数实现轮廓提取
- OpenCV3.0 Examples学习笔记(9)-fitellipse.cpp-fitEllipse函数实现椭圆拟合
- OpenCV3.0 Examples学习笔记(11)-houghcircles.cpp-houghcircles函数实现圆形检测
- OpenCV3.0 Examples学习笔记(12)-houghlines.cpp-通过HoughLinesP函数实现直线检测
- OpenCV3.0 Examples学习笔记(15)-lsd_lines.cpp-LSD直线检测
- OpenCV3.0 Examples学习笔记(18)-pca.cpp-PCA类实现降维处理
- OpenCV3.0 Examples学习笔记(10)-grabcut.cpp-grabcut函数实现人机交互高效分割图像前景背景
- OpenCV3.0 Examples学习笔记(13)-kmeans.cpp-kmeans函数实现对图像位置进行聚类
- OpenCV3.0 Examples学习笔记(14)-laplace.cpp-LaplaLacian实现对摄像头预览图边缘检测
- OpenCV3.0 Examples学习笔记(17)-morphology2.cpp-erode,dilate,morphologyEx函数实现数学形态学变换
- java之mybatis参数映射
- 从数组到HashMap之算法解释
- 配置虚拟主机的坑
- spark漫游记之-思维导图系列
- spring event-listener模型
- OpenCV3.0 Examples学习笔记(16)-minarea.cpp-minAreaRect,minEnclosingTriangle,minEnclosingCircle获取点集外接形状
- Gradle统一配置工程下每个project
- PAT 1100. Mars Numbers (20)
- meset,strcpy等函数
- Java的序列化和反序列化,我所遇到的坑
- AndroidStudio2.2.3版本使用CMake编译NDK工程(ffmpeg)
- winForm 读取Web.config 包含(xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0")
- WordPress出现循环重定向解决办法
- 帮助新手理解MVC,可以看看,思维导图原件可以找我