Page 1 of 1

Transparency: Cutting Out Bordered Objects

Posted: 2015-05-12T04:23:03-07:00
by thewickedd
Dear All,

I have images enclosed with a black anti-aliased edge on a white background. I have been reading the tutorials on how to remove the white background (only surrounding the image) and make it transparent, and have found http://www.imagemagick.org/Usage/masking/#bg_remove. However, i have the same problems with the floodfill as identified there, i end up with some bordering almost-white pixels.

Then i read in the next section, http://www.imagemagick.org/Usage/masking/#border_cut. It sounds very promising and applicable to my situation. If i can tell imageMagick that my background is white and the border black, it'll know how to deal with the fuzzy edges perfectly. However, sadly this section is under construction. How can I achieve this?

Sample image can be found here:
https://www.dropbox.com/s/yzexsn2sn5qvn ... 0.png?dl=0
https://www.dropbox.com/s/1v8k2bhw3mnff ... 0.png?dl=0

I am running the latest ImageMagick on windows 8.1 64bit: ImageMagick 6.9.1-2 Q16 x64 2015-04-14.

Thank you for your time and all the best!

Re: Transparency: Cutting Out Bordered Objects

Posted: 2015-05-12T08:59:33-07:00
by fmw42
Note your file has a space before the period. I removed that. This works for me. Adjust the fuzz value as desired

Code: Select all

convert 001.png -fuzz 10% -fill none -draw "matte 0,0 floodfill" 001_trans.png

Re: Transparency: Cutting Out Bordered Objects

Posted: 2015-05-13T02:17:42-07:00
by thewickedd
Dear Fred,

Thanks for the reply, that is pretty close. However, when putting the image on a darker background, I can see some whitish pixels around its borders.

The http://www.imagemagick.org/Usage/masking/#border_cut section hints that it is possible to do transparency at the borders of the image perfectly, in that e.g. fully white would be fully transparent, and fully black fully opaque and the rest in between (I'm saving to png so have a full resolution alpha channel). The only thing I have found online that I hoped would be of relevance was the filltoborder option, but this appears to also be a boolean operation. Using

Code: Select all

convert 001.png -fuzz 90% -fill none -bordercolor black -draw "matte 0,0 filltoborder" 001_trans.png
I managed to get exactly the same result as you.

Is it currently possible to do such transparency modeling of the borders when you tell imagemagick the background and border colors, or is that bit in the manual under construction exactly because it isn't yet?

If not, that's ok, then I'll implement what I need with matlab.

Thanks a lot!

Re: Transparency: Cutting Out Bordered Objects

Posted: 2015-05-13T09:22:26-07:00
by fmw42
I am not sure what you mean by "transparency modeling". You can use less fuzz value and extract the alpha channel as binary mask. Then use -morphology erode or dilate depending upon the binary polarity to remove pixels around the border. Then replace the alpha channel with the new trimmed mask.

This seems to work for me:

Code: Select all

convert 001.png -fuzz 10% -fill none -bordercolor black -draw "matte 0,0 floodfill"  \
-channel alpha -morphology erode octagon:1 +channel 001_trans.png
The larger the integer octagon value the more it will remove white aliased pixels around the border of the object.

Try adjusting both the fuzz value and the octagon value to get what you want

Re: Transparency: Cutting Out Bordered Objects

Posted: 2015-05-15T01:10:33-07:00
by thewickedd
Thanks Fred, that works pretty (very) well!