Ana
Ana

Reputation: 51

How to link OpenCV with MATLAB for macOS

I am trying to run C++ code that uses the OpenCV library with MATLAB and mex command (the code already has a mexFunction).

I already have the OpenCV library installed, I used it with Python before. I assume that it should work for running C++ code. I am running this command to compile the code on MATLAB:

mex -setup
mex fileName.cpp -L/usr/local/lib -I/usr/local/include/

and I also tried:

mex fileName.cpp -L/usr/local/lib -I/usr/local/include/ -llibopencv_core

But I got different errors and it does not compile successfully, such as:

error: non-constant-expression cannot be narrowed from type 'int' to 'unsigned long' in initializer list [-Wc++11-narrowing]

note: insert an explicit cast to silence this issue const long unsigned int

I feel that there is something wrong with linking. How can I link OpenCV with MATLAB to run C++ code using the Clang compiler on macOS?

in c++ code: #include <opencv2/opencv.hpp>

and opencv2 is inside -I/usr/local/include/

inside /usr/local/lib , I can only find these opencv libraries

Upvotes: 1

Views: 178

Answers (1)

Ana
Ana

Reputation: 51

1- The fix was by using the correct paths to the opencv library and Include folder, since I have on my computer several files for opencv that are installed using pip for python projects, I change the path to opencv folder that installed by brew. 2- I also added all the needed -l files not only -llibopencv_core. By the way, I needed to remove (lib) form llibopencv_core so it becomes like this -lopencv_core so the compiler (clang) can see it.

3- Finally, to silent the narrowing error, I made some changes on types of the variable to make it consistence and that's all, (however, some compilers consider it as warning and can work fine without any changes).

Upvotes: 1

Related Questions