程序及分析

/*
* FileName : random_gen.c
* Author : xiahouzuoxin @163.com
* Version : v1.0
* Date : Tue 29 Jul 2014 08:31:41 PM CST
* Brief :
*
* Copyright (C) MICL,USTB
*/
#include <cv.h>
#include <highgui.h>
#include <iostream> using namespace std;
using namespace cv; const char wndname[] = "Drawing";
const int RAND_N = 100; void help(void)
{
cout<<"Usage:./drawing"<<endl;
} static Scalar random_color(RNG& rng)
{
int icolor = (unsigned)rng; return Scalar(icolor&0xFF, (icolor>>8)&0xFF, (icolor>>16)&0xFF);
} int main(int argc, char *argv[])
{
int line_type = CV_AA;
int i = 0;
int width = 1000;
int height = 700;
int x1 = -width/2;
int x2 = width*3/2;
int y1 = -height/2;
int y2 = height*3/2;
const int DELAY = 10; RNG rng(0xFFFFFFFF);
Mat image = Mat::zeros(height, width, CV_8UC3); imshow(wndname, image);
waitKey(DELAY); for (i=0; i<RAND_N; i++) {
Point pt1;
Point pt2; pt1.x = rng.uniform(x1, x2);
pt1.y = rng.uniform(y1, y2);
pt2.x = rng.uniform(x1, x2);
pt2.y = rng.uniform(y1, y2); line(image, pt1, pt2, random_color(rng), rng.uniform(1,5), line_type);
}
imshow(wndname, image);
waitKey(0); for (i=0; i<RAND_N; i++) {
Point org;
org.x = rng.uniform(x1, x2);
org.y = rng.uniform(y1, y2);
putText(image, "OpenCV",org, rng.uniform(0,8),rng.uniform(0,10)*0.5+0.1,
random_color(rng), rng.uniform(1, 10), line_type);
} imshow(wndname, image);
waitKey(0);
return 0;
}
  1. RNG是OpenCV中的随机数生成类,其定义在core.hpp中,

    class CV_EXPORTS RNG
    {
    public:
    enum { UNIFORM=0, NORMAL=1 }; RNG();
    RNG(uint64 _state);
    //! updates the state and returns the next 32-bit unsigned integer random number
    unsigned next(); operator uchar();
    operator schar();
    operator ushort();
    operator short();
    operator unsigned();
    //! returns a random integer sampled uniformly from [0, N).
    unsigned operator()(unsigned N);
    unsigned operator ()();
    operator int();
    operator float();
    operator double();
    //! returns uniformly distributed integer random number from [a,b) range
    int uniform(int a, int b);
    //! returns uniformly distributed floating-point random number from [a,b) range
    float uniform(float a, float b);
    //! returns uniformly distributed double-precision floating-point random number from [a,b) range
    double uniform(double a, double b);
    void fill( InputOutputArray mat, int distType, InputArray a, InputArray b );
    //! returns Gaussian random variate with mean zero.
    double gaussian(double sigma); uint64 state;
    };

    提供了两种随机数——均匀分布(uniform)和高斯正态分布(gaussian)。

    本文使用的是随机分布,两个參数分布表示均匀分布的下限和上限。

    RNG rng(0xFFFFFFFF);中的0xFFFFFFFF表示初始的随机值。

  2. Mat矩阵初始化:

    Mat image = Mat::zeros(height, width, CV_8UC3);
  3. line用于绘制直线,也定义在core.hpp中,

    //! draws the line segment (pt1, pt2) in the image
    CV_EXPORTS_W void line(Mat& img, Point pt1, Point pt2, const Scalar& color,int thickness=1, int lineType=8, int shift=0);

    还有其他画图函数circle、ellipse、rectangle等也也能够从core.hpp中找到原型。可用到时自行学习。

  4. putText能够将文字加入到图片中。

    //! renders text string in the image
    CV_EXPORTS_W void putText( Mat& img, const string& text, Point org,
    int fontFace, double fontScale, Scalar color,
    int thickness=1, int linetype=8,
    bool bottomLeftOrigin=false );

    其第一个參数img就是要加入文字的图像,第二个參数就是要加入的文字(程序中是"OpenCV")

  5. 关于颜色:颜色是用RGB三通道表示的,因此上面函数中颜色參数的类型都是Scalar类型。Scalar在OpenCV中相似于向量,但其长度最大为4通道。源程序中

    Scalar(icolor&0xFF, (icolor>>8)&0xFF, (icolor>>16)&0xFF);

    将随机数的值取出分别作为RGB三个通道的颜色值。

效果

随机线条的效果

加入“OpenCV”文字后效果

OpenCV基础篇之画图及RNG随机数对象的更多相关文章

  1. OpenCV基础篇之读取显示图片

    程序及分析 /* * FileName : read.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Tue 13 May ...

  2. opencv——基础篇

    一 . opencv是什么及其作用? OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效— ...

  3. OpenCV基础篇之查找表

    程序及分析 /* * FileName : lookup_table.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Su ...

  4. OpenCV基础篇之像素操作对照度调节

    程序及分析 /* * FileName : contrast.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Tue 29 ...

  5. Java学习日记基础篇(四)——类,对象之成员变量,成员方法,构造方法

    面向对象(Object Oriented) 一.面向对象杂谈 面向对象(Object Oriented),我的翻译是以物体为目标的,就是说编程的时候是建立一个物体,然后对这个物体进行操作. Java语 ...

  6. 什么是图像 -- opencv基础

    opencv基础篇--到底什么是图像 什么是图像?英语中有两个单词来形容图像,一个是picture,一个是image.这两者虽然是形容同一个东西,但却又有着区别.picture代表实而有物的真实图像: ...

  7. java基础篇---I/O技术(三)

    接上一篇java基础篇---I/O技术(二) Java对象的序列化和反序列化 什么叫对象的序列化和反序列化 要想完成对象的输入或输出,还必须依靠对象输出流(ObjectOutputStream)和对象 ...

  8. opencv 61篇

    (一)--安装配置.第一个程序 标签: imagebuildincludeinputpathcmd 2011-10-21 16:16 41132人阅读 评论(50) 收藏 举报  分类: OpenCV ...

  9. 《量化投资:以MATLAB为工具》连载(1)基础篇-N分钟学会MATLAB(上)

    http://blog.sina.com.cn/s/blog_4cf8aad30102uylf.html <量化投资:以MATLAB为工具>连载(1)基础篇-N分钟学会MATLAB(上) ...

随机推荐

  1. HDU--杭电--4504--威威猫系列故事——篮球梦--DP

    威威猫系列故事——篮球梦 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  2. 微软 自带 AJAX 拓展

    <内容有点乱,自己找记忆的~~~> 微软自带AJAX 控件大全:控件简介: ScriptManager 控件 为启用了 AJAX 的 ASP.NET 网页管理客户端脚本. ScriptMa ...

  3. HDU 1242——Rescue(优先队列)

    题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...

  4. cronjob不跑得原因

    能是环境的不同,能够在cronjob中加个env > /tmp/env.output查看 应用要同一时候输出标准错误合标准输出到一个文件能够&> /tmp/t

  5. 关于使用commons-email包测试发送邮件遇到的问题

    项目中有个需求是这样的:客户办理某一项业务,当用户成功提交业务办理信息后,系统生成一个业务随机码给用户,以此作为以后的业务办理结果查询依据.鉴于随机码较长,方便用户记录,在生成随机码的同时,提供用户发 ...

  6. hdu 1007 最近点对问题(Splay解法)

    为什么要写这个题..经典啊,当然,别以为我用分治做的,不过主要思想还是那神奇的六个点共存(一个h*2h的矩形中最多能放下多少个点使得两两距离不超过h) 其实我是在这里看到的 http://commun ...

  7. 【智能家居篇】wifi网络接入原理(上)——扫描Scanning

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,常常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入passwor ...

  8. 温故知新-------jQuery层次选择器

    <html xmlns="http://www.w3.org/1999/xhtml">  <head>     <title></titl ...

  9. leetcode解析回文子串拆分

    转载请注明来自souldak,微博:@evagle Given a string s, partition s such that every substring of the partition i ...

  10. 用VLC搭建流媒体server

    VLC开元项目相当强大,我们既能够将其作为播放核心用于二次开发,又能够将其作为高性能的流媒体server.今篇博客主要讲用VLC搭建流媒体server. VLC搭建流媒体server步骤非常easy: ...