Page 1 of 1

Subtract background image from foreground

Posted: 2016-10-24T12:20:32-07:00
by winkleink
I want to subtract background details from a picture.

The command I'm using on Linux is:
/usr/bin/convert /home/pi/chromaCam/actualimage.png \( backplain.png -channel A -threshold 99% \) -compose dst_out -composite /home/pi/chromaCam/Destination.png

Note: backslashes includes as being run on the command line

I based the command on the command here (viewtopic.php?t=17478) that worked for someone else.


Image with object in it - actualimage.png
https://drive.google.com/file/d/0B23BvT ... sp=sharing

Background - backplain.png
https://drive.google.com/file/d/0B23BvT ... sp=sharing

Subtract backplain from actualimage - Destination.png
https://drive.google.com/file/d/0B23BvT ... sp=sharing


The output is a completely transparent image.
Any thoughts, tips appreciated.

convert -version
Version: ImageMagick 6.8.9-9 Q16 arm 2016-09-23 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

Re: Subtract background image from foreground

Posted: 2016-10-24T16:46:49-07:00
by fmw42
The code in your link has an image that is transparent. Thus they deal with the alpha channel. Your image has no transparent background.

Furthermore, your two images do not have the exact same background color or texture, so using one to remove the other directly via subtraction is hard to do without normalizing the background image color to the same color as the actual image color.

Here are two approaches. The first removes the yellow line but gives a better definition of the object.

# does a divide of each image by the other and adds them together
# then thresholds to get a mask
# then applies morphology to remove small islands of black or white
# the puts the mask into the alpha channel

Code: Select all

convert actualimage.png backplain.png -alpha off \
\( -clone 0 -clone 1 -colorspace gray +swap -compose divide -composite -negate \) \
\( -clone 0 -clone 1 -colorspace gray -compose divide -composite -negate \) \
\( -clone 2 -clone 3 -compose plus -composite -threshold 7% \
-morphology smooth octagon:2 \) \
-delete 1-3 -alpha off -compose over -compose copy_opacity -composite result1.png
Image


# converts the image the HSV colorspace
# separates the Hue channel
# converts the background "gray(63)" to white and all the rest to black
# then applies morphology to remove small islands of black or white
# the puts the mask into the alpha channel

Code: Select all

convert actualimage.png -alpha off \
\( -clone 0 -colorspace HSV -channel red -separate +channel \
-fuzz 8% -fill white +opaque "gray(63)" -fill black +opaque white \
-morphology smooth octagon:2 \) \
-alpha off -compose over -compose copy_opacity -composite result2.png
Image

Re: Subtract background image from foreground

Posted: 2016-10-25T00:55:23-07:00
by winkleink
Thank you. WIll try it tonight.