Background Subtraction

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
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Background Subtraction

Post by madhu »

Hi,

I am trying to subtract the background of the images.
I have the first Image with Black background and red circle as an object.
Second image is only black background.

I want to subtract the Second image from first image.
The resultant image should be red circle only with no background...

Thanks,
Madhu.
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by madhu »

Hi,

I would like to show the images.So I have generated the url.

My first image is http://imageshack.us/photo/my-images/191/alp1q.png/
My second image is http://imageshack.us/photo/my-images/694/blacklq.png/
I want to subtract second image from the first.

I am expecting the output to be "Red circle with no background"


Thanks,
Madhu.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background Subtraction

Post by fmw42 »

This is one way. You find the difference image and threshold to binary and then put that as the alpha channel of the first image.


convert alp1q.png blacklq.png \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -compose copy_opacity -composite tmp.png

This is for unix. If on windows, see difference in syntax at http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Background Subtraction

Post by anthony »

You can do this, but it will only work if the foreground does not contain any of the background colors.

That is to say any part of the foreground object that is colored the same as the background oject will become transparent. This is exactly how green screens for Movies and TV work. The background colors are removed from the image, a technique known as Chroma Masking.

Also it has sever problems with anti-aliasing issues. That is pixels tend to be all, or nothing, and edges tend to have a halo of the background color in them. Blonde hair on a green screen for example is one of the most difficult things to correct!

Essentually the technqiue works but only superficially.

What is missing is enough information to determine, not just if a pixel is foreground or background, but exactly how much of the pixel is foreground or background. That is how transparent each pixel is. To fix that you need at least two images with the exact same foreground image on two different (and known) backgrounds.

See IM examples Background Removal using Two Backgrounds
http://www.imagemagick.org/Usage/maskin ... background
for the full details.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by madhu »

Hi,

Thanks for the reply fmw42.
I am trying to run to run Imagemagick 6.6.9 in "cygwin".
When I am running command which you have suggested then it is displaying error syntax error near unexpected token '('.
Is there any problem with the bracket in cygwin?.Why is it showing that error???

Is there any other way to run the command which you have suggested without using brackets?


Thanks,
Madhu.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background Subtraction

Post by fmw42 »

madhu wrote:Hi,

Thanks for the reply fmw42.
I am trying to run to run Imagemagick 6.6.9 in "cygwin".
When I am running command which you have suggested then it is displaying error syntax error near unexpected token '('.
Is there any problem with the bracket in cygwin?.Why is it showing that error???

Is there any other way to run the command which you have suggested without using brackets?


Thanks,
Madhu.

Show us your exact command. You must have a space between ( ) and any command inside the parens.

see http://www.imagemagick.org/Usage/windows/#cygwin for issue between windows cygwin and other unix systems
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by madhu »

Hi,

Thanks for the reply anthony.

I have tried the background subtraction with the same examples in the link.I got the correct image as shown.

I have tried the same method for other examples which has fabric as an object.
The fabric is a transparent or netted fabric with different background i.e., same fabric in pink and dark blue background, but I got the output with the light blue background.
Can you suggest me a way for retaining the originality of the fabric.

Thanks,
Madhu.
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by madhu »

Hi,

This is my command " convert alp1.png black.png ( -clone 0 -clone 1 -compose difference -composite -threshold 0 ) -delete 1 -compose copy_opacity -compose tmp.png"

The error is " bash: syntax error near unexpected token '(' "
Suggest me if I need to make any changes.....

madhu
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by madhu »

Hi,

That brackets issue is resolved fmw42.
The link which You sent me helped in resolving the problem.

But I have the problem which I have posted to anthony.
The background subtraction for the transparent fabric object.

Madhu
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background Subtraction

Post by fmw42 »

best I can suggest is to post links to your fabric images so others can see what is going on.
madhu
Posts: 20
Joined: 2011-07-06T22:34:20-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by madhu »

Hi,

I am posting the link of the fabric.

http://imageshack.us/photo/my-images/695/pinknet.png/
http://imageshack.us/photo/my-images/27/bluenet.png/

The resultant image should be only the fabric without any background.


Thanks,
Madhu.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background Subtraction

Post by fmw42 »

madhu wrote:Hi,

I am posting the link of the fabric.

http://imageshack.us/photo/my-images/695/pinknet.png/
http://imageshack.us/photo/my-images/27/bluenet.png/

The resultant image should be only the fabric without any background.


Thanks,
Madhu.

Since your backgrounds are not complementary and not constant color (and you have jpg images which will be compressed and change colors), the best I can do is:


convert bluenet.png pinknet.png -alpha off \
\( -clone 0 -clone 1 -compose difference -composite -auto-level -threshold 20% -negate -write 1tmpb.png \) \
-delete 1 -compose copy_opacity -composite net_recovered.png

But it needs the colors corrected as in what is done when you have complementary flat colors at http://www.imagemagick.org/Usage/maskin ... background

The backgrounds would need to be for example constant blue and yellow and saved as gif or png not jpg.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Background Subtraction

Post by anthony »

madhu wrote:Hi,

Thanks for the reply anthony.

I have tried the background subtraction with the same examples in the link.I got the correct image as shown.

I have tried the same method for other examples which has fabric as an object.
The fabric is a transparent or netted fabric with different background i.e., same fabric in pink and dark blue background, but I got the output with the light blue background.
Can you suggest me a way for retaining the originality of the fabric.

Thanks,
Madhu.
The examples assume the exact background color used is at pixel 0,0 (top left corner). With fabric covering the whole image that may not be the case. If that is so, you will need to adjust the examples appropriately, to give it the background color more directly.


And don't use GIF, whcih can be worse than JPEG in color loss for real images due to its limited color table. Use PNG to TIFF (without JPEG compression)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sin
Posts: 46
Joined: 2011-04-23T11:06:13-07:00
Authentication code: 8675308

Re: Background Subtraction

Post by sin »

Can you please explain that command for background subtraction???
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Background Subtraction

Post by anthony »

sin wrote:Can you please explain that command for background subtraction???
Which specific command? Did you have a look at the links?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply