简体   繁体   English

OpenCV 3似乎不适用于Qt

[英]OpenCV 3 doesn't seem to work with Qt

I followed this tutorial to install OpenCV to work with Qt. 我按照本教程进行操作,以安装OpenCV与Qt一起使用。 I used OpenCV with Qt before, a couple of years ago, and I had to build both OpenCV and Qt myself. 几年前,我在Qt上使用了OpenCV,我必须自己构建OpenCV和Qt。 I was now happy that a solution exists by building only OpenCV. 现在,我很高兴存在仅通过构建OpenCV解决方案的问题。

CMake, as usual, had a lot of errors. 像往常一样,CMake有很多错误。 I had to disable a bunch of extras, like opencv_apps and examples, but then it built the libraries correctly. 我不得不禁用一些其他功能,例如opencv_apps和示例,但随后它正确地构建了库。 The dynamic libs didn't work, but I planned to use it statically anyway. 动态库不起作用,但是我计划仍然以静态方式使用它。

After linking the OpenCV libraries in Qt, not forgetting that the opening of images has been moved to "imgcodecs" 在Qt中链接OpenCV库后,不要忘记图像的开头已移至“ imgcodecs”

LIBS += -lopencv_core300 -lopencv_highgui300 -lopencv_imgproc300
LIBS += -lopencv_imgcodecs300

I tried a simple program: 我尝试了一个简单的程序:

#include <opencv.hpp>

// ...

cv::Mat image = cv::imread("testimage.png");
cv::namedWindow("Test image");
cv::imshow("Test image", image);
cv::waitKey(1000);

It didn't work. 没用

matrix.cpp:-1: error: undefined reference to `ippicviSum_8u_C1R@20' matrix.cpp:-1:错误:未定义对'ippicviSum_8u_C1R @ 20'的引用

Searching for "ippicviSum" on Google returns absolutely nothing (well, until this question will be crawled by them). 在Google上搜索“ ippicviSum”绝对不会返回任何结果(好吧,直到他们抓到这个问题)。

I know that the libraries at least somewhat work, or are at least found , because if I remove lopencv_imgcodecs300 from the project file, I get an additional error, which wasn't there when the library was included: 我知道这些库至少可以正常工作,或者至少可以找到 ,因为如果我从项目文件中删除lopencv_imgcodecs300 ,我会得到一个附加错误,当包含该库时,该错误不存在:

error: undefined reference to `cv::imread(cv::String const&, int)' 错误:未定义引用`cv :: imread(cv :: String const&,int)'


I gave up on the C++ API, and tried to use the classical C API. 我放弃了C ++ API,并尝试使用经典的C API。 (In my trial 2 years ago, the C++ API also had some problems - the basic opening and displaying worked but the more complicated algorithms gave linker errors, so I resorted to the C API, which worked correctly) (在2年前的试用版中,C ++ API也存在一些问题-基本的打开和显示有效,但更复杂的算法却给出了链接器错误,因此我求助于C API,它可以正常工作)

#include <cv.h>
#include <cvaux.h>
#include <cvwimage.h>
#include <cxcore.h>
#include <highgui.h>

// ...

IplImage *image = cvLoadImage("testimage.png");

The result: 结果:

loadsave.cpp:-1: error: undefined reference to `cv::String::deallocate()' loadsave.cpp:-1:错误:对`cv :: String :: deallocate()'的未定义引用

'ippicviSum_8u_C1R' is the intel performance primitives (ipp) library that now comes with opencv for free. “ ippicviSum_8u_C1R”是现在免费提供的opencv的英特尔性能基元(ipp)库。 It looks like you are missing the download, I normally build opencv from source but if you got an installer you might need to get the Intel library separately. 看来您缺少下载,我通常从源代码构建opencv,但是如果您有安装程序,则可能需要单独获取Intel库。

There is no need to use the cv::String type, just use regular std::string. 无需使用cv :: String类型,只需使用常规的std :: string。 The cv types are there to support old embedded platforms with obsolete c++ compilers. 那里的cv类型可以使用过时的c ++编译器来支持旧的嵌入式平台。 It is possible that some Qt macro is redefining "String" and breaking the code ? 是否有可能某些Qt宏正在重新定义“字符串”并破坏代码?

edit: The problem might also be that you are using the legacy C api. 编辑:问题可能还在于您正在使用旧版C api。 instead do: 而是:

cv::Mat image = cv::imread("testimage.png");

or if you still have an issue, 或者如果您仍然有问题,

cv::Mat image = cv::imread(std::string("testimage.png"));

The problem seemed to be that OpenCV 3.0 is incompatible with the mingw version (491) shipped with Qt 5.4. 问题似乎是OpenCV 3.0与Qt 5.4附带的mingw版本(491)不兼容。

Upgrading to Qt 5.5, which uses mingw 492, solved the problem. 升级到使用mingw 492的Qt 5.5可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM