Ask Your Question

C++ code equivalent in java

asked 2014-05-13 23:21:39 -0500

rezasa gravatar image


I'm a java developer and I want convert the following code from c++ to java

mask= new Scalar::all(0);

Mat_<uchar>::iterator itMask= mask.begin<uchar>();

Mat_<uchar>::iterator end= mask.end<uchar>();

for( ; itMask!=end; ++itMask)


I'm using opencv-249.jar

Would you please help me?

Thanks alot

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-11-26 17:13:55 -0500

Krt gravatar image

Use this:

MatOfPoint2f idx = new MatOfPoint2f();
Core.findNonZero(mask, idx);
idx.convertTo(idx, CvType.CV_32FC2);
RotatedRect box = Imgproc.minAreaRect(idx);
edit flag offensive delete link more

answered 2014-11-06 08:00:10 -0500

mrfazolka gravatar image

updated 2014-11-06 08:04:51 -0500

I dealt with the same problem. After some attempts I solved it by this code in java:

ArrayList <org.opencv.core.Point> pointsInterestList = new ArrayList();

//iterate over Mat
for (int j = 0; j < mask.rows(); j++)
   for (int k = 0; k < mask.cols(); k++)
    double[] pixel = mask.get(j, k);
    if(pixel[0] == 255)
       //add Point of Mat to list of points
       org.opencv.core.Point point = new org.opencv.core.Point(k, j);

MatOfPoint2f m2fFromList = new MatOfPoint2f();
m2fFromList.fromList(pointsInterestList); //create MatOfPoint2f from list of points
MatOfPoint2f m2f = new MatOfPoint2f();
m2fFromList.convertTo(m2f, CvType.CV_32FC2); //convert to type of MatOfPoint2f created from list of points

RotatedRect minRect = Imgproc.minAreaRect(m2fFromList);

It should works same like c++ code. I hope it helps you.

edit flag offensive delete link more



calling get() for a single pixel will be slow as molasses.

look here , i think, your answer can be improved a lot.

berak gravatar imageberak ( 2014-11-06 08:11:55 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-05-13 23:21:39 -0500

Seen: 455 times

Last updated: Nov 26 '14