opencv 随机生成器和文本

创建一个随机数生成器对象(RNG):

RNG rng(0xFFFFFFFF)
  • 创建一个初始化为零地矩阵(黑色),指定其高度,宽度和类型:
  • Mat image = Mat :: zeros(window_height,window_width,CV_8UC3);
    imshow(window_name,image);

    可以尝试画一些几何图形

  • c = Drawing_Random_Circles(image,window_name,rng);
    if(c!= 0)return 0;
    c = Drawing_Random_Rectangles(image,window_name,rng);
    if(c!= 0)return 0;

    检出函数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 );  //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 ); // randomColor()用于指定颜色
       imshow( window_name, image );
       if( waitKey( DELAY ) >= 0 )
       { return -1; }
      }
      return 0;
    }

    Display_Random_Text和Dispalying_Big_End的功能

  • Display_Random_Text

  1. 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;
    }

    Dispalying_Big_End

int Displaying_Big_End( Mat image, char* window_name, RNG 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); //,image2是图像和Scalar :: all(i)的减法。事实上,这里发生的一切是,image2的每个像素都将减去图像的每个像素减去i的值(记住,对于每个像素,我们考虑三个值,如R,G和B,因此每个像素会受到影响)
    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;
}

测试: 

 

你可能感兴趣的:(图像处理)