Non-rectangular face extraction with openCV

That’s my first message here - I’m really seduced by openFrameworks but I’m still struggling a lot with it, I’m coming from actionscript / processing, and I didn’t know anything of C++ until last week.
Also, please excuse my english that can sometimes sucks.

I’m trying to use the method haarFinder of the openCV addon, which works great, but in all of the examples that I’ve seen so far, you can only extract the regions that match as ofRect instances.

How would I proceed to draw in a texture the shape of a head ? I’ve seen you can extract blobs with openCV, but I didn’t manage to get anything to work.

There’s on vimeo & youtube videos of installations made with oF that extracts heads as non-rectangular shapes, so I guess this is possible (such as this one :…-my/term/32)… Any help would be greatly appreciated.

Many thanks in advance !

There’s some more info on another thread describing how that exact installation was accomplished, with source:

I don’t think it actually “extracts” face-shaped images, but it applies a blurred elliptical mask to the rectangular image.

I can’t look at the video you linked and answer quickly but you could look at 2 functions in openCV:
Searching the minimal contour enclosing Circle or Ellipse

Thanks a lot for your answers. Actually I did it in a different way, thanks to some topics I found on the forum.

First, I load a grayscale mask picture (basically an ellipse with a gaussian blur), that I resize to the ROI dimensions.
I then loop through the pixels of the videoGrabber image, copy just the ROI portion (as explained here) and then set the alpha pixels to match the mask one (as explained here).

I made the video below using this techniques.

But I will look for cvMinEnclosingCircle() and cvFitEllipse2() as well :slight_smile:

That’s actually really entertaining to watch!

If you’re willing to deal with a little latency, you could try smoothing out the rectangle center and size over time (which would help avoid the jumping around).

Also, detecting the eyes and then using that for determining the width rather than the rectangle might be more accurate (especially in near-portrait cases).


You could also use jupapy’s great code example as posted here
and here

to stabilize the “face shaking”.

Hey, thanks a lot for the hints, I will try this today.
That’s great to see such a reactive community - you guys really rock.