opencv 3.0 绘图 highgui

news/2024/7/8 5:02:16 标签: opencv 3.0, 图形, opencv

绘图的时候最常用的是highgui

需注意的是:绘图填充 需要赋值thickness 

thickness – 绘图边框的粗细. 负值(CV_FILLED = -1)

相关函数介绍

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, 图像数据原点在左上角.


示例代码

/**
 * @file Drawing_1.cpp
 * @brief Simple sample code
 */

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#define w 400

using namespace cv;

/// Function headers
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );

/**
 * @function main
 * @brief Main function
 */
int main( void ){

  /// Windows names
  char atom_window[] = "Drawing 1: Atom";
  char rook_window[] = "Drawing 2: Rook";

  /// Create black empty images
  Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
  Mat rook_image = Mat::zeros( w, w, CV_8UC3 );

  /// 1. Draw a simple atom:
  /// -----------------------

  /// 1.a. Creating ellipses
  MyEllipse( atom_image, 90 );
  MyEllipse( atom_image, 0 );
  MyEllipse( atom_image, 45 );
  MyEllipse( atom_image, -45 );

  /// 1.b. Creating circles
  MyFilledCircle( atom_image, Point( w/2, w/2) );

  /// 2. Draw a rook
  /// ------------------

  /// 2.a. Create a convex polygon
  MyPolygon( rook_image );

  /// 2.b. Creating rectangles
  rectangle( rook_image,
         Point( 0, 7*w/8 ),
         Point( w, w),
         Scalar( 0, 255, 255 ),
         -1,
         8 );

  RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
  ellipse(rook_image, rRect, Scalar(255,255,0));

  /// 2.c. Create a few lines
  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 ) );

  /// 3. Display your stuff!
  imshow( atom_window, atom_image );
  moveWindow( atom_window, 0, 200 );
  imshow( rook_window, rook_image );
  moveWindow( rook_window, w, 200 );

  waitKey( 0 );
  return(0);
}

/// Function Declaration

/**
 * @function MyEllipse
 * @brief Draw a fixed-size ellipse with different angles
 */
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 );
}

/**
 * @function MyFilledCircle
 * @brief Draw a fixed-size filled circle
 */
void MyFilledCircle( Mat img, Point center )
{
  int thickness = -1;
  int lineType = 8;

  circle( img,
      center,
      w/32,
      Scalar( 0, 0, 255 ),
      thickness,
      lineType );
}

/**
 * @function MyPolygon
 * @function Draw a simple concave polygon (rook)
 */
void MyPolygon( Mat img )
{
  int lineType = 8;

  /** Create some points */
  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 );
}

/**
 * @function MyLine
 * @brief Draw a simple line
 */
void MyLine( Mat img, Point start, Point end )
{
  int thickness = 2;
  int lineType = 8;
  line( img,
    start,
    end,
    Scalar( 0, 0, 0 ),
    thickness,
    lineType );
}

实验结果




转自

http://blog.csdn.net/ubunfans/article/details/24421981


http://www.niftyadmin.cn/n/1002881.html

相关文章

android开发小知识3

1.启动程序无需动画 Java代码 myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); //1.5的应该使用,这样就可以没有动画效果了 getWindow().setWindowAnimations(0 ) //1.6的应该使用,这不要忘记放在activity myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATI…

Win2008 Server R2个人PC化设置

Win2008 Server R2个人PC化设置 1、安装驱动  安装完系统后首先是安装硬件驱动&#xff0c;可能你的电脑的提供商并没有提供针对windows server 2008的驱动&#xff0c;但一般来讲针对vista或WIN 7的驱动是很容易找到的。  驱动的安装顺序通常是&#xff1a;芯片组、显卡、…

opencv 3.0 旋转矩阵ROI 直方图分析

使用Mat mask的方式是解决特殊形状ROI的较好方法 一般需要使用支持mask的函数解决 这里是一个旋转矩形mask opencv 不能直接绘制填充的旋转矩形&#xff0c;所以我使用的是填充矩形&#xff0c;外加旋转。 也可以用 fillPoly 绘制填充多边形 ROI操作是做直方图。 注意&…

DirectX截图黑屏的解决办法

好久没有更新博客了&#xff0c;今天开始继续耕耘。 生活要继续 工作要继续 梦想也一定要继续&#xff01; 之前写过一篇关于DirectX截屏的文章&#xff0c;其中有网友留言提到了截图黑屏的问题&#xff0c;于是这些日子研究了一下&#xff0c;与大家一同分享。 为什么会黑屏&a…

快速下载TUM数据集

TUM数据集下载慢怎么办最近在学习ORB-SLAM不可避免的要用到TUM数据集。然而在国内下载国外的东西&#xff0c;这速度令人满意&#xff08;个鬼&#xff09;。忽然想起百度网盘的离线下载功能&#xff0c;试了试相当好用。 TUM数据集下载地址 第一步点击下载 第二步复制网址 第…

#if...#endif的用法总结

在写到 pwm音阶程序的时候&#xff0c;在代码中有#if...#endif的用法问题&#xff0c;相关音阶的代码如下&#xff1a; 1 /*******************************************************/2 /* pwm音阶程序 */3 /*********************…

求最大连续子数列和(只扫描一次数列)

一、什么是求最大连续子数列和 首先来看看这是个怎样的问题的&#xff0c;问题描述&#xff1a;一个整型数组&#xff0c;数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组&#xff0c;每个子数组都有一个和&#xff0c;求所有子数组的和的最大值。注意&#x…

Android提高十七篇之多级树形菜单的实现

在 Android里要实现树形菜单&#xff0c;都是用ExpandableList(也有高手自己继承ListView或者LinearLayout来做)&#xff0c;但是 ExpandableList一般只能实现2级树形菜单......本文也依然使用ExpandableList&#xff0c;但是要实现的是3级树形菜单。 本文程序运行效果图&…