Page 1 of 1

LAB channels don't behave during threshold

Posted: 2015-02-26T08:43:20-07:00
by tom_dl
I'm having trouble getting the result I want while using ImageMagick 6.8.9-5 Q16 x64 on Windows 7 64bit via Cygwin.

I want to take an input image, convert it to LAB colorspace, apply a threshold on the 'A' channel, and output this as a grayscale (more specifically black&white, as a threshold should be). I am using the following line:

Code: Select all

convert input.png -colorspace lab -channel G -separate -threshold 50% +channel ouput.png
The result I get is not grayscale at all, but rather a 'greenscale' with 126 colours (according to identify), rather than the 2 I'm expecting. I've tried using this line:

Code: Select all

convert input.png -colorspace lab -channel G -separate -threshold 50% -channel RB -evaluate set 0 +channel output.png
to zero channels L and B (i.e. R and B) as explained in the online documentation, but that's still an awful green mess. Can someone explain where I'm going wrong please?

Re: LAB channels don't behave during threshold

Posted: 2015-02-26T08:54:08-07:00
by tom_dl
Turns out I needed to -separate again:

Code: Select all

convert input.png -colorspace lab -channel G -separate -threshold 50% -channel G -separate output.png
Thought I'd leave this here for anyone else confused by separating channels.

Re: LAB channels don't behave during threshold

Posted: 2015-02-26T12:15:57-07:00
by snibgo
convert input.png -colorspace lab -channel G -separate -threshold 50% ouput.png
There are two problems with that command. The first is that PNG can't store Lab, so IM converts it back to sRGB. The cure is to lie to IM and tell it that the image is actually sRGB so there is no need to convert it. Do this with "-set colorspace sRGB".

The second problem is that "-channel G" remains in effect until cancelled with another "channel" command. Use "+channel" to get back to the default, which is RGB.

So we get:

Code: Select all

convert in.png -colorspace Lab -set colorspace sRGB -channel G -separate +channel -threshold 50% out.png

Re: LAB channels don't behave during threshold

Posted: 2015-03-12T09:03:41-07:00
by tom_dl
@Snibgo - thanks very much for your answers. I didn't realise PNG didn't support Lab, so tricking imagemagick didn't cross my mind. Great solution!