Page 1 of 1

change image background

Posted: 2018-09-29T02:38:01-07:00
by kmanoj24
Hi
I am using image magic command to convert the image background :

exec("convert test.JPG -resize 500x500 -quality 100 demo.png");
exec("convert demo.png \( +clone -fx 'p{10,10}' \) -compose Difference -composite -modulate 100,0 -alpha off difference.png");
exec("convert difference.png -bordercolor black -border 0 -threshold 15% -blur 0x3 -sharpen 0x10 halo_mask.png");
exec("convert demo.png -bordercolor white -border 0 halo_mask.png -alpha Off -compose CopyOpacity -composite test_halo.png");
demo.png
demo.png (346.05 KiB) Viewed 41159 times
converted image
test_halo.png
test_halo.png (310.89 KiB) Viewed 41158 times
converted image quality is not good and it also not make whole background white

Expected image :
test_halo.png
test_halo.png (310.89 KiB) Viewed 41158 times
Anybody please help me to found the expected solution.

Re: change image background

Posted: 2018-09-29T11:11:56-07:00
by fmw42
What is your Imagemagick version and platform, since syntax may vary.

Re: change image background

Posted: 2018-09-29T11:39:34-07:00
by fmw42
I have taken a different approach to work in HSL colorspace. Since your shoes are red and black would be similar in HSL, I rotate the colors by 180 degrees (-modulate 100,100,0) and separate the saturation channel (after reviewing all 3 channels for the best one to use). The color rotation was probably not needed since I am using saturation rather than hue. I then threshold the result and use -connected-components to remove noise and fill gaps. Then I use some morphology to smooth the result. The result is a black/white mask that I put into the alpha channel of the image.

Input:
Image

Unix Syntax:

Code: Select all

convert demo.png \
\( -clone 0 -modulate 100,100,0 -colorspace HSL \
-channel g -separate +channel -threshold 6% -type bilevel \
-define connected-components:area-threshold=150 \
-define connected-components:mean-color=true \
-connected-components 4 \
-morphology smooth octagon:2 \) \
-alpha off -compose copy_opacity -composite \
demo_nobg.png
Image


If you want a white background, then one way is

Code: Select all

convert demo.png \
\( -clone 0 -modulate 100,100,0 -colorspace HSL \
-channel g -separate +channel -threshold 6% -type bilevel \
-define connected-components:area-threshold=150 \
-define connected-components:mean-color=true \
-connected-components 4 \
-morphology smooth octagon:2 \) \
-alpha off -compose copy_opacity -composite \
-background white -alpha background -alpha off \
demo_nobg2.png
Image