How to use OpenCV in Matlab? - 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

Related

How to use mxnet on windows with matlab bindings

Can anyone tell me if it is possible to use mxnet with matlab on windows? Using the windows packaged releases from https://github.com/dmlc/mxnet/releases, there is no "matlab" directory, which I can see is present in the newer versions, which do not have windows packages.
is there a way to get the best of both worlds?
Yes, it works, even thought I doubt it has been extensively tested. Their Matlab wrapping assumes the library lies in the lib directory, which is inconsistent with their python wrapping that assumes it lies in the build directory. So you will either have to copy libraries or change paths in callmxnet.m. Also, the demo needs data that is currently fetched by a bash script -- not very Windows friendly but it runs flawlessly with MSYS2.
IMHO the Matlab demo is currently more a proof-of-concept than a real Matlab wrapper. It is nowhere near the python bindings.
The matlab directory is directly on the repo not as a release:
https://github.com/dmlc/mxnet/tree/master/matlab
Maybe you can compile it for windows using cygwin or msys2, On the documentation it say that it was tested for matlab version:
Matlab binding has been tested with the following version:
R2016b (9.1.0.441655) 64-bit (glnxa64)
Which is a linux version; I think that it should be posible to reproduce the instructions in the readme to make it work woth cygwin or msys2 with windows

Matlab mex wrappers within opencv

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.

How to run a Matlab GUI program without having Matlab installed?

My program performs some simple calculations, look-ups and outputs an answer. What is efficient way to use a Matlab GUI program like I have as a standalone desktop application, which does not require Matlab as a prerequisite?
In order to run any Matlab program on a machine that doesn't have Matlab installed you will need to instal the Matlab Compiler Runtime (MCR).
This is a pre-requisite as it contains the Matlab core functionality that is required by your application and it is royalty-free.
So these are the steps you need:
In your Matlab installation, use the Matlab Compiler to generate a standalone executable of your program.
In the machine you want to deploy your application, Install the same MCR version than the Matlab version you used to generate your program.
Run the program
If you want to run a MATLAB application as stand-alone... whether it be a GUI or a script, it's not required that MATLAB be installed on your computer. You can, in fact, distribute any MATLAB application that you develop to those who don't have MATLAB installed on their computers, but you need the MATLAB Runtime libraries installed before doing so.
On the developer's end, the use of the MATLAB Compiler is required to make your MATLAB application stand-alone. You need to be cautious in that certain MATLAB functions from certain toolboxes cannot be compiled with the MATLAB Compiler if they are used as part of your MATLAB code in order to create your stand-alone application. This link from MathWorks gives a very comprehensive list of what is compatible per toolbox: http://www.mathworks.com/products/compiler/supported/compiler_support.html
On the user's end, simply get them to install the MATLAB Runtime libraries. That can be found here: http://www.mathworks.com/products/compiler/mcr/
Once the developer uses the MATLAB Compiler to create their stand-alone application and once the user installs the MATLAB Runtime libraries, then you can run any stand-alone MATLAB application. Be advised that there is no way around installing the MATLAB Runtime libraries. You need these for any MATLAB application to run stand-alone.

MATLAB Compiler

I use MATLAB 2012a. I need to compile a mex file in my program. Everything works fine with Microsoft SDK 7.1 but I need a minor compiler in order to work my program in other computers. Suggested compilers on MathWorks website are compilers that need to be installed as packages. Is there any compilers that work stand-alone and don't need to be installed?
You can use MinGW, but it is not straightforward. See this Q&A. The accepted answer suggest to compile and link MEX files outside of MATLAB, which is fine, but it's simple to just create mexopts.bat to do this.
A good mexopts.bat for MING is hosted here. Pick a MINGW distribution from win-builds and set the paths in mexopts. Then you can use mex -setup with this file.
gnumex is a disaster, IMO.

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.