使用 cv::Mat 类型时如何在图像上覆盖文本

2022-01-09 00:00:00 opencv computer-vision c++

我正在使用 opencv 2.1.在我的代码中,我将一些图像存储为 Mat 对象,初始化如下:

I am using opencv 2.1. In my code I have a few images stored as Mat objects initialized like this:

Mat img1 = imread("img/stuff.pgm", CV_LOAD_IMAGE_GRAYSCALE);

矩阵运算完成后,我可以使用 imshow() 正确显示它们.现在我想在图像上添加一些文字来描述发生了什么.查看文档似乎 cvPutText() 将是我需要的功能.但是当我尝试这样的事情时:

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:

cvPutText(result, "区分两个图像.", cvPoint(30,30), &font, GREEN);

我收到以下编译错误: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?

推荐答案

我看错地方了.我在较新的 OpenCV 文档 cpp 中找到了答案.有一个名为 putText() 的新函数接受 cv::Mat 对象.所以我尝试了这条线并且它有效:

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:

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.

相关文章