本文介绍了如何MatOfPoint转换为MatOfPoint2f在OpenCV中的Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下的例子codequestion通过使用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 questionby 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() );

这篇关于如何MatOfPoint转换为MatOfPoint2f在OpenCV中的Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 14:42