Lab colorspace - clipping

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?".
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Lab colorspace - clipping

Post by GreenKoopa »

Thank you Fred! I have been searching my computer and the internet for such a profile but had turned up nothing. I wondered if such a creature was even possible, and snibgo pointed out that such a profile was unnecessary. My stupid question paid off!
fmw42 wrote:IM's LAB was corrected some time ago to fix issues by adding offsets so that it would work in non-HDRI mode.
I looked into the offset of a & b. IM adds 32768 (#8000). The standard seems to be 32896 (#8080). That makes sense from a historic perspective, when a signed 8-bit value needed to be offset to an unsigned value by adding 128. It also makes sense when you need to write to a 8-bit file. IM's selection of offset means that a image saved as 24-bit Lab can't be exactly grayscale. Also, the Lab space modes of tiff and pdf probably expect this standard too. My thoughts anyway.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lab colorspace - clipping

Post by fmw42 »

I looked into the offset of a & b. IM adds 32768 (#8000). The standard seems to be 32896 (#8080).
I know little about the math for producing such and odd standard or who uses that "standard". But IM is just offsetting the values by half the dynamic range or your IM compile. So for Q16 compile that dynamic range would be 0 to 65535 and the halfway is 32767.5 if you just divide by 2 or if you look at it as a range of 65536 and divide by 2, you get exactly 32768. That is mid gray in Q16. It is needed when you do not allow negative values such as in HDRI where you can have negatives and the Lab a and b are properly positive and negative about zero. In non-hdri, you need to offset them so you can save an image without clipping the negatives.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lab colorspace - clipping

Post by snibgo »

fmw42 wrote:If you want a LAB.icc profile, the Mac OSX ships with one (also one for XYZ.icc). I have no experience using it.
Thanks for that.

It's an interesting profile. It converts from colorspace Lab to colorspace Lab, and in the opposite direction. It transforms via the connection space Lab.

As we might expect, the transformation matrices are unity, and the look-up tables for each channel have two only values: 0->0 and 65535->65535. Experimenting with it, it appears to make no changes at all to images, even with different intents.

In itself, I can't see any use for it. But it could be used as the basis for a tweaked profile, for example to tweak saturation in Lab space.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Lab colorspace - clipping

Post by GreenKoopa »

Sorry, I could have explained offsets better.

Observation 1:
Here I convert a black-to-white gradient to Lab using two methods. Using profiles, a & b are near #8080 (32896). Using IM's -colorspace Lab a & b are near #8000 (32768).

Code: Select all

convert -size 10x1 gradient:#000-#FFF -profile sRGB_IEC61966-2-1_black_scaled.icc -profile GenericLabProfile.icc -set colorspace RGB txt:-

# ImageMagick pixel enumeration: 10,1,65535,rgb
0,0: (    0,32896,32896)  #000080808080  rgb(0,128,128)
1,0: ( 6835,32897,32895)  #1AB38081807F  rgb(10.4295%,50.1976%,50.1946%)
2,0: (15607,32898,32894)  #3CF78082807E  rgb(23.8148%,50.1991%,50.193%)
3,0: (23687,32898,32894)  #5C878082807E  rgb(36.144%,50.1991%,50.193%)
4,0: (31307,32899,32894)  #7A4B8083807E  rgb(47.7714%,50.2007%,50.193%)
5,0: (38593,32899,32893)  #96C18083807D  rgb(58.8891%,50.2007%,50.1915%)
6,0: (45619,32900,32893)  #B2338084807D  rgb(69.6101%,50.2022%,50.1915%)
7,0: (52429,32900,32892)  #CCCD8084807C  rgb(80.0015%,50.2022%,50.19%)
8,0: (59057,32900,32892)  #E6B18084807C  rgb(90.1152%,50.2022%,50.19%)
9,0: (65535,32896,32896)  #FFFF80808080  rgb(255,128,128)

Code: Select all

convert -size 10x1 gradient:#000-#FFF -colorspace Lab -set colorspace RGB txt:-

# ImageMagick pixel enumeration: 10,1,65535,rgb
0,0: (    0,32768,32768)  #000080008000  rgb(0%,50.0008%,50.0008%)
1,0: ( 6841,32768,32766)  #1AB980007FFE  rgb(10.4387%,50.0008%,49.9977%)
2,0: (15610,32768,32766)  #3CFA80007FFE  rgb(23.8193%,50.0008%,49.9977%)
3,0: (23687,32769,32765)  #5C8780017FFD  rgb(36.144%,50.0023%,49.9962%)
4,0: (31308,32769,32765)  #7A4C80017FFD  rgb(47.7729%,50.0023%,49.9962%)
5,0: (38594,32769,32765)  #96C280017FFD  rgb(58.8907%,50.0023%,49.9962%)
6,0: (45618,32769,32764)  #B23280017FFC  rgb(69.6086%,50.0023%,49.9947%)
7,0: (52429,32770,32764)  #CCCD80027FFC  rgb(80.0015%,50.0038%,49.9947%)
8,0: (59058,32770,32763)  #E6B280027FFB  rgb(90.1167%,50.0038%,49.9931%)
9,0: (65534,32770,32763)  #FFFE80027FFB  rgb(99.9985%,50.0038%,49.9931%)
Observation 2:
IM uses a different offset when in 8-bit vs 16-bit. This results in a color shift when moving between these, like when opening a 8-bit file in 16-bit IM. A 8-bit Lab grayscale image should remain Lab grayscale in 16-bit.

History:
The full range of a & b nearly fits into an unsigned byte (-128...127*), so it was used**. When unsigned is needed, 128 is added to shift the range to 0...255. Then 16-bit processing began. To keep compatibility with still existing 8-bit processing and old 8-bit files, the offset was kept the same. 128 (#80) upped to 16-bit is 32896 (#8080). You can try this in IM. The color is named fractal. The color #8000 has no name in IM.
* This range is a computer math thing, totally unrelated to imaging or color profiles
** unscaled, so with a litte clipping of the full Lab space. A nice visualization at the bottom of this page.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lab colorspace - clipping

Post by fmw42 »

I believe the IM code comes from Lindbloom
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: Lab colorspace - clipping

Post by jwtrexler »

The first Window in this thread shows the Command line is " Convert infile -colorspace lab -separate outfile" wherein the generated output file to that command is a table displaying the mean, max, and min Lab values of the infile. When I use this command the output file does not display this info, but creates an image. Additionally, I would like to know the standard deviations of the three Lab values. Suggestions with respect to what I am doing wrong and should do?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lab colorspace - clipping

Post by fmw42 »

try

Code: Select all

convert infile -colorspace lab -separate -verbose info:
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: Lab colorspace - clipping

Post by jwtrexler »

I am still experiencing a couple issues/stumbling blocks to extracting the Cielab statistics from an image originally in RGB format.

First, I can use the -verbose info: command to obtain the RGB values for the image, however when I utilize the command convert infile.jpg -depth 16 -colorspace lab -verbose info:, I do not get the correct values for the L*, a* and b* channels, which I believe from what I have read, correspond to the output channels RGB, respectively. I have manually calculated the Cielab variables from the RGB values output utilizing the -verbose info: command on the original RGB image.

Second, when I include the -separate in the command line above, I get a dumping of several K-bytes of data with the summary data at the beginning of the respective channel dumpings. As with the explanation above, the Cielab values output are not correct.

Any other thoughts? Also, it would be nice to have a minimal amount of select data output such as mean, std dev, max, min, kurtosis and skewness for each of the three Cielab variables, as in the -verbose info: output file, but not sure if this is possible with minimal coding.

Thank you as always,
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: Lab colorspace - clipping

Post by jwtrexler »

Amendment to last sentence in second paragraph above:

I have manually calculated the Cielab variables from the RGB values output utilizing the -verbose info: command on the original RGB image and do get the calculated correct L* a* and b* values for the digital image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lab colorspace - clipping

Post by fmw42 »

Does it give better results if you do

Code: Select all

convert infile -set colorspace sRGB -colorspace lab -separate -set colorspace sRGB -verbose info:
You also have to remember that in IM, the A and B channels are 50% biased to avoid negative values.

The get limited output from -verbose info, you can use sed to filter it (on unix). You can also output a few statistics using string formats.

Code: Select all

convert image -format "%[mean] %[standard_deviation] %[skewness] %[kurtosis]" info:
for values in your Q range (Q16 is 0 to 65535). Or

Code: Select all

convert image -format "%[fx:mean] %[fx:standard_deviation] %[fx:skewness] %[fx:kurtosis]" info:
for values in the range of 0 to 1


Where image is any one channel from

convert image -colorspace lab -separate result

Or you can list all 3 images (channels) and get a listing for each from the above code

Code: Select all

convert result1 result2 result3 -format "%[mean] %[standard_deviation] %[skewness] %[kurtosis]" info:

Or you can do it all at once

Code: Select all

convert image -colorspace lab -separate -format "%[mean] %[standard_deviation] %[skewness] %[kurtosis]" info:

See
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/script/fx.php
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lab colorspace - clipping

Post by snibgo »

1. Calculate the average of the red channel, the average of green channel, and the average of blue channel. Convert the resulting RGB to Lab.

2. Convert the RGB image to Lab. Calculate the average of each channel.

The result from (1) will generally be different to (2).

Is this the problem?
snibgo's IM pages: im.snibgo.com
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: Lab colorspace - clipping

Post by jwtrexler »

fmw42: I will try as you have suggested, but there's still a bug in the colorspace command if the image is 8 bit, so that's why I converted the depth of the image to 8 bit then next convert it to a tiff file before the colorspace command. I'll get back with you and let you know what I find.

Snibgo: I will try both options and see which one gives the most correctly calculated Cielab variables based on the RGB inputs to the two methods of conversion. Would you give me some guidance on the command to us that will allow me to average the channels. I believe convert infile -depth 16 -??type (jpg-to-tiffconversion)?? -channel -"mean" "??output??". In other words, I will need to read more on how to, in one command line, convert the file from jpg-to-tiff, determine if channel along accomplishes the separation into 3 channels or do I need to add Lab to the -colorspace, how to get the mean of the channels, and how to output the results into a useful format (visual on the screen or a file at first). It would be best if, when I get further along with what I'm trying to objective, I can output the 5 pieces of statistical data on a single line separated by commas and have the line pushed/triggered to an open file where the respective stats could be appended to a file having the same stats for images previously evaluated. I know, I'm trying to accomplish a lot to not have been more familiar with IM and image analysis in general, but I'm much closer than I was when I first began thinking and attempting to accomplish this objective not even knowing at that time the best software to accomplish my task.

Again, both of you have been very helpful and I do appreciate you time and thoughts.

Cheers,
Jack
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lab colorspace - clipping

Post by fmw42 »

but there's still a bug in the colorspace command if the image is 8 bit
Can you explain in more detail with your command and an example at 16bits and 8bits so we can see what you mean?
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: Lab colorspace - clipping

Post by jwtrexler »

Fmw42,
If you look at my post under "Possible bug reporting Lab colors as pixels: values" on 7/3/14 @ 22:03:37 and the following corresponding response(s) from Snibgo, I provided the inputs wrt 8 bit vs 16 bit and the corresponding calculated Lab outputs which Snibgo confirmed and helped with the work-around.

At this point, I do have one more question: I have created a bat file with exactly the same commands that works at the command line and am not having succes getting it to run by drag and drop approach; when a drop the file, the bat file opens a command window that contiuously echos the command in a continuous loop. For example, if I create a bat file having: convert -verbose *.jpg then drag/drop a file called infile.jpg, the window opens and the continuous looping begins. I have read and tried many options to no avail. Your help/suggestions would be greatly appreciated.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Lab colorspace - clipping

Post by snibgo »

jwtrexler wrote:For example, if I create a bat file having: convert -verbose *.jpg then drag/drop a file called infile.jpg, the window opens and the continuous looping begins.
Your bat file ignores the input, and processes all the files in the directory matching *.jpg.

Perhaps you wanted to process %1 instead.
snibgo's IM pages: im.snibgo.com
Post Reply