溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用OpenCV怎么繪制一個(gè)正多邊形

發(fā)布時(shí)間:2021-03-10 16:47:52 來源:億速云 閱讀:170 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用OpenCV怎么繪制一個(gè)正多邊形,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

具體內(nèi)容如下

#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
// 圖像、中心點(diǎn)、半徑、邊數(shù)、旋轉(zhuǎn)角度、線寬
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; // 計(jì)算旋轉(zhuǎn)角度
 double a = radius * cos(PI / brim);  // 計(jì)算垂直向下的長度
 double s = 2 * radius * sin(PI / brim); // 計(jì)算邊長
 
 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);
 
 // 第一條邊的兩個(gè)點(diǎn)
 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); //中心點(diǎn)
 
 EquilateralPolygon(&Img, O, 100, 3, 0, -1); // 填充的正三角形
 EquilateralPolygon(&Img, O, 200, 3, 0, 1); // 不填充的正三角形
 EquilateralPolygon(&Img, O, 200, 3, 30, 1); // 不填充的正三角形,順時(shí)針旋轉(zhuǎn)30度
 EquilateralPolygon(&Img, O, 200, 3, 60, 1); // 不填充的正三角形,順時(shí)針旋轉(zhuǎn)60度
 EquilateralPolygon(&Img, O, 200, 3, 90, 1); // 不填充的正三角形,順時(shí)針旋轉(zhuǎn)90度
 EquilateralPolygon(&Img, O, 200, 3, 120, 1);// 不填充的正三角形,順時(shí)針旋轉(zhuǎn)120度
 EquilateralPolygon(&Img, O, 200, 3, 150, 1);// 不填充的正三角形,順時(shí)針旋轉(zhuǎn)150度
 EquilateralPolygon(&Img, O, 200, 3, 180, 1);// 不填充的正三角形,順時(shí)針旋轉(zhuǎn)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怎么繪制一個(gè)正多邊形了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI