# Anyone knows how to use getConvexHull();?

**URL:** <https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268>\
**Category:** Libraries\
**Created:** [February 10, 2019, 3:55pm UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268 "2019-02-10T15:55:43Z")\
**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:** [February 10, 2019, 3:55pm UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/1 "2019-02-10T15:55:43Z")

</div>

I am using the OpenCV library, the example called “FindContours”.

I tried to obtain a new contour based on the ConvexHull function, but I get the same thing.

So I tried:

```auto
for (Contour contour : contours) {
    stroke(0, 255, 0);
    contour.getConvexHull();
    contour.draw();
    
    stroke(255, 0, 0);
    beginShape();
    for (PVector point : contour.getPoints()) {
     //contour.setPolygonApproximationFactor(3);
     //vertex(point.x, point.y);
    }
    endShape();
  }

```

Anyone knows why my contour is the same, with or without contour.getConvexHull(); line?

Here is a pic I prepared for testing

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

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 11, 2019, 2:07am UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/2 "2019-02-11T02:07:17Z")

</div>

Hi! If you look at the source code

> <https://github.com/atduskgreg/opencv-processing/blob/master/src/gab/opencv/Contour.java#L109>

  
it seems like the method returns a new contour but you are not using it (not even storing it in a variable).

---

<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:** [February 12, 2019, 10:39pm UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/3 "2019-02-12T22:39:30Z")

</div>

Thanks for your help.  
I was able to get a new contour. It has a different number of points than the original.  
However , the area of the new contour is identical with the area of the original contour.  
I wonder why. Can you help?  
void ConvexHull()  
{ contours2 = opencv.findContours(true,true); // Passing ‘true’ sorts them by descending area.

for (int i=0; i\<contours2.size(); i++) {

```
Contour contour = contours2.get(i);
println (i);
Rectangle r = contour.getBoundingBox();

if ((contour.area() > 0.9 * src.width * src.height) || (r.width < blobSizeThreshold || r.height < blobSizeThreshold))
  continue;

```

contour= contour.getConvexHull();  
noFill();  
strokeWeight(2);  
// rect(r.x, r.y, r.width, r.height);

```
   stroke(255, 255, 0);
  contour.draw();
    float ConvexArea = contour.area();
  println ("ConvexArea =" + ConvexArea);

  int ContourPointsV1_Convex= contour.numPoints();
  println ("ConvexHull Points : "+ ContourPointsV1_Convex);

```

}  
}  
Here is the 2 contours. ![ConvexHull](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/75ecd7823437bc609a8443220f069dce6b55f247.png)  
type or paste code here

```auto

```

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 13, 2019, 4:37am UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/4 "2019-02-13T04:37:11Z")

</div>

> [@laptophead](#):
>
> Can you help?

Maybe if you mention what you need? 🙂

---

<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:** [February 13, 2019, 6:33am UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/5 "2019-02-13T06:33:27Z")

</div>

As you see above there are 2 contours. A regular one and the CovexHull.

When I extract the area of each of them I get the same number of pixels.  
That is erroneous since the yellow bound contour is larger as you can see.

How do I get an accurate area for each?

My application requires that I compare the two areas.  
What to do ? Thanks

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 14, 2019, 7:01am UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/6 "2019-02-14T07:01:22Z")

</div>

Hi, so if you again look at the source code of `.area()` to look at what it does, you see this:

> <https://github.com/atduskgreg/opencv-processing/blob/master/src/gab/opencv/Contour.java#L175>

There you can see that it only returns the size of the bounding box, and the bounding box is equal in both cases. If you want the precise area you could try the third approach shown here:

> **[How to Calculate the Area of a Polygon](https://www.wikihow.com/Calculate-the-Area-of-a-Polygon)**
>
> Calculating the area of a polygon can be as simple as finding the area of a regular triangle or as complicated as finding the area of an irregular eleven-sided shape. If you want to know how to find the area of a variety of polygons, just...

---

<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:** [February 14, 2019, 3:38pm UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/7 "2019-02-14T15:38:04Z")

</div>

Thanks  
I never realized I was getting the area of a bounding box.  
I guess, I will have to fill my contours, do a saveFrame, bring the pic back to sketch and do a pixel count of the filled areas.  
My areas are highly irregular, thanks for the polygon formula. It wont apply since I have curbed edges.

I wish there was a way to Snapshot my object right after the contour is drawn. Would make the sketch faster. Saving to hard drive and reloading seems cumbersome.  
Thanks

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 15, 2019, 1:15am UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/8 "2019-02-15T01:15:08Z")

</div>

You don’t need to save and load an image, you can draw things to a `PGraphics` and then access the pixels from there. See `createGraphics`.

---

<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:** [February 16, 2019, 8:48pm UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/9 "2019-02-16T20:48:23Z")

</div>

I tried your method in the example “FindContours” from the OpenCV library.

after the PG definition in the setup, I tried:

```auto
  for (Contour contour : contours) {
    
    
    stroke(0, 255, 0);
    
    pg.beginDraw();
    pg.background(100);
    contour.draw();
    pg.endDraw();
    image(pg, 9, 30); 
  image(pg, 51, 30);
    
    stroke(255, 0, 0);
    beginShape();
    for (PVector point : contour.getPolygonApproximation().getPoints()) {
      vertex(point.x, point.y);
    }
    endShape();
  }

```

I was only able to get a gray square, but there was no contours in it.  
I tried the pg.contour.draw(); hoping to make it work but it would not compile.  
Is there a way to do this ?

Thanks a lot.  
Mitch

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 17, 2019, 7:40am UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/10 "2019-02-17T07:40:44Z")

</div>

Maybe something in this direction?

```auto
pg.beginDraw();
pg.background(100);
pg.noStroke();
pg.fill(255);
for (Contour contour : contours) {
  pg.beginShape();
  for (PVector point : contour.getPolygonApproximation().getPoints()) {
    pg.vertex(point.x, point.y);
  }
  pg.endShape();
}
pg.endDraw();

```

---

<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:** [February 17, 2019, 5:03pm UTC](https://discourse.processing.org/t/anyone-knows-how-to-use-getconvexhull/8268/11 "2019-02-17T17:03:31Z")

</div>

Thanks a lot, that worked,

If others need this, I ended up with:

```auto
void draw() {
  scale(0.5);
  image(src, 0, 0);
  image(dst, src.width, 0);

  noFill();
  strokeWeight(3);
  
  pg.beginDraw();
 //pg.background(0);
//pg.noStroke();
  pg.stroke(255, 0, 0);
  pg.strokeWeight (3);
  pg.noFill();
  pg.fill(255);
  
  for (Contour contour : contours) {
   pg.beginShape();
    stroke(255, 255, 0);
    contour.draw();
    
     pg.beginShape();
    for (PVector point : contour.getPolygonApproximation().getPoints()) {
      pg.vertex(point.x, point.y);
    }
     pg.endShape();
  }
  

    pg.endDraw();
       image(pg, 700, 0); 
   
}
 
and here is the image I was able to generate:![PNG|518x346](upload://xy9oMpVdGB2Xn5G2gBpZpVivHhR.jpeg) 

```

You are from Berlin? I have been there , Viellen Daken

This helps a lot with my project/  
I also wonder if there is a way to capture the graphics for the  
" contour.draw();" that does not relay on vertexes.

Thanks
