Matlab mex wrappers within opencv - matlab

I am trying to use Matlab mex wrappers provided by the OpenCV contrib repository https://github.com/opencv/opencv_contrib/tree/master/modules/matlab.
During the OpenCV installation everything goes fine and cmake can find Matlab installation. My OS is ubuntu 16.04 64 bit and I am using Matlab R2015b.
After successful installation, in order to solve conflicts due to internal dynamic libraries, I have put libstdc++ or libgcc_s in the LD_PRELOAD variable before launching matlab. Moreover, based on opencv documentation (opencv_contrib/modules/matlab/) I have added the binding to the matlab path using "addpath('/path/to/opencv/matlab/');"
after running the following code (provided in the opencv documentation):
% load an image (Matlab)
I = imread('cameraman.tif');
% compute the DFT (OpenCV)
If = cv.dft(I, cv.DFT_COMPLEX_OUTPUT);
My code works fine, but whenever I use
I=imread('cameraman.tif');
cv.imshow('test',I)
or
cv.imread('cameraman.tif');
(or I think all modules provided in the opencv highgui module), I face with the following error in matlab:
Invalid MEX-file '/usr/local/opencv-3.1.0/matlab/+cv/imread.mexa64': /usr/lib/libgdal.so.1:
undefined symbol: _ZN11xercesc_3_111InputSource11setEncodingEPKt
I searched a lot to solve the problem, but I couldn't achieve any success. I also tried to build opencv with the flag -D WITH_GDAL=OFF during the opencv cmake installation, but not only it solves the problem, but also makes matlab crash after calling the above-mentioned codes.

Related

Missing file .mex error on a newer matlab version

I have a .mex code that properly works on Matlab 2012b (32bit).
However, I would like to make it work also on Matlab 2019b (64bit).
When I run it in Matlab 2019, even though the directory is introduced in the path, the error that I get is:
Missing file directm.mex
Have somebody encountered this kind of problem?
Specifically, the toolbox is "forcefeedback toolbox"
https://de.mathworks.com/matlabcentral/fileexchange/4939-force-feedback-toolbox-for-matlab-simulink
Many thanks!
Your MEX-file is a 32-bit binary. You cannot combine it with a 64-bit binary (the newer MATLAB program). The only way to get this to work is to recompile the MEX-file with the newer MATLAB. Unfortunately it looks like this File Exchange submission doesn't come with the sources to the MEX-file. So you are, unfortunately, out of luck.

How to use OpenCV in Matlab?

I have looked at many resources. I just want a simple way to use the newest OpenCV with Matlab 2017a. MinGW doesn't work and a compiler but I can't seem to get Visual C++ working. I can use OpenCV in Visual Studio but what compiler does Matlab want from me and how do I obtain it?
There is a way to use opencv from matlab without compiling any mex files, and without using the computer vision toolbox.
You can call opencv through matlab via python commands.
1) Download and install the CPython distribution, not Anaconda
2) from the command prompt, install the opencv and other packages you want using the pip command > python -m pip install opencv-python
3) Follow the procedure in the matlab help for ensuring matlab knows where to look for the python installation
4) use the syntax py.cv2.function to call the opencv function
If you keep the data types as python data types in matlab while using the py.cv2 functions there isn't too much of a speed hit.
As #Miki mentions in the their comment, have you checked out this page on the MathWorks site? It has many resources, the main one is the Computer Vision System Toolbox OpenCV Interface. It comes with some examples which would be a good starting point. And if those aren't close to what you want, try another one of the community examples on the File Exchange and trying to make small changes to it.
For each version of MATLAB you can find the supported compilers in the documentation. You can also use -v flag as shown blow to see all the paths where MATLAB looks for a C++ compiler.
mex -setup -v

Visualizing HOG features MATLAB

I was trying to use the iHOG found in this link : http://web.mit.edu/vondrick/ihog/#code to visualize my HOG features. However, I had a compilation problem when following the steps. I am using MAC Mavericks MATLAB 2014a, does anyone ever met a compilation problem when using same system or maybe knows why? This is a screenshot of the error given by MATLAB after running 'compile' as indicated by this link https://github.com/CSAILVision/ihog/blob/master/README.md#ihog-inverting-histograms-of-oriented-gradients
I tried changing the -o to -output as told bye #rayryeng. However still I have the following error occuring:
Though this is not related to HOG, take a look at this Github page regarding mexopencv by Kota Yamaguchi: https://github.com/kyamagu/mexopencv/issues/117. The error that you are encountering was experienced in compiling his MEX OpenCV wrappers.
Essentially, the mex -o option is for MATLAB versions from R2013b and below. As of R2014a, you need to use the -output flag when using mex. You are probably using a MATLAB version that is R2014a or later. Therefore, go into the compile.m file, and change all instances of the -o flag to -output.

Calling pdftops does not work in Matlab

I want to use the export_fig package for Matlab. At one point this worked, but now it's broken. The error occurs here:
>> system('pdftops -h')
dyld: Library not loaded: /opt/local/lib/libtiff.5.dylib
Referenced from: /opt/local/bin/pdftops
Reason: Incompatible library version: pdftops requires version 8.0.0 or later, but libtiff.5.dylib provides version 6.0.0
pdftops -h: Trace/breakpoint trap
But when I call pdftops from my own bash terminal everything works fine. I suppose it must be because Matlab has some bad library paths or something, but I don't know where to go from here?
Some relevant environmental vars used by matlab:
>> system('env')
SHELL=/bin/bash
OSG_LD_LIBRARY_PATH=/Applications/MATLAB_R2013b.app/sys/openscenegraph/lib/maci64
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin
__CHECKFIX1436934=1
XFILESEARCHPATH=/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/locale/%L/%T/%N%S:
DYLD_FRAMEWORK_PATH=/Applications/MATLAB_R2013b.app/sys/os/maci64:/Applications/MATLAB_R2013b.app/bin/maci64/../../Contents/MacOS:/Applications/MATLAB_R2013b.app/bin/maci64:/Applications/MATLAB_R2013b.app/extern/lib/maci64:/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/./native_threads:/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/./server:/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/./lib/jli
SHLVL=1
__KMP_REGISTERED_LIB_33586=0x124404710-cafeb339-libiomp5.dylib
DYLD_LIBRARY_PATH=/Applications/MATLAB_R2013b.app/sys/os/maci64:/Applications/MATLAB_R2013b.app/bin/maci64/../../Contents/MacOS:/Applications/MATLAB_R2013b.app/bin/maci64:/Applications/MATLAB_R2013b.app/extern/lib/maci64:/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/./native_threads:/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/./server:/Applications/MATLAB_R2013b.app/sys/java/jre/maci64/jre/lib/./lib/jli
If you take a look at the error message, MATLAB uses libtiff to call pdftops, but only has version 6.0.0. It says it needs version 8.0.0 or later. There has been a case where this happened before here on StackOverflow. Someone was trying to install mexopencv and has encountered the same error that you're talking about: Compiling mexopencv in OS X 10.9 with Xcode 5 and Matlab R2013b
Though this is unrelated to what you're doing, they encountered the libtiff error that you speak of. They provide two solutions how to fix this error:
Go into MATLAB's root directory, then go bin/os directory where os is the directory of the operating system you are using (in my case, it's maci64). To get into the root directory, in the command prompt do this: cd/(matlabroot). Once you're here, locate the libtiff.5.dylib file and rename it to something like libtiff.5.dylib.bak so that the libtiff libraries that are used are the ones that are default to your system rather than what MATLAB is using internally in the program.
Force MATLAB to point to the library that is part of the system rather than using the one built into MATLAB. As such, in MATLAB's command prompt, run this command:
setenv('DYLD_INSERT_LIBRARIES','/opt/local/lib/libtiff.5.dylib');
Once you do this, you may get further errors that are similar but are with other libraries. As such, you need to keep appending these libraries in the second parameter of setenv and colon-delimiting them until you resolve the issues. You need to make sure that you're pointing to the ones on your system rather than the ones installed with MATLAB. In your case, you have to do:
setenv('DYLD_INSERT_LIBRARIES', ...
'/opt/local/lib/libtiff.5.dylib:/opt/local/lib/l‌​ibcurl.4.dylib');
Glad this helped!

Mex cannot find a file, but "which" can

I am trying to compile a C++ function (found here). I downloaded the requisite packages, installed Microsoft Windows SDK 7.1, linked it to MATLAB using mex -setup, and then unzipped the function packages into my MATLAB path.
When I run the command:
mex -output Faddeeva_w -O Faddeeva_w_mex.cc Faddeeva.cc
I get the error message:
C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: 'Faddeeva_w_mex.cc' not found.
But if I type which Faddeeva_w_mex.cc I get:
C:\Users\craigim\Documents\Code\Matlab\Faddeeva-MATLAB\Faddeeva_w_mex.cc
So MATLAB and the which command have no problems finding the file, but mex is lost. This is my very first attempt at compiling something with mex, so I'm really not sure where to start here. Does mex have a different path variable that I have to set?
I'm using MATLAB 2013a on a Windows 7 64bit machine with the version of SDK 7.1 that was linked from the MATLAB List of supported compilers.
Either cd into the directory containing the files (assuming they both in the same place), or specify full/relative path to the C++ files when invoking mex function
Try putting addpath('C:\Users\craigim\Documents\Code\Matlab\Faddeeva-MATLAB\'); in your m-file. The path that MATLAB uses seems to be internal and independent of whatever your OS is using.
Documentation on addpath here.
And a similar discussion here.
You may also have to set your PATH environment variable to the string returned by the command fullfile(matlabroot,'bin',computer('arch')) in MATLAB. Here are some instructions on setting environment variables for Windows and Mac or Linux
There is also a pretty good overview of building mex-files here although I get the impression that the basics are not going to answer your question.