OpenCV3.0 Examples学习笔记(16)-minarea.cpp-minAreaRect,minEnclosingTriangle,minEnclosingCircle获取点集外接形状

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012566751/article/details/54618084
这个系列的目的是通过对OpenCV示例,进一步了解OpenCV函数的使用,不涉及具体原理。

目录
简介
Example运行截图
Example分析
Example代码

简介
本文记录了对OpenCV示例 minarea.cpp 的分析。
资料地址:http://docs.opencv.org/3.0.0/d4/d32/minarea_8cpp-example.html

这个示例主要演示了如何使用 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函数
3.《 cvRound

猜你喜欢

转载自blog.csdn.net/u012566751/article/details/54618084
今日推荐