Schütze Schütze - 23 days ago 17
C++ Question

Opencv Java fillConvexPoly and approxPolyDP functions

I have my C++ code of following:

// ROI by creating mask for the trapezoid
Mat mask = Mat(frame.rows, frame.cols, CV_8UC1, Scalar(0));

// Create Polygon from vertices
approxPolyDP(pointsForTrapezoid, roiPolygonized, 1.0, true);

// Fill polygon white
fillConvexPoly(mask, &roiPolygonized[0], roiPolygonized.size(), 255, 8, 0);


which I want to convert to Java, since I'll be using OpenCV in Java from now on.

In Java, I tried the following:

// ROI by creating mask for the trapezoid
Mat mask = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC1, new Scalar(0));

// Create Polygon from vertices
MatOfPoint2f tmpTrapeziod = new MatOfPoint2f();
MatOfPoint2f tmpROI = new MatOfPoint2f();
tmpTrapeziod.fromList(pointsForTrapezoid);
tmpROI.fromList(roiPolygonized);
Imgproc.approxPolyDP(tmpTrapeziod, tmpROI, 1.0, true);

// Fill polygon white
Imgproc.fillConvexPoly(mask, tmpROI, new Scalar(255), 8, 0);


However the last line is giving the error of:

The method fillConvexPoly(Mat, MatOfPoint, Scalar, int, int) in the type Imgproc is not applicable for the arguments (Mat, MatOfPoint2f, Scalar, int, int)


I am quite confused at the moment about the types
MatOfPoint
and
MatOfPoint2f
, seems it is not as straightforward as in C++.

Did anyone face a similar issue?

Answer

Try

Imgproc.fillConvexPoly(mask, new MatOfPoint(tmpROI), new Scalar(255), 8, 0);

Doc - so explanatory that I need to emphasize the Try