Page 1 of 1

Background Subtraction

Posted: 2011-07-14T02:59:31-07:00
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.

Re: Background Subtraction

Posted: 2011-07-14T04:14:39-07:00
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.

Re: Background Subtraction

Posted: 2011-07-14T10:03:10-07:00
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/

Re: Background Subtraction

Posted: 2011-07-14T17:40:44-07:00
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.

Re: Background Subtraction

Posted: 2011-07-15T09:39:37-07:00
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.

Re: Background Subtraction

Posted: 2011-07-15T09:59:50-07:00
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

Re: Background Subtraction

Posted: 2011-07-15T10:02:06-07:00
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.

Re: Background Subtraction

Posted: 2011-07-15T10:16:50-07:00
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

Re: Background Subtraction

Posted: 2011-07-15T10:24:30-07:00
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

Re: Background Subtraction

Posted: 2011-07-15T11:00:25-07:00
by fmw42
best I can suggest is to post links to your fabric images so others can see what is going on.

Re: Background Subtraction

Posted: 2011-07-15T11:12:58-07:00
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.

Re: Background Subtraction

Posted: 2011-07-15T12:31:57-07:00
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.

Re: Background Subtraction

Posted: 2011-07-15T21:05:33-07:00
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)

Re: Background Subtraction

Posted: 2011-08-17T00:20:34-07:00
by sin
Can you please explain that command for background subtraction???

Re: Background Subtraction

Posted: 2011-08-17T20:39:57-07:00
by anthony
sin wrote:Can you please explain that command for background subtraction???
Which specific command? Did you have a look at the links?