1

I currently have a line of code

List<MatOfPoint> contours = new Vector<MatOfPoint>();

which I'm looking to convert to MatOfPoint2f so I can call the following function on it:

Imgproc.arcLength(contours, true);

However, I'm really not sure how to convert a List<MatOfPoint> to MatOfPoint2f, which is what contours has to be to work with arcLength. Can anyone show me how to do this?

Zetland
  • 569
  • 2
  • 11
  • 25
  • [Perhaps this answer will help](http://stackoverflow.com/questions/11273588/how-to-convert-matofpoint-to-matofpoint2f-in-opencv-java-api) – gtgaxiola Mar 27 '15 at 13:55
  • Thanks. I have seen this thread already but I am wondering how to convert `List` to `MatOfPoint`. Could you give me any advice on this? Will I have to loop through it or something? – Zetland Mar 27 '15 at 13:57
  • I'm assuming you will have a `List` so you will have to loop over each `MatOfPoint` to create your appropriate `MatOfPoint2f` element. – gtgaxiola Mar 27 '15 at 13:59
  • No, it's `List` - no `2f`. Does that change anything? – Zetland Mar 27 '15 at 14:00
  • Your original question says `List` to `MatOfPoint2f` now you are saying from a List of `MatOfPoint` to a single `MatOfPoint`? – gtgaxiola Mar 27 '15 at 14:02
  • So I don't have to convert `List` to 'MatOfPoint` before I convert it to `MatOfPoint2f`? – Zetland Mar 27 '15 at 14:08

1 Answers1

3

It seems that you want:

 List<MatOfPoint> contours;  //already initialized somewhere in your code
 List<MatOfPoint2f> newContours = new ArrayList<>();
 for(MatOfPoint point : contours) {
     MatOfPoint2f newPoint = new MatOfPoint2f(point.toArray());
     newContours.add(newPoint);
 }
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • Thanks for this. I get the following error message though: "The method arcLength(MatOfPoint2f, boolean) in the type Imgproc is not applicable for the arguments (List, boolean)" – Zetland Mar 27 '15 at 14:22
  • Any way we can get rid of the `List<>` side of things? – Zetland Mar 27 '15 at 14:23
  • 1
    You are confusing a List of `MatOfPoint2f` with a single `MatOfPoint2f`. Just access each element individually to your `arcLength` operation – gtgaxiola Mar 27 '15 at 14:24