Masking Image

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
Gurthfin
Posts: 11
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Masking Image

Post 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 :-? )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Masking Image

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Masking Image

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Masking Image

Post 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
Gurthfin
Posts: 11
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Re: Masking Image

Post 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 ... :?:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Masking Image

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Masking Image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Gurthfin
Posts: 11
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Re: Masking Image

Post by Gurthfin »

if I have reverse option:
Image
Image

can I create mask?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Masking Image

Post by fmw42 »

what is the issue/problem?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Masking Image

Post 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. :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Masking Image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Gurthfin
Posts: 11
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Re: Masking Image

Post 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)
Post Reply