Page 1 of 1
Extract Hand from photo
Posted: 2016-10-03T11:37:37-07:00
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 :
Thanks for help.
Re: Extract Hand from photo
Posted: 2016-10-03T13:44:36-07:00
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:
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
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.
Re: Extract Hand from photo
Posted: 2016-10-03T22:56:11-07:00
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:
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.
Re: Extract Hand from photo
Posted: 2016-10-04T19:40:29-07:00
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
Re: Extract Hand from photo
Posted: 2016-10-04T20:28:42-07:00
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
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.