Automated Image Background removal Software

10,784

Solution 1

Try this (using ImageMagick):

convert picture.jpg -fill none -fuzz 12% -draw "matte 0,0 floodfill" -flop  -draw "matte 0,0 floodfill" -flip  -draw "matte 0,0 floodfill" -flop  -draw "matte 0,0 floodfill" -flip  result.png

It basically takes an image file (i.e. picture.jpg), and using a fuzz factor of 12% (you can play with this value for better results) floodfills the image based on the pixel colors of the image's four corners. After all that process, the image outputted is result.png.

Based on this post: http://snippets.aktagon.com/snippets/558-how-to-remove-a-background-with-imagemagick

Solution 2

How are you using Imagemagick? I am doing something similar in one project, with this commands:

# Detect modified pixels
composite -compose difference picture1.jpg picture2.jpg diff.png
# Ignore minor differences (jpeg noise)
convert -threshold 25% diff.png diff2.png
# Apply mask
composite -compose CopyOpacity diff2.png picture.jpg result.png

Note: I use a very old imageMagick version (6.2.4.3). Commands syntax may have changed.

Share:
10,784
user_rz_jaz
Author by

user_rz_jaz

Senior Software Engineer with a focus on C# and ASP.NET.

Updated on June 04, 2022

Comments

  • user_rz_jaz
    user_rz_jaz almost 2 years

    I am looking for a tool that can remove the background from an image in an automated way requiring zero human interaction. I'm currently experimenting with a tool called Image Magick (http://www.imagemagick.org/script/index.php). I'm using actual photos that are taken by me. I first take a photo of a background, then I introduce an object into that same frame and take another shot. I have the camera on a tripod so that there is no movement. The shadows that are cast by that object seems to effect the results as well as the texture of the background. I don't mind if the preparation of the scene takes more time as long as I can successfully remove the entire background without having to touch it up in photoshop (or any other image editing application). The issues I'm facing with ImageMagick, is that it is also removing parts of the object in the foreground. Does anyone know of any other tool or suggestions on how to "prep" the scene in a way that I can cleanly remove the background only using this tool or any other? All tips/advice/suggestions are greatly appreciated. Thanks everyone!

  • user_rz_jaz
    user_rz_jaz about 13 years
    Hi Paco. We're using it like this: convert overlaid.jpg background.jpg -compose ChangeMask -fuzz 10000 -composite background_removed.png
  • user_rz_jaz
    user_rz_jaz about 13 years
    Any chance I can get your email address Paco to communicate directly and ask you more questions about imagemagick? Maybe you can help me out?
  • Francisco R
    Francisco R about 13 years
    I'm sorry but i think questions should be kept at Stackoverflow so they can be read by other programmers with similar problems. Also, questions will get to a bigger audience, potentially getting more answers.
  • Francisco R
    Francisco R about 13 years
    Maybe just playing with fuzz factor (lowering it) is enough. BTW, I prefer the old method (generating a difference mask) because of its greater flexibility. Have a look at this ImageMagick examples Difference Image Masking and Feathering. The last one, blurring the mask, may do the trick.
  • user_rz_jaz
    user_rz_jaz about 13 years
    Thanks,I'll keep trying. The problem is the many variables involved, mainly sunlight and shadowing. When I introduce a bigger object into the frame, it seems to absorb some of the light and cast a shadow of course, causing the overall picture to be slightly darker which does change at the pixel level and is perceived from imageMagick as a difference in the picture causing parts of the object to be removed. I believe this is the reason why it's not able to perfectly separate an object from a background. Tell me, are you able to remove it perfectly when you take a picture of a real world scene?
  • Francisco R
    Francisco R about 13 years
    I never tried with real world scenes. Are you adjusting your camera settings to manual mode (fixed aperture, fixed shutter speed)? If you are using "auto" mode and you introduce an object in the center of your scene, aperture and/or shutter speed should change and that could be the reason why you are getting pictures with different brightness. Try adjusting (manually or auto) your camera with the object already in place, remove the object, and take a second shot with the same settings of the first shot. It should allow you to reduce the fuzz factor in ImageMagick and may be solve the problem.
  • cancerbero
    cancerbero almost 5 years
    nice! With ImageMagick 7 now your command would be convert img.png -fill none -fuzz 12% -draw 'color 0,0 floodfill' -flop -draw 'color 0,0 floodfill' -flip -draw 'color 0,0 floodfill' -flop -draw 'color 0,0 floodfill' -flip result.png