1. 程式人生 > >opencv 基本圖形繪制

opencv 基本圖形繪制

-- 橢圓 turn con cor 圓心 oid clas inf

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

#include <opencv2/imgproc/imgproc.hpp>


#define WINDOW_NAME1 "【繪制圖1】"        //為窗口標題定義的宏 
#define WINDOW_NAME2 "【繪制圖2】"        //為窗口標題定義的宏 
#define WINDOW_WIDTH 600//定義窗口大小的宏

// 橢圓
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 ); }
// void DrawFilledCircle( Mat img, Point center ) { int thickness = -1; int lineType = 8; circle( img, center, WINDOW_WIDTH/32, Scalar( 0, 0, 255 ), thickness, lineType ); } // 多邊形 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] }; int npt[] = { 20 }; fillPoly( img, ppt, npt, 1, Scalar( 255, 255, 255 ), lineType ); } // void DrawLine( Mat img, Point start, Point end ) { int thickness = 2; int lineType = 8; line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType ); } int main( void ) { // 創建空白的Mat圖像 Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 ); Mat rookImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 ); //【1.1】先繪制出橢圓 DrawEllipse( atomImage, 90 ); DrawEllipse( atomImage, 0 ); DrawEllipse( atomImage, 45 ); DrawEllipse( atomImage, -45 ); //【1.2】再繪制圓心 DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) ); imshow( WINDOW_NAME1, atomImage ); // moveWindow( WINDOW_NAME1, 0, 200 ); // ----------------------------<2>繪制組合圖----------------------------- //【2.1】多邊形 DrawPolygon( rookImage ); // 【2.2】繪制底部矩形 rectangle( rookImage, Point( 0, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH, WINDOW_WIDTH), Scalar( 0, 255, 255 ), -1, 8 ); // 【2.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 ) ); // ---------------------------<3>顯示繪制出的圖像------------------------ imshow( WINDOW_NAME2, rookImage ); //moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 ); waitKey(0); return(0); }

技術分享圖片

opencv 基本圖形繪制