inconsistency in transforming to gray scale

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Antosha

inconsistency in transforming to gray scale

Post by Antosha » 2008-04-08T02:31:00-07:00

I compared two ways of transforming an image to gray scale. And for some reason they don't match exactly:

convert rose: -fx intensity channel_gray_2.gif
convert rose: -colorspace Gray channel_gray_1.gif
compare -metric MSE channel_gray_1.gif channel_gray_2.gif null

The last command gives:
123.073 dB
70,46,BMP

(The file sizes are also different)

So, some pixels are different.

I think that these two command should give an exact result.
I think this a bug related to some rounding done in different order.

User avatar
magick
Site Admin
Posts: 10957
Joined: 2003-05-31T11:32:55-07:00

Re: inconsistency in transforming to gray scale

Post by magick » 2008-04-08T09:49:11-07:00

We have a patch for the problem you reported in ImageMagick 6.4.0-5 Beta available sometime tomorrow. Thanks.

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

Re: inconsistency in transforming to gray scale

Post by fmw42 » 2008-04-08T15:55:08-07:00

Just curious but should you also get the same result using:

convert rose: -type Grayscale channel_grayscale.gif

I found that it does.

convert rose: -type Grayscale rose_grayscale.gif
convert rose: -colorspace Gray rose_gray.gif
compare -metric rmse rose_gray.gif rose_grayscale.gif null:
0 (0)

But what about

convert rose: -colorspace HSL -channel Blue -separate rose_lightness.gif
convert rose: -fx intensity rose_intensity.gif

strange - but these do not compare favorably

compare -metric rmse rose_intensity.gif rose_lightness.gif null:
4016.37 (0.0612858)

and neither do these

compare -metric rmse rose_grayscale.gif rose_lightness.gif null:
4016.37 (0.0612858)

compare -metric rmse rose_gray.gif rose_lightness.gif null:
4016.37 (0.0612858)

I would have expected that rose_lightness.gif and rose_intensity.gif should be identical! (Unless the -fx intensity is using the single hexcone Brighness?)

However, I am not sure converting to intensity by either -fx or -colorspace HSL will/should necessarily result in the same answer as using the more direct -type Grayscale or -colorspace Gray, since the conversion to Lightnes (double hexcone HSL) should be using (min(R,G,B)+max(R,G,B))/2 and the more direct method should be using (R+G+B)/3.

I hope magick can double check and look into this and report back.

To me the confusion is what is meant by "intensity" in the -fx listing when it is right along with Hue and Saturation. As such I would have expected it to be Lightness ( HSL double hexcone model - which uses (min(R,G,B)+max(R,G,B))/2) or Brightness (HSB single hexcone model - which uses max(R,G,B))

Antosha

Re: inconsistency in transforming to gray scale

Post by Antosha » 2008-04-09T00:12:23-07:00

To me the confusion is what is meant by "intensity" in the -fx listing when it is right along with Hue and Saturation. As such I would have expected it to be Lightness ( HSL double hexcone model - which uses (min(R,G,B)+max(R,G,B))/2) or Brightness (HSB single hexcone model - which uses max(R,G,B))
I think that intensity should mean luminous intensity (http://en.wikipedia.org/wiki/Luminosity),
that is their should be separate names lightness and brightness
for the cases that you mentioned.
And intensity should match GrayScale.
the more direct method should be using (R+G+B)/3
I would assume that it uses more advanced weights:
0.299 R + 0.587 G + 0.114 B

What ever the solution, it should be clearly stated in the ImageMagick documentation:
What is meant by intensity, grayscale, brightness. It would be even better if a direct formula (including the rounding, truncating, ...) that is being used in the program is given. (That is the results should be repeatable and it shouldn't be ambiguous)

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

Re: inconsistency in transforming to gray scale

Post by fmw42 » 2008-04-09T12:21:12-07:00

I would agree that it would be nice if the formulae for each was more clearly described somewhere.

I should point out that -fx allows reference to intensity, saturation, hue and luminosity. So I am still not sure of the exact definitions of intensity and luminosity here.

Again, I hope that magick can illuminate us (sorry for the pun :wink: )

User avatar
magick
Site Admin
Posts: 10957
Joined: 2003-05-31T11:32:55-07:00

Re: inconsistency in transforming to gray scale

Post by magick » 2008-04-10T06:30:08-07:00

The FX intensity is the standard 0.299*red+0.587*green+0.114*blue. Hue, saturation, and luminosity result from the standard published RGB to HSL algorithm.

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

Re: inconsistency in transforming to gray scale

Post by fmw42 » 2008-04-10T11:01:49-07:00

1) Are you saying that luminosity is really lightness from HSL?

2) Luminosity generally has a different meaning from those, perhaps wrongly identified. See Wikipedai.org

"In Adobe Photoshop's imaging operations, luminosity is the term used incorrectly to refer to the luma component of a color image signal; that is, a weighted sum of the nonlinear red, green, and blue signals. It seems to be calculated with the Rec. 601 luma co-efficients (Rec. 601: Luma (Y’) = 0.299 R’ + 0.587 G’ + 0.114 B’)."

So it appears that IM is using intensity as luminosity (luma) and luminosity as lightness :?: :?
Confusing terminology for me. (Perhaps you would consider changing your fx reference to luminosity to lightness as it is properly identified in the RGB to HSL algorithms to be consistent with Hue, Saturation and Lightness)


3) What algorithms are used for RGB to Gray using:

convert rgbimage -colorspace Gray grayimage

convert rgbimage -type Grayscale grayimage

Is it (R+G+B)/3? --- This is what I believe is normally considered as Intensity!

Thanks

Fred

Antosha

Re: inconsistency in transforming to gray scale

Post by Antosha » 2008-04-10T11:51:31-07:00

3) What algorithms are used for RGB to Gray using:

convert rgbimage -colorspace Gray grayimage

convert rgbimage -type Grayscale grayimage

Is it (R+G+B)/3? --- This is what I believe is normally considered as Intensity!
I think it's the standard formula used in Computer Graphics, when an rgb image is needed to be transformed to a grayscale image:
0.299*red+0.587*green+0.114*blue
So it appears that IM is using intensity as luminosity (luma) and luminosity as lightness :?: :?
I agree there is some confusion, since i would expect that luminosity is the same as intensity. That is, the term intensity usually refers to luminosity as Luminous intensity in http://en.wikipedia.org/wiki/Luminous_intensity.
Intensity is a term from physics, and therefore it should be based on a physical formula, instead of abstract color space formulas.

User avatar
magick
Site Admin
Posts: 10957
Joined: 2003-05-31T11:32:55-07:00

Re: inconsistency in transforming to gray scale

Post by magick » 2008-04-10T12:11:02-07:00

Luminosity changed to lightness in the latest Beta. We added luminance which is defined as 0.2126 R + 0.7152 G + 0.0722 B. Wikipedia (http://en.wikipedia.org/wiki/Grayscale) says
To convert any color to its most approximate level of gray, first one must obtain the values of its red, green and blue (RGB) primaries.

Then, add 30% of the red value, 59% of the green value, and 11% of the blue value, together. Regardless of the scale employed (0.0 to 1.0, 0 to 255, 0% to 100%, etc.), the resultant number is the desired gray value.
Furthermore it refers to this value as the gray intensity.

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

Re: inconsistency in transforming to gray scale

Post by fmw42 » 2008-04-10T12:30:34-07:00

So let me see if I now understand:

1) -fx intensity stays 0.299*red+0.587*green+0.114*blue
2) -fx luminosity is replaced by -fx lightness = (max(r,g,b) + min(r,g,b))/2
3) -fx luminance (new) becomes 0.2126*red + 0.7152*green + 0.0722*blue

But what is the formula for

convert rgbimage -colorspace Gray grayimage
and
convert rgbimage -type Grayscale grayimage

Can you confirm that it is the same now as 3) above i.e. 0.2126*red + 0.7152*green + 0.0722*blue
or
is it the simple average of (red+green+blue)/3

and are or should -colorspace Gray and -type Grayscale give the same result

Thanks

Fred

User avatar
magick
Site Admin
Posts: 10957
Joined: 2003-05-31T11:32:55-07:00

Re: inconsistency in transforming to gray scale

Post by magick » 2008-04-10T12:45:07-07:00

The colorspace gray formula is 0.299*red+0.587*green+0.114*blue. However, you can select the luma version as Rec709LumaColorspace (e.g. 0.2126*red + 0.7152*green + 0.0722*blue).

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

Re: inconsistency in transforming to gray scale

Post by fmw42 » 2008-04-10T13:39:12-07:00

Thanks. So a final summary is as follows:

1) -fx intensity stays 0.299*red+0.587*green+0.114*blue
2) -fx luminosity is replaced by -fx lightness = (max(r,g,b) + min(r,g,b))/2
3) -fx luminance (new) becomes 0.2126*red + 0.7152*green + 0.0722*blue (see luma below)
4) -colorspace Gray is the same as -fx intensity, i.e. 0.299*red+0.587*green+0.114*blue
5) -type Grayscale is the same as -fx intensity, i.e. 0.299*red+0.587*green+0.114*blue

6) others (brightness, luma, etc ) can be done by -colorspace COLORSPACE -channel CHANNEL -separate
e.g
convert rgbimage -colorspace HSB -channel Blue -separate brightnessimage

where one would have to look up the definition of the channel from the colorspace conversion
e.g.
brightness should be max(r,g,b)
or
luma = 0.2126*red + 0.7152*green + 0.0722*blue from colorspace Rec709Luma
(which is the same as new -fx luminance above)

etc

Correct me if I have made any errors above.
Last edited by fmw42 on 2008-05-13T10:38:30-07:00, edited 1 time in total.

sparr

Re: inconsistency in transforming to gray scale

Post by sparr » 2008-04-29T16:06:20-07:00

I don't have the latest beta to check for myself... Can someone confirm that the new names match the names for -channel? I expect "-channel Luminance -fx luminance" and "-channel Luminosity -fx luminosity" (or "-channel Lightness -fx lightness") to produce no change in the image, and the expected change if the fx is modified in some way.

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

Re: inconsistency in transforming to gray scale

Post by fmw42 » 2008-04-29T17:11:40-07:00

IM 6.4.0-11 Q16-hdri

Code: Select all

convert -list channel
All
Alpha
Black
Blue
Cyan
Default
Gray
Green
Hue
Index
Luminance
Luminosity
Magenta
Matte
Opacity
Red
Saturation
Yellow

So luminosity has not been changed to lightness in -channels.

Next, I am not sure I am doing this correctly, but:

Code: Select all

convert rose: -channel Luminance -separate rose_luminance.png
convert rose: -fx "luminance" rose_fx_luminance.png
compare -metric rmse rose_luminance.png rose_fx_luminance.png null:
40735.9 (0.621589)

identify -verbose rose_luminance.png
...
  Channel depth:
    gray: 8-bit
  Channel statistics:
    gray:
      min: 24 (0.0941176)
      max: 255 (1)
      mean: 80.4683 (0.315562)
      standard deviation: 55.1114 (0.216123)

identify -verbose rose_fx_luminance.png
...
  Channel depth:
    gray: 1-bit
  Channel statistics:
    gray:
      min: 1 (1)
      max: 1 (1)
      mean: 1 (1)
      standard deviation: -0 (-0)
So this looks suspicious, but perhaps I am not using -fx correctly?

Likewise:

Code: Select all

convert rose: -channel Luminosity -separate rose_luminosity.png
convert rose: -fx "luminosity" rose_fx_luminosity.png
---convert: unable to parse expression `luminosity'.
convert rose: -fx "lightness" rose_fx_lightness.png
convert rose: -channel Lightness -separate rose_lightness.png
---convert: unrecognized channel type `Lightness'.
compare -metric rmse rose_luminosity.png rose_fx_lightness.png null:
10600.1 (0.161747)


identify -verbose rose_luminosity.png
...
  Colorspace: Gray
  Depth: 16/8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    gray:
      min: 24 (0.0941176)
      max: 255 (1)
      mean: 80.4683 (0.315562)
      standard deviation: 55.1114 (0.216123)

identify -verbose rose_fx_lightness.png
...
  Channel depth:
    gray: 16-bit
  Channel statistics:
    gray:
      min: 9509 (0.145098)
      max: 65535 (1)
      mean: 29310.8 (0.447254)
      standard deviation: 13019.8 (0.198669)
I probably should be using -colorspace in doing the channels. For example

Code: Select all

convert rose: -colorspace HSL -channel Blue -separate rose_lightness.png
but I don't know what colorspaces to use with Luminance or Luminosity? Lab, Rec601Luma or Rec709Luma?

If the either of the latter, Rec601Luma or Rec709Luma, then which channel is the luminance? Or are those conversions simply to grayscale.


I am really not sure I am doing a proper comparison above.

OK. correcting my conversion to Lightness:

Code: Select all

convert rose: -colorspace HSL -channel Blue -separate rose_lightness.png
convert rose: -fx "lightness" rose_fx_lightness.png
compare -metric rmse rose_fx_lightness.png rose_lightness.png null:
0 (0)
So those match.

But:

Code: Select all

convert rose: -channel Luminosity -separate rose_luminosity.png
compare -metric rmse rose_lightness.png rose_luminosity.png null:
10600.1 (0.161747)

convert rose: -colorspace Rec601Luma -channel Red rose_rec601luma.png
convert rose: -colorspace Rec709Luma -channel Red rose_rec709luma.png
convert rose: -channel luminance rose_luminance.png
convert rose: -fx "luminance" rose_fx_luminance.png

compare -metric rmse rose_rec601luma.png rose_rec709luma.png null:
1797.41 (0.0274267)

compare -metric rmse rose_luminance.png rose_fx_luminance.png null:
36451.2 (0.55621)

compare -metric rmse rose_luminance.png rose_rec601luma.png null:
10162.5 (0.15507)

compare -metric rmse rose_fx_luminance.png rose_rec601luma.png null:
34982.2 (0.533794)
Last edited by fmw42 on 2008-04-29T17:39:23-07:00, edited 4 times in total.

User avatar
magick
Site Admin
Posts: 10957
Joined: 2003-05-31T11:32:55-07:00

Re: inconsistency in transforming to gray scale

Post by magick » 2008-04-29T17:17:11-07:00

The lightness channel is available in ImageMagick 6.4.1-0 Beta.

Post Reply