1 #include<iostream> 2 #include<vector> 3 #include<opencv2/opencv.hpp> 4 5 using namespace cv; 6 using namespace std; 7 8 int main() 9 { 10 Mat srcImage(Size(600, 600), CV_8UC3, Scalar(0)); 11 12 RNG &rng = theRNG(); 13 14 char key; 15 while (1) 16 { 17 //随机生成一些点 18 //首先就是随机生成点的总数量 19 int g_nPointCount = rng.uniform(3, 30); 20 //接下来就是随机生成一些点的坐标 21 vector<Point> points; 22 for (int i = 0; i < g_nPointCount; i++) 23 { 24 Point midPoint; 25 26 midPoint.x = rng.uniform(srcImage.cols / 4, srcImage.cols * 3 / 4); 27 midPoint.y = rng.uniform(srcImage.rows / 4, srcImage.rows * 3 / 4); 28 29 points.push_back(midPoint); 30 } 31 32 //显示刚刚随机生成的那些点 33 for (int i = 0; i < g_nPointCount; i++) 34 { 35 circle(srcImage, points[i], 0, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3); 36 } 37 38 //在生成的那些随机点中寻找最小包围矩形 39 //rect变量中得到了矩形 40 Rect rect = boundingRect(points); 41 42 //根据得到的矩形 绘制矩形 43 rectangle(srcImage, rect, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3); 44 45 imshow("【绘制结束后的图像】", srcImage); 46 47 key = waitKey(); 48 if (key == 27) 49 break; 50 else 51 srcImage = Scalar::all(0); 52 }