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

How to overlay text on image when working with cv::Mat type(使用 cv::Mat 类型时如何在图像上覆盖文本)
本文介绍了使用 cv::Mat 类型时如何在图像上覆盖文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

这篇关于使用 cv::Mat 类型时如何在图像上覆盖文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)