温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用OpenCV怎么绘制一个正多边形

发布时间:2021-03-10 16:47:52 来源:亿速云 阅读:158 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关使用OpenCV怎么绘制一个正多边形,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

具体内容如下

#include <iostream> 
#include <opencv2\core\core.hpp>
#include <opencv2\opencv.hpp> 
#include <opencv2\highgui\highgui.hpp> 
#include <opencv2\contrib\contrib.hpp> 
 
#include <fstream> 
 
using namespace cv;
using namespace std;
 
void DeleteRepetition(vector<Point> &Data)
{
 vector<Point>::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<Point> 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 < 3) return;
 if (rotate > 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<Point> Dots;
 Point D1, D2;
 D1.x = origin.x + radius*cos(-(((180 - nAgree) / 2) + rotate) * 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 < brim - 2; i++)
 {
 double dSinRot = sin((nAgree * (i + 1)) * PI / 180);
 double dCosRot = cos((nAgree * (i + 1)) * PI / 180);
 int x = origin.x + dCosRot * (D2.x - origin.x) - dSinRot * (D2.y - origin.y);
 int y = origin.y + dSinRot * (D2.x - origin.x) + dCosRot * (D2.y - origin.y);
 Dots.push_back(Point(x, y));
 }
 Patterns(src, Dots, fill);
 Dots.clear();
}
 
int main()
{
 
 Mat Img = Mat::zeros(800, 800, CV_8UC3);
 Point O = Point(400, 400);
 
 circle(Img, O, 2, Scalar(0, 0, 255), -1); //中心点
 
 EquilateralPolygon(&Img, O, 100, 3, 0, -1); // 填充的正三角形
 EquilateralPolygon(&Img, O, 200, 3, 0, 1); // 不填充的正三角形
 EquilateralPolygon(&Img, O, 200, 3, 30, 1); // 不填充的正三角形,顺时针旋转30度
 EquilateralPolygon(&Img, O, 200, 3, 60, 1); // 不填充的正三角形,顺时针旋转60度
 EquilateralPolygon(&Img, O, 200, 3, 90, 1); // 不填充的正三角形,顺时针旋转90度
 EquilateralPolygon(&Img, O, 200, 3, 120, 1);// 不填充的正三角形,顺时针旋转120度
 EquilateralPolygon(&Img, O, 200, 3, 150, 1);// 不填充的正三角形,顺时针旋转150度
 EquilateralPolygon(&Img, O, 200, 3, 180, 1);// 不填充的正三角形,顺时针旋转180度
 
 EquilateralPolygon(&Img, O, 230, 4, 0, 2); // 不填充的正四边形
 EquilateralPolygon(&Img, O, 250, 5, 0, 3); // 不填充的正五边形
 EquilateralPolygon(&Img, O, 270, 6, 0, 4); // 不填充的正六边形
 EquilateralPolygon(&Img, O, 290, 7, 0, 5); // 不填充的正七边形
 EquilateralPolygon(&Img, O, 310, 8, 0, 6); // 不填充的正八边形
 EquilateralPolygon(&Img, O, 330, 9, 0, 7); // 不填充的正九边形
 EquilateralPolygon(&Img, O, 350, 10, 0, 8);// 不填充的正十边形
 
 imshow("正多边形", Img);
 waitKey(0);
 return 0;
}

上述就是小编为大家分享的使用OpenCV怎么绘制一个正多边形了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI