OpenCV, Matlab and STL containers - c++

Many functions in the latest release of OpenCV require the use of STL containers. I run into problems when trying to use them in a Matlab MEX file. I am compiling the MEX files from within Matlab. Both OpenCV and Matlab use the "/MD" flag which is "Multithreaded DLL" for code generation.
Compiler: MSVC++ 9.0
Matlab 2010a
OpenCV latest from SVN, 2.11 I think.
The code I am using is very simple:
vector<KeyPoint> keypoints_vec;
SurfFeatureDetector surf;
surf.detect(cvImg,keypoints_vec);
This compiles but crashes when run in a Matlab MEX file. The crash is within OpenCV in vector::resize. The old interface (without STL containers) works fine but is deprecated. How can I use STL containers between Matlab and OpenCV?

I fought with this very problem in the last two days. The problem is this:
libmex.dll (and a whole Matlab) uses Microsoft.VC80.CRT (version=8.0.50727.4053)
But your OpenCV uses Microsoft.VC90.CRT (version=9.0.21022.8)
So you can use the previous version of VC (VS 2005 with SP1 as far as I know), or as a workaround, you can use gcc (MINGW) (in this case they use totally different stl, so they can't interfere).
I did the latter and it works, and it will work with the next versions of Matlab (hopefully).

A long time ago I had problems with Matlab <-> VS interop. It might be some microsoft visual c++ runtime library discrepancy. Check what runtime lib is required by matlab and what version does your visual studio have. I remember using Depends to get the dll dependencies for my program. Check your call stack after crashing (by attaching your msdev debugger) it might give you some hints.
It was a long time ago so I'm just giving hints of what I remember.

I had a similar problem in the past few days, and was able to resolve the issue with some help from the friendly folks at MathWorks.
From the original post at http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error :
You are probably seeing an incompatibility between the stl library and or compiler options used by your pre-compiled dll and those used by MATLAB and the MEX command. MATLAB 2009b was built with MSVC 2005.
You may be able to fix the problem by changing the options used by mex or by building your mex file directly with MSVC. One example of an option that may effect things is SECURE_SCL=0. I would start by building your test program with the options MATLAB is using to find the problematic option then try removing that option when building the mex file.
Because of this sort of incompatibility use of stl objects in the api's of third party compiled libraries is usually a bad idea.
Following his advice, I removed the SECURE_SCL=0 option from the mex options file at
C:\Users\ThePit\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat
Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.

The data in a vector should still be stored as a single contiguous block
std::vector<int> data;
int *array = &data[0];
int *array = &data.front();
Should give you 'c' style pointers to the data, try passing these to matlab
see also: How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?

Related

Any way to profile C++ MEX code NOT built with the 'Mex' command?

I need to profile some C++ Mex code.
It is built with G++, using arguments similar to those mex would pass. I can absolutely pass -o0 and -g or -g3, no problem.
The current solutions I have found:
MATLAB Profiler - Requires it was built with mex. I tried this myself to no avail. (It provided no information)
Visual Studio - Also apparently requires that it was built with mex. I tried this myself with these results sadly. (It just froze)
MATLAB Engine - Apparently also requires it was built with mex.
Building a C++ driver for it instead of the MATLAB script is likely not an option, as the script is complex and does significant setup (some with other mex files) before the section I want is called.
Is there anything that will work with MEX files not built with mex?

Unable to use dll built with Rcpp package in Visual Studio [duplicate]

I am wondering if there was a workaround for using the R package Rcpp within a Visual C++ project within Visual Studio. According to the Rcpp FAQ, this is not possible. I also came across another R package called R.NET. Can this only work with C# but there seems to be no examples from within Visual C++? Is there another option for this that I am missing? Even if I am forced to work with C#, can anyone envision latency issues with this for a high frequency trading environment?
Thanks for any feedback
We address this very question clearly in the Rcpp FAQ (which comes as vignette in the package, is on the CRAN site and its mirrors, and on my website).
In short, Rcpp supports the exact same toolchain which R supports. And on Windows that is MinGW (the port of gcc to Windows) provided by the Rtools package for R.
R Core only support MinGW, and we do the same. If one could compile R with Visual Studio, things would be easier, but as Rcpp sits on top of R we are playing the hand we are dealt with.
So sorry, the answer is a "No" to Visual Studio.
The question is what you really want to connnect with what. If you want to use R within C++ Rcpp seems to be the wrong decision as it connects C++ to R (and not the other way round as your question implies.).
If you want to connect R to C++ and use Visual C++ for creating your C++ code only (instead of linking R to C++), you might want to check this recent blog post (found via R-bloggers.com). It tells you a possible way of doing so.
Visual Studio works just fine with Rcpp. Has done for years. I disagree with Dirk Eddelbuettel.
This is an important fact - For example, there are very important libraries (Intels MKL, QT graphics framework) which are actually not compatible with minGW.
I maintain a (patched and cut-down) fork of Rcpp on github (https://github.com/rohan-shah/Rcpp) suitable for compilation with Visual Studio with cmake. The patch is something like five lines, I believe.
Disclaimer: It's fiddly to use, and you have to know what you're doing. If you do know what you're doing though, I've done quite a bit of the work for you. See https://github.com/rohan-shah/mpMap2 for an example of how to use patched Rcpp with Visual Studio.

Why compile OpenCV with QT?

I am using Qt & OpenCV on a new project am about to compile OpenCV to work with MinGW.
A thought has arose that I can compile the OpenCV libs with Qt support but I don't really understand why I would want to do that. If I don't compile the libs with Qt support I can still route cv::mat to a QImage using a method like this.
So what am I missing? What value is provided in compiling Qt with OpenCV?
I have searched online and pages like this, this & this (from searching "why compile OpenCV with Qt") only show me how to compile with Qt and not why.
LE: misunderstood the question
WITH_QT option is used by highgui module to create windows using qt, so the QImage to cv::Mat conversion and vice-versa will work no matter how WITH_QT option is set.
First some clarifications: you are not compiling OpenCV with Qt, Qt is not a compiler so you can't compile anything with it.
Qt is a C++ library (it's called a framework because it imposes some design some rules to your application source code, but basically it's a C++ library, just like OpenCV).
Now, in C++ world* to use some libraries together you need to build those libraries with the same compiler (and in some cases even the same compiler settings), so must decide which C++ compiler you want to use and get both Qt and OpenCV built with the same compiler not necessarily build by you, binaries can be obtained from their websites.
If you want to use MinGW you will need to build OpenCV with MinGW compiler, because OpenCV (at least, version 2410) comes build only with Visual C++ version 10, 11, 12 - that means Visual Studio 2010, 2012 and 2013.
So if you decide to use some Visual Studio version, depending on version you choose, you might be able to use Qt with OpenCV without having to build neither yourself, but if you want MinGW compiler you need to build OpenCV with MinGW.
*you can get away with it if your libraries only export a C interface, but that is not the case with neither Qt nor latest OpenCV versions.
//if you want more details about this use your favorite internet search engine to search for: c++ binary compatibility and or c++ abi
You'd like to compile OpenCV with Qt for at least two reasons:
it gives you the zoom (mouse wheel) in imshow
it gives you the pixel RGB value when hovering over this pixel
Without WITH_QT, you just have a bare window, with none of these features (and you'll miss extra buttons too, like Save the picture), which make the image processing debugging more tedious.

Integrate MATLAB code in Visual Studio 2010 (C++)

I've seen that it is possible to call MATLAB functions from VS2010 (VC++). I was wondering if it is possible to create executable files using VC++ that do not require any MATLAB software on the computer (like the MATLAB Compiler Runtime (MCR))?
I would like to create a program written in VC++ that calls some of the MATLAB functions, but is completely standalone and can run from any Windows computer without the need of any prior MATLAB software to have been installed.
Thanks.
It is not possible to install without MCR in any documented way and I even think that it is not legal.
However, there are some examples like Imatest, who managed to do it in some of their earlier versions.
Edit(1)
I almost forgot about MatlabCoder. If you buy it, it can transform your Matlab code directly into c code. But it will not work with some of the toolboxes, like image processing.

Converting Matlab to C++

I need to convert some MATLAB code to C++. I am using Visual Studio 2010 and have MATLAB compiler installed. I am wondering how do I go about doing this.
Also when I publish my project will the end user have to install anything by MATLAB? (this cannot be the case)
You can compile Matlab scripts into standalone executables, but I'm not familiar with a Matlab tool to convert Matlab code to Visual C++ code. You can convert Matlab code to C or C++ using Simulink Coder (formerly Real-Time Workshop), which is available from Mathworks as a separate product.
To run the standalone executable, the user will have to install the Matlab Compiler Runtime. From the Matlab Compiler web page:
Executables and libraries created with the MATLAB Compiler product use a runtime engine called the MATLAB Compiler Runtime (MCR). The MCR is provided with MATLAB Compiler for distribution with your application and can be deployed royalty-free.
You will not get rid of the Matlab runtimes. I often had problems using Matlab Compiler. It basically is like a wrapper around the Matlab runtime, generates clumsy code and did introduce many problems for deployment. C alternatives ... ? I could not find any. So I recently switched to ilnumerics, which is like Matlab in C# - only much faster and no translation necessary. it works out great so far because we use ASP.NET anyway and hence save jumping between languages and can directly use the 3d plots of ilnumerics.
Another option you have is to use the MATLAB Engine. It probably isn't an option for real-time applications, but otherwise it's really nice. You basically call MATLAB functions right from C++. Figures (plots) will be display just as if you were running local MATLAB. When the program starts, a MATLAB console is created.