OpenCV学习笔记(17)OpenCV之基本绘图

2024-09-02 19:32

本文主要是介绍OpenCV学习笔记(17)OpenCV之基本绘图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载自http://blog.csdn.net/ubunfans/article/details/24421981

相关函数介绍

Point

该数据结构表示了由其图像坐标 和 指定的2D点。可定义为:

Point pt;

pt.x = 10;

pt.y = 8;

或者

Point pt = Point(10, 8);

Scalar

表示了具有4个元素的数组。次类型在OpenCV中被大量用于传递像素值。

本节中,我们将进一步用它来表示RGB颜色值(三个参数)。如果用不到第四个参数,则无需定义。

我们来看个例子,如果给出以下颜色参数表达式:

Scalar( a, b, c )

那么定义的RGB颜色值为:Red = c, Green = b and Blue= a

Rectangle

C++: void rectangle(Mat& img,Point pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)

C++: void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )

Parameters:

  • img – 画矩形的对象
  • pt1 – 矩形的一个顶点,左上角的.
  • pt2 – 另一个顶点,右下角的.
  • rec – 确定矩形的另一种方式,给左上角坐标和长宽
  • color – 指定矩形的颜色或亮度(灰度图像),scalar(255,0,255)既可指定.
  • thickness – 矩形边框的粗细. 负值(like CV_FILLED)表示要画一个填充的矩形
  • lineType – 边框线型. (   

8 (or 0) - 8-connected line(8邻接)连接 线。

4 - 4-connected line(4邻接)连接线。

CV_AA - antialiased 线条。)

  • shift –坐标点的小数点位数

Line

C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)

Parameters:

  • img – 图像.
  • pt1 – 线条起点.
  • pt2 – 线条终点.
  • color – 线条颜色.
  • thickness – 线条宽度.
  • lineType – 线型

Type of the line:

    • 8 (or omitted) - 8-connected line.
    • 4 - 4-connected line.
    • CV_AA - antialiased line.
  • shift – 坐标点小数点位数.

Circle

C++: void circle(Mat&img, Point center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)

Parameters:

  • img – 要画圆的那个矩形.
  • center – 圆心坐标.
  • radius – 半径.
  • color – 圆边框颜色,scalar类型的
  • thickness – 正值表示圆边框宽度. 负值表示画一个填充圆形
  • lineType – 圆边框线型
  • shift – 圆心坐标和半径的小数点位数

Ellipse

C++: void ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle, const Scalar& color,int thickness=1, int lineType=8, int shift=0)

C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)

Parameters:

  • img – 椭圆所在图像.
  • center – 椭圆中心.
  • axes – 椭圆主轴一半的长度
  • angle – 椭圆旋转角度
  • startAngle – 椭圆弧起始角度
  • endAngle –椭圆弧终止角度
  • box – 指定椭圆中心和旋转角度的信息,通过 RotatedRect 或 CvBox2D. 这表示椭圆画在旋转矩形上(矩形是不可见的,只是指定了一个框而已)
  • color – 椭圆边框颜色.
  • thickness – 正值代表椭圆边框宽度,负值代表填充的椭圆
  • lineType – 线型
  • shift – 椭圆中心坐标和坐标轴的小数点位数

PolyLine

C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Parameters:
  • img – 折线所在图像.
  • pts – 折线中拐点坐标指针.
  • npts – 折线拐点个数指针.
  • ncontours – 折线线段数量.
  • isClosed – 折线是否闭合.
  • color – 折线颜色.
  • thickness – 折线宽度.
  • lineType – 线型.
  • shift – 顶点坐标小数点位数.

PutText

C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )

Parameters:
  • img – 显示文字所在图像.
  • text – 待显示的文字.
  • org – 文字在图像中的左下角 坐标.
  • font – 字体结构体.
  • fontFace – 字体类型, 可选择字体:FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX,以上所有类型都可以配合 FONT_HERSHEY_ITALIC使用,产生斜体效果。
  • fontScale – 字体大小,该值和字体内置大小相乘得到字体大小
  • color – 文本颜色
  • thickness –  写字的线的粗细,类似于0.38的笔尖和0.5的笔尖
  • lineType – 线性.
  • bottomLeftOrigin – true, 图像数据原点在左下角. Otherwise, 图像数据原点在左上角.


示例代码

[cpp]  view plain copy
print ?
  1. /** 
  2.  * @file Drawing_1.cpp 
  3.  * @brief Simple sample code 
  4.  */  
  5.   
  6. #include <opencv2/core/core.hpp>  
  7. #include <opencv2/highgui/highgui.hpp>  
  8.   
  9. #define w 400  
  10.   
  11. using namespace cv;  
  12.   
  13. /// Function headers  
  14. void MyEllipse( Mat img, double angle );  
  15. void MyFilledCircle( Mat img, Point center );  
  16. void MyPolygon( Mat img );  
  17. void MyLine( Mat img, Point start, Point end );  
  18.   
  19. /** 
  20.  * @function main 
  21.  * @brief Main function 
  22.  */  
  23. int main( void ){  
  24.   
  25.   /// Windows names  
  26.   char atom_window[] = "Drawing 1: Atom";  
  27.   char rook_window[] = "Drawing 2: Rook";  
  28.   
  29.   /// Create black empty images  
  30.   Mat atom_image = Mat::zeros( w, w, CV_8UC3 );  
  31.   Mat rook_image = Mat::zeros( w, w, CV_8UC3 );  
  32.   
  33.   /// 1. Draw a simple atom:  
  34.   /// -----------------------  
  35.   
  36.   /// 1.a. Creating ellipses  
  37.   MyEllipse( atom_image, 90 );  
  38.   MyEllipse( atom_image, 0 );  
  39.   MyEllipse( atom_image, 45 );  
  40.   MyEllipse( atom_image, -45 );  
  41.   
  42.   /// 1.b. Creating circles  
  43.   MyFilledCircle( atom_image, Point( w/2, w/2) );  
  44.   
  45.   /// 2. Draw a rook  
  46.   /// ------------------  
  47.   
  48.   /// 2.a. Create a convex polygon  
  49.   MyPolygon( rook_image );  
  50.   
  51.   /// 2.b. Creating rectangles  
  52.   rectangle( rook_image,  
  53.          Point( 0, 7*w/8 ),  
  54.          Point( w, w),  
  55.          Scalar( 0, 255, 255 ),  
  56.          -1,  
  57.          8 );  
  58.   
  59.   RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);  
  60.   ellipse(rook_image, rRect, Scalar(255,255,0));  
  61.   
  62.   /// 2.c. Create a few lines  
  63.   MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );  
  64.   MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );  
  65.   MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );  
  66.   MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );  
  67.   
  68.   /// 3. Display your stuff!  
  69.   imshow( atom_window, atom_image );  
  70.   moveWindow( atom_window, 0, 200 );  
  71.   imshow( rook_window, rook_image );  
  72.   moveWindow( rook_window, w, 200 );  
  73.   
  74.   waitKey( 0 );  
  75.   return(0);  
  76. }  
  77.   
  78. /// Function Declaration  
  79.   
  80. /** 
  81.  * @function MyEllipse 
  82.  * @brief Draw a fixed-size ellipse with different angles 
  83.  */  
  84. void MyEllipse( Mat img, double angle )  
  85. {  
  86.   int thickness = 2;  
  87.   int lineType = 8;  
  88.   
  89.   ellipse( img,  
  90.        Point( w/2, w/2 ),  
  91.        Size( w/4, w/16 ),  
  92.        angle,  
  93.        0,  
  94.        360,  
  95.        Scalar( 255, 0, 0 ),  
  96.        thickness,  
  97.        lineType );  
  98. }  
  99.   
  100. /** 
  101.  * @function MyFilledCircle 
  102.  * @brief Draw a fixed-size filled circle 
  103.  */  
  104. void MyFilledCircle( Mat img, Point center )  
  105. {  
  106.   int thickness = -1;  
  107.   int lineType = 8;  
  108.   
  109.   circle( img,  
  110.       center,  
  111.       w/32,  
  112.       Scalar( 0, 0, 255 ),  
  113.       thickness,  
  114.       lineType );  
  115. }  
  116.   
  117. /** 
  118.  * @function MyPolygon 
  119.  * @function Draw a simple concave polygon (rook) 
  120.  */  
  121. void MyPolygon( Mat img )  
  122. {  
  123.   int lineType = 8;  
  124.   
  125.   /** Create some points */  
  126.   Point rook_points[1][20];  
  127.   rook_points[0][0]  = Point(    w/4,   7*w/8 );  
  128.   rook_points[0][1]  = Point(  3*w/4,   7*w/8 );  
  129.   rook_points[0][2]  = Point(  3*w/4,  13*w/16 );  
  130.   rook_points[0][3]  = Point( 11*w/16, 13*w/16 );  
  131.   rook_points[0][4]  = Point( 19*w/32,  3*w/8 );  
  132.   rook_points[0][5]  = Point(  3*w/4,   3*w/8 );  
  133.   rook_points[0][6]  = Point(  3*w/4,     w/8 );  
  134.   rook_points[0][7]  = Point( 26*w/40,    w/8 );  
  135.   rook_points[0][8]  = Point( 26*w/40,    w/4 );  
  136.   rook_points[0][9]  = Point( 22*w/40,    w/4 );  
  137.   rook_points[0][10] = Point( 22*w/40,    w/8 );  
  138.   rook_points[0][11] = Point( 18*w/40,    w/8 );  
  139.   rook_points[0][12] = Point( 18*w/40,    w/4 );  
  140.   rook_points[0][13] = Point( 14*w/40,    w/4 );  
  141.   rook_points[0][14] = Point( 14*w/40,    w/8 );  
  142.   rook_points[0][15] = Point(    w/4,     w/8 );  
  143.   rook_points[0][16] = Point(    w/4,   3*w/8 );  
  144.   rook_points[0][17] = Point( 13*w/32,  3*w/8 );  
  145.   rook_points[0][18] = Point(  5*w/16, 13*w/16 );  
  146.   rook_points[0][19] = Point(    w/4,  13*w/16 );  
  147.   
  148.   const Point* ppt[1] = { rook_points[0] };  
  149.   int npt[] = { 20 };  
  150.   
  151.   fillPoly( img,  
  152.         ppt,  
  153.         npt,  
  154.             1,  
  155.         Scalar( 255, 255, 255 ),  
  156.         lineType );  
  157. }  
  158.   
  159. /** 
  160.  * @function MyLine 
  161.  * @brief Draw a simple line 
  162.  */  
  163. void MyLine( Mat img, Point start, Point end )  
  164. {  
  165.   int thickness = 2;  
  166.   int lineType = 8;  
  167.   line( img,  
  168.     start,  
  169.     end,  
  170.     Scalar( 0, 0, 0 ),  
  171.     thickness,  
  172.     lineType );  
  173. }  

实验结果




这篇关于OpenCV学习笔记(17)OpenCV之基本绘图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1130859

相关文章

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

C/C++和OpenCV实现调用摄像头

《C/C++和OpenCV实现调用摄像头》本文主要介绍了C/C++和OpenCV实现调用摄像头,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录准备工作1. 打开摄像头2. 读取视频帧3. 显示视频帧4. 释放资源5. 获取和设置摄像头属性

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O