1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#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; }
|