Page 1 of 2

why grey color looks violet?

Posted: Sun Nov 26, 2017 10:34 pm
by askom
Hello all,

I transform RGB image (bitmap) to grey scale.
The quick and rough way is just to average matrices of R, G and B values.
More precisely I have to do weighted average.

Any way, I get an image with one color grades, but it is not grey color??
It is always violet, like attached.

Why?

Thanks a lot,
Sasha.

Re: why grey color looks violet?

Posted: Sun Nov 26, 2017 10:44 pm
by Phil Last
Can you show us the maths?

I'm seeing a single blue on black.

Re: why grey color looks violet?

Posted: Mon Nov 27, 2017 3:51 pm
by ray
"..the normal eye's colour response is not linear, the strongest response is to green (59%), then red (30%) and lowest to blue(11%)." (Taken from my Vector artcale "Windows BMP Bitmap Files and APL" vol 10 no 1 July 1993.)

If you have mixed the RGB colours in equal proportions, the blue content will be over represented, hence not grey.

For printing, an alternative to a RGB grey scale is Monochrome. By just using black and white ink, (should that be "bi-chrome"?), at a higher resolution with "dithering".

That is to say, to produce a 64 grey scale, each RGB pixel is replace by a 8x8 "boolean" array. By varying the total numbers of "1" in the array the required intensity of the grey can be achieved.

The actual position of each "1" in the array need to chosen with some care to prevent "Moiré" patterns appearing. I advocate creating the first 8 arrays (intensity 1-8 out of 64) using an "8 Queens on a chessboard" algorithm (adding 1 queen at each level), and then repeating this process starting from different squares, till all 64 are filled. (In fact this produces 65 levels, one pure white and 63 greys, and one pure black.)

I hope this helps.

Re: why grey color looks violet?

Posted: Tue Nov 28, 2017 8:08 pm
by gil
Hard to say without seeing your code, but my guess is that if you are creating 24-bit images but only write values in the range 0-255, then you effectively only use the blue channel. You'd need to encode it using all 3 channels to get gray:
Say for a given pixel you calculate an average of 200, rather than writing the value 200 you encode it as a triplet of 200:
      256⊥3/200

Re: why grey color looks violet?

Posted: Thu Nov 30, 2017 9:52 pm
by askom
Phil,

thanks a lot for your request. Preparing "math" to answer to you I found that everything works well:-))

⍝ google ‘lena.bmp’ for a favorite testing image (or use any
other image.bmp)
⍝ put it to your current directory and create bitmap:
'bmp'⎕wc 'bitmap' ('file' 'lena.bmp')
⍴cbits←bmp.CBits
512 512
'f' ⎕wc 'form'
f.Picture←bmp
⍝ red, green and blue mixture for each pixel
⍴rgb←(3/256)⊤cbits
3 512 512
⍝ what does matlab and others suggest?
⍝ google: convert rgb to grey scale
⍝ Y = .2126 * R^gamma + .7152 * G^gamma + .0722 * B^gamma
w←.2126 .7152 .0722
⍴grey←⌊.5+w+.×rgb
512 512
⍝ the range is
(⌊/,⌈/),grey
9 240
⍴grey←256⊥⊃3/⊂grey
512 512
'gbmp'⎕WC'bitmap'('cbits' grey)
f.Picture←gbmp
Well, it looks like expected...

Regards,
Sasha.

Phil Last wrote:Can you show us the maths?

I'm seeing a single blue on black.

Re: why grey color looks violet?

Posted: Thu Nov 30, 2017 10:10 pm
by askom
Hi Ray,

Thank you for your respond and very nice to contact you again!

Your answer sounds a bit depressing... About 8x8 arrays... Do you see my post, that the problem is resolved easily?

Regards,
sasha

ray wrote:"..the normal eye's colour response is not linear, the strongest response is to green (59%), then red (30%) and lowest to blue(11%)." (Taken from my Vector artcale "Windows BMP Bitmap Files and APL" vol 10 no 1 July 1993.)

If you have mixed the RGB colours in equal proportions, the blue content will be over represented, hence not grey.

For printing, an alternative to a RGB grey scale is Monochrome. By just using black and white ink, (should that be "bi-chrome"?), at a higher resolution with "dithering".

That is to say, to produce a 64 grey scale, each RGB pixel is replace by a 8x8 "boolean" array. By varying the total numbers of "1" in the array the required intensity of the grey can be achieved.

The actual position of each "1" in the array need to chosen with some care to prevent "Moiré" patterns appearing. I advocate creating the first 8 arrays (intensity 1-8 out of 64) using an "8 Queens on a chessboard" algorithm (adding 1 queen at each level), and then repeating this process starting from different squares, till all 64 are filled. (In fact this produces 65 levels, one pure white and 63 greys, and one pure black.)

I hope this helps.

Re: why grey color looks violet?

Posted: Thu Nov 30, 2017 10:13 pm
by askom
I sent some "code" here& what is opinion now? thanks.

/sasha

gil wrote:Hard to say without seeing your code, but my guess is that if you are creating 24-bit images but only write values in the range 0-255, then you effectively only use the blue channel. You'd need to encode it using all 3 channels to get gray:
Say for a given pixel you calculate an average of 200, rather than writing the value 200 you encode it as a triplet of 200:
      256⊥3/200

Re: why grey color looks violet?

Posted: Fri Dec 01, 2017 7:52 am
by gil
The code you posted looks correct. Unless you changed your code since you posted the question I can't explain why you got a blue on black image before.

Well, the important thing is it's working now :)

Re: why grey color looks violet?

Posted: Fri Dec 01, 2017 3:49 pm
by PGilbert
Here is the code I am using to convert a color image to grey:

      GrayPic picname;bits
⍝ TRANSFORM THE COLORS OF A PICTURE ELEMENT INTO 256 SHADES OF GRAY
⍝ picname = NAME OF THE APL Picture ELEMENT
⍝ FOR A SINGLE PIXEL THE EQUATION IS: GRAY←256⊥3⍴⌊0.5+(+/256 256 256⊤RGB)÷3

:IF 0=⍴picname ⎕WI 'self' ⋄ picname," DOES NOT EXIST !" ⋄ →0 ⋄ :END
:IF ~'Picture'⊣(picname ⎕WI 'class') ⋄ picname," MUST BE A PICTURE !" ⋄ →0 ⋄ :END

bits←picname ⎕WI 'image'
bits←⌊0.5+(+/[⎕IO]256 256 256⊤bits)÷3
bits←256⊥(3,⍴bits)⍴bits
picname ⎕WI 'bitmap' bits


It is part of the workspace 'PicUtil' written in APL+Win that can be found on the APLWiki.

There is also a free dll (FreeImage) that can be used with more options. The Dyalog workspace with explanations can be found also on the APLWiki.

Regards,

Pierre Gilbert

Re: why grey color looks violet?

Posted: Tue Dec 19, 2017 10:01 pm
by askom
gil wrote:The code you posted looks correct. Unless you changed your code since you posted the question I can't explain why you got a blue on black image before.

Well, the important thing is it's working now :)


preparing some math on your request, I googled a bit and and found, that
⍝ Y = .2126 * R^gamma + .7152 * G^gamma + .0722 * B^gamma
is "better", than +/RGB÷3

thanks for your response,
sasha.