# OpenCV "FindContours" wont get me the number of Points

**URL:** https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749
**Category:** Libraries
**Created:** [January 22, 2019, 9:27pm UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749 "2019-01-22T21:27:40Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [January 22, 2019, 9:27pm UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/1 "2019-01-22T21:27:40Z")

</div>

I am using the example “findContours” from the Open CV library.  
Here I found the reference for the library.  
[http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/Contour.html](http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/Contour.html)

I am trying to determine how many dots are in the contour.  
So I tried:

```auto
import gab.opencv.*;

PImage src, dst;
OpenCV opencv;

ArrayList<Contour> contours;
ArrayList<Contour> polygons;

void setup() {
  src = loadImage("test.jpg"); 
  size(1080, 360);
  opencv = new OpenCV(this, src);

  opencv.gray();
  opencv.threshold(70);
  dst = opencv.getOutput();

  contours = opencv.findContours();
  contours = opencv.getPoints();
  println("found " + contours.size() + " contours");
  println("found " + contours.numPoints() + " contours");

```

The scketch wont even run.  
It says that getPoints and numPoints “function does not exist”.

What am I doing wrong?  
Thanks

---

<div class="post-metadata">

### Author: ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)
#### Post date: [January 22, 2019, 11:07pm UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/2 "2019-01-22T23:07:49Z")

</div>

might wanna file this under [Processing Libraries](https://discourse.processing.org/c/processing/processing-libraries) for help regarding openCV

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [January 23, 2019, 1:06am UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/3 "2019-01-23T01:06:17Z")

</div>

I did, still no one can help me.

Any ideas?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 23, 2019, 5:17am UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/4 "2019-01-23T05:17:25Z")

</div>

when i run the full example  
Processing IDE / File / Examples / OpenCV for Processing / FindContours /  
and not your code snipplet  
it works here:  
Raspberry Pi / Raspbian Linux / Processing 3.5.1 / JAVA mode

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [January 23, 2019, 6:42am UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/5 "2019-01-23T06:42:20Z")

</div>

Yes, the example works for me too, but I need to get the number of pixels of the contour,

Any ideas?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 23, 2019, 7:22am UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/6 "2019-01-23T07:22:53Z")

</div>

yes  
( if you would have marked the 2 lines you added, it might have been more easy )

```auto
  contours = opencv.findContours();
  println("found " + contours.size() + " contours"); // shows found 166 contours
  
// @laptophead
// contours = opencv.getPoints();
// println("found " + contours.numPoints() + " contours");
  for (Contour contour : contours) { // kll add
    println("found " + contour.numPoints() + " points in contour " +contour);
  }

```

but that might be related to vertex points ( not same as pixels )

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [January 23, 2019, 2:42pm UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/7 "2019-01-23T14:42:04Z")

</div>

Thanks , that worked.  
I made a simple image for testing.

 ![elipse-testing](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/05ce3314050710debaca17741d15ba1ea0112fb3.png)

The outbound box is found as a contour and came up at 2388 points. Close enough to the 600x4 dimension. I guess some points are cut a the corners.  
But then the weirdest thing: The circle inside is not found as an edge.

Why would that be?  
Thanks

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 23, 2019, 2:54pm UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/8 "2019-01-23T14:54:34Z")

</div>

sorry, i know nothing about that library,  
but possibly the meaning of CONTOUR is outbounds?

-a- did you check all examples?  
-b- did you check on his book

> **[atduskgreg/opencv-processing-book](https://github.com/atduskgreg/opencv-processing-book)**
>
> An introduction to computer vision with OpenCV for Processing. - atduskgreg/opencv-processing-book

  
-c- did you try 2 concentric circles and what “he” find?

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [January 24, 2019, 12:33am UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/9 "2019-01-24T00:33:04Z")

</div>

Thanks ,  
I looked a the book, very little on this subject matter (contour).

I wonder if you’d know how to write your line:

```auto
println("found " + contour.numPoints() + " points in contour " +contour);

```

Specific for each contour.  
The sketch finds a number of contours and indexes them 0,1,2,3,…  
Right now the numPoints refers to the sum total of all of them.  
Is there syntax that would extract the points for contour 1 for instance?

Thanks  
My C++ is weak , sorry,

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 24, 2019, 1:01am UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/10 "2019-01-24T01:01:25Z")

</div>

> [@laptophead](#):
>
> Right now the numPoints refers to the sum total of all of them.

no, that’s why the loop was needed,  
contours is a array(list)  
and each contour in contours has numPoints,

so actually if you need all points must calc yourself:

```auto
  int allnum = 0;
  for (Contour contour : contours) { 
    int num = contour.numPoints();
    println("found " + num + " points in contour " +contour);
    allnum += num;
  }
  println("points sum: "+allnum);

```

p.s. array and arraylists have to be treated differently,  
pls check like

> **[StringList / Reference](https://processing.org/reference/StringList.html)**
>
> Helper class for a list of String objects. Lists are designed to have some features of ArrayList, but to maintain the simplicity and efficiency of working with arrays. Functions such as …

so there will be no  
contours[0]  
but  
contours.get(0).numPoints() might work.

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [January 26, 2019, 8:41pm UTC](https://discourse.processing.org/t/opencv-findcontours-wont-get-me-the-number-of-points/7749/11 "2019-01-26T20:41:00Z")

</div>

kll  
Thanks again, you’re a big help.  
The code above worked, however the result of println is not giving me the contour number as a 0,1,2 but I get:  
found 219 points in contour gab.opencv.Contour@56005110  
found 563 points in contour gab.opencv.Contour@118bf155  
found 3588 points in contour gab.opencv.Contour@3621520a

I wonder what those numbers mean. Is there a way to make them comprehensible?

also I tried

```auto
   int PointsFor0 = contours.get(0).numPoints() ;
    int PointsFor1 = contours.get(1).numPoints() ;
    int PointsFor2 = contours.get(2).numPoints() ;
    println(PointsFor0+ " " +PointsFor1+ " " +PointsFor2);

```

And it worked really well.  
I made a simple jpeg for testing and the numbers make sense.

 ![test1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c3d5ced8a93145364514a82f50dba945d2dde038.jpeg)

If you still want to help, there is one more thing. You could do this for the entire Processing community.  
Greg, the author of this library has also developed a compare contours function. That would be really useful to me and probably others.  
All the class definitions and stuff are here. I just wish I knew how to generate an example.  
But I dont. Wonder if you’d want to make one.

[http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/ContourComparator.html](http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/ContourComparator.html)
