C++ 使用 OpenCV putText 在图像上打印时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9204953/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:29:09  来源:igfitidea点击:

Error when printing on an image using OpenCV putText

c++opencv

提问by bsidd

I am trying to print text on an image (cv::Mat) using cv::putText

我正在尝试cv::Mat使用以下方法在图像 ( )上打印文本cv::putText

string text = "Funny text inside the box";
int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontScale = 2;
int thickness = 3;  
cv::Point textOrg(10, 130);
cv::putText(img, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness,8);

But this results in an access violation error at runtime. While

但这会在运行时导致访问冲突错误。尽管

cv::putText(img, "text", textOrg, fontFace, fontScale, Scalar::all(255), thickness,8);

results in "???text" being printed on the image. Any idea what could be going wrong?

导致“???text”被打印在图像上。知道可能出了什么问题吗?

回答by Julia Schwarz

I am not sure what the correct protocol for this is, but I had the exact same problem as @arbguy and my fix was because of @Vlad's comment. From @Vlad's comment I checked to see if I was using the correct (debug) libraries for my Debug build. I was not. Setting the libraries to the debug version of of libraries fixed my error and I was able to use cv::putText correctly. Here's what I did (using visual studio 2012):

我不确定正确的协议是什么,但我遇到了与 @arbguy 完全相同的问题,我的修复是因为 @Vlad 的评论。从@Vlad 的评论中,我检查了我的调试版本是否使用了正确的(调试)库。我不是。将库设置为库的调试版本修复了我的错误,我能够正确使用 cv::putText。这是我所做的(使用 Visual Studio 2012):

  1. Set all of my openCV .lib references to the "d.lib" versions. Your references will be different based on which libraries you are using but here are my references:

    opencv_imgproc242d.lib
    opencv_core242d.lib
    opencv_highgui242d.lib
    
  2. I then had to copy over the correct .dll libraries to my executable directory. I actually have a post-build step that copies the necessary .dll files into the binary directory. I also had to copy "tbb_debug.dll"as well. This is located in %OPENCVDIR%\build\common\tbb\ia32\vc10(for my 32-bit build). the final list of .dll files I had to copy over was: opencv_imgproc242d.dll opencv_core242d.dll opencv_highgui242d.dll

  1. 将我所有的 openCV .lib 引用设置为“d.lib”版本。根据您使用的库,您的参考资料会有所不同,但这里是我的参考资料:

    opencv_imgproc242d.lib
    opencv_core242d.lib
    opencv_highgui242d.lib
    
  2. 然后我不得不将正确的 .dll 库复制到我的可执行目录中。我实际上有一个构建后步骤,将必要的 .dll 文件复制到二进制目录中。我也不得不复制"tbb_debug.dll"。它位于%OPENCVDIR%\build\common\tbb\ia32\vc10(对于我的 32 位版本)。我必须复制的 .dll 文件的最终列表是: opencv_imgproc242d.dll opencv_core242d.dll opencv_highgui242d.dll

After doing this everything worked! NOTE: I am not a C++ expert so if anybody has a better suggestion for doing this I'm happy. Also, I am not sure how to properly credit @Vlad for actually solving this problem since he is the one that suggested the correct solution.

这样做后一切正常!注意:我不是 C++ 专家,所以如果有人有更好的建议,我很高兴。另外,我不确定如何正确地归功于@Vlad 实际解决了这个问题,因为他是提出正确解决方案的人。