Skip to content

Using JavaCV in MATLAB

Samuel Audet edited this page May 31, 2014 · 1 revision

Introduction

Although we can create objects and call methods of Java directly from MATLAB, it does not support some features like nested classes very well, for which we need to use an alternate syntax.

Details

At the command line of MATLAB, after copying all the JAR files (javacpp.jar, javacv.jar, ffmpeg*.jar, opencv*.jar, etc.) to some directory of your choice (namely /path/to/) and after loading JavaCV this way:

>> javaaddpath('/path/to/javacv.jar')
>> import org.bytedeco.javacpp.opencv_core.*
>> import org.bytedeco.javacpp.opencv_imgproc.*
>> import org.bytedeco.javacpp.opencv_highgui.*
...

we can start calling directly any functions of FFmpeg or OpenCV such as

>> image = cvLoadImage('/path/to/some/image/file.jpg')
image =
IplImage[width=512,height=512,depth=8,nChannels=3]

However, MATLAB does not support nested classes very well. To create a native array of ten CvPoint, for example, we would have to call:

>> pts = javaObject('org.bytedeco.javacpp.opencv_core$CvPoint', 10)
pts =
(0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0)

and similarly to call static methods of nested classes, for instance:

>> mat = javaMethod('create', 'org.bytedeco.javacpp.opencv_core$CvMat', 3, 3)
mat =
[ 0.0, 0.0, 0.0
  0.0, 0.0, 0.0
  0.0, 0.0, 0.0 ]

to create a 3x3 CvMat object.

Aside from that, the syntax is nearly identical to Java or Scala. For example, to copy some MATLAB matrix

>> A = fliplr(diag([1,2,3]))
A =
     0     0     1
     0     2     0
     3     0     0

into our mat object, we can use CvMat.put() as demonstrated in the README.md file for Java, as follows:

>> mat.put(reshape(A',1,[]))
mat =
[ 0.0, 0.0, 1.0
  0.0, 2.0, 0.0
  3.0, 0.0, 0.0 ]

Some unexpected problems can occur with MATLAB because it comes with its own duplicate set of libraries, potentially conflicting with the system libraries. If we cannot get MATLAB to use the system libraries instead, we may have to rebuild OpenCV and JavaCV using the ones that come with the version of MATLAB we need to use...