Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV - cvPutText is adding noise to my images

Tags:

opencv

I am completely new with OpenCV. I started reading Learing OpenCV a few days ago and now I would like to do some tests.

I wanted to program an ASCII art conversor. I have done the image iteration and grouped pixels by squares to calculate each region brightness to associate it to a respective character. So now I need to know how bright is a character in order to link each image region to a character. My idea was to make another program that gives me the brightness of some characters. The approach is as follows:

  • Create an image of size 100*100 (for example) - black background and 1 channel
  • Draw the character somewhere in it in white color (cvPutText)
  • Iterate the image and count the number of white pixels
  • Normalize the result

I think that my program is ok, but I'm getting strange results... Just to check it, I am displaying the images with each character and something strange is going on. The image shows what I am talking about.

cvPutText() result

This is the code I am using:

... 
char c = 'a'; //or whatever
IplImage *img = cvCreateImage(cvSize(150, 150), IPL_DEPTH_8U, 1);

char buffer[7];
sprintf(buffer, "%c", c);

CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
cvPutText(img, buffer, cvPoint(2, 15), &font, cvScalar(255));

//iteration to calculate brightness will go here

cvNamedWindow(buffer);
cvShowImage(buffer, img);
cvMoveWindow(buffer, 50*(c-first), 20);
...

I am developing on MacOS Lion (if it helps...). Thank you for reading (and helping!).

like image 729
Ricard Pérez del Campo Avatar asked Nov 13 '22 09:11

Ricard Pérez del Campo


1 Answers

You did not initialize the image content: you've just allocate the memory and you see some kind of memory garbage.

Just add cvSet(img, cvScalarAll(0)) to set your image to black.

like image 91
cedrou Avatar answered Jan 11 '23 13:01

cedrou