Page 1 of 2

How to erase everything inside a border?

Posted: 2014-07-24T11:06:10-07:00
by ooker
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.

Re: How to erase everything inside a border?

Posted: 2014-07-24T11:21:08-07:00
by fmw42
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.

Re: How to erase everything inside a border?

Posted: 2014-07-24T11:28:32-07:00
by fmw42
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.

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:00:59-07:00
by ooker
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

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:22:34-07:00
by fmw42
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.

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:33:28-07:00
by ooker
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.

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:36:19-07:00
by fmw42
It would work if your border has no gaps. Your image edges clip off some red and that must be put back.

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:42:34-07:00
by fmw42
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

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:55:25-07:00
by ooker
Ahhhh, I see. After I change the position of the object to the better position, the command works. Thank you so much :D

Re: How to erase everything inside a border?

Posted: 2014-07-24T23:59:37-07:00
by ooker
Your latest code works perfectly. Thank you :D
Image

Re: How to erase everything inside a border?

Posted: 2014-07-25T00:08:15-07:00
by ooker
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.)

Re: How to erase everything inside a border?

Posted: 2014-07-25T00:09:06-07:00
by fmw42
No problem. As long as you got it to work.

Re: How to erase everything inside a border?

Posted: 2014-07-25T07:18:19-07:00
by ooker
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?

Re: How to erase everything inside a border?

Posted: 2014-07-25T10:42:25-07:00
by fmw42
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

Re: How to erase everything inside a border?

Posted: 2014-07-25T21:59:16-07:00
by iil888
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