Ask Your Question
0

Best way to render some text on an image?

asked 2015-07-31 06:53:27 -0600

drewm1980 gravatar image

What is/are the recommended way(s) to render some text onto an image message in ROS Indigo / Ubuntu 14.04?

I tried opencv/highgui/addText/fontQt, but that introduces an opencv compile-time dependency on qt, which is not switched on in the Debian/Ubuntu OpenCV builds which ROS depends on.

One solution is to build opencv from source and switch on the QT support, but I am worried that will be a bit of a nightmare, since the Debian opencv version is used by a bunch of stuff on my system, and ripping it out will probably break a bunch of other stuff in my ROS install.

I know I can go shop around for a generic font rendering library, but maybe there is already something that already works with ROS image messages out of the box. I am working in Ubuntu 14.04.

edit retag flag offensive close merge delete

Comments

Why should you need Qt for OpenCV? I do not get it... (well except of course using fontQt)

cyborg-x1 gravatar imagecyborg-x1 ( 2015-07-31 08:45:47 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2015-07-31 08:48:02 -0600

updated 2015-07-31 08:49:44 -0600

Maybe do it like this, it shouldn't require Qt:

http://docs.opencv.org/modules/core/d...

string text = "Funny text inside the box";
int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontScale = 2;
int thickness = 3;

Mat img(600, 800, CV_8UC3, Scalar::all(0));

int baseline=0;
Size textSize = getTextSize(text, fontFace,
                            fontScale, thickness, &baseline);
baseline += thickness;

// center the text
Point textOrg((img.cols - textSize.width)/2,
              (img.rows + textSize.height)/2);

// draw the box
rectangle(img, textOrg + Point(0, baseline),
          textOrg + Point(textSize.width, -textSize.height),
          Scalar(0,0,255));
// ... and the baseline first
line(img, textOrg + Point(0, thickness),
     textOrg + Point(textSize.width, thickness),
     Scalar(0, 0, 255));

// then put the text itself
putText(img, text, textOrg, fontFace, fontScale,
        Scalar::all(255), thickness, 8);

And for going to opencv use(if you do not already know):

http://wiki.ros.org/cv_bridge

edit flag offensive delete link more

Comments

Thanks! I was looking at documentation highgui (which is broken in debian stable), and which doesn't link back to the parts of the old (but actually working) C API font stuff.

drewm1980 gravatar imagedrewm1980 ( 2015-08-10 08:20:04 -0600 )edit

You're welcome ;-)

cyborg-x1 gravatar imagecyborg-x1 ( 2015-08-10 10:19:10 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-07-31 06:53:27 -0600

Seen: 507 times

Last updated: Jul 31 '15