Page 1 of 1

Masking Image

Posted: 2013-04-30T18:56:12-07:00
by Gurthfin
I have image
Image
and mask
Image

when I merge it with

Code: Select all

convert CB01-M.bmp -negate CB01-M.png
convert CB01.png CB01-M.png -alpha off -compose copy-opacity -composite CB02ImageMagic.png
result is
Image

but when I merge it with Photoshop, result is far better (look at hair)
Image

what do I wrong?

(sorry for my English, if is bad :-? )

Re: Masking Image

Posted: 2013-04-30T19:10:17-07:00
by fmw42
try this and adjust the "2" to a value you like.


convert image.bmp \( mask.bmp -negate -evaluate multiply 2 \) -alpha off -compose copy_opacity -composite result.png

Re: Masking Image

Posted: 2013-04-30T19:13:26-07:00
by snibgo
What version of IM are you using? I can repreduce the Photoshop result with...

Code: Select all

"%IMG685%convert" CB01-M.bmp -gamma 0.45 -negate CB01-M.png
"%IMG685%convert" CB01.bmp CB01-M.png -alpha off -compose copy-opacity -composite CB02ImageMagic2.png
...using IM v6.8.5 on Windows 7.

Re: Masking Image

Posted: 2013-04-30T19:16:44-07:00
by fmw42
This gets close by changing the mask to linear gray

convert image.bmp \( mask.bmp -colorspace gray -negate \) -alpha off -compose copy_opacity -composite 1tmp3.png

In Windows, remove the two \

This also gets close

convert image.bmp \( mask.bmp -negate \) -set colorspace RGB -alpha off -compose copy_opacity -composite -set colorspace sRGB 1tmp6.png

Using IM 6.8.5.3 Q16 Mac OSX Snow Leopard

Re: Masking Image

Posted: 2013-04-30T19:52:18-07:00
by Gurthfin
fmw42 wrote:This gets close by changing the mask to linear gray

convert image.bmp \( mask.bmp -colorspace gray -negate \) -alpha off -compose copy_opacity -composite 1tmp3.png

In Windows, remove the two \

This also gets close

convert image.bmp \( mask.bmp -negate \) -set colorspace RGB -alpha off -compose copy_opacity -composite -set colorspace sRGB 1tmp6.png

Using IM 6.8.5.3 Q16 Mac OSX Snow Leopard
Thank you, I ended with merge your ideas:

Code: Select all

convert CB01.bmp \( CB01-M.bmp -negate -evaluate multiply 1.2 \) -set colorspace RGB -alpha off -compose copy_opacity -composite -set colorspace sRGB CB02ImageMagic2.png
result is perfect

One more question: is there any way to create mask from image without alfa? If I have only image, but no mask and want perfect result.. or is it too complicated ... :?:

Re: Masking Image

Posted: 2013-04-30T21:06:18-07:00
by fmw42
One more question: is there any way to create mask from image without alfa? If I have only image, but no mask and want perfect result.. or is it too complicated ...
For most images, you would not get a good mask by thresholding, floodfilling, etc, which are the common ways to extract a mask. Sometimes you can convert the image to grayscale (if it has a clean white or black background) and then threshold an blur a little to anti-alias. But this is likely image dependent and probably won't get you a clean enough mask, especially when you have fine detail such as hair. But perhaps other users can comment further on their experiences.

Re: Masking Image

Posted: 2013-04-30T23:32:19-07:00
by anthony
The only way to get a perfect mask, is to have the same forground image on two different backgrounds.

Without that you basically do not have enough information to...
1/ get the right transparency along the edges.
2/ get the right color along the edges

That is you need two sources of information to get the 2 types of missing data.

See Background Removal using Two Backgrounds
http://www.imagemagick.org/Usage/maskin ... background


Without this input, all you can do is... make a rough guess.

Re: Masking Image

Posted: 2013-05-20T04:44:21-07:00
by Gurthfin
if I have reverse option:
Image
Image

can I create mask?

Re: Masking Image

Posted: 2013-05-20T10:55:53-07:00
by fmw42
what is the issue/problem?

Re: Masking Image

Posted: 2013-05-20T18:14:19-07:00
by anthony
No. You need known colors.

The only difference between these images is that the image was darkened uniformly, that does not add more information to determine the actual foreground color of a pixel when it has been anti-aliased by the background (get how 'transparent' each pixel is). You need different (and known' background colors)

Now if you have the same foreground image on a white background as well as the previously given black background the whole thing becomes easy!

Actually even a image overlayed on variable (but different) known backgrounds can be used. I have recovered transparency from anime images where the image was overlayed on two very different background images, and I had a copy of the original background images.

You have one (foreground on black). Do you have another image with the character overlaid on a known background image (say a image of the 'room' the anime character was in) and a image of that room (preferably as light as posible but with no black pixels in that background)

Backgrounds just have to be as different as posible, and known. Not necessary a flat color.

PS: yes I know what sort of 'game' this was from. :-)

Re: Masking Image

Posted: 2013-05-20T18:44:10-07:00
by anthony
Addendum... one way to make a rough guess as to the original color of a anti-aliased pixel (at least for cartoon characters) is to dilate the image once and only deal with a pixel next to pure black in the original image..

Hmmm a fudged (not exact) solution.

boolean mask of all non-black pixels

Code: Select all

convert anime.png -fill white +opaque black boolean_mask.png
Get the 'edge' pixels which should be almost (but all, the anti-aliased pixels)
For example it will fail for thin wedges from her hair.

Code: Select all

convert boolean_mask.png -morphology edgein square edge_mask.png
This last can be improved with a difference image against ANY other background even if that background is unknown!

now dilate colors (actual colors by intensity not channels) to expand 'lighter' colors,

Code: Select all

convert anime.png -morphology dilateI diamond color_dilate.png
and mask compose those over the original to get what should be roughly the right 'edge' color

Code: Select all

convert anime.png color_dilate.png edge_mask.png -composite  edge_colored.png
A compare of the original against this last image will show what edges are failing to get color good result.
At this point I quit the attempt, as there were too many problem areas with this image 'shape'.

Get me the image on other backgrounds (any backgrounds) and I will proceed further.

Re: Masking Image

Posted: 2013-05-21T07:47:58-07:00
by Gurthfin
sorry, no other background... character images are only with black background. I have only mask in .alp files, but that is format created in developer studio, so no program can handle it. (Its only one studio, so I can live with it.. but one good thing, I better understood masking mechanics, thx for effort)