2

I have a question following:

I want to print the variable on the image, I know there is a function putText in OpneCV, but this function can only print the text I set at the begining. The thing I want to print is as follow:

Mat img;
for(int i=0; i<=10; i++)
{
 imshow("IMG",img);
}

I want to print every i value on the image "img", which means img need to show i from 0 to 10. Is there any function that I can print the variable on the image instead of the set word ?

HenryChen
  • 153
  • 3
  • 12
  • Can you please be more clear? – Miki Mar 07 '16 at 11:30
  • Convert the value to a string first: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ or http://stackoverflow.com/questions/2125880/converting-a-float-to-stdstring-in-c – SpamBot Mar 07 '16 at 12:04

2 Answers2

2

The documentation gives you already an extensive example:

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);

Steps to do: basically use putText

  1. Put text into string
  2. Set text origin (=lower left corner of the text; use Point)
  3. Set scale, thickness, font
holzkohlengrill
  • 1,044
  • 19
  • 29
0
#include <stdio.h>
int your_variable=6;
Mat img=your_image;
/*Your code here*/
char no[5];
sprintf(no,"Variable = %d",your_variable);
putText(img,no,Point(10,30),FONT_HERSHEY_TRIPLEX,1,Scalar(255,255,255),1);
imshow("with variable",img);
Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39