OpenCV 4.12.0
开源计算机视觉
加载中...
搜索中...
无匹配项
samples/cpp/minarea.cpp
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout << "此程序演示了如何使用 minAreaRect() minEnclosingTriangle() minEnclosingCircle() 函数查找一组点的最小外接矩形、三角形或圆形。\n"
<< "生成随机点,然后将其包围。\n\n"
<< "按 ESC、'q' 或 'Q' 退出,按任何其他键重新生成点集。\n\n";
<< "按 ESC、'q' 或 'Q' 退出,按任何其他键重新生成点集。\n\n";
}
int main( int /*argc*/, char** /*argv*/ )
{
help();
Mat img(500, 500, CV_8UC3, Scalar::all(0));
RNG& rng = theRNG();
for(;;)
{
int i, count = rng.uniform(1, 101);
vector<Point> 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);
}
// 查找最小面积外接矩形
Point2f vtx[4];
RotatedRect box = minAreaRect(points);
box.points(vtx);
// 查找最小面积外接三角形
vector<Point2f> triangle;
minEnclosingTriangle(points, triangle);
// 查找最小面积外接圆
Point2f center;
float radius = 0;
minEnclosingCircle(points, center, radius);
img = Scalar::all(0);
// 绘制点
for( i = 0; i < count; i++ )
circle( img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA );
// 绘制外接矩形
for( i = 0; i < 4; i++ )
line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, LINE_AA);
// 绘制三角形
for( i = 0; i < 3; i++ )
line(img, triangle[i], triangle[(i+1)%3], Scalar(255, 255, 0), 1, LINE_AA);
// 绘制圆形
circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA);
imshow( "矩形、三角形和圆形", img );
char key = (char)waitKey();
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
return 0;
}
n 维密集数组类
定义 mat.hpp:830
_Tp y
点的 y 坐标
定义 types.hpp:202
_Tp x
点的 x 坐标
定义 types.hpp:201
随机数生成器。
Definition core.hpp:2879
int uniform(int a, int b)
返回 [a,b) 范围内均匀分布的整数随机数
此类表示平面上旋转的(即,非垂直)矩形。
定义 types.hpp:538
void points(Point2f pts[]) const
CV_8UC3
#define CV_8UC3
int cvRound(double value)
将浮点数舍入为最接近的整数。
Definition fast_math.hpp:200
@ 三角形
定义 gr_skig.hpp:63
@ circle
定义 gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
在指定窗口中显示图像。
int waitKey(int delay=0)
等待按键按下。
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制连接两点的线段。
double minEnclosingTriangle(InputArray points, OutputArray triangle)
查找包含 2D 点集的最小面积三角形,并返回其面积。
RotatedRect minAreaRect(InputArray points)
查找包含输入 2D 点集的最小面积的旋转矩形。
void minEnclosingCircle(InputArray points, Point2f &center, float &radius)
查找包含二维点集的最小面积圆。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
STL 命名空间。