opencv3寻找最小包围圆形-minEnclosingCircle函数

http://blog.csdn.net/qq_23880193/article/details/49257637

版权声明:本文为博主原创文章,未经博主允许不得转载。

  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.         Point2f center;  
  40.         float radius;  
  41.         minEnclosingCircle(points, center, radius);  
  42.   
  43.         //根据得到的圆形和半径  绘制圆形  
  44.         circle(srcImage, static_cast<Point>(center), (int)radius  
  45.             , Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3);  
  46.   
  47.         imshow("【绘制结束后的图像】", srcImage);  
  48.   
  49.         key = waitKey();  
  50.         if (key == 27)  
  51.             break;  
  52.         else  
  53.             srcImage = Scalar::all(0);  
  54.     }  
  55.   
  56.     return 0;  
  57. }  

posted on 2017-09-14 16:05 zmj 阅读(4101) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理