How to erase everything inside a 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?".

How to erase everything inside a border?

Postby ooker » 2014-07-24T11:06:10+00:00

I have many monochrome images which always have a closed border. I want everything inside that border to be cleared, the output is only the border.
Input: Image Output: Image

If you have a direct option, then it's great. If not, I have three aproaches:
1. Change the color of the border, then remove red lines.
2. Change the color of the outside to red, then take it and convert it back.
3. Change the color of the inside to red, then take it and convert it back.

I don't know what to do. Do you have any ideas? Thank you.
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-24T11:21:08+00:00

Your image is not monochrome -- that would be only two colors and typically black and white.


Nevertheless, you have red pixels inside the border that touch the border. How are you going to change the border pixels only?

Lets say you can change only the border red pixels to blue, call this blue.png

Code: Select all
convert blue.png -fuzz XX% -fill white +opaque blue -fill red -opaque blue red.png


-fuzz XX% will allow the border pixels not to be perfectly red (for example if anti-aliased). If they are all perfectly red, then remove -fuzz or set XX to 0.

The trick is isolating only the border red pixels from the other red pixels interior that are also red and touch the border.

Your image also has an alpha channel, which seems to be there for anti-aliasing. So the above would need to have the image flattened first.

Even removing the alpha channel shows that the underlying image has black mixed with the red. So that complicates it.
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-24T11:28:32+00:00

You could also floodfill from the outside to the boundary. Then convert that to a binary mask. Then use -morphology edgein/edgeout (?) to replace your boundary and color it red.

This seems to do it. But you have to choose the edge thickness that matches the thickness of your boundary.

Unix syntax:

Code: Select all
convert e1.png -background white -flatten -write show: -bordercolor white -border 1 \
-fill blue -draw "color 0,0 floodfill" -alpha off -shave 1x1 \
-fill white +opaque blue -fill black -opaque blue \
-morphology edgein octagon:3 \
-fill red -opaque white -fill white +opaque red result.png



Windows syntax:

Code: Select all
convert e1.png -background white -flatten -write show: -bordercolor white -border 1 ^
-fill blue -draw "color 0,0 floodfill" -alpha off -shave 1x1 ^
-fill white +opaque blue -fill black -opaque blue ^
-morphology edgein octagon:3 ^
-fill red -opaque white -fill white +opaque red result.png


This requires IM 6.5.9-3 or higher for -morphology

There are other ways to do it as well, but most that I have tried, leave the black pixels mixed with your red border pixels.
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby ooker » 2014-07-24T23:00:59+00:00

Thanks, your command works. However it only works for small objects, it doesn't for a larger and more complicate one.

Input: Image
Result: Image

If possible, the small dot outside the border should be remove too. All I need is the border.

Also, I would like the output to have a transparent background. Is there a way to put this command
Code: Select all
convert result.png -transparent white result1.png
to your code? Thank you.

PS: I use Unix
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-24T23:22:34+00:00

I am not sure what you mean by it does not work for larger images? Also what do you mean by the (red) border. I was assuming you just wanted the outer boundary and not any parts that are interior and that the outer boundary was complete with no gaps and there were no outside points or clusters of red. Please clarify what you want or show a good result for your latest image.

Also what version of IM are you using?

Here is the code modified to make the background transparent.

Code: Select all
convert e1.png -background white -flatten -bordercolor white -border 1 \
-fill blue -draw "color 0,0 floodfill" -shave 1x1 \
-fill white +opaque blue -fill black -opaque blue \
-morphology edgein octagon:3 \
-channel rgba -fill red -opaque white -fill none +opaque red result.png


I do not think I know any way to remove the smaller red marks and leave all the other unchanged. Some very small one can be removed using -morphology close. But if the size of the close it near than the thickness of your red curves, it will remove even those.

Perhaps someone else might have some thought or suggestions.
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby ooker » 2014-07-24T23:33:28+00:00

Yes it is. I want the output is like this:
Image

The white square you see is when I erased it and forgot to make it transperate again.

Edited: Just see your edited post. Thank you for your help.
Last edited by ooker on 2014-07-24T23:37:26+00:00, edited 1 time in total.
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-24T23:36:19+00:00

It would work if your border has no gaps. Your image edges clip off some red and that must be put back.
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-24T23:42:34+00:00

try this. It seems to work for this image.


Code: Select all
w2=`convert overlaycba2f.png -format "%[fx:w-2]" info:`
h2=`convert overlaycba2f.png -format "%[fx:h-2]" info:`
convert overlaycba2f.png -background white -flatten -bordercolor red -border 1 \
-fill blue -draw "color 2,2 floodfill color $w2,2 floodfill color $w2,$h2 floodfill color 2,$h2 floodfill"  \
-fill white +opaque blue -fill black -opaque blue \
-morphology edgein octagon:3 \
-channel rgba -fill red -opaque white -fill none +opaque red -shave 1x1 result.png
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby ooker » 2014-07-24T23:55:25+00:00

Ahhhh, I see. After I change the position of the object to the better position, the command works. Thank you so much :D
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby ooker » 2014-07-24T23:59:37+00:00

Your latest code works perfectly. Thank you :D
Image
Last edited by ooker on 2014-07-25T01:46:49+00:00, edited 2 times in total.
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby ooker » 2014-07-25T00:08:15+00:00

Sorry, when I used your "lastest" code, I didn't notice that I just pasted the old code.

(Since you have delete your post, can you also delete this one? I can't find anywhere to delete this.)
Last edited by ooker on 2014-07-25T00:10:11+00:00, edited 1 time in total.
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-25T00:09:06+00:00

No problem. As long as you got it to work.
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby ooker » 2014-07-25T07:18:19+00:00

How can I do this with another color besides red? Say black. At first, I think I can change all the red to black, but the output is all black. However, when I change to other colors like orange, the output is good. I have tried changing some other color in other positions in your code but haven't got any success. Do you have any ideas?
ooker
 
Posts: 22
Joined: 2014-07-24T10:32:57+00:00

Re: How to erase everything inside a border?

Postby fmw42 » 2014-07-25T10:42:25+00:00

This should work with any color lines. And you can now change the color of the lines in the output using the color argument.


Code: Select all
infile="test_black.png"
color="black"
w2=`convert $infile -format "%[fx:w-2]" info:`
h2=`convert $infile -format "%[fx:h-2]" info:`
convert $infile -background white -flatten -fill black +opaque white -bordercolor black -border 1 \
-fill none -draw "matte 2,2 floodfill matte $w2,2 floodfill matte $w2,$h2 floodfill matte 2,$h2 floodfill" \
-fill white +opaque none -fill black -opaque none \
-alpha off -morphology edgein octagon:3 \
-channel rgba -fill none +opaque white -fill $color -opaque white -shave 1x1 result.png
User avatar
fmw42
 
Posts: 15003
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to erase everything inside a border?

Postby iil888 » 2014-07-25T21:59:16+00:00

Thanks.
This is definitely going to help me to solve my problem.
I have been trying to erase any line inside a border and was having a similar issue. Let's just say that I am still not an expert wit imagemagick.

______________________________
iil888
Last edited by iil888 on 2014-09-14T01:40:07+00:00, edited 1 time in total.
iil888
 
Posts: 1
Joined: 2014-07-25T21:52:41+00:00

Next

Return to Users

Who is online

Users browsing this forum: No registered users and 22 guests