随机数发生器&绘制文字-----学习记录(6)

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
#define window_high 500
#define window_width 800
#define NUMBER 10
#define DELAY 500
#define x_1 1
#define x_2 100
#define y_1 1
#define y_2 400

int Drawing_Random_lines( Mat, char *, RNG );
static Scalar randomColor( RNG );
int Drawing_Random_Rectangles( Mat, char*, RNG );
int Displaying_Random_Text( Mat, char*, RNG );

int main(int argc, char* argv[])
{
	int c;
	RNG rng( 0xFFFFFFFF );
	char* name1 = "suijishu";
	namedWindow( name1, 1 );
	Mat map = Mat::zeros( window_high, window_width, CV_8UC3 );
	c = Drawing_Random_lines( map, name1, rng );
	if( c != 0 ) return 0;
	c = Drawing_Random_Rectangles( map , name1, rng );
	if( c != 0 ) return 0;
	c = Displaying_Random_Text( map , name1, rng );
	if( c != 0 ) return 0;
	imshow( name1, map );
	waitKey(0);
	
	return 0;
}


int Drawing_Random_lines( Mat map, char* window_name, RNG rng ){
	int lineType = 8;
	Point p1, p2;
	for ( int i = 0; i < NUMBER; i++ )
	{	
		p1.x = rng.uniform( x_1, x_2 );
		p1.y = rng.uniform( y_1, y_2 );
		p2.x = rng.uniform( x_2, x_2 );
		p2.y = rng.uniform( y_1, y_2 );
		line( map, p1, p2, randomColor( rng.uniform( 1, 10 ) ), lineType );
		imshow( window_name, map );
	if ( waitKey( DELAY ) >= 0 )
		{
			return -1;
		}
	}
	
	
	return 0;
}

static Scalar randomColor( RNG rng ){
	int icolor = ( unsigned )rng;
	return Scalar( icolor & 255, ( icolor >> 8 )&255, ( icolor >> 16)&255 ); //限制在0-255之间
}

int Drawing_Random_Rectangles( Mat map, char* window_name, RNG rng ){
	int lineType = 8;
	Point p1, p2;
	for ( int i = 0; i < NUMBER; i++ )
	{	
		p1.x = rng.uniform( x_1, x_2 );
		p1.y = rng.uniform( y_1, y_2 );
		p2.x = rng.uniform( x_2, x_2 );
		p2.y = rng.uniform( y_1, y_2 );
		rectangle( map, p1, p2, randomColor( rng.uniform( 1, 10 ) ), lineType );
		imshow( window_name, map );
		if ( waitKey( DELAY ) >= 0 )
		{
			return -1;
		}
	}
	return 0;
}

int Displaying_Random_Text( Mat map, 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( map, "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, map );
		if( waitKey( DELAY ) >= 0 ) //是说50ms中按任意键进入此if块,作用是刷新图像
		{ return -1; }
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值