How to crop heads/faces from a photo?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pinoguin
Posts: 7
Joined: 2011-03-15T08:01:04-07:00
Authentication code: 8675308
Location: Philippines

How to crop heads/faces from a photo?

Post by pinoguin »

Hi,

My mission is to let users upload their own photos then the site crops the face+hair and pastes them on an ecard.

I understand there are ajax or js face detection solutions out there, but what stumps me is how do I crop an odd shaped pattern. ImageMagick as I know can only crop a rectangular or square shaped object (please correct me if I'm wrong). Will this pretty much destroy the whole idea until im has the functionality to crop odd shapes? or are there other ways to crop circles or polygons?

Then I've heard about using stencils, if I could somehow allow the user to plot points in the image, form the lines to create the path or polygon then fill a solid color outside the face path. Then I merge both the stencil + the photo, leaving me with the head/face of the photo with a solid bg. The solid color is then converted to transparent so it can be pasted onto an ecard, anyone done this before?

What other methods can I use which could be simpler?
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: How to crop heads/faces from a photo?

Post by HugoRune »

Cropping is limited to rectangular areas, since image files are by definition rectangular.

You can however mask/make transparent any shape you want, see http://www.imagemagick.org/Usage/masking/#editing

Automatically recognizing the boundaries of faces is very hard; for skin you might be able to do some color recognition, but hair in front of an unknown background is almost impossible.

You can let users supply the shapes manually, but the results will probably look a bit chopped.
pinoguin
Posts: 7
Joined: 2011-03-15T08:01:04-07:00
Authentication code: 8675308
Location: Philippines

Re: How to crop heads/faces from a photo?

Post by pinoguin »

Many thanks for the info,

@chopped: is there some sort of feathering option when cutting off the image or creating this stencil for masking? or other means? it's kinda important since while I can use curves to draw the stencil it's still too sharp and ugly looking when posted an ecard.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to crop heads/faces from a photo?

Post by fmw42 »

see http://www.imagemagick.org/Usage/blur/#feathering and http://www.imagemagick.org/Usage/masking/#difference

the general idea that I have used on a binary mask is to use a linear blur followed by a mapping to make the blur only on one side (or the other) of the mask, depending upon what you want to do. So if the mask is white on black:

convert mask -blur 5x65000 -level 0x50% featheredmask

or

convert mask -blur 5x65000 -level 50x100% featheredmask
pinoguin
Posts: 7
Joined: 2011-03-15T08:01:04-07:00
Authentication code: 8675308
Location: Philippines

Re: How to crop heads/faces from a photo?

Post by pinoguin »

Success!

Image

However the plotting of the curves in js2draw is smoother than IM's 'path' that I used, is there such a thing as 'curve' in IM? (http://jsdraw2d.jsfiction.com/demo/curvesbezier.htm)


These are the commands used (size may vary):

Code: Select all

This command will create our stencil (coordinates generated by js2draw): 

convert -size 450x125 xc:black -fill white -stroke black -draw "path 'M +60+9 +94+18 +96+19 +84+27 +92+36 +97+43 +103+56 +102+58 +109+66 +109+74 +101+68 +98+76 +98+84 +95+88 +98+91 +106+95 +110+99 +111+103 +99+106 +89+108 +73+112 +56+109 +40+109 +26+103 +37+97 +46+91 +48+88 +39+80 +36+71 +32+78 +27+72 +30+61 +35+55 +42+41 +30+37 +40+24 +51+14 +156+9 +197+6 +236+8 +269+16 +265+36 +248+50 +222+52 +213+35 +198+24 +174+18 +155+13 +60+9'" dummy.gif


We will then combine the two: 

convert logo.jpg dummy.gif -alpha off -compose CopyOpacity -composite final.png


Finally we feather out the edges: 

convert final.png -alpha set -virtual-pixel transparent -channel A -blur 0x0.7 -level 50,100% +channel -background none -flatten test.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to crop heads/faces from a photo?

Post by anthony »

pinoguin wrote:However the plotting of the curves in js2draw is smoother than IM's 'path' that I used, is there such a thing as 'curve' in IM?
Yes in a fairly raw way... Cubic Curves.
http://www.imagemagick.org/Usage/draw/#cubic

Unfortunately it needs an extra control point for each 'knot' in the curve to define the slope and strength of the curve at each point.

I think one of Fred Weinhaus's scripts can define this curve in a more automated way.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pinoguin
Posts: 7
Joined: 2011-03-15T08:01:04-07:00
Authentication code: 8675308
Location: Philippines

Re: How to crop heads/faces from a photo?

Post by pinoguin »

Many thanks for the assistance but I can't find the curves script http://www.fmwconcepts.com/imagemagick/curves/index.php , this one looks like it's for colors. He has too many scripts to browse I see...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to crop heads/faces from a photo?

Post by fmw42 »

pinoguin wrote:Many thanks for the assistance but I can't find the curves script http://www.fmwconcepts.com/imagemagick/curves/index.php , this one looks like it's for colors. He has too many scripts to browse I see...
First, the script you might be wanting is called spline, though the curves script uses spline to apply a transformation to the image.

Second, the feathering is usually done to the mask, not the image. It is usually feathered from interior to the mask to the edge of the mask so that it does not extend outside the bounds of the mask, but tapers up to that edge. So that should be

convert mask -blur 5x65000 -level 50x100% featheredmask

(note adjust the 5 in 5x65000 as desired for amount of feathering)

Then the feathered mask is applied as an alpha channel to the image via (one possible method)

convert image featheredmask -alpha off -compose copy_opacity -compose -background white -flatten resultimage

Other compose methods may be more efficient.

Other feathering techniques can be achieved using -morphology distance measures, which may be better. My method above is rather simplistic, but works well for most images where there is not a very narrow area or corner. See the docs on morphology.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to crop heads/faces from a photo?

Post by Bonzo »

Do you have a link for js2draw as I can not find it ?
pinoguin
Posts: 7
Joined: 2011-03-15T08:01:04-07:00
Authentication code: 8675308
Location: Philippines

Re: How to crop heads/faces from a photo?

Post by pinoguin »

@fmw42: Thanks a lot!
Bonzo wrote:Do you have a link for js2draw as I can not find it ?
I posted it above, http://jsdraw2d.jsfiction.com/ try clicking on the demos to get a better look.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to crop heads/faces from a photo?

Post by Bonzo »

Thanks for the link; I think I must have "Skipped" that line :?

It would be good if you could automaticaly get the points into a php variable.
Post Reply