Remove Border

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
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Remove Border

Post by JagVoylla »

All,

I have a simple image, where I need to remove one single color from background from this image

https://dl.dropboxusercontent.com/u/793 ... G_1033.png

I tried this command :-

convert TEST.png -fuzz 20% -fill white -opaque "#4DC2B2" M2_TEST_1.png

I get a clean image, but it has a green border. Is there a way I can remove this

Many Thanks for a great tool. I am able to save a lot of time !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Border

Post by fmw42 »

If you don't mind a totally grayscale image (where you lose the color of the earing), then just add -modulate 100,0,100 to the end of your command.

Code: Select all

convert TEST.png -fuzz 20% -fill white -opaque "#4DC2B2" -modulate 100,0,100 M2_TEST_3.png
Otherwise, try this in unix syntax, where I create your image, the grayscale image and a mask image that I fill any inside holes and then erode the edges, the composite together.

Code: Select all

convert TEST.png \
\( -clone 0 -fuzz 20% -fill white -opaque "#4DC2B2" \) \
\( -clone 1 -modulate 100,0,100 \) \
\( -clone 0 -fuzz 20% -fill black -opaque "#4DC2B2" -fill white +opaque black \
-morphology close octagon:5 -morphology erode octagon:10 \) \
-delete 0 -swap 0,1 -compose over -composite M2_TEST_4.png
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Remove Border

Post by JagVoylla »

Thanks you so much for your reply.

The second command works for jewellery that is more of silver and gold and lacks other colors.

For an image here the jewllery itself has some color, I am trying to use a different background and do a floodfill after reducing fuzz to 8 or 9% to avoid color going in the jewllery. I see that on these I still see a distinct border. Is there a way to avoid this border completely? can I specify mannequins color and tell imagemagick to drarw white on 4-5 pixel outside this boundary?

Started with this image :- https://dl.dropboxusercontent.com/u/793 ... I20178.png
Result after applying above commands : - https://dl.dropboxusercontent.com/u/793 ... ST_5_6.png

There is still a solid line around the mannequin which I would like to remove. Can not use fuzz >8 as that results in image gettig overwritten.


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

Re: Remove Border

Post by fmw42 »

try this with your last example

Code: Select all

convert TEST.png \
\( -clone 0 -fuzz 20% -fill white -draw "color 3904,2607 floodfill" \) \
\( -clone 1 -modulate 100,0,100 \) \
\( -clone 0 -fuzz 20% -fill black -draw "color 3904,2607 floodfill" -fill white +opaque black \
-morphology erode octagon:4 \) \
\( -clone 3 -morphology edgeout octagon:4 \) \
\( -clone 2 -clone 4 -clone 4 -compose over -composite \) \
-swap 2,5 -delete -1--2 \
-delete 0 -swap 0,1 -compose over -composite TEST_2.png
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Remove Border

Post by JagVoylla »

Thanks,

I got this as output :

https://dl.dropboxusercontent.com/u/79303486/SV_2.png

Everything else is okay, only thing is I am getting some gray lines inside the earrings. The dark color inside the image is also getting a border. Is there a way to specify clear border only adjacent to a particular color(white dummy) , only when color is a specific color(blue of background in this case)

Almost there!

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

Re: Remove Border

Post by fmw42 »

Either of these should fix the issue. The binary mask image of the head must have any interior black (from the earings cleared to white). This is two ways to do that. The second may be quicker than the first. The first uses morphology close to clear any black spots inside the head. The second uses another floodfill inside the head. The morphology erode and the morphology edgeout are eroding the mask a bit and then adding white border to clear any color or black around the head.

Code: Select all

convert TEST.png \
\( -clone 0 -fuzz 20% -fill white -draw "color 3904,2607 floodfill" \) \
\( -clone 1 -modulate 100,0,100 \) \
\( -clone 0 -fuzz 20% -fill black -draw "color 3904,2607 floodfill" -fill white +opaque black \
-morphology close octagon:7 -morphology erode octagon:4 \) \
\( -clone 3 -morphology edgeout octagon:4 \) \
\( -clone 2 -clone 4 -clone 4 -compose over -composite \) \
-swap 2,5 -delete -1--2 \
-delete 0 -swap 0,1 -compose over -composite TEST_2.png

Code: Select all

convert TEST.png \
\( -clone 0 -fuzz 20% -fill white -draw "color 3904,2607 floodfill" \) \
\( -clone 1 -modulate 100,0,100 \) \
\( -clone 0 -fuzz 20% -fill red -draw "color 3904,2607 floodfill" -fill white +opaque red \
-fill white -draw "color 2400,1000 floodfill" -fill black -opaque red -morphology erode octagon:4 \) \
\( -clone 3 -morphology edgeout octagon:4 \) \
\( -clone 2 -clone 4 -clone 4 -compose over -composite \) \
-swap 2,5 -delete -1--2 \
-delete 0 -swap 0,1 -compose over -composite TEST_3.png
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Remove Border

Post by JagVoylla »

Works perfectly!


Thank you so much!
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Remove Border

Post by JagVoylla »

Hi

I tried the same command on another image where I am using neckup design and I get this :https://dl.dropboxusercontent.com/u/79303486/TEST4.png

Please pardon my ignorance but I assumed white masking would work here as well. Should I change parameters of this command.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Border

Post by fmw42 »

Each image may be different and require different argument or different code. It would appear that your image does not have a full border around the head, so floodfill will stop. You may try putting a border around your image of the same color as around the head. Then you will need to find appropriate coordinates to floodfill so that it gets a full flood around the whole object.
Post Reply