Ask Your Question

puttext with black background

asked 2014-02-05 01:51:16 -0600

Flart gravatar image

updated 2017-08-01 11:29:19 -0600

I want to write value on picture using puttext with black background. Code:

rectangle(frame, Rect(
     Point(50,50) + Point(0,-height), 
     Point(50,50) + Point(width,0)), 
putText(frame, Tostr(value), Point(50,50),1,1,Scalar(255,255,255),2);

But change of capacity value leads change of required width. How this width is determined, or did I miss some params of putText, which resolve my issue?

edit retag flag offensive close merge delete


Actually your question is quite unclear to me. You are drawing a rectangle based on the width of the text that will be displayed on it. The width of putText is determined by the amount of characters that need to be displayed. Take a standard distance for each character (just do some tests to get a good guess) and use that as size? How are you calculating width and heigth for the moment?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-05 02:17:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-02-05 02:44:39 -0600

updated 2014-02-05 02:46:02 -0600

void setLabel(cv::Mat& im, const std::string label, const cv::Point & or)
    int fontface = cv::FONT_HERSHEY_SIMPLEX;
    double scale = 0.4;
    int thickness = 1;
    int baseline = 0;

    cv::Size text = cv::getTextSize(label, fontface, scale, thickness, &baseline);
    cv::rectangle(im, or + cv::Point(0, baseline), or + cv::Point(text.width, -text.height), CV_RGB(0,0,0), CV_FILLED);
    cv::putText(im, label, or, fontface, scale, CV_RGB(255,255,255), thickness, 8);

cv::Mat src = cv::imread(.......);
setLabel(src,"here is the text to display", cvPoint(10,10));
edit flag offensive delete link more


Thank you, I have missed that cv::getTextSize function :)

Flart gravatar imageFlart ( 2014-02-05 03:32:14 -0600 )edit

Question Tools


Asked: 2014-02-05 01:51:16 -0600

Seen: 16,589 times

Last updated: Feb 05 '14