如何MatOfPoint转换为MatOfPoint2f在OpenCV中的Java API转换为、MatOfPoint2f、MatOfPoint、API

2023-09-12 11:29:24 作者:迩何曾让涐懂

我想实现以下的例子code question 通过使用OpenCV的的Java API。为贯彻落实 findContours(灰色,轮廓,CV_RETR_LIST,CV_CHAIN​​_APPROX_SIMPLE); 在java中我用这个语法 Imgproc.findContours(灰色,轮廓,新材料() ,Imgproc.RETR_LIST,Imgproc.CHAIN​​_APPROX_SIMPLE);

I'm trying to implement the example code of the following question by using opencv java api. To implement findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); in java i used this syntax Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);.

所以,现在的轮廓应该是名单,其中,MatOfPoint>轮廓=新的ArrayList< MatOfPoint>(); ,而不是矢量<矢量< CV ::点> >轮廓;

So now contours should be List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); rather than vector<vector<cv::Point> > contours;.

那么我就需要实现这个 approxPolyDP(马太福音(轮廓[I]),约,弧长(马太福音(轮廓[I]),真)* 0.02,真); 。在的Java API,Imgproc.approxPolyDP接受的说法是 approxPolyDP(MatOfPoint2f曲线,MatOfPoint2f approxCurve,双ε,布尔关)。我如何我可以转换M​​atOfPoint到MatOfPoint2f?

Then i need implement this approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);. In java api, Imgproc.approxPolyDP accept argument as approxPolyDP(MatOfPoint2f curve, MatOfPoint2f approxCurve, double epsilon, boolean closed). How i can i convert MatOfPoint to MatOfPoint2f?

或者是有没有办法使用的载体是相同的C ++实现这个接口。任何建议或样品code是大大AP preciated。

Or is there a way to use vectors as same as c++ interface to implement this. Any suggestion or sample code is greatly appreciated.

推荐答案

MatOfPoint2f不同于MatOfPoint只中的元素(分别为32位浮点和32位int)的类型。的可行的选择(虽然性能上的损失)是制造MatOfPoint2f实例并设置其元素(在循环中)等于源MatOfPoint的元素

MatOfPoint2f differs from MatOfPoint only in the type of the elements (32-bit float and 32-bit int respectively). The viable option (though with a performance penalty) is to create MatOfPoint2f instance and set its elements (in a loop) to be equal to the elements of of the source MatOfPoint.

 public void fromArray(Point... lp);
 public Point[] toArray();

在两种类的方法。

所以,你可以做到这

 /// Source variable
 MatOfPoint SrcMtx;

 /// New variable
 MatOfPoint2f  NewMtx = new MatOfPoint2f( SrcMtx.toArray() );