简体   繁体   中英

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. 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.

I work in MacOS Sierra 10.12.4. OpenCV version is 2.4.13. Matlab version is 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`

When I run it in the terminal, It works well.

But when I run it in the matlab, it failed

It failed because Matlab uses its own OpenCV dylib which is incompatible with your app's version. Just use DYLD_INSERT_LIBRARIES (equals to LD_PRELOAD in Linux) to force Matlab to use your app's OpenCV version.

You can find your app's version with otool (equals to ldd in linux)

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

This will give all dylibs your app uses. Open your Matlab with

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

Run your code and this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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