Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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();

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) //TODO: tady se určuje barva pozadí? 
    {
       org.opencv.core.Point point = new org.opencv.core.Point(k, j);
       pointsInterestList.add(point);
    }
   }
}

MatOfPoint2f m2fFromList = new MatOfPoint2f();
m2fFromList.fromList(pointsInterestList);
MatOfPoint2f m2f = new MatOfPoint2f();
m2fFromList.convertTo(m2f, CvType.CV_32FC2);

RotatedRect minRect = Imgproc.minAreaRect(m2fFromList);

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

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) //TODO: tady se určuje barva pozadí? 
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);
m2fFromList.fromList(pointsInterestList); //create MatOfPoint2f from list of points
MatOfPoint2f m2f = new MatOfPoint2f();
m2fFromList.convertTo(m2f, CvType.CV_32FC2);
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.