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");
converted image
converted image quality is not good and it also not make whole background white
Expected image :
Anybody please help me to found the expected solution.
change image background
change image background
- Attachments
-
- process.jpg (7.6 KiB) Viewed 80447 times
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: change image background
What is your Imagemagick version and platform, since syntax may vary.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: change image background
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:
Unix Syntax:
If you want a white background, then one way is
Input:
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
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