Ask Your Question
2

C++ code equivalent in java

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

rezasa gravatar image

Hi,

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)

    if(itMask==255)
           pointsInterest.push_back(itMask.pos());

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
0

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

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
0

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

mrfazolka gravatar image

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

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);
       pointsInterestList.add(point);
    }
   }
}

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

Comments

1

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 -0600 )edit

Question Tools

Stats

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

Seen: 631 times

Last updated: Nov 26 '14