Extract Hand from 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
luya
Posts: 1
Joined: 2016-10-03T11:31:34-07:00
Authentication code: 1151

Extract Hand from photo

Post by luya »

Hello,

I would like extract hand from photo for apply other filter after.

It's possible ?

This corresponds to a little magic wand photoshop.

It's example of photo :
Image
Thanks for help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract Hand from photo

Post by snibgo »

There are many methods for this. One is "GrowCut Segmentation", which needs my process module, and a swipe mask. Here, I made the swipe mask in Gimp.
The swipe mask, mainMsk.png:
Image

Code: Select all

%IMDEV%convert ^
  main.jpg ^
  ( +clone ^
    mainMsk.png -process growcut ^
    -alpha off ^
    -fill Black -opaque Lime ^
    -fill White -opaque Red ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  main_only.png
Image
If you have a large number of similar photos (the hand in different positions), you could use data from the manual swipe mask to make all the others automatically.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract Hand from photo

Post by snibgo »

Instead of manually creating a swipe mask, we can make it automatically, by specifying a pair of rectangles: one at the hand, and another at a ceiling tile. This gives the result:
Image
The script for this is slightly complex. It works in HC space (HCL, with L set to constant 50%). It makes a map of average colours in the user-specified rectangles, plus a gray to represent the transitions. It remaps to those colours (with no dithering). It does a "-connected-components", keeping the two largest components, making the rest transparent. It dilates the transparent areas with disk:3. The result is the swipe mask for GrowCut, as above.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract Hand from photo

Post by snibgo »

Here's a much simpler method. Lower quality, but not bad:

Code: Select all

convert ^
  main.jpg ^
  ( +clone ^
    -colorspace Lab -set colorspace sRGB ^
    -channel R -evaluate Set 50%% ^
    +dither -colors 2 ^
    -set colorspace Lab -colorspace sRGB ^
    -channel RGB -auto-level +channel ^
    -colorspace Gray -auto-level -negate ^
    -blur 0x1 ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  main_ab2.png
Image
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract Hand from photo

Post by fmw42 »

Here is another approach. Measure the skin color at some point on the hand that is representative of the proper hue. Convert the color and extract the hue. Then convert the image to extract the hue and threshold on each side of the hue, since it is close to 360. The hue is about 97% so threshold at 90 and above. And at 10% and below to get a mask and use the mask to isolate the hand in the original. Adjust the 90% and 10% as desired to get a better isolation.

In Unix code:

Code: Select all

color="rgb(139,71,82)"
hue=`convert xc:$color -colorspace HSV txt: | sed -n 's/^.*hsv[(]\([^,]*\),.*,.*[)].*$/\1/p'`
hue=`convert xc: -format "%[fx:100*$hue/360]" info:`
echo "hue=$hue;"
convert main.jpg \
\( -clone 0 -colorspace HSV -channel red -separate +channel -threshold 90% \) \
\( -clone 0 -colorspace HSV -channel red -separate +channel -threshold 10% -negate \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 -compose over \
-alpha off -compose copy_opacity -composite result.png
Image

Unfortunately, hue=0 is red, but also cannot be distinguished from black/gray/white where the saturation is near zero. So you should create other masks to remove the black and the white (or near zero saturation). But that may remove part of the hand which is very dark, if the threshold is not very low.
Post Reply