#include "stdafx.h"
#include"opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv[])
{
Mat img(500, 500, CV_8UC3);
RNG rng = theRNG();//随机数类
for (;;)
{
int count = rng.uniform(1, 101);//uniform()返回指定范围的随机数
vector<Point>points;
for (int 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);
}
RotatedRect box = minAreaRect(Mat(points));//点集的最小外接旋转矩形
Point2f tr[4], center;
float radius=0;
box.points(tr);
minEnclosingCircle(Mat(points), center, radius);//点集的最小外接圆
img = Scalar::all(0);
for (int i = 0; i < count; i++)
{
circle(img, points[i], 3, Scalar(0, 0, 255), CV_FILLED, CV_AA);
}
for (int i = 0; i < 4; i++)
{
line(img, tr[i], tr[(i + 1) % 4], Scalar(0, 255, 0), 1, CV_AA);
}
circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, CV_AA);
imshow("j", img);
char key = (char)waitKey();
if (key == 27 || key == 'q' || key == 'Q')
break;
}
return 0;
}
OpenCV学习笔记(3):点集的最小外接矩形/圆,RNG类
最新推荐文章于 2024-08-18 09:39:23 发布