#include <opencv2//highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
//*********************************
// 创建包围轮廓的圆形边界
//*********************************
int main(int argc, char *argv[], char **env)
{
Mat image(600, 600, CV_8UC3);
RNG&rng = theRNG();
while (1)
{
//随机数量点数
int count = rng.uniform(3, 103);
vector<Point>points;
//生成随机坐标点
for (int i = 0; i < count; ++i)
{
Point point;
point.x = rng.uniform(image.cols / 4, image.cols * 3 / 4);
point.y = rng.uniform(image.rows / 4, image.rows * 3 / 4);
points.push_back(point);
}
//寻找最小面积的外包圆
Point2f center;
float radius = 0;
minEnclosingCircle(Mat(points), center, radius);
//绘制随机颜色的点
image = Scalar::all(0);
for (int i = 0; i < count; ++i)
{
circle(image,
points[i],
3,
Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)),
FILLED,
LINE_AA
);
}
//绘制最小外包圆的
circle(image,
center,
cvRound(radius),
Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)),
2,
LINE_AA
);
imshow("圆形包围示例", image);
if (char(waitKey()) == 27)
break;
}
return 0;
}