Remove (or blur or shade) "white" background

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
grhey
Posts: 10
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Remove (or blur or shade) "white" background

Post by grhey »

Hi,
I'm sure this question has already been asked, but I can't find a clear answer.
Assuming an input picture (photograph) with a few (say, 1 to 3) objects on a "white" background. Something like the following:

Image

Is there a way to "clean up" that white background (and ideally also the surrounding noise, in this case the shadow of a finger in the lower left corner, and some non-white stuff in the middle of the right border) to obtain a picture like the following:

Image

A blurred or shaded background (to have a homogeneous, more "natural", gray background) would be even better.
Thanks in advance for your help!

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

Re: Remove (or blur or shade) "white" background

Post by fmw42 »

Not easy in ImageMagick. Try using the online tool at remove.bg

grhey
Posts: 10
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Remove (or blur or shade) "white" background

Post by grhey »

Hmmm... I have a large number of pictures to process, and it's for a hobby project, so using remove.bg it bit pricey...
Also, it seems that remove.bg solve a more general problem than what I want, I already assume a "white" background.
If necessary, I can take care of the "big" noise manually (shadow of the finger and stuff on the right).
You write that it's not easy, but can something that approaches the desired result not feasible?

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

Re: Remove (or blur or shade) "white" background

Post by fmw42 »

In your original image, the background is not even close to white. If you really have a nearly white background then do a fuzzy flood fill (or if desired a fuzzy simple threshold)

Code: Select all

convert image -fuzz XX% -fill white -draw "color x,y floodfill" -alpha off result
Pick x,y at some coordinates where the image background is white. Adjust XX% as desired for how close to white your background actually is and for variations in "white".

See

https://imagemagick.org/script/command- ... s.php#fuzz
https://imagemagick.org/Usage/draw/#color
https://imagemagick.org/Usage/quantize/#threshold

grhey
Posts: 10
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Remove (or blur or shade) "white" background

Post by grhey »

Thank you fmw42 for your answers!
Yes, I know that the background is not white (255,255,255), which is why I wrote "white" in quotes.
However the objet itself is white: these are white sheets, it's only because of the light and the camera that they become "non-white".
Are there no better solutions?

grhey
Posts: 10
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Remove (or blur or shade) "white" background

Post by grhey »

Note that, by iterating with the command you suggest around the borders of the picture, I obtain the following output:

Image

Surely there must be a way to remove the remaining noise pixels?

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

Re: Remove (or blur or shade) "white" background

Post by fmw42 »

Threshold the above image to that everything that is not white is black. Then use connected components to remove all regions smaller than some threshold. Save that as a mask and then use the mask to composite white over the areas you do not want. See https://imagemagick.org/script/connected-components.php

grhey
Posts: 10
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Remove (or blur or shade) "white" background

Post by grhey »

Many thanks again for your answer!
I'm trying to do that... but I'm struggling with the connected-components step.
Do you perhaps have a sample script that I could use as a basis?

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

Re: Remove (or blur or shade) "white" background

Post by fmw42 »

Here is an example using your image:

Input:
Image


1 ) read the input
2 ) clone and colorize with white
3-7 ) clone and make black/white, then use ccl to remove all regions smaller than 1000 pixels in area (optionally saved as c_mask.png)
8 ) composite the input, the white image and the final mask image

Code: Select all

convert c.jpg \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -fuzz 15% -fill white -opaque white \
-fill black +opaque white -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=1000 \
-connected-components 4 +write c_mask.png \) \
-compose over -composite -type truecolor \
c_filtered.jpg
Mask from CCL processing:
Image

Result:
Image

grhey
Posts: 10
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Remove (or blur or shade) "white" background

Post by grhey »

Wow! Thank you *very much* again!
This is exactly what I was trying to do...
It would have taken me days to figure out how to write that command line.
So apparently it is not so difficult to do with IM :-)

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

Re: Remove (or blur or shade) "white" background

Post by fmw42 »


Post Reply