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 名稱空間。