【问题标题】:Put variable on the image using opencv使用opencv将变量放在图像上
【发布时间】:2016-06-20 20:21:14
【问题描述】:

我有以下问题:

我想在图像上打印变量,我知道OpneCV中有一个函数putText,但是这个函数只能打印我一开始设置的文本。我要打印的内容如下:

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

我想打印图像“img”上的每个 i 值,这意味着 img 需要显示从 0 到 10 的 i。 是否有任何功能可以在图像上打印变量而不是设置的单词?

【问题讨论】:

标签: image function opencv text mat


【解决方案1】:
#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);

【讨论】:

    【解决方案2】:

    documentation 已经为您提供了一个广泛的示例:

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

    步骤:基本使用putText

    1. 将文本放入字符串
    2. 设置文本原点(=文本的左下角;使用 Point)
    3. 设置比例、粗细、字体

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      相关资源
      最近更新 更多