Ask Your Question
1

Convert C++ Code to java (Iterator)

asked 2014-06-27 03:25:23 -0600

bbx gravatar image

Hello everybody, i have many problems to unterstand a tutorial from a Book called " Mastering openCV ". The Code examples are written in C++ and i want to import to android java. I converted many lines but stucked at this :

//Check new floodfill mask match for a correct patch. //Get all points detected for get Minimal rotated Rect

        vector<Point> pointsInterest;
        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());

        RotatedRect minRect = minAreaRect(pointsInterest);

How can i translate these lines to java ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-11-06 07:59:03 -0600

mrfazolka gravatar image

updated 2014-11-06 08:04:21 -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

Question Tools

Stats

Asked: 2014-06-27 03:25:23 -0600

Seen: 832 times

Last updated: Nov 06 '14