ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Best way to render some text on an image?

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

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


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

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-31 08:45:47 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

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

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

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

string text = "Funny text inside the box";
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),
// ... 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):

edit flag offensive delete link more


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 image drewm1980  ( 2015-08-10 08:20:04 -0500 )edit

You're welcome ;-)

cyborg-x1 gravatar image cyborg-x1  ( 2015-08-10 10:19:10 -0500 )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


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

Seen: 762 times

Last updated: Jul 31 '15