Converting C++ code to java
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));
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
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 reading2
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 mefindContours, convexHull, etc: sample code
(also have a look at the other android samples for inspiration !)
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 officialopencv
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.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 workyou can find java tutorials here especially Background Removal using Canny