#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
static void ShowHelpText() {
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第13个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION);
printf("\n\n ----------------------------------------------------------------------------\n");
printf("\n\n\t\t\t欢迎来到【寻找最小面积的包围圆】示例程序~\n");
printf("\n\n\t按键操作说明: \n\n"
"\t\t键盘按键【ESC】、【Q】、【q】- 退出程序\n\n"
"\t\t键盘按键任意键 - 重新生成随机点,并寻找最小面积的包围圆\n");
}
int main() {
ShowHelpText();
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("Result", image);
char key = (char)waitKey();
if (key == 27 || key == 'q' || key == 'Q')
break;
}
return 0;
}