【发布时间】:2016-11-14 13:33:32
【问题描述】:
我的 C++ 代码如下:
// 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);
我想将其转换为 Java,因为从现在开始我将在 Java 中使用 OpenCV。
在 Java 中,我尝试了以下方法:
// 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);
但是最后一行给出的错误是:
The method fillConvexPoly(Mat, MatOfPoint, Scalar, int, int) in the type Imgproc is not applicable for the arguments (Mat, MatOfPoint2f, Scalar, int, int)
目前我对MatOfPoint 和MatOfPoint2f 类型感到很困惑,似乎它不像C++ 中那么简单。
有人遇到过类似的问题吗?
【问题讨论】:
-
我不太懂 Java,但方法签名表明它接受
MatOfPoint而不是MatOfPoint2f所以它想要 int coords 而不是 float coords -
如果我这样做,那么另一个函数会出错。
-
在 C++ 版本中,我所做的只是在向量中收集点,然后将此向量传递给这些绘制和填充梯形的函数。我想在 Java 中做同样的事情,但由于某种原因我面临着很多问题。
-
你的意思是
approxPolyDP?我不知道,但事实仍然是它是不兼容的类型,因此您需要转换回 int 点 -
我希望这两个函数能够正常工作而不会相互造成问题,就像我在上面分享的 C++ 版本一样。如果有人可以帮助我将这些函数的参数转换为 Java 参数,我会很高兴。