Converting C++ code to java

asked 2015-12-09 01:46:21 -0600

life Evader gravatar image

updated 2015-12-09 07:30:58 -0600

I am new to both OpenCv and Java, I have been doing a bit of reading and research on how I can perform a background subtraction and thanks to sturkmen I have a piece of code that actually works, except it is in C== and my enviroment is Java, I could have converted the code but there is a few challenging thing, Like in Java Canny only has 4 parameters so its a bit of a challenge for me, here is the code:

Mat src= imread( argv[1] );
Mat original = src.clone();
Mat gray,mask;
cvtColor( src, gray, COLOR_BGR2GRAY );
blur( gray, gray, Size(3,3) );
Canny( gray, mask, 100, 300, 3 );
vector<Point> contours;
vector<Point> convexHull_pts;
findNonZero( mask, contours );

convexHull( contours, convexHull_pts);
fillConvexPoly( mask, convexHull_pts, Scalar(255) );

I tried Converting it based on my knowledge and here is what I got:

     Mat src= new Mat();
    Utils.bitmapToMat(bitmap, src);
    Mat original = src.clone();
    Mat gray = new Mat();
    Mat mask = new Mat();
    Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.blur(gray, gray, new Size(3, 3));

    Imgproc.Canny(gray, mask, 100, 300, 3);

    MatOfPoint contours = new MatOfPoint(); // i am not sure how to initialize MatOfPoint ( blind suggestion )
    MatOfPoint convexHull_pts = new MatOfPoint();

    Core.findNonZero(mask, contours);
    Imgproc.convexHull(contours, convexHull_pts);
    Imgproc.fillConvexPoly(mask, convexHull_pts, Imgproc.Scalar(255));
edit retag flag offensive close merge delete

Comments

3

Here you have the docs for Java OpenCV and here the docs for C++ OpenCV. Try yourself, otherwise you will learn nothing. If you really find trouble, come back with specific errors

LorenaGdL gravatar imageLorenaGdL ( 2015-12-09 01:54:53 -0600 )edit

I have looked at the documentation that is why I pointed out that in java Canny only has 4 parameters and besides and a new user I cannot just learn everything by reading 2 documentations at a go, am learning slowly, that is not the complete code, I have done most of the work its only this that is really puzzling me

life Evader gravatar imagelife Evader ( 2015-12-09 04:49:44 -0600 )edit
1

findContours, convexHull, etc: sample code

(also have a look at the other android samples for inspiration !)

berak gravatar imageberak ( 2015-12-09 05:00:32 -0600 )edit

wow so you cannot just explain the whole thing for me? we ask questions because we tried and it did not go so well, there are very very few android tutorials/java based on OpenCV most of them are C++ , C# and python, I have been struggling with this for the past 3 weeks, I have just showed you all what I have done based on the C++ code I earlier posted for help, all help I needed was the meaning of this vector<point> contours; vector<point> convexHull_pts; Why Canny in java has 4 parameter and what I should do to the extra parameter in my case, I expected better response from here than starckoverflow so much for being the official opencv forum if all you tell people in need is to go read a bunch of books even when they have clearly showed you they tried.

life Evader gravatar imagelife Evader ( 2015-12-09 05:15:36 -0600 )edit

although i am not familiar with java, i tried to edit your code. could you test it.

i think Imgproc.Canny(gray, mask, 100, 300, 3); should work

sturkmen gravatar imagesturkmen ( 2015-12-09 06:56:49 -0600 )edit
1

you can find java tutorials here especially Background Removal using Canny

sturkmen gravatar imagesturkmen ( 2015-12-09 07:06:48 -0600 )edit