【问题标题】:OpenCV: Text does not display when using the Qt library with cvFontQt and cvAddText on Windows 10 in VS2015 x64 buildOpenCV:在 VS2015 x64 版本中在 Windows 10 上使用带有 cvFontQt 和 cvAddText 的 Qt 库时不显示文本
【发布时间】:2016-06-15 23:16:10
【问题描述】:

我正在尝试使用 OpenCV 从文本创建图像。图像可能有也可能没有需要与其合并的叠加层。文本的字体需要与 OpenCV 的 C++ 接口自带的 HERSHEY 文本不同。我在this post 和其他人看到它可以使用 cvFontQt 和 cvAddText 来完成。

我正在使用 OpenCV 2.4.13 和 Qt 5.6。在下面的示例代码中,我可以使用 cvPutTextcvFont 和 HERSHEY 输出第一行和第三行,但是第二行使用 cvAddText cvFontQt 不显示。

#include "stdafx.h"
#include "opencv2\core\core_c.h"        
#include "opencv2\highgui\highgui_c.h"  

int main()
{
    // Create a window for a container to hold the image
    cvNamedWindow("cvtest", CV_WINDOW_AUTOSIZE);

    IplImage *img = cvCreateImage(cvSize(600, 300), IPL_DEPTH_64F, 4);

    // Set the image background to white
    cvSet(img, cvScalar(255, 255, 255));

    CvFont font;

    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
    cvPutText(img, "cvInitFont; cvPutText: First line.", cvPoint(4, 30), &font, cvScalar(255));

    CvFont fontqt = cvFontQt("Courier New", -1, cvScalar(0,0,255), CV_FONT_NORMAL, CV_STYLE_NORMAL, 0);
    cvAddText(img, "cvFontQV; cvAddText:   Second line.", cvPoint(4, 60), &fontqt);

    cvPutText(img, "cvInitFont; cvPutText: Third line.", cvPoint(4, 90), &font, cvScalar(255));

    cvShowImage("cvtest", img);

    cvWaitKey(0);

    cvSaveImage("C:\\OpenCvTest64F.jpg", img);

    // Cleanup
    cvDestroyAllWindows();
    cvReleaseImage(&img);

    return 0;
}

生成的图像(“C:\OpenCvTest64F.jpg”):

Output Image

我是否错误地使用了 cvFontQt 或 cvAddText?关于为什么它没有出现在图像中的任何想法?

【问题讨论】:

    标签: c++ qt opencv


    【解决方案1】:

    您使用浮点图像的任何具体原因?如果不是,请尝试将图像初始化调整为:

    IplImage *img = cvCreateImage(cvSize(600, 300), IPL_DEPTH_8U, 3);
    

    使用 3 通道 8 位图像似乎可以解决问题。

    请注意 - 为浮点图像分配颜色时,该值应在 [0, 1] 范围内,而不是 [0, 255] 范围内。另外,C++ api 更好!

    【讨论】:

    • 谢谢弗里克!!那成功了。我同意 C++ api 更好。如果我不需要等距字体,我会使用它。
    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    相关资源
    最近更新 更多