OpenCV中的Rand随机数函数簇学习

OpenCV生成随机矩阵---randn与randu OpenCV中提供了两个函数来生成随机数矩阵,可以用来创建随机图像或是用来人为增加随机噪点。 阅读详情

因为学习卡尔曼滤波,所以发现了OpenCV居然还能发生随机数的。特意把随机数函数簇拿出来学习下。

注:正态分布曲线的对称轴是正态样本的平均值;样本的平均值增大,曲线向右侧平移,样本的平均值减小,曲线向左侧平移。

正态样本的标准差越大,则正态分布曲线越平坦,峰值越小。

RandInit

Initializes random number generator state

void cvRandInit( CvRandState* state, double param1, double param2, int seed,
int distType=CV_RAND_UNI );
state
Pointer to the initialized random number generator state structure. 传入结构CvRandState,必须,
param1
The first distribution parameter. In case of uniform distribution it is the inclusive lower boundary of random numbers range. In case of normal distribution it is the standard deviation of random numbers.
在平均分布是下限,在正态分布是标准差
param2
The second distribution parameter. In case of uniform distribution it is the exclusive upper boundary of random numbers range. In case of normal distribution it is the mean value of random numbers.
在平均分布是上限,正态分布是平均值
seed
Initial 32-bit value to start a random sequence.
种子数,可以用+-1代替,也可以用GetTickCount()代替。
distType
Distribution type:
CV_RAND_UNI - uniform distribution 平均分布
CV_RAND_NORMAL - normal or Gaussian distribution 正态分布,高斯分布

The function cvRandInit initializes the state structure that is used for generating uniformly distributed numbers in the range [param1, param2) or normally distributed numbers with param1 mean and param2 standard deviation. The parameters are set for all the dimensions simultaneously - resemble that RNG has separate parameters for each of 4 dimensions. A multiply-with-carry generator is used.

RandSetRange RandInit用于设置全局,可以指定其中的一个通道用其它的方式发生随机数。可见Rand可以发生N个通道的CvArr*

Changes the range of generated random numbers without touching RNG state

void cvRandSetRange( CvRandState* state, double param1, double param2, int index=-1 );
state
State of random number generator (RNG).
param1
New lower boundary/deviation of generated numbers.
param2
New upper boundary/mean value of generated numbers.
index
The 0-based index of dimension/channel for which the parameter are changed, -1 means changing the parameters for all dimensions.

The function cvRandSetRange changes the range of generated random numbers without reinitializing RNG state. It is useful if a few arrays of different types need to initialized with random numbers within a loop. Alternatively, you may have a separate generator for each array, but then you should provide several uncorrelated initialization seeds - one per each generator.

 

Rand 发生随机数到CVArr* cvbRand产生随机数到数组[]

Fills array with random numbers and updates the RNG state

void cvRand( CvRandState* state, CvArr* arr );
state
RNG state initialized by RandInit and, optionally, customized by RandSetRange .
arr
The destination array.

The function cvRand fills the destination array with uniformly or normally distributed random numbers within the pre-set range and updates RNG state. In the sample below this and two functions above are used to put a few normally distributed floating-point numbers to random locations within a 2d array

/* let's noisy_screen be the floating-point 2d array that is to be "crapped" */
CvRandState rng_state;
int i, pointCount = 1000;
/* allocate the array of coordinates of points */
CvMat* locations = cvCreateMat( pointCount, 1, CV_32SC2 );
/* array of random point values */
CvMat* values = cvCreateMat( pointCount, 1, CV_32FC1 );
CvSize size = cvGetSize( noisy_screen );

cvRandInit( &rng_state,
0, 1, /* use dummy parameters now and adjust them further */
0xffffffff /* just use a fixed seed here */,
CV_RAND_UNI /* specify uniform type */ );

/* customize the RNG to use it for initialiazing locations:
the 0-th dimension is used for x's and the 1st - for y's */
cvRandSetRange( &rng_state, 0, size.width, 0 );
cvRandSetRange( &rng_state, 0, size.height, 1 );

/* initialize the locations */
cvRand( &rng_state, locations );

/* modify RNG to make it produce normally distributed values */
rng_state.disttype = CV_RAND_NORMAL;
cvRandSetRange( &rng_state,
30 /* deviation */,
100 /* average point brightness */,
-1 /* initialize all the dimensions */ );
/* generate values */
cvRand( &rng_state, values );

/* set the points */
for( i = 0; i < pointCount; i++ )
{
CvPoint pt = *(CvPoint*)cvPtr1D( locations, i, 0 );
float value = *(float*)cvPtr1D( values, i, 0 );
cvSetReal2D( noisy_screen, pt.y, pt.x, value );
}

/* not to forget to release the temporary arrays */
cvReleaseMat( &locations );
cvReleaseMat( &values );

/* cvRandInit does not allocate any memory, so there is no need
(and no function) to deinitialize it */


RandNext 产生单个随机数

Returns 32-bit unsigned integer and updates RNG

unsigned cvRandNext( CvRandState* state );

state
RNG state initialized by RandInit and, optionally, customized by RandSetRange (though, the latter function does not affect on the discussed function outcome).

The function cvRandNext returns uniformly-distributed (regardless of the RNG distribution type settings) "plain" integer random number and updates RNG state. It is similar to rand() function from C runtime library, but it always generates 32-bit number whereas rand() returns a number in between 0 and RAND_MAX which is 2**16 or 2**32, depending on the platform.

The function is useful for generating scalar random numbers, such as points, patch sizes, table indices etc, where integer numbers of a certain range can be generated using modulo operation and floating-point numbers can be generated by scaling to 0..1 of any other specific range. Here is the example from the previous function discussion rewritten using cvRandNext :

/* the input and the task is the same as in the previous sample. */ CvRandState rng_state; int i, pointCount = 1000; /* ... - no arrays are allocated here */ CvSize size = cvGetSize( noisy_screen ); /* make a buffer for normally distributed numbers to reduce call overhead */ #define bufferSize 16 float normalValueBuffer[bufferSize]; CvMat normalValueMat = cvMat( bufferSize, 1, CV_32F, normalValueBuffer ); int valuesLeft = 0; /* initialize RNG to produce normally distributed values. Coordinates will be uniformly distributed within 0..2**32 anyway as they are generated using cvRandNext */ cvRandInit( &rng_state, 100, 30, 0xffffffff /* just use a fixed seed here */, CV_RAND_NORMAL /* specify uniform type */ ); for( i = 0; i < pointCount; i++ ) { CvPoint pt; /* generate random point */ pt.x = cvRandNext( &rng_state ) % size.width; pt.y = cvRandNext( &rng_state ) % size.height; if( valuesLeft <= 0 ) { /* fulfill the buffer with normally distributed numbers if the buffer is empty */ cvRand( &rng_state, &normalValueMat ); valuesLeft = bufferSize; } cvSetReal2D( noisy_screen, pt.y, pt.x, normalValueBuffer[--valuesLeft]); } /* there is no need to deallocate normalValueMat because we have both the matrix header and the data on stack. It is a common and efficient practice of working with small, fixed-size matrices */


OpenCV-Python实战(6)——OpenCV中的色彩空间和色彩映射 为了更好的表示色彩,人们建立了多种色彩模型以一维、二维、三维等坐标系来描述不同色彩,这种坐标系所能定义的色彩范围即色彩空间;而色彩映射是将图像在一个色彩空间映射至另一色彩空间的操作,通常可以将灰度图像着色为等效的伪色彩图像。本文将介绍OpenCV中的色彩空间和色彩映射,并进行实战以应用。 阅读详情

相关推荐

Unity中5自由度机械臂运动控制与夹爪交互仿真实践

本文详细介绍了在Unity中实现5自由度机械臂运动控制与夹爪交互仿真的完整流程。从模型导入、物理组件配置到关节运动控制算法(包括逆运动学IK),再到夹爪抓取逻辑和工业分拣案例实战,提供了可落地的代码示例与性能优化方案,助力开发者快速掌握机械臂仿真核心技术。

weixin_29021757的博客 446

OpenCV中的随机数与随机种子

我们先从在C/C++中的随机数与随机种子说起。 rand()、srand() rand()可以生成一个0~RAND_MAX之间的一个随机数,返回值是一个unsigned int类型值。rand函数不是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子。如果你在第一次调用rand()之前没有调用srand(),那么系统会为你自动调用srand()。而使用同种子相同的数调用...

Young_Fan 2208

大厂嵌入式面试常考八股文及算法

1.指向已被释放或无效的内存地址的指针是野指针。互斥锁是一种用于线程同步的机制,用于确保同一时间只有一个线程访问共享资源。多态是面向对象编程中的一种特性,它允许以一种统一的方式处理不同类型的对象,通过相同的接口可以表现出不同的行为。大小端(Endian)指的是在计算机存储和处理多字节数据时,字节的存储顺序。大端序(Big Endian):高位字节存储在低地址,低位字节存储在高地址。类似于将数字的高位放在左边,低位放在右边的表示方式。例如整数值0x12345678在内存中按照大端序排列为。

谢谢大家的关注 6155

rand函数生成的随机数范围_【从零学习OpenCV 4】图像噪声的种类与生成

本文首发于“小白学视觉”微信公众号,欢迎关注公众号 本文作者为小白,版权归人民邮电出版社发行所有,禁止转载,侵权必究! 经过几个月的努力,小白终于完成了市面上第一本OpenCV 4入门书籍《OpenCV 4开发详解》。为了更让小伙伴更早的了解最新版的OpenCV 4,小白与出版社沟通,提前在公众号上连载部分内容,请持续关注小白。图像在获取或者传输过程中会受到随机信号的干扰产生噪声,例如电阻引起的热...

weixin_39718173的博客 451

PS/Ai/PR/AE等adobe出现正版弹窗提示:Your non-genuine Adobe app will be disabled soon解决办法

就是用专门的清理工具下载,清理工具在文章下方。然后新建三个空的记事本文件,重命名为 Adobe Genuine Launcher.exe,AdobeGCClient.exe,AGMService.exe。在该文件目录中,分别找到 Adobe Genuine Launcher.exe,AdobeGCClient.exe,AGMService.exe,然后在防火墙入站规则里,阻止Adobe Genuine Launcher.exe,AdobeGCClient.exe,AGMService.exe。

Q82556499的博客 1万+

奇异值分解(SVD)C++实现

奇异值分解SVD(紧奇异值分解,截断奇异值分解)C++实现

weixin_45804601的博客 4172

opencv rand函数

关于rand()的用法求解释 由 AngleLove » 2011-08-06 17:40 我知道int rand(void)返回一随机数值,范围在0至RAND_MAX 间。在调用此函数产生随机数前,必须先利用srand()设好随机数种子,如果未设随机数种子,rand()在调用时会自动设随机数种子为1。但是有句代码我还是不懂,急求解释! 代码如下: cvScalar color =

canlan818的专栏 4123

yolov11添加P2小目标检测头与添加P6超大目标检测头

本文介绍了YOLO目标检测模型中上下采样的原理及应用。上采样通过插值算法放大特征图,YOLO通常采用最近邻插值;下采样则通过带步长的卷积缩小特征图提取高层语义特征。文章详细阐述了这两种操作在YOLO中的实现方式、结构特点及对称性问题,指出下采样后会使用C3模块进行特征整合,上采样后通过concat抑制分割感。此外,还分析了不同分辨率特征图适合检测不同尺寸目标的原因,并提出添加4倍小目标检测头的方法,即对上采样后的特征进行concat和C3模块处理。最后给出了YOLOv5原模型结构和添加4倍检测头后的结构对比

m0_64661770的博客 820

学习OpenCV2——产生随机数

本文详细介绍了OpenCV中随机数的用法

Markala的博客 2万+

zemax 如何编译用户自定义DLL(含VS下载教程)

zemax 如何编译用户自定义DLL(含VS下载教程)

weixin_72050316的博客 491

基于物联网设计的蜂箱智能监测系统设计

为解决传统养蜂业依赖人工、管理效率低下、干扰蜂群等问题,本项目设计并实现了一套基于物联网(IoT)的蜂箱智能监测系统。

683

OpenCV 随机数

简介 产生随机数是编程中经常用到的操作,特别在进行初始化的时候需要赋一些随机值。C和C++中产生随机数的方法如rand()、srand()等在OpenCV中仍可以用。此外,OpenCV还特地编写了C++的随机数类RNG,C的随机数类CvRNG,还有一些相关的函数,使用起来更加方便。下面,一一介绍。 说明 关键字前带cv的都是C里的写法,不带cv的是C++里的写法,比如CvRNG和RNG,其本...

qq_27278957的博客 2179

量化交易python代码解析:从狙击手算法到最优限价算法

在金融市场的竞技场上,量化交易如同一位冷静的策略大师,它以数学模型为剑,以计算机程序为盾,精准出击,捕捉市场的微妙波动。本文将深入探讨五种核心量化交易算法:直接委托算法(DMA)、时间加权平均算法(TWAP)、冰山算法(Iceberg)、狙击手算法(Sniper)、条件委托算法(Stop)以及最优限价算法(BestLimit),揭开它们背后的运作机制与实战应用。

木头大左的博客 1884

c++和opencv小知识:RNG随机数的超级详细解析

———————————————— 版权声明:本文为优快云博主「Markala」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.youkuaiyun.com/gdfsg/article/details/50830489 产生随机数是编程中经常用到的操作,特别在进行初始化的时候需要赋一些随机值。C和C++中产生随机数的方法如rand()、s...

qq_43702097的博客 5514

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

opencv中产生随机数的类CvRNG,以及C++底层随机数方法

近期做图像检测时用到的获取随机数的方法,以及遇到的一些问题。 操作背景:在一张大的背景图上获取n个随机矩阵,提取这些矩阵区域小图像的特征值,依次进行分类器处理分类。 首先使用opencv封装类CvRNG //获取随机矩形,opencv中封装的随机类CvRNG ///////////////////////////////////////////////////////////////// /

POTATO的博客 822

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Opencv中随机数

RNG 初始化随机数生成器状态 CvRNG cvRNG( int64 seed=-1 ); seed64-bit 的值用来初始化一个随机序列 函数 cvRNG 初始化随机数生成器并返回其状态。指向这个状态的指针可以传递给函数 cvRandInt, cvRandReal 和 cvRandArr . 在通常的实现中使用一个 multiply-with-carry generato...

Joanne0106的博客 211

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

C++产生各种随机数

      c++产生随机数 本博客主要内容包括: 1. rand()函数; 2.std库的default_random_eigine类; 3.OpenCV库。   1. rand()函数产生float随机数 #include <stdlib.h> srand((unsigned int)time(0));//随机种子,不要写在循环体中 int a = rand() % 1...

踏雪飞鸿的博客 1412

OpenCV函数生成随机数

利用OpenCV函数生成随机数,填充数组。主要参考OpenCV中文网站OpenCV手册    http://www.opencv.org.cn/index.php/Cxcore%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C    函数:RandArr #include "stdafx.h"  #include #include #inclu

peixian2003的专栏 763

C++产生随机数

此文章为转载,原文地址为:https://www.cnblogs.com/xiaokang01/p/9786751.html C++产生随机数 C++产生随机数 </h1> <div class="clear"></div> <div...

OpenCV 图像辨识的博客 483

OpenCV随机数生成

<!-- @page {margin:2cm} p {margin-bottom:0.21cm; direction:ltr; color:#000000; text-align:justify; widows:0; orphans:0} p.western {font-family:"Times New Roman",serif; font-size:1

Hard Working 3095

OpenCV中随机数的相关函数

这里书写的并不全,只是看到相关的就在这里jil

Ruby 884

OpenCV 随机数生成

参考: http://www.opencv.org.cn/index.php/Cxcore%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C#.E9.9A.8F.E6.9C.BA.E6.95.B0.E7.94.9F.E6.88.90 RNG 初始化随机数生成器状态 CvRNG cvRNG( int64 seed=-1 ); seed64-bit

令狐少侠 1810

np.random.rand方法在opencv中的应用

np.random.rand方法是numpy库中用于生成随机数的函数。在OpenCV中,可以使用它来随机生成图像或设置图像的像素值。例如,可以使用np.random.rand函数生成一个随机噪声图像,或者使用它来随机设置图像的某些像素值。 ...

weixin_35755562的博客 101

openCV中的随机数函数与C语言中对应函数的区别

cvRNG() 初始化随机数生成器状态。 跟一般的C语言srand()使用方法一样,要先给它一个种子,但srand()用到的是unsigned int的32位种子范围,而cvRNG()用的是64位长整数种子。初始化CvRNG资料结构,假如seed给0,它将会自动转成-1。函数 cvRNG 初始化随机数生成器并返回其状态。指向这个状态的指针可以传递给函数 cvRandInt, cvRandR

ShaderJoy 的兴趣技术杂货铺 3273

Opencv 中产生随机数种子的赋值

在编程中用到了shuij 参考: http://www.opencv.org.cn/index.php/Cxcore%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C#.E9.9A.8F.E6.9C.BA.E6.95.B0.E7.94.9F.E6.88.90 RNG 初始化随机数生成器状态 CvRNG cvRNG( int64 seed=-1 );

qingfenglu的专栏 1104
上一篇: [转帖]卡尔曼滤波器通俗解释 – Kalman Filter
下一篇: [转]nafxcwd.lib error LNK2005的解决方法。
darkread
博客等级 码龄25年 42粉丝 81原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值