OpenCV繪制正多邊形的方法
OpenCV 繪制正多邊形的具體代碼,供大家參考,具體內(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ì)算垂直向下的長(zhǎng)度
double s = 2 * radius * sin(PI / brim); // 計(jì)算邊長(zhǎng)
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;
}
效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:opencv利用霍夫變換檢測(cè)直線進(jìn)行圖片校正
欄 目:C語(yǔ)言
本文標(biāo)題:OpenCV繪制正多邊形的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/544.html
您可能感興趣的文章
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 01-10c語(yǔ)言內(nèi)存泄露示例解析
- 01-10C語(yǔ)言 擴(kuò)展歐幾里得算法代碼
- 01-10shared_ptr線程安全性全面分析
- 01-10pcre函數(shù)詳細(xì)解析
- 01-10php正則表達(dá)式的基本語(yǔ)法總結(jié)
- 01-10分享C++面試中string類的一種正確寫法
- 01-10floyd算法實(shí)現(xiàn)思路及實(shí)例代碼
- 01-10c語(yǔ)言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具代碼分享
- 01-10封裝常用正則表達(dá)式的用法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


