随机产生器和文本

目标

  • Use the Random Number generator class (cv::RNG ) and how to get a random number from a uniform distribution.
  • Display text on an OpenCV window by using the function cv::putText

代码

int cv::waitKey ( int delay = 0 ): 返回按键值或者-1(时间走完仍没有按键)

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

这样根据返回值,随时可以结束。
RNG rng( 0xFFFFFFFF ):RNG implements a random number generator. In this example, rng is a RNG element initialized with the value 0xFFFFFFFF

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

一个window可以持续写入多个imshow,仍是同一个Mat。

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

Mat变了。

你可能感兴趣的:(随机产生器和文本)