Skip to content
Samuel Audet edited this page Mar 15, 2015 · 1 revision

Hints for Converting OpenCV C/C++ code to JavaCV.

Created Mar 24, 2010 by shervin.emami

Hints for Converting OpenCV C/C++ code to JavaCV

JavaCV is designed so that most OpenCV functions can be used in JavaCV in the exact same way as they would in C/C++ code. However there are many functions that need to be treated differently, such as when a pointer to a struct needs to be passed to OpenCV. So here is a basic guide to help anyone port their OpenCV code to JavaCV.

How to use certain OpenCV functions in JavaCV

Here are examples for using the following functions in JavaCV:

OpenCV Code:

JavaCV Equivalent:

int i = cvRound(f);
int i = (int)Math.round(f);
cvNamedWindow("Out");
CanvasFrame canvas = new CanvasFrame("Out");
cvShowImage("Out", img);
canvas.showImage(img);
cvDestroyWindow("Out");
canvas.dispose();
CvCapture *cap = cvCreateFileCapture("lena.avi");
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("lena.avi");
grabber.start();
CvCapture *cap = cvCreateCameraCapture(0);
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start();
IplImage *img = cvQueryFrame(cap);
if (!img) return;
IplImage img = grabber.grab();
if (img == null) return;
cvReleaseImage(&img);
cvReleaseImage(img);
int key = cvWaitKey(10);
if (key) return;
KeyEvent key = canvas.waitKey(10);
if (key != null) return;
CvVideoWriter *videoWriter = cvCreateVideoWriter("out.avi",
CV_FOURCC('M','J','P','G'), 15, cvSize(320,240), 1);
FrameRecorder recorder = new OpenCVFrameRecorder("out.avi", 320, 240);
recorder.setVideoCodec(CV_FOURCC('M','J','P','G'));
recorder.setFrameRate(15);
recorder.setPixelFormat(1);
recorder.start();
// Look for corners in 'img' that seem good for tracking,
// storing them into 'corners' and 'count'.
CvPoint2D32f corners[MAX_POINTS];
int count = MAX_POINTS;

cvGoodFeaturesToTrack(img, eig, temp, corners,
&count, CORNER_QUALITY, CORNER_MIN_DISTANCE, MASK,
CORNER_BLOCK_SIZE, USE_HARRIS, K);
// Look for corners in 'img' that seem good for tracking,
// storing them into 'corners' and 'count'.
// This internally allocates MAX_POINTS points contiguously.
// They are accessed using Pointer.position(i).
CvPoint2D32f corners = new CvPoint2D32f(MAX_POINTS);
int[] count = { MAX_POINTS };

cvGoodFeaturesToTrack(img, eig, temp, corners,
count, CORNER_QUALITY, CORNER_MIN_DISTANCE, MASK,
CORNER_BLOCK_SIZE, USE_HARRIS, K);
// Use Optical Flow to compare 'prev_img' and 'prev_points'
// with 'img', generating 'current_points' and 'status' arrays.
char status[MAX_POINTS];
CvPoint2D32f current_points[MAX_POINTS];
CvPoint2D32f prev_points[MAX_POINTS];

cvCalcOpticalFlowPyrLK(prev_img, img, prev_pyramid, pyramid,
prev_points, current_points, count,
cvSize(OPFLOW_WIN_SIZE, OPFLOW_WIN_SIZE),
OPFLOW_PYR_LEVEL, status, null, cvTermCriteria(CV_TERMCRIT_ITER |
CV_TERMCRIT_EPS, OPFLOW_MAX_ITERATIONS, OPFLOW_EPSILON), flags);
// Use Optical Flow to compare 'prev_img' and 'prev_points'
// with 'img', generating 'current_points' and 'status' arrays.
byte[] status = new byte[MAX_POINTS];
CvPoint2D32f current_points = new CvPoint2D32f(MAX_POINTS);
CvPoint2D32f prev_points = new CvPoint2D32f(MAX_POINTS);

cvCalcOpticalFlowPyrLK(prev_img, img, prev_pyramid, pyramid,
prev_points, current_points, count,
cvSize(OPFLOW_WIN_SIZE, OPFLOW_WIN_SIZE),
OPFLOW_PYR_LEVEL, status, null, cvTermCriteria(CV_TERMCRIT_ITER |
CV_TERMCRIT_EPS, OPFLOW_MAX_ITERATIONS, OPFLOW_EPSILON), flags);
// Allocate and fill array, given x[], y[], and z[] arrays.
CvPoint3D32f points[nPoints];
for (int i = 0; i < nPoints; i++) {
points[i] = cvPoint3D32f(x[i], y[i], z[i]);
}
// Allocate and fill points array, given Point3d[] pts array.
CvPoint3D32f points = new CvPoint3D32f(nPoints);
for (int i = 0; i < nPoints; i++) {
points.position(i).put(pts[i].x, pts[i].y, pts[i].z);
}
// The position is part of the state, but can be reset this way:
targetPoints.position(0);
...
...
...
...
...
...
...
...
...
...
...
...