LAB channels don't behave during threshold

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
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

LAB channels don't behave during threshold

Post 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?
Last edited by tom_dl on 2015-02-26T08:54:25-07:00, edited 1 time in total.
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Re: LAB channels don't behave during threshold

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

Re: LAB channels don't behave during threshold

Post 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
snibgo's IM pages: im.snibgo.com
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Re: LAB channels don't behave during threshold

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