20

I've noticed that Matlab is very popular among the computer vision and image processing community still to this day, even though OpenCV is a very mature package for C++. I've never used Matlab, but looking at it I see no advantages over OpenCV in C++. It's so commonly used however that I'm considering picking it up.

Why is it so popular among this crowd? What are its advantages over OpenCV?

zebra
  • 6,373
  • 20
  • 58
  • 67
  • 1
    Here is a similar question that might prove useful to your question: http://stackoverflow.com/questions/3953204/matlab-vs-aforge-vs-opencv – O_O Jan 12 '12 at 01:06
  • Possible duplicate of : http://stackoverflow.com/questions/179904/what-is-matlab-good-for-why-is-it-so-used-by-universities-when-is-it-better-th/8347327#comment11022195_8347327 – Andrey Rubshtein Jan 12 '12 at 06:52

5 Answers5

47

I am a phD student in computer vision, and I've already answered about the comparison between matlab and python for scientist in this question: What is MATLAB good for? Why is it so used by universities? When is it better than Python?

I will update my answer for the comparison between matlab and opencv for computer vision:

I used to code only with C++/OpenCV a while ago, but since the begining og my phD (3 years a go), I code only in Matlab. so I know well the topic.

There is ONE reason why matlab is so good and so widely used, compared to opencv:

EXTREMELY FAST CODING

Personally, I code around 10 times faster in matlab than in OpenCV/C++, and there is much less bug at the end.

1) Computer Vision Researchers need fast prototyping

In research environment, we have (hopefully) often new ideas, and we want to test them really quick to see if it's worth keeping on in that direction. And most often only a tiny sub-part of what we code will be useful. Moreover it's often not possible to guess beforehand if an idea is going to work or not.

Matlab is often a bit slower at execution time and opencv is definitely the fastest for running time, but we don't care much. Because we don't know in advance what method is going to be successful, we have to try many things, so our bottle neck is programming time, because our code will most often run a few times to get the results to publish, and that's all.

So let's see how matlab can help to improving programming time.

2) Everything I need is already there

Matlab has really a lot of functions that I need, so that I don't have to reinvent them all the time:

change the index of a matrix to 2d coordinate: ind2sub extract all patches of an image: im2col; compute a histogram of an image: hist(Im(:)); find the unique elements in a list unique(list); add a vector to all vectors of a matrix bsxfun(@plus,M,V); convolution on n-dimensional arrays convn(A); calculate the computation time of a sub part of the code: tic; %%code; toc; graphical interface to crop an image: imcrop(im);

The list could be very long... And they are very easy to find by using the help.

However, conserning the pure computer vision functions, I think that the core Opencv is a bit more exhaustive, than matlab plus toolboxes. But Nowadays, so many researchers publish their source code in matlab that if you want to test the latest discovery you basically have to use matlab.

3) No C++ specific problems

No need to allocate and free memory. Matlab does that for you, so you can focus on your work.

No buffer overflow. So, no more long time trying to find out where it crashes. Matlab stops automatically, and tells you where the code tries to get a value outside of the matrix range.

No compilation time...

No header to write...

4) IDE

An example: I launch a script. It produces an error because of a matrix. I can still execute code with the command line. I visualize it doing: imagesc(matrix). I see that the last line of the matrix is weird. I fix the bug. All variables are still set. I select the remaining of the code, press F9 to execute the selection, and everything goes on. Debuging becomes fast, thanks to that.

Matlab underlines some of my errors before execution. So I can quickly see the problems. It proposes some way to make my code faster.

With OpenCV/C++/Visual Studio, I can debug. But this debugger does not allow me to execute code during the debugging, so I cannot for instance visualize matrices and so on.. So in practice, I have to copy paste some code to dump matrices, to check where is the error. This is very painful.

There is an awesome profiler included in the IDE. KCahcegrind for C++ is such a pain to use compared to that.

I wrote more there: Are there any alternative editors for .m files?

5) Concise code

Matlab code is more consize, which mean easier to debug, to read, to understand and: the code looks like my formulas.

To normalize all the columns of a matrix ( which I need all the time), I do: bsxfun(@times,A,1./sqrt(sum(A.^2)))

To remove from a matrix all colums with small sum:

A(:,sum(A)<e)=[]

To do the computation on the GPU:

gpuX = gpuarray(X); 
%%% code normally and everything is done on GPU

To paralize my code:

parfor n=1:100
%%% code normally and everything is multi-threaded

What language can beat that?

And of course, I rarely need to make loops, everything is included in functions, which make the code way easier to read, and no headache with indices. So I can focus, on what I want to program, not how to program it.

6) Plotting tools

Matlab is famous for its ploting tools. They are very helpful. OpenCV has just the basic plotting functions.

7) Excellent documentation

And it's very easy to acces it, by typing doc

PS: And what I hate with matlab: its price

Community
  • 1
  • 1
Oli
  • 15,935
  • 7
  • 50
  • 66
  • 1. Very important is no memory allocation error, stack overflow 2. Easy to save and load variables 3. Easy view the matrix values 4. No compilation... Matlab is great... – Vinoj John Hosan Jun 14 '13 at 07:04
  • @VinojJohnHosan, that's right, I will add that. – Oli Jun 14 '13 at 14:54
  • @VinojJohnHosan, I have added points 1 and 4. Opencv does have nice tools to save and load matrices. It also can visualize matrix values, even during the debugging (this is very recent). – Oli Jun 19 '13 at 02:40
  • Matlab's workspace, U can visualize matrix values quickly and instantly – Vinoj John Hosan Jun 20 '13 at 05:18
6

The C++ API of OpenCV is rather new. Also, it had many bugs when it came out, now it is more ironed out. Before that, with the C API it was quite a pain to write code with OpenCV. E.g. you would not have easy access to matrix cells, there was differentiation between IplImage and other matrices and there were no Matrix Expressions.

I think that is the main reason why most people in computer vision still adhere to the "Matlab for rapid development" idea. It is true that for most cv tasks nowadays it does not take much longer time / more burden or more lines of code in C++ & OpenCV than in Matlab.

However there are more reasons to use Matlab than that:

  • Matlab comes with the whole GUI framework, where it is very easy to inspect your variables, matrices, with plots and all kind of stuff. Debugging is also easier due to it being interpreted language.
  • Matlab has some stuff in toolboxes that OpenCV is still lacking, like the Wavelet toolbox.
  • Most researchers are not good C++ programmers. It's true. Typically, they are at best mediocre C++ programmers. And this leads to bugs or unexpected behavior etc. It is so easy to fall into traps with C++ if you are not an expert. OpenCV's strange operator= overload does not help to it. And then they will say, in Matlab they need less time to get things done and working.

I would like to add that I feel it is a pity that a whole research community is stuck to a proprietary environment, where free & good alternatives are available.

ypnos
  • 50,202
  • 14
  • 95
  • 141
3

Matlab is suitable for computer vision researchers, it has enormous algorithms that saves a lot of researchers' time; when you implement a new algorithm, you will also find Matlab saves your time than OpenCV. But Matlab is not good if performance is your important concern.

The advantage of OpenCV is that it's high customizable and the performance is good thanks to native C/C++.

If you are making a product related to computer vision, or you need to process lots of images in a short time, in other words, performance is critical, then you should use OpenCV.

If you are doing research on some novel vision or image algorithm, and performance is not a concern, please choose Matlab.

shader
  • 447
  • 3
  • 13
2

Matlab has not only image-processing capabilities, but, also mathematical functions that in C++ you would have to hook in other libraries for (and, when you're doing image processing, math libs are very important).

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
  • 2
    I'll add that Matlab is interpreted so there no compile cycle. This is great for playing with ideas. Also, there are many more people in the world who are familiar with Matlab than with the OpenCV API. – ahoffer Jan 12 '12 at 01:03
  • The fact that Matlab is interpreted is the most important thing. It keep the variables in memory so it's possible to run just a part of the code. It's just great for prototyping. – R.Falque Jul 19 '13 at 04:52
2

Matlab is an order of magnitude faster in development time compared to C++. I can't say an exact number, but anyone experienced in both languages will support the claim.

When you wish to play around with algorithms, if Matlab has the functionality you need, it is immediately chosen over C++.

Leo
  • 1,213
  • 2
  • 13
  • 26