Thursday, July 28, 2005

OpenCV: Font, Add text to image

char buffer [50];

cvNamedWindow("text", HG_AUTOSIZE);

cvMoveWindow( "text", 510, 150 );

sprintf (buffer, "[%d] Mouse position:(%d, %d) ||",n_blob, x,y);

CvFont font;

cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX_SMALL, .6, .6, 0, 1, 6);

cvPutText( textImg, buffer, cvPoint(5,20+down), &font, CV_RGB(255,255,0) );

Another example to show text:
char wndname[] = "Demo";
int line_type = CV_AA;
int y = 0;
CvPoint pt1;
CvFont font;
CvSize text_size;
int width = 1000, height = 700;
IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
IplImage* image2;

cvNamedWindow(wndname, 1 ); // create window
cvZero( image );
cvShowImage(wndname,image);

cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );
cvGetTextSize( "OpenCV !", &font, &text_size, &y );

pt1.x = (width - text_size.width)/2;
pt1.y = (height + text_size.height)/2;
image2 = cvCloneImage(image);

for( i = 0; i < 255; i++ )
{
cvSubS( image2, cvScalarAll(i), image, 0 );
cvPutText( image, "OpenCV!", pt1, &font, CV_RGB(255,i,i));
cvShowImage(wndname,image);
cvWaitKey(5);
}
cvReleaseImage(&image);
cvReleaseImage(&image2);
cvDestroyWindow(wndname);

sprintf: One way to change integer to characters.

itoa: Convert integer to string. atoi, atof

fopen: "w+"(create new(will remove exist) file), "r+"(create, otherwise append) -- In Matlab: 'w+','a+', 'wt' means "write text"

No comments:

Post a Comment