简体   繁体   English

在Matlab中调用OpenCV在C ++中无法读取图像

[英]Fail to read an image with opencv in C++ when call it in matlab

I'm going to load an image using cv::imread in a C++ program. 我将在C ++程序中使用cv :: imread加载图像。 When I run it directly in the terminal, it works well. 当我直接在终端中运行它时,它运行良好。 But when I call it in matlab with "system" or "dos", it always return empty Mat without any error. 但是,当我在matlab中使用“系统”或“ dos”调用它时,它总是返回空Mat,而不会出现任何错误。

I work in MacOS Sierra 10.12.4. 我在MacOS Sierra 10.12.4中工作。 OpenCV version is 2.4.13. OpenCV版本是2.4.13。 Matlab version is R2015b. Matlab版本是R2015b。 Could anyone help me? 有人可以帮我吗? Please send me some suggestions. 请给我一些建议。

For example, I write a test code. 例如,我编写一个测试代码。

#include <opencv2/opencv.hpp>
int main(){
    std::string  path = "/Users/zhefeng.wzf/0001.jpg";
    cv::Mat image = cv::imread(path);

    if(image.empty()){
        printf("Cannot load image:%s\n",path.c_str());
    }else{
        printf("Load the image successfully.\n");
        cv::imshow("image",image);
        cv::waitKey();
    }

    return 0;
}

and then I compile it with 然后我用

g++ readImg.cpp -o readImg `pkg-config --cflags --libs opencv` g ++ readImg.cpp -o readImg`pkg-config --cflags --libs opencv`

When I run it in the terminal, It works well. 当我在终端中运行它时, 它运行良好。

But when I run it in the matlab, it failed 但是当我在Matlab中运行它时, 它失败了

It failed because Matlab uses its own OpenCV dylib which is incompatible with your app's version. 它失败了,因为Matlab使用自己的OpenCV dylib,它与您的应用程序的版本不兼容。 Just use DYLD_INSERT_LIBRARIES (equals to LD_PRELOAD in Linux) to force Matlab to use your app's OpenCV version. 只需使用DYLD_INSERT_LIBRARIES (在Linux中等于LD_PRELOAD )来强制Matlab使用您应用的OpenCV版本。

You can find your app's version with otool (equals to ldd in linux) 您可以使用otool查找应用的版本(在Linux中等于ldd

otool -L /path/to/your/app/app

This will give all dylibs your app uses. 这将为您的应用程序使用所有dylib。 Open your Matlab with 使用打开您的Matlab

DYLD_INSERT_LIBRARIES="path/to/dylib.dylib:another/path/dylib2.dylib" /path/to/matlab

Run your code and this should work. 运行您的代码,这应该可以工作。

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

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