C++ OpenCV绘制几何图形
程序员文章站
2024-01-02 16:30:34
本文实例为大家分享了c++ opencv绘制几何图形的具体代码,供大家参考,具体内容如下绘制几何图形 直线 矩形 多边形 圆形 椭圆 文字 api直线cv_e...
本文实例为大家分享了c++ opencv绘制几何图形的具体代码,供大家参考,具体内容如下
绘制几何图形
- 直线
- 矩形
- 多边形
- 圆形
- 椭圆
- 文字
api
直线
cv_exports_w void line(inputoutputarray img, point pt1, point pt2, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0);
矩形
cv_exports_w void rectangle(inputoutputarray img, point pt1, point pt2, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0);
上方函数通过两点确定矩形的位置和大小,下方函数则是通过矩形对象 rect 来确定。
cv_exports_w void rectangle(inputoutputarray img, rect rec, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0);
多边形
cv_exports void polylines(inputoutputarray img, const point* const* pts, const int* npts, int ncontours, bool isclosed, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0 );
cv_exports_w void polylines(inputoutputarray img, inputarrayofarrays pts, bool isclosed, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0 );
圆形
cv_exports_w void circle(inputoutputarray img, point center, int radius, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0);
椭圆
cv_exports_w void ellipse(inputoutputarray img, const rotatedrect& box, const scalar& color, int thickness = 1, int linetype = line_8);
cv_exports_w void ellipse(inputoutputarray img, point center, size axes, double angle, double startangle, double endangle, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0);
文字
cv_exports_w void puttext( inputoutputarray img, const string& text, point org, int fontface, double fontscale, scalar color, int thickness = 1, int linetype = line_8, bool bottomleftorigin = false );
示例
官方示例一
#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #define w 400 using namespace cv; void myellipse( mat img, double angle ); void myfilledcircle( mat img, point center ); void mypolygon( mat img ); void myline( mat img, point start, point end ); int main( void ){ char atom_window[] = "drawing 1: atom"; char rook_window[] = "drawing 2: rook"; mat atom_image = mat::zeros( w, w, cv_8uc3 ); mat rook_image = mat::zeros( w, w, cv_8uc3 ); myellipse( atom_image, 90 ); myellipse( atom_image, 0 ); myellipse( atom_image, 45 ); myellipse( atom_image, -45 ); myfilledcircle( atom_image, point( w/2, w/2) ); mypolygon( rook_image ); rectangle( rook_image, point( 0, 7*w/8 ), point( w, w), scalar( 0, 255, 255 ), filled, line_8 ); myline( rook_image, point( 0, 15*w/16 ), point( w, 15*w/16 ) ); myline( rook_image, point( w/4, 7*w/8 ), point( w/4, w ) ); myline( rook_image, point( w/2, 7*w/8 ), point( w/2, w ) ); myline( rook_image, point( 3*w/4, 7*w/8 ), point( 3*w/4, w ) ); imshow( atom_window, atom_image ); movewindow( atom_window, 0, 200 ); imshow( rook_window, rook_image ); movewindow( rook_window, w, 200 ); waitkey( 0 ); return(0); } void myellipse( mat img, double angle ) { int thickness = 2; int linetype = 8; ellipse( img, point( w/2, w/2 ), size( w/4, w/16 ), angle, 0, 360, scalar( 255, 0, 0 ), thickness, linetype ); } void myfilledcircle( mat img, point center ) { circle( img, center, w/32, scalar( 0, 0, 255 ), filled, line_8 ); } void mypolygon( mat img ) { int linetype = line_8; point rook_points[1][20]; rook_points[0][0] = point( w/4, 7*w/8 ); rook_points[0][1] = point( 3*w/4, 7*w/8 ); rook_points[0][2] = point( 3*w/4, 13*w/16 ); rook_points[0][3] = point( 11*w/16, 13*w/16 ); rook_points[0][4] = point( 19*w/32, 3*w/8 ); rook_points[0][5] = point( 3*w/4, 3*w/8 ); rook_points[0][6] = point( 3*w/4, w/8 ); rook_points[0][7] = point( 26*w/40, w/8 ); rook_points[0][8] = point( 26*w/40, w/4 ); rook_points[0][9] = point( 22*w/40, w/4 ); rook_points[0][10] = point( 22*w/40, w/8 ); rook_points[0][11] = point( 18*w/40, w/8 ); rook_points[0][12] = point( 18*w/40, w/4 ); rook_points[0][13] = point( 14*w/40, w/4 ); rook_points[0][14] = point( 14*w/40, w/8 ); rook_points[0][15] = point( w/4, w/8 ); rook_points[0][16] = point( w/4, 3*w/8 ); rook_points[0][17] = point( 13*w/32, 3*w/8 ); rook_points[0][18] = point( 5*w/16, 13*w/16 ); rook_points[0][19] = point( w/4, 13*w/16 ); const point* ppt[1] = { rook_points[0] }; int npt[] = { 20 }; fillpoly( img, ppt, npt, 1, scalar( 255, 255, 255 ), linetype ); } void myline( mat img, point start, point end ) { int thickness = 2; int linetype = line_8; line( img, start, end, scalar( 0, 0, 0 ), thickness, linetype ); }
官方示例二
#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> #include <stdio.h> using namespace cv; const int number = 100; const int delay = 5; const int window_width = 900; const int window_height = 600; int x_1 = -window_width/2; int x_2 = window_width*3/2; int y_1 = -window_width/2; int y_2 = window_width*3/2; static scalar randomcolor( rng& rng ); int drawing_random_lines( mat image, char* window_name, rng rng ); int drawing_random_rectangles( mat image, char* window_name, rng rng ); int drawing_random_ellipses( mat image, char* window_name, rng rng ); int drawing_random_polylines( mat image, char* window_name, rng rng ); int drawing_random_filled_polygons( mat image, char* window_name, rng rng ); int drawing_random_circles( mat image, char* window_name, rng rng ); int displaying_random_text( mat image, char* window_name, rng rng ); int displaying_big_end( mat image, char* window_name, rng rng ); int main( void ) { int c; char window_name[] = "drawing_2 tutorial"; rng rng( 0xffffffff ); mat image = mat::zeros( window_height, window_width, cv_8uc3 ); imshow( window_name, image ); waitkey( delay ); c = drawing_random_lines(image, window_name, rng); if( c != 0 ) return 0; c = drawing_random_rectangles(image, window_name, rng); if( c != 0 ) return 0; c = drawing_random_ellipses( image, window_name, rng ); if( c != 0 ) return 0; c = drawing_random_polylines( image, window_name, rng ); if( c != 0 ) return 0; c = drawing_random_filled_polygons( image, window_name, rng ); if( c != 0 ) return 0; c = drawing_random_circles( image, window_name, rng ); if( c != 0 ) return 0; c = displaying_random_text( image, window_name, rng ); if( c != 0 ) return 0; c = displaying_big_end( image, window_name, rng ); if( c != 0 ) return 0; waitkey(0); return 0; } static scalar randomcolor( rng& rng ) { int icolor = (unsigned) rng; return scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ); } int drawing_random_lines( mat image, char* window_name, rng rng ) { point pt1, pt2; for( int i = 0; i < number; i++ ) { pt1.x = rng.uniform( x_1, x_2 ); pt1.y = rng.uniform( y_1, y_2 ); pt2.x = rng.uniform( x_1, x_2 ); pt2.y = rng.uniform( y_1, y_2 ); line( image, pt1, pt2, randomcolor(rng), rng.uniform(1, 10), 8 ); imshow( window_name, image ); if( waitkey( delay ) >= 0 ) { return -1; } } return 0; } int drawing_random_rectangles( mat image, char* window_name, rng rng ) { point pt1, pt2; int linetype = 8; int thickness = rng.uniform( -3, 10 ); for( int i = 0; i < number; i++ ) { pt1.x = rng.uniform( x_1, x_2 ); pt1.y = rng.uniform( y_1, y_2 ); pt2.x = rng.uniform( x_1, x_2 ); pt2.y = rng.uniform( y_1, y_2 ); rectangle( image, pt1, pt2, randomcolor(rng), max( thickness, -1 ), linetype ); imshow( window_name, image ); if( waitkey( delay ) >= 0 ) { return -1; } } return 0; } int drawing_random_ellipses( mat image, char* window_name, rng rng ) { int linetype = 8; for ( int i = 0; i < number; i++ ) { point center; center.x = rng.uniform(x_1, x_2); center.y = rng.uniform(y_1, y_2); size axes; axes.width = rng.uniform(0, 200); axes.height = rng.uniform(0, 200); double angle = rng.uniform(0, 180); ellipse( image, center, axes, angle, angle - 100, angle + 200, randomcolor(rng), rng.uniform(-1,9), linetype ); imshow( window_name, image ); if( waitkey(delay) >= 0 ) { return -1; } } return 0; } int drawing_random_polylines( mat image, char* window_name, rng rng ) { int linetype = 8; for( int i = 0; i< number; i++ ) { point pt[2][3]; pt[0][0].x = rng.uniform(x_1, x_2); pt[0][0].y = rng.uniform(y_1, y_2); pt[0][1].x = rng.uniform(x_1, x_2); pt[0][1].y = rng.uniform(y_1, y_2); pt[0][2].x = rng.uniform(x_1, x_2); pt[0][2].y = rng.uniform(y_1, y_2); pt[1][0].x = rng.uniform(x_1, x_2); pt[1][0].y = rng.uniform(y_1, y_2); pt[1][1].x = rng.uniform(x_1, x_2); pt[1][1].y = rng.uniform(y_1, y_2); pt[1][2].x = rng.uniform(x_1, x_2); pt[1][2].y = rng.uniform(y_1, y_2); const point* ppt[2] = {pt[0], pt[1]}; int npt[] = {3, 3}; polylines(image, ppt, npt, 2, true, randomcolor(rng), rng.uniform(1,10), linetype); imshow( window_name, image ); if( waitkey(delay) >= 0 ) { return -1; } } return 0; } int drawing_random_filled_polygons( mat image, char* window_name, rng rng ) { int linetype = 8; for ( int i = 0; i < number; i++ ) { point pt[2][3]; pt[0][0].x = rng.uniform(x_1, x_2); pt[0][0].y = rng.uniform(y_1, y_2); pt[0][1].x = rng.uniform(x_1, x_2); pt[0][1].y = rng.uniform(y_1, y_2); pt[0][2].x = rng.uniform(x_1, x_2); pt[0][2].y = rng.uniform(y_1, y_2); pt[1][0].x = rng.uniform(x_1, x_2); pt[1][0].y = rng.uniform(y_1, y_2); pt[1][1].x = rng.uniform(x_1, x_2); pt[1][1].y = rng.uniform(y_1, y_2); pt[1][2].x = rng.uniform(x_1, x_2); pt[1][2].y = rng.uniform(y_1, y_2); const point* ppt[2] = {pt[0], pt[1]}; int npt[] = {3, 3}; fillpoly( image, ppt, npt, 2, randomcolor(rng), linetype ); imshow( window_name, image ); if( waitkey(delay) >= 0 ) { return -1; } } return 0; } int drawing_random_circles( mat image, char* window_name, rng rng ) { int linetype = 8; for (int i = 0; i < number; i++) { point center; center.x = rng.uniform(x_1, x_2); center.y = rng.uniform(y_1, y_2); circle( image, center, rng.uniform(0, 300), randomcolor(rng), rng.uniform(-1, 9), linetype ); imshow( window_name, image ); if( waitkey(delay) >= 0 ) { return -1; } } return 0; } int displaying_random_text( mat image, char* window_name, rng rng ) { int linetype = 8; for ( int i = 1; i < number; i++ ) { point org; org.x = rng.uniform(x_1, x_2); org.y = rng.uniform(y_1, y_2); puttext( image, "testing text rendering", org, rng.uniform(0,8), rng.uniform(0,100)*0.05+0.1, randomcolor(rng), rng.uniform(1, 10), linetype); imshow( window_name, image ); if( waitkey(delay) >= 0 ) { return -1; } } return 0; } int displaying_big_end( mat image, char* window_name, rng ) { size textsize = gettextsize("opencv forever!", font_hershey_complex, 3, 5, 0); point org((window_width - textsize.width)/2, (window_height - textsize.height)/2); int linetype = 8; mat image2; for( int i = 0; i < 255; i += 2 ) { image2 = image - scalar::all(i); puttext( image2, "opencv forever!", org, font_hershey_complex, 3, scalar(i, i, 255), 5, linetype ); imshow( window_name, image2 ); if( waitkey(delay) >= 0 ) { return -1; } } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
C++ OpenCV绘制几何图形
-
cdr怎么绘制几何图形? cdr常用几何图形的画法
-
相机标定示例(OpenCV /C++ /matlab工具箱TOOLBOX_calib)
-
OpenCV_Python API 官方文档学习_ cv2 绘制基本几何图形
-
Opencv——查找并绘制凸包、凸包与轮廓的关系
-
Opencv一维直方图的绘制
-
【基于C++和Python的Opencv3学习笔记之基本图形的绘制】
-
C++和Java命令行绘制心形图案
-
OpenCV-Python 绘制矩形,绘制文本,获取文本大小【rectangle(),getTextSize(),putText()】
-
AE中怎么绘制各种形状的几何图形?