Ask Your Question
3

Is it possible to measure in pixels a character outputed from putText()?

asked 2012-07-09 12:35:57 -0600

Rui Marques gravatar image

updated 2012-07-09 12:38:22 -0600

I know this is a bit weird question but i actually came to a situation where i would need this. So I appreciate the patience if someone knows the answer =)

From these parameteres:

int fontFace, 
double fontScale
int fontThickness 
int fontLineType

Is it possible to calculate the width of a character in pixels?

Note 1: If the font is not of fixed width, one can assume it is the letter 'a' or do any other assumption.

Note 2:The idea is not to have some image processing doing this, the idea is just to do some math with the putText() parameters.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2012-07-10 03:03:54 -0600

Niu ZhiHeng gravatar image

You can refer to my following code.

string text("Any Text You Want To Put!!!");
int baseline=0;
Size text_size = getTextSize(text,FONT_HERSHEY_COMPLEX,0.75,2,&baseline);
Point text_pos(1,image.rows-text_size.height);
Rect text_rect(text_pos.x,text_pos.y-text_size.height-baseline,text_size.width,text_size.height*2);
rectangle(image,text_rect,CV_RGB(0,0,0),CV_FILLED);
putText(image, text, text_pos, FONT_HERSHEY_COMPLEX, 0.75, CV_RGB(255,255,255),2,8);
edit flag offensive delete link more

Comments

Function getTextSize() seems to do what I was looking for. Wasn't aware that such function existed, thank you.

Rui Marques gravatar imageRui Marques ( 2012-07-10 03:57:05 -0600 )edit
1

answered 2012-07-09 14:04:00 -0600

Abid Rahman K gravatar image

updated 2012-07-09 14:05:02 -0600

1 - Put that letter in white color on a black image ( Grayscale or binary) using putText() function,

2 - then find the contour,

3 - find the boundingrect which gives you its width.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-07-09 12:35:57 -0600

Seen: 6,297 times

Last updated: Jul 10 '12