Sunday, November 04, 2007

OpenCV: use random colors for drawing

Function to create random colors:
CvScalar random_color(CvRNG* rng)
{
int color = cvRandInt(rng);
return CV_RGB(color&255, (color>>8)&255, (color>>16)&255);
}

The applications to use the random colors: line, rectangular, ellipse, polyline, and circle
CvRNG rng;
int line_type = CV_AA;
CvPoint pt1,pt2;
double angle;
CvSize sz;
int arr[2];
arr[0] = 5;
arr[1] =5;

cvLine( image, pt1, pt2, random_color(&rng),
cvRandInt(&rng)%10, line_type, 0 );

cvRectangle( image,pt1, pt2, random_color(&rng),
cvRandInt(&rng)%10-1, line_type, 0);

cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,
random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );

cvPolyLine( image, pt, arr, 2, 1, random_color(&rng),
cvRandInt(&rng)%10, line_type, 0 );

cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );

cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),
cvRandInt(&rng)%10-1, line_type, 0 );

No comments:

Post a Comment