OpenCV示例学习(二): 基本图形绘制算子:line(),circle(),fillPoly(), ellipse()
生活随笔
收集整理的這篇文章主要介紹了
OpenCV示例学习(二): 基本图形绘制算子:line(),circle(),fillPoly(), ellipse()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
OpenCV示例學習(二): 基本圖形繪制算子:line(),circle(),fillPoly(), ellipse()
#include <opencv2/opencv.hpp>using namespace cv;#define WINDOW_NAME1 "【繪制圖1】" //為窗口標題定義的宏 #define WINDOW_NAME2 "【繪制圖2】" //為窗口標題定義的宏 #define WINDOW_WIDTH 600 //定義窗口大小的宏void DrawEllipse(Mat img, double angle);//繪制橢圓 void DrawFilledCircle(Mat img, Point center);//繪制圓 void DrawPolygon(Mat img);//繪制多邊形 void DrawLine(Mat img, Point start, Point end);//繪制線段int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);// 創建空白的Mat圖像Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);//一、繪制化學中的原子示例圖// 1、先繪制出橢圓DrawEllipse(atomImage, 90);DrawEllipse(atomImage, 0);DrawEllipse(atomImage, 45);DrawEllipse(atomImage, -45);// 2、再繪制圓心DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));//二、繪制組合圖//1、先繪制出橢圓DrawPolygon(rookImage);//2、繪制矩形rectangle(rookImage,Point(0, 7 * WINDOW_WIDTH / 8),Point(WINDOW_WIDTH, WINDOW_WIDTH),Scalar(0, 255, 255),-1,8);//3、繪制一些線段DrawLine(rookImage, Point(0, 15 * WINDOW_WIDTH / 16), Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));DrawLine(rookImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));DrawLine(rookImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));DrawLine(rookImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));//三、顯示繪制出的圖像imshow(WINDOW_NAME1, atomImage);moveWindow(WINDOW_NAME1, 0, 200);imshow(WINDOW_NAME2, rookImage);moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);waitKey(0); }2)DrawEllipse()實現了繪制不同角度、相同尺寸的橢圓:
void DrawEllipse(Mat img, double angle) {int thickness = 2;int lineType = 8;ellipse(img,Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), // 橢圓中心Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), // 外切矩形angle, // 橢圓旋轉角度0,360,Scalar(255, 129, 0), // 藍色thickness, // 線寬lineType); // 線性 (連通性) }3)DrawFilledCircle()實現了實心圓的繪制:
void DrawFilledCircle(Mat img, Point center) {int thickness = -1;int lineType = 8;circle(img,center,WINDOW_WIDTH / 32,Scalar(0, 0, 255),thickness,lineType); }4)DrawPolygon()實現了凹多邊形的繪制:
void DrawPolygon(Mat img) {int lineType = 8;//創建一些點Point rookPoints[1][20];rookPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);rookPoints[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);rookPoints[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);rookPoints[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);rookPoints[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);rookPoints[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);rookPoints[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);rookPoints[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);rookPoints[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);rookPoints[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);rookPoints[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);rookPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);const Point *ppt[1] = { rookPoints[0] }; // 一個元素的 point 指針數組:每個元素表示指向一個point結構類型的存儲地址//const Point** ppt = &(rookPoints[0]);//int npt[] = { 20 };int ival = 20;int *npt = &ival;fillPoly(img,&ppt[0], // 多邊形頂點集合npt, // 多邊形頂點數目1,Scalar(255, 255, 255),lineType); }5)DrawLine()實現了線的繪制
void DrawLine(Mat img, Point start, Point end) {int thickness = 2;int lineType = 8;line(img,start, // 起始點end, // 終止點Scalar(0, 0, 0),thickness,lineType); }結果:
總結
以上是生活随笔為你收集整理的OpenCV示例学习(二): 基本图形绘制算子:line(),circle(),fillPoly(), ellipse()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows10和ubuntu双系统w
- 下一篇: mysql 优化not null_mys