C++ 使用 cv::Mat 类型时如何在图像上叠加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5175628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to overlay text on image when working with cv::Mat type
提问by Aras
I am using opencv 2.1. In my code I have a few images stored as Mat objects initialized like this:
我正在使用 opencv 2.1。在我的代码中,我将一些图像存储为 Mat 对象,初始化如下:
Mat img1 = imread("img/stuff.pgm", CV_LOAD_IMAGE_GRAYSCALE);
I can display them properly using imshow() after my matrix operations are done. Now I want to add some text on the image to describe what has happened. Looking at the documentation it seems like cvPutText()
would be the function I need. But when I try something like this:
完成矩阵运算后,我可以使用 imshow() 正确显示它们。现在我想在图像上添加一些文字来描述发生了什么。查看文档似乎cvPutText()
是我需要的功能。但是当我尝试这样的事情时:
cvPutText(result, "Differencing the two images.", cvPoint(30,30), &font, GREEN);
cvPutText(result, "Differencing the two images.", cvPoint(30,30), &font, GREEN);
I get the following compile error:
error: cannot convert ‘cv::Mat' to ‘CvArr*' for argument ‘1' to ‘void cvPutText(CvArr*, const char*, CvPoint, const CvFont*, CvScalar)'
我收到以下编译错误:
error: cannot convert ‘cv::Mat' to ‘CvArr*' for argument ‘1' to ‘void cvPutText(CvArr*, const char*, CvPoint, const CvFont*, CvScalar)'
What do I need to do to be able to add some text when displaying this image?
我需要做什么才能在显示此图像时添加一些文本?
回答by Aras
I was looking at the wrong place. I found the answer in the newer OpenCV documentation for cpp. There is a new function called putText() that accepts cv::Mat objects. So I tried this line and it works:
我看错地方了。我在cpp的较新OpenCV 文档中找到了答案。有一个名为 putText() 的新函数,它接受 cv::Mat 对象。所以我尝试了这条线并且它有效:
putText(result, "Differencing the two images.", cvPoint(30,30),
FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200,200,250), 1, CV_AA);
Hope this helps someone.
希望这可以帮助某人。
回答by Stan James
For C++ basic use:
对于 C++ 基本用途:
cv::putText(yourImageMat,
"Here is some text",
cv::Point(5,5), // Coordinates
cv::FONT_HERSHEY_COMPLEX_SMALL, // Font
1.0, // Scale. 2.0 = 2x bigger
cv::Scalar(255,255,255), // BGR Color
1, // Line Thickness (Optional)
cv::CV_AA); // Anti-alias (Optional)
See putText()in OpenCV docs.
请参阅OpenCV 文档中的putText()。
回答by Bilal Saeed
putText(result, "Differencing the two images.", cvPoint(30,30),
FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200,200,250), 1, CV_AA);
In the above line "result" should be a cvArr* or an IplImage*. but from the code provided here, I guess you are passing a cv::Mat object. So, you either need to convert it using cvarrToMat() or pass &result instead of result.
在上面的行中,“结果”应该是一个 cvArr* 或一个 IplImage*。但是从这里提供的代码来看,我猜你正在传递一个 cv::Mat 对象。因此,您要么需要使用 cvarrToMat() 转换它,要么传递 &result 而不是 result。
Hope it helps
希望能帮助到你
回答by RawMean
You can also do the following to print text and variables.
您还可以执行以下操作来打印文本和变量。
std::ostringstream str;
str << "Here is some text:" << myVariable;
cv::putText(image, cv::Point(10,10), str.str(), CV_FONT_HERSHEY_PLAIN, CV_RGB(0,0,250));
回答by agodinhost
One nasty detail that I saw into my test code: pay attention into the import stament - it is not displayed into most examples and it needs to be the right import.
我在测试代码中看到的一个令人讨厌的细节:注意导入语句 - 大多数示例中都没有显示它,它需要是正确的导入。
My test code used only the putText sample above and I did included the imgproc.h just as I did into some of my oldcode. The code compiled and linked fine however I was facing one weirdy behaviour with the putText (some garbage into my image).
我的测试代码只使用了上面的 putText 示例,并且我确实包含了 imgproc.h,就像我在一些旧代码中所做的那样。代码编译并链接得很好,但是我遇到了 putText 的一种奇怪行为(我的图像中有一些垃圾)。
It was a PITA until I figured out that the import was messing up with my social life ...
这是一个 PITA,直到我发现导入扰乱了我的社交生活......
imageText.cpp
图像文本文件
#include "Imaging/imageText.h"
#include "Commons/xLog.h"
#include "opencv2/imgproc.hpp" // << Seems to work right?
using namespace cv;
namespace imaging
{
inline Mat image2mat( SmartImage image ) NOEXCEPTION
{
//TODO: hard coded to work only with CV_8UC3, see the other cases ...
Mat mat(
Size( image->WIDTH, image->HEIGHT ),
CV_8UC3,
image->buffer,
Mat::AUTO_STEP
);
return mat;
}
inline void _writeText_( SmartImage image, const string TEXT )
{
Mat mat( image2mat( image ) );
string text = "Funny text inside the box";
int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontScale = 2;
int thickness = 3;
Point textOrg( 10, 130 );
putText( mat, text, textOrg, fontFace, fontScale, Scalar::all( 255 ), thickness, 8 );
}
const bool writeText( SmartImage image, const string text ) NOEXCEPTION
{
try
{
_writeText_( image, text );
return true;
}
catch( cv::Exception& e )
{
LOG_ERROR( "writeText OpenCV ERROR: " << e.what() << "!" );
}
catch( ... )
{
LOG_ERROR( "writeText ERROR!" );
}
return false;
}
}
Then I just changed the imgproc import above to
然后我只是将上面的 imgproc 导入更改为
#include <opencv2/opencv.hpp> // << It does includes ALL opencv stuff
My 5 cents.
我的5美分。
回答by George B
putText(img1, "TextString123", cvPoint(50,200), FONT_HERSHEY_SCRIPT_SIMPLEX, 2.5, cvScalar(255,0,0,255), 3, CV_AA);
putText(img1, "TextString123", cvPoint(50,200), FONT_HERSHEY_SCRIPT_SIMPLEX, 2.5, cvScalar(255,0,0,255), 3, CV_AA);
You can find more information here: http://docs.opencv.org/2.4.9/modules/core/doc/drawing_functions.html
您可以在此处找到更多信息:http: //docs.opencv.org/2.4.9/modules/core/doc/drawing_functions.html
The main diference between this answer and the answers from above is the value of the 7-th parameter, the thickness level. With thickness==1 this function have not worked for me.
这个答案与上面的答案之间的主要区别是第 7 个参数的值,即厚度水平。厚度==1 这个功能对我不起作用。