#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;
}