用OpenCV实现随机发生器与文本

时间:2022-06-25 23:32:47

目标

学习使用随机数发生器类(RNG)并从一个均匀分布中得到一个随机数。
利用putText函数来展现文本

代码

将使用随机数作为绘画的参数,并用大量的几何图形来组成图像。既然会随机初始化,这个过程会使用loop来自动实现。
代码在OpenCV的sample文件夹中(一般是%your_opencv_folder%\sources\samples\cpp\tutorial_code\core\Matrix);也可以从这里获得;在本文最后也附上了。

解释

1.先从主函数开始看起。可看到首先做的事就是创建一个随机数发生器对象(RNG)。

 RNG rng(0xFFFFFFFF)

2.然后创建了一个初始为0的矩阵(全黑),确定了其高度、宽度和类型。

/// Initialize a matrix with zeros
Mat image = Mat::zeros(window_height, window_width, CV_8UC3);

/// Show it in a window during DELAY ms
imshow(window_name, image);

3.接着进行画疯狂道具

/// Draw some lines
c = Drawing _Random_Lines(image, window_name. rng);
if(c != 0) return 0;

...

/// Display text in random positoins
c = Displaying_Random_Text(image, window_name, rng);
if(c != 0) return 0;

/// Displaying the big end
c = Displaying_Big_End(image, window_name, rng);

所有这些函数都遵循相同的模式。

4.查看Drawing_Random_Lines函数。

int Drawing_Random_Lines( Mat image, char* window_name, RNG rng )
{
int lineType = 8;
Point pt1, pt2;

for( int i = 0; i < NUMBER; i++ )
{
pt1.x = rng.uniform( x_1, x_2 );
pt1.y = rng.uniform( y_1, y_2 );
pt2.x = rng.uniform( x_1, x_2 );
pt2.y = rng.uniform( y_1, y_2 );

line( image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8 );
imshow( window_name, image );
if( waitKey( DELAY ) >= 0 )
{ return -1; }
}
return 0;
}

共绘制了NUMBER条直线,因直线的绘制函数line所在代码块被循环了NUMBER次。
直线的端点由pt1和pt2决定,而pt1和pt2都是随机决定的(坐标值x和y都分别由函数rng.uniform(inclusive in a, exclusive b)产生)。
直线的颜色由random(rng)函数得到,其中randomColor()的实现为

static Scalar randomColor( RNG& rng )
{
int icolor = (unsigned) rng;
return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
}

也是随机生成的。

5.查看Display_Random_Text函数:

int Displaying_Random_Text( Mat image, char* window_name, RNG rng )
{
int lineType = 8;

for ( int i = 1; i < NUMBER; i++ )
{
Point org;
org.x = rng.uniform(x_1, x_2);
org.y = rng.uniform(y_1, y_2);

putText( image, "Testing text rendering", org, rng.uniform(0,8),
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);

imshow( window_name, image );
if( waitKey(DELAY) >= 0 )
{ return -1; }
}

return 0;
}

代码与前面的类似除了下面这个表达式

putText( image, "Testing text rendering", org, rng.uniform(0,8),
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);



附录:代码

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


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;

/// Global Variables
const int NUMBER = 100;
const int DELAY = 5;

const int window_width = 900;
const int window_height = 600;
int x_1 = -window_width/2;
int x_2 = window_width*3/2;
int y_1 = -window_width/2;
int y_2 = window_width*3/2;

/// Function headers
static Scalar randomColor( RNG& rng );
int Drawing_Random_Lines( Mat image, char* window_name, RNG rng );
int Drawing_Random_Rectangles( Mat image, char* window_name, RNG rng );
int Drawing_Random_Ellipses( Mat image, char* window_name, RNG rng );
int Drawing_Random_Polylines( Mat image, char* window_name, RNG rng );
int Drawing_Random_Filled_Polygons( Mat image, char* window_name, RNG rng );
int Drawing_Random_Circles( Mat image, char* window_name, RNG rng );
int Displaying_Random_Text( Mat image, char* window_name, RNG rng );
int Displaying_Big_End( Mat image, char* window_name, RNG rng );


/**
* @function main
*/

int main( void )
{
int c;

/// Start creating a window
char window_name[] = "Drawing_2 Tutorial";

/// Also create a random object (RNG)
RNG rng( 0xFFFFFFFF );

/// Initialize a matrix filled with zeros
Mat image = Mat::zeros( window_height, window_width, CV_8UC3 );
/// Show it in a window during DELAY ms
imshow( window_name, image );
waitKey( DELAY );

/// Now, let's draw some lines
c = Drawing_Random_Lines(image, window_name, rng);
if( c != 0 ) return 0;

/// Go on drawing, this time nice rectangles
c = Drawing_Random_Rectangles(image, window_name, rng);
if( c != 0 ) return 0;

/// Draw some ellipses
c = Drawing_Random_Ellipses( image, window_name, rng );
if( c != 0 ) return 0;

/// Now some polylines
c = Drawing_Random_Polylines( image, window_name, rng );
if( c != 0 ) return 0;

/// Draw filled polygons
c = Drawing_Random_Filled_Polygons( image, window_name, rng );
if( c != 0 ) return 0;

/// Draw circles
c = Drawing_Random_Circles( image, window_name, rng );
if( c != 0 ) return 0;

/// Display text in random positions
c = Displaying_Random_Text( image, window_name, rng );
if( c != 0 ) return 0;

/// Displaying the big end!
c = Displaying_Big_End( image, window_name, rng );
if( c != 0 ) return 0;

waitKey(0);
return 0;
}

/// Function definitions

/**
* @function randomColor
* @brief Produces a random color given a random object
*/

static Scalar randomColor( RNG& rng )
{
int icolor = (unsigned) rng;
return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
}


/**
* @function Drawing_Random_Lines
*/

int Drawing_Random_Lines( Mat image, char* window_name, RNG rng )
{
Point pt1, pt2;

for( int i = 0; i < NUMBER; i++ )
{
pt1.x = rng.uniform( x_1, x_2 );
pt1.y = rng.uniform( y_1, y_2 );
pt2.x = rng.uniform( x_1, x_2 );
pt2.y = rng.uniform( y_1, y_2 );

line( image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8 );
imshow( window_name, image );
if( waitKey( DELAY ) >= 0 )
{ return -1; }
}

return 0;
}

/**
* @function Drawing_Rectangles
*/

int Drawing_Random_Rectangles( Mat image, char* window_name, RNG rng )
{
Point pt1, pt2;
int lineType = 8;
int thickness = rng.uniform( -3, 10 );

for( int i = 0; i < NUMBER; i++ )
{
pt1.x = rng.uniform( x_1, x_2 );
pt1.y = rng.uniform( y_1, y_2 );
pt2.x = rng.uniform( x_1, x_2 );
pt2.y = rng.uniform( y_1, y_2 );

rectangle( image, pt1, pt2, randomColor(rng), MAX( thickness, -1 ), lineType );

imshow( window_name, image );
if( waitKey( DELAY ) >= 0 )
{ return -1; }
}

return 0;
}

/**
* @function Drawing_Random_Ellipses
*/

int Drawing_Random_Ellipses( Mat image, char* window_name, RNG rng )
{
int lineType = 8;

for ( int i = 0; i < NUMBER; i++ )
{
Point center;
center.x = rng.uniform(x_1, x_2);
center.y = rng.uniform(y_1, y_2);

Size axes;
axes.width = rng.uniform(0, 200);
axes.height = rng.uniform(0, 200);

double angle = rng.uniform(0, 180);

ellipse( image, center, axes, angle, angle - 100, angle + 200,
randomColor(rng), rng.uniform(-1,9), lineType );

imshow( window_name, image );

if( waitKey(DELAY) >= 0 )
{ return -1; }
}

return 0;
}

/**
* @function Drawing_Random_Polylines
*/

int Drawing_Random_Polylines( Mat image, char* window_name, RNG rng )
{
int lineType = 8;

for( int i = 0; i< NUMBER; i++ )
{
Point pt[2][3];
pt[0][0].x = rng.uniform(x_1, x_2);
pt[0][0].y = rng.uniform(y_1, y_2);
pt[0][1].x = rng.uniform(x_1, x_2);
pt[0][1].y = rng.uniform(y_1, y_2);
pt[0][2].x = rng.uniform(x_1, x_2);
pt[0][2].y = rng.uniform(y_1, y_2);
pt[1][0].x = rng.uniform(x_1, x_2);
pt[1][0].y = rng.uniform(y_1, y_2);
pt[1][1].x = rng.uniform(x_1, x_2);
pt[1][1].y = rng.uniform(y_1, y_2);
pt[1][2].x = rng.uniform(x_1, x_2);
pt[1][2].y = rng.uniform(y_1, y_2);

const Point* ppt[2] = {pt[0], pt[1]};
int npt[] = {3, 3};

polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);

imshow( window_name, image );
if( waitKey(DELAY) >= 0 )
{ return -1; }
}
return 0;
}

/**
* @function Drawing_Random_Filled_Polygons
*/

int Drawing_Random_Filled_Polygons( Mat image, char* window_name, RNG rng )
{
int lineType = 8;

for ( int i = 0; i < NUMBER; i++ )
{
Point pt[2][3];
pt[0][0].x = rng.uniform(x_1, x_2);
pt[0][0].y = rng.uniform(y_1, y_2);
pt[0][1].x = rng.uniform(x_1, x_2);
pt[0][1].y = rng.uniform(y_1, y_2);
pt[0][2].x = rng.uniform(x_1, x_2);
pt[0][2].y = rng.uniform(y_1, y_2);
pt[1][0].x = rng.uniform(x_1, x_2);
pt[1][0].y = rng.uniform(y_1, y_2);
pt[1][1].x = rng.uniform(x_1, x_2);
pt[1][1].y = rng.uniform(y_1, y_2);
pt[1][2].x = rng.uniform(x_1, x_2);
pt[1][2].y = rng.uniform(y_1, y_2);

const Point* ppt[2] = {pt[0], pt[1]};
int npt[] = {3, 3};

fillPoly( image, ppt, npt, 2, randomColor(rng), lineType );

imshow( window_name, image );
if( waitKey(DELAY) >= 0 )
{ return -1; }
}
return 0;
}

/**
* @function Drawing_Random_Circles
*/

int Drawing_Random_Circles( Mat image, char* window_name, RNG rng )
{
int lineType = 8;

for (int i = 0; i < NUMBER; i++)
{
Point center;
center.x = rng.uniform(x_1, x_2);
center.y = rng.uniform(y_1, y_2);

circle( image, center, rng.uniform(0, 300), randomColor(rng),
rng.uniform(-1, 9), lineType );

imshow( window_name, image );
if( waitKey(DELAY) >= 0 )
{ return -1; }
}

return 0;
}

/**
* @function Displaying_Random_Text
*/

int Displaying_Random_Text( Mat image, char* window_name, RNG rng )
{
int lineType = 8;

for ( int i = 1; i < NUMBER; i++ )
{
Point org;
org.x = rng.uniform(x_1, x_2);
org.y = rng.uniform(y_1, y_2);

putText( image, "Testing text rendering", org, rng.uniform(0,8),
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);

imshow( window_name, image );
if( waitKey(DELAY) >= 0 )
{ return -1; }
}

return 0;
}

/**
* @function Displaying_Big_End
*/

int Displaying_Big_End( Mat image, char* window_name, RNG )
{
Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
Point org((window_width - textsize.width)/2, (window_height - textsize.height)/2);
int lineType = 8;

Mat image2;

for( int i = 0; i < 255; i += 2 )
{
image2 = image - Scalar::all(i);
putText( image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
Scalar(i, i, 255), 5, lineType );

imshow( window_name, image2 );
if( waitKey(DELAY) >= 0 )
{ return -1; }
}

return 0;
}