Ask Your Question
0

Convert c++ code to java code

asked 2013-06-09 07:59:35 -0600

akapelko gravatar image

updated 2013-06-10 08:30:29 -0600

Hi, I need to convert workable example of OpenCV application to the Java code. To be exactly, I'm trying to convert this project https://github.com/ytsutano/bookscan from OpenCV C++ completely to Java.

As Java library I use opencv-245.jar from opencv/build/java folder of OpenCV.

I already found out following transitions from c++ to java:

IplImage => Mat
CvMat => Mat
cvSaveImage => Highgui.imwrite
cvReleaseImage(img) => img.release()
CvCapture capture = cvCreateCameraCapture(0); => VideoCapture capture = new VideoCapture(0);
cvSetCaptureProperty(capture, ...)  => capture.set(...)
Mat src_img = cvQueryFrame(capture); =>  capture.grab(); Mat src_img; capture.retrieve(src_img);
cvCloneImage(src_img); => src_img.clone();
cvGetSize(src_img) => src_img.size();
cvSize(-1, -1) => new Size(-1, -1)
cvPoint(0, 0) => new Point(0, 0)

But there are many other a lot of code that I don't how to write with Java. For example:

imshow("image", im);
cvNamedWindow("Source", 0);
cvResizeWindow("Source", 480, 640);
cvWaitKey(10)
cvShowImage("Source", src_img);
cvCreateMemStorage(0);
cvFindContours(bw_img, storage, contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0))
cvAdaptiveThreshold(gray_img, bw_img, 128, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 31, 8);
cvCvtColor(src_img, gray_img, CV_BGR2GRAY);
cvFindCornerSubPix(src_img, ...)
cvCreateMat(3, 3, CvType.CV_64FC1);

These constants from types_c.h files are also located in unknown places: IPL_DEPTH_8U, CV_ADAPTIVE_THRESH_MEAN_C, CV_RETR_LIST, CV_CHAIN_APPROX_NONE etc.

Following classes seems doesn't exist in Java wrapper: CvPoint2D32f, CvMemStorage, CvSeq.

And all of this is not a complete list.

So, is there some tool or documentation that can help me in this Java code rewriting? http://docs.opencv.org/java/ is not very helpful. Also, I'd be very grateful if you can answer how to write some of provided lines using Java.

And is it possible to rewrite this using JavaCL (maybe, it'll be faster?)?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-06-10 10:29:20 -0600

comwizz2 gravatar image

updated 2013-06-10 13:55:17 -0600

From the looks of the quick google searches and documentation, there is no full Highgui implementation in Java, which means no imshow, NamedWindow, ResizeWindow, WaitKey or ShowImage.

Using Java, you should not need createMemStorage, Java handles that kind of stuff for you, that is for c (not even c++ needs it according to the docs!) version of the library.

The rest seem to mostly be in the org.opencv.imgproc namespace, which is explained in that link you provided ;P

Also you shouldn't need create mat, you should make a new instance of the Mat class. see org.opencv.core.Mat

Also most of those constants are not exactly the same name, but they are found in the Java docs. org.opencv.core.Point replaces CvPoint2D32f. Take the time to really look in the docs. If you are using a different language you have to expect some differences, especially given Java's strict memory management system when compared to the lofty C binding you are comparing it to.

edit flag offensive delete link more

Comments

hi @comwizz2, do you know what equal method of CvSeqReader, cvStartReadSeq, and CvQuadEdge2D in opencv java?

orochi gravatar imageorochi ( 2013-06-18 09:30:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-06-09 07:59:35 -0600

Seen: 2,055 times

Last updated: Jun 10 '13