Anyone knows how to use getConvexHull();?


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:

for (Contour contour : contours) {
    stroke(0, 255, 0);
    stroke(255, 0, 0);
    for (PVector point : contour.getPoints()) {
     //vertex(point.x, point.y);

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

Here is a pic I prepared for testing


Hi! If you look at the source code

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


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))

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

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

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

Here is the 2 contours.ConvexHull
type or paste code here


Maybe if you mention what you need? :slight_smile:


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


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

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:


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.


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.