簡體   English   中英

在Matlab中調用OpenCV在C ++中無法讀取圖像

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

我將在C ++程序中使用cv :: imread加載圖像。 當我直接在終端中運行它時,它運行良好。 但是,當我在matlab中使用“系統”或“ dos”調用它時,它總是返回空Mat,而不會出現任何錯誤。

我在MacOS Sierra 10.12.4中工作。 OpenCV版本是2.4.13。 Matlab版本是R2015b。 有人可以幫我嗎? 請給我一些建議。

例如,我編寫一個測試代碼。

#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;
}

然后我用

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

當我在終端中運行它時, 它運行良好。

但是當我在Matlab中運行它時, 它失敗了

它失敗了,因為Matlab使用自己的OpenCV dylib,它與您的應用程序的版本不兼容。 只需使用DYLD_INSERT_LIBRARIES (在Linux中等於LD_PRELOAD )來強制Matlab使用您應用的OpenCV版本。

您可以使用otool查找應用的版本(在Linux中等於ldd

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

這將為您的應用程序使用所有dylib。 使用打開您的Matlab

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

運行您的代碼,這應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM