这篇文章主要为大家详细介绍了OpenCV绘制正多边形的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
OpenCV 绘制正多边形的具体代码,供大家参考,具体内容如下
#include #include #include #include #include #include using namespace cv; using namespace std; void DeleteRepetition(vector &Data) { vector::iterator it, it1; for (it = ++Data.begin(); it != Data.end();) { it1 = find(Data.begin(), it, *it); if (it1 != it) it = Data.erase(it); else it++; } } void Patterns(Mat *src, vector Dots, int fill) { DeleteRepetition(Dots); if (fill == -1) { Point *ImgDot = new Point(Dots.size()); for (int i = 0; i <Dots.size(); i++) { ImgDot[i] = Dots[i]; } const Point* ppt = ImgDot; int npt = Dots.size(); RNG &rng = theRNG(); Scalar color = Scalar(rng.uniform(100, 255), rng.uniform(100, 255), rng.uniform(100, 255)); cv::fillPoly(*src, &ppt, &npt, 1, color); } else { Dots.push_back(Dots[0]); RNG &rng = theRNG(); Scalar color = Scalar(rng.uniform(100, 255), rng.uniform(100, 255), rng.uniform(100, 255)); for (int i = 0; i <Dots.size() - 1; i++) { line(*src, Dots[i], Dots[i + 1], color, fill); } } } // https://www.w3cplus.com/canvas/drawing-regular-polygons.html // http://www.cnblogs.com/xcywt/p/9456526.html // 图像、中心点、半径、边数、旋转角度、线宽 void EquilateralPolygon(Mat *src, Point origin, int radius, int brim, int rotate, int fill) { if (brim 360) return; #define PI 3.14159265 #define ROTATE_COUNT 180 double nAgree = 360 / brim; // 计算旋转角度 double a = radius * cos(PI / brim); // 计算垂直向下的长度 double s = 2 * radius * sin(PI / brim); // 计算边长 vector Dots; Point D1, D2; D1.x = origin.x + radius*cos(-(((180 - nAgree) / 2) + rotat来源gao@!dai!ma.com搞$$代^@码!网e) * PI / 180); D1.y = origin.y - radius*sin(-(((180 - nAgree) / 2) + rotate) * PI / 180); D2.x = origin.x + radius*cos(-(((180 - nAgree) / 2) + nAgree + rotate) * PI / 180); D2.y = origin.y - radius*sin(-(((180 - nAgree) / 2) + nAgree + rotate) * PI / 180); // 第一条边的两个点 Dots.push_back(D1); Dots.push_back(D2); for (int i = 0; i
效果如下:
以上就是OpenCV绘制正多边形的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!