Ask Your Question
15

Matlab API for OpenCV

asked 2013-03-17 18:40:27 -0600

hilton bristow gravatar image

There has been a lot of public interest in Matlab/Octave wrappers for some time now, so we are taking this project on board and aim to have a wrapper generator available within a few months. I will be leading this project under the supervision of Vincent Rabaud and Vadim Pisarevsky. For those who don't know me, I'm a computer vision PhD student and a long time lover of Matlab.

What we would like within this Q&A is to open the floor to suggestions on implementation details. I've already mapped out 96% of the code, but I'm always willing to listen if people have particular requests.

edit retag flag offensive close merge delete

Comments

When the wrapper will be available?

mrgloom gravatar imagemrgloom ( 2013-03-18 00:46:52 -0600 )edit

One of the best things that happened to OpenCV in a while. Congrats!

sammy gravatar imagesammy ( 2013-03-18 02:38:27 -0600 )edit

Google Summer of Code starts on June 17: http://www.google-melange.com/gsoc/events/google/gsoc2013. Assuming we get the project approved, it should be completed a few months after that. It is therefore likely going to miss the OpenCV 2.5 release.

hilton bristow gravatar imagehilton bristow ( 2013-03-18 18:44:13 -0600 )edit
1

Our project got accepted!

hilton bristow gravatar imagehilton bristow ( 2013-06-13 20:44:32 -0600 )edit

5 answers

Sort by ยป oldest newest most voted
4

answered 2013-03-18 18:40:14 -0600

hilton bristow gravatar image

Detail 1: Conversion types

The excellent mexopencv utility uses a conversion type called MxArray as a means of converting to and from native Matlab (mxArray) and OpenCV and related types (cv::Mat, std::string, etc). Do you like this type of encapsulation, and if so do you prefer explicit conversion (assuming here prhs is of type mxArray**):

std::string name = MxArray(prhs[0]).toString();

or implicit conversion:

std::string name = MxArray(prhs[0]);

Or would you rather a set of utility functions that make memcopys and ownership more explicit:

std::string name = mxArrayToString(prhs[0]);
edit flag offensive delete link more
2

answered 2013-03-18 19:03:20 -0600

hilton bristow gravatar image

Detail 2: OpenCV classes in Matlab

There is no ideal solution to referencing C++ classes from Matlab. A set of best practices have been devised by the Matlab community that results in the least amount of undefined behaviour, memory leaks, dangling pointers, etc.

Even so, this requires that the memory address of the C++ object be passed back to Matlab (as a uint64_t) and trusted in the hands of the user (though it is private and invisible).

A better way to do this is to store all of the object instances in a map:

static std::unordered_map<KeyType, OpenCVClass> instances_;

and then give the user a key to accessing their object. That way, if they attempt to modify their key, they'll get a std:out_of_range exception, rather than a segfault. Memory addresses of the objects make good keys since they're not contiguous, so it'll be harder for the user to "guess" another valid instance.

edit flag offensive delete link more
1

answered 2013-03-19 20:27:29 -0600

hilton bristow gravatar image

Detail 4: Finding Matlab

The FindMatlab.cmake distributed with cmake by KitWare is woefully inadequate at finding any recent releases of Matlab. So it's time to write a new one. With a bit of crowd-sourcing I've been able to come up with the default install paths of Matlab on different architectures (in order of likelihood)

Linux: /usr/local /opt/local /usr /opt

Apple: /Applications /usr/local /opt/local /usr /opt

Windows: C:\Program Files C:\Program Files (x86)

In the case of Windows machines, assuming everything is in C: is a baaaad idea, so there are (gulp) recommended methods of hunting through the registry for paths:

execute_process(COMMAND reg query HKLM\\SOFTWARE\\MathWorks\\MATLAB /f * /k)

In the case of Unix derivatives, grep and regex are your friends.

OpenCV eschews the regular package finding convention of find_package(<package>) which locates a Find<package>.cmake file on the CMAKE_MODULE_PATH, in favour of having all the packages called OpenCV[Find/Detect]<package>.cmake and then simply including them in the root CMakeLists.txt as include(cmake/OpenCV[Find/Deteect]<package>.cmake). This is for one very simple and deliberate reason:

They want to make sure their own cmake files are loaded and not other ones on the system

edit flag offensive delete link more
1

answered 2013-03-18 19:17:25 -0600

hilton bristow gravatar image

updated 2013-03-18 19:30:00 -0600

Detail 3: Other useful utilities

As part of writing the code generator, it would be useful to expose some utilities that make it easier to use OpenCV constructs in mex files, and Matlab features in OpenCV. I was thinking of implementing:

  • Matlab (.mat) I/O for cv::FileStorage. I have an implementation which needs refactoring.
  • A mex script which automatically finds OpenCV and adds the relevant build flags, so compiling your own C++ function containing OpenCV declarations within matlab should be as simple as:

    cvmex my_awesome_function.cpp

  • Conversion class/operations for easily getting between mxArray and cv:: std:: types

What would you like to see?

edit flag offensive delete link more

Comments

Hey dude! why not adding cvmatio as a decoder/encoder independently of having matlab installed? I'm only interested in loading .mat files.

eriba gravatar imageeriba ( 2015-10-13 08:47:20 -0600 )edit
0

answered 2014-09-18 04:32:49 -0600

el_baro gravatar image

Hello i'm trying to build opencv 3.0 with Matlab support but I get always "NOT WORKING" warning message.

My config is: - opencv 3.0 alpha release - Matlab 2014a - Visual Studio 2012. - Cmake 2.8 (or the 3.0.2 version) - List item

image description

What's wrong with my configuration?

Regards, ElBaro.

edit flag offensive delete link more

Comments

Unfortunately the same here. First I thought that this very exciting project would not be followed any more. It is not included in the openCV 3.0 Beta distribution, and the original author's (Hilton Bristow's) openCV branch at github.com doesn't show any activity since a long time. Too sad! Well, I found that the project is alive and you have to search for it outside of the master branch in an opencv_contrib branch which you can find here. https://github.com/Itseez/opencv_cont...

talby gravatar imagetalby ( 2015-02-12 09:37:23 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-03-17 18:40:27 -0600

Seen: 4,759 times

Last updated: Sep 18 '14