基本绘图、随机生成器、字体

官网链接

常用的画图函数

  • 官网在 imgproc 模块下 -> Drawing Functions
  • 画图函数默认的坐标方向:
    在这里插入图片描述

线段

  1. void cv::line
    (
    InputOutputArray img,     // 在哪个图中画
    Point pt1,          //第一个点
    Point pt2,         // 第二个点
    const Scalar & color,        // Scalar(255,0,0) 蓝色
    int thickness = 1,        // 宽度,FILLED:填充
    int lineType = LINE_8,        // 画线的算法类型,一般选择LINE_8就行,
    int shift = 0         //端点坐标小数的个数,大部分情况 0 即可
    )
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 );
}



椭圆

  1. void cv::ellipse (
    InputOutputArray img,     //目标图像
    Point center,        //椭圆的中心点
    Size axes,        //两个主轴的大小一半,下图中青色的线,相当于AXES_WIDTH,AXES_HEIGHT
    double angle,        //椭圆的旋转角度
    double startAngle,        // 画弧用的(蓝色),画弧的起始角度,如果画椭圆 就是 0
    double endAngle,        // 画弧用的(蓝色),画弧的结束角度,如果画椭圆 就是 360
    const Scalar & color,        //颜色:Scalar(255,0,0)就是蓝色
    int thickness = 1,        // 线的宽度,FILLED:填充
    int lineType = LINE_8,        // 画线的算法类型:LINE_8就行
    int shift = 0         // 中心点坐标的小数位个数,大部分是整数,0即可
    )

在这里插入图片描述


  ellipse( img,
       Point( 200, 200 ), 
       Size( 100, 50), 长轴200,短轴100
       45,
       0,
       360,
       Scalar( 255, 0, 0 ),
       2,
       LINE_8);

结果:图是400*400 大小





矩形

  1. void cv::rectangle (
    InputOutputArray img,        // 目标图像
    Point pt1,        // 矩形定点1,
    Point pt2,        // 矩形定点2,和1相对(对角线)
    const Scalar & color,        // 颜色:Scalar(255,0,0)蓝色
    int thickness = 1,        // 线宽度,FILLED:填充
    int lineType = LINE_8,        // 画线算法
    int shift = 0         // 定点坐标小数点的位数,0即可
    )

  rectangle( rook_image,
         Point( 0, 350 ),
         Point( 400, 400),
         Scalar( 0, 255, 255 ),
         FILLED,
         LINE_8 );

结果:图像大小:400*400
在这里插入图片描述





  1. void cv::circle (
    InputOutputArray img,        // 目标图像
    Point center,        // 圆的中心点
    int radius,        // 圆的半径
    const Scalar & color,        // 颜色
    int thickness = 1,        // 线宽度,FILLED:填充
    int lineType = LINE_8,        //线类型
    int shift = 0         // 中点,半径 的小数点个数
    )
	circle(img,oint(200,200),200,Scalar(0,0,255),FILLED,LINE_8);

结果:400*400的图中画了半径200的圆ff
在这里插入图片描述





箭头

  1. void cv::arrowedLine (
    InputOutputArray img,         //
    Point pt1,        // 开始点
    Point pt2,        // 指向的 点
    const Scalar & color,        //
    int thickness = 1,        //
    int line_type = 8,        //
    int shift = 0,        //
    double tipLength = 0.1         // 箭头的长度是线段的0.1
    )



填充多边形

  1. void cv::fillPoly (
    InputOutputArray img,        //
    const Point ** pts,        //定点的首地址的二级指针
    const int * npts,        // 多边形定点个数
    int ncontours,        //
    const Scalar & color,        //
    int lineType = LINE_8,        //
    int shift = 0,        //
    Point offset = Point()         //
    )
  w=400;
  Point rook_points[20];
  
  rook_points[0]  = Point(    w/4,   7*w/8 );
  rook_points[1]  = Point(  3*w/4,   7*w/8 );
  rook_points[2]  = Point(  3*w/4,  13*w/16 );
  rook_points[3]  = Point( 11*w/16, 13*w/16 );
  rook_points[4]  = Point( 19*w/32,  3*w/8 );
  rook_points[5]  = Point(  3*w/4,   3*w/8 );
  rook_points[6]  = Point(  3*w/4,     w/8 );
  rook_points[7]  = Point( 26*w/40,    w/8 );
  rook_points[8]  = Point( 26*w/40,    w/4 );
  rook_points[9]  = Point( 22*w/40,    w/4 );
  rook_points[10] = Point( 22*w/40,    w/8 );
  rook_points[11] = Point( 18*w/40,    w/8 );
  rook_points[12] = Point( 18*w/40,    w/4 );
  rook_points[13] = Point( 14*w/40,    w/4 );
  rook_points[14] = Point( 14*w/40,    w/8 );
  rook_points[15] = Point(    w/4,     w/8 );
  rook_points[16] = Point(    w/4,   3*w/8 );
  rook_points[17] = Point( 13*w/32,  3*w/8 );
  rook_points[18] = Point(  5*w/16, 13*w/16 );
  rook_points[19] = Point(    w/4,  13*w/16 );

  const Point* ppt =  rook_points ;
  int npt = 20 ;
  fillPoly( img,
        &ppt, //
        &npt, //多边形定点个数
        1,
        Scalar( 255, 255, 255 ),
        LINE_8);

结果:400*400的图
在这里插入图片描述




字体相关函数

写字

  1. void cv::putText (
    InputOutputArray img,        //
    const String & text,        // 字符串
    Point org,        // 右下角的坐标
    int fontFace,        // 字体
    double fontScale,        // 字的大小
    Scalar color,        //
    int thickness = 1,        //
    int lineType = LINE_8,        //
    bool bottomLeftOrigin = false         // 默认false,ture的话,字将是倒的
    )
putText(img,"julian",Point(10,50),5,2,Scalar(255,0,0))

结果:
在这里插入图片描述



得到字符串宽度和高度

  1. Size cv::getTextSize (
    const String & text,        // 字符串
    int fontFace,        // 字体
    double fontScale,        // 字大小
    int thickness,        //线宽度
    int * baseLine         //
    )
  • 可以使用此函数来得到预显示的字符串的文本框大小,进行布局,比如我想将字符串放中间
	int base =0;
	Size textsize = getTextSize("julian",5,2,2,&base);
	Point xx;
	xx.x=(atom_image.cols-textsize.width)/2;
	xx.y=(atom_image.rows-textsize.height)/2;
	putText(atom_image,"julian",xx,5,2,Scalar(255,0,0));


结果:
在这里插入图片描述




随机数生成器:cv:: RNG

  • 随机数生成器都是伪随机,只要种子不变,生成的随机算法不变,每次的执行结果就是一样的。

构造函数

  1. cv::RNG::RNG()
  2. cv::RNG::RNG ( uint64 state)
RNG rng; // 第一种,默认以2^32-1 做种子
RNG rng(1);//第二种,以给定的值做种子


得到随机数

普通:

  1. unsigned cv::RNG::next ( ) :使用MWC算法更新种子,返回32位的随机数
  2. cv::RNG::operator double ( ) :返回下一个 double型的数
  3. cv::RNG::operator float ( ) :返回下一个 float型的数
  4. cv::RNG::operator schar ( ) :。。。
  5. cv::RNG::operator short ( ) :。。。
  6. 。。。。。。。。。()//都差不多

  7. unsigned cv::RNG::operator() ()://和next一样
  8. unsigned cv::RNG::operator() ( unsigned N ) ://返回的数对N取模,等价于返回 [0 N)

均匀分布:

  1. int cv::RNG::uniform (
    int a,      // 均匀分布的下限
    int b       //均匀分布的上限
    )

  2. float cv::RNG::uniform (
    float a,
    float b
    )

  3. double cv::RNG::uniform (
    double a,
    double b
    )

返回的值是int 、float、还是double只取决于传入的参数,和被赋值的左值类型无关。如果你想调用浮点数,传入的是整数,要么向a1那样进行类型转化,要么在后面加一个点

RNG rng;
// always produces 0
double a = rng.uniform(0, 1);

// produces double from [0, 1)
double a1 = rng.uniform((double)0, (double)1);

// produces float from [0, 1)
float b = rng.uniform(0.f, 1.f);

// produces double from [0, 1)
double c = rng.uniform(0., 1.);


高斯分布:
  1. double cv::RNG::gaussian ( double sigma )
    :sigma (高斯分布的标准差 σ \sigma σ),返回一个从高斯分布N(0, σ \sigma σ)的采样值


填充特定分布的数据到矩阵

  1. void cv::RNG::fill (
    InputOutputArray mat,      //被填充的矩阵,
    int distType,      // 分布类型 RNG::UNIFORM 或者 RNG::NORMAL.
    InputArray a,      // 是均匀分布这个值是下限值a,是高斯分布的话就是均值
    InputArray b,      //是均匀分布这个值是上限值b,是高斯分布的话就是标准差 σ \sigma σ
    bool saturateRange = false       //只对均匀分布有效,如果是ture:将会产生[ a, b ]之间的值;如果是false:最终填充的数据将会根据图像数据类型进行截断,比如传入图像是CV_C8U1:最终填充数据将是[0,255]
    )

如果是多通道的图像,填充数据分别对每个通道单独进行填充,比如对3通道进行高斯分布数据填充,并不是直接由3*3的协方差矩阵形成的高斯分布,然后采样填充到图像中。而是分别形成3个高斯分布,再分别进行采样填充到3个通道。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值