简体   繁体   English

从 QT 资源加载 openCV Haarcascade

[英]Loaad a openCV Haarcascade from QT ressources

I want to add the openCV cascades to the ressource managment of QT.我想将 openCV 级联添加到 QT 的资源管理中。 But i'm failed.但我失败了。

I've tested this code:我测试了这段代码:

// load cascades
QUrl *location_cascade= new QUrl("qrc:/cascades/haarcascade_frontalface_alt.xml");
std::string file=location_cascade->toString().toStdString();
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( file);

But than the compiler said:但比编译器说的:

cannot convert 'std::string* {aka std::basic_string<char>*}' to 'const char*' for argument '1' to 'void* cvLoad(const char*, CvMemStorage*, const char*, const char**)'

Has someone an idea?有人有想法吗?

Update Currently the file is in the source folder of the project.更新当前文件位于项目的源文件夹中。 Can i access this file by relative path?我可以通过相对路径访问这个文件吗? maybe by a constant that points to the src.dir?也许通过一个指向 src.dir 的常量?

greetings问候

Compiler tells you that first argument of cvLoad should be const char* , but you are passing std::string .编译器告诉您cvLoad第一个参数应该是const char* ,但您正在传递std::string You need to call it like cvLoad(file.c_str()) .您需要像cvLoad(file.c_str())一样调用它。

But even if you manage to compile that code it will probably not work, as openCV knows nothing about Qt resources and won't be able to open the file with that name.但即使您设法编译该代码,它也可能无法工作,因为 openCV 对 Qt 资源一无所知,也无法打开具有该名称的文件。

Even so, this question is 2 years old, I want to share the workaround I used.即便如此,这个问题已经有 2 年历史了,我想分享我使用的解决方法。

QString sPath = QCoreApplication::applicationDirPath().append("/frontalface.xml")
QFile::copy(":/android-sources/OpenCV/haarcascade_frontalface_default.xml" , sPath);
cv::CascadeClassifier faceCascade = cv::CascadeClassifier(sPath.toStdString());
QFile::remove(sPath);

It works, but you have to go the detour over the local harddrive.它有效,但您必须绕道本地硬盘驱动器。 Still usefull for android applications.对于android应用程序仍然有用。

using namespace cv;    
QFile xmlRes(":/new/cascades/res/cascade.xml");
    if (xmlRes.open(QIODevice::ReadOnly)) {
        QString data(xmlRes.readAll());
        FileStorage fs(data.toStdString().c_str(), FileStorage::MEMORY | FileStorage::FORMAT_XML);
        if (!cascade.read(fs["cascade"]))
            qDebug() << "ERROR: Could not read classifier cascade.";
    }

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

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