# Questions about the rgb2gray function

3 visualizaciones (últimos 30 días)

Comentada: 志远 任 el 10 de Sept. de 2022
I am trying to convert the RGB pixel (120, 119, 112) to gray pixel, When using the conversion equation '0.299 * R + 0.587 * G + 0.114 * B' provided by the function document, as following codes shows,
uint8(120 * 0.2989 +119 * 0.587 + 112 * 0.114)
the results is 118. But when using rgb2gray function as follows,
im2gray(aa) % aa is a 1x1x3 matrix [120, 119, 112]
the results is 119.
Does anyone knows why? Thanks!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Stephen23 el 10 de Sept. de 2022
Editada: Stephen23 el 10 de Sept. de 2022
"does anyone know why?"
The values shown in the documentation are shown only to 3 decimal places.
The actual values used for the conversion are:
format long G
M = inv([1,0.956,0.621;1,-0.272,-0.647;1,-1.106,1.703]); % inv of YIQ->RGB
C = M(1,:).' % the actual values
C = 3×1
0.298936021293775 0.587043074451121 0.114020904255103
[120,119,112]*C
ans =
118.500789691508
im2gray(cat(3,120,119,112)/255)*255
ans =
118.500789691508
And if you are wondering where that matrix comes from, take a look at the conversions here:
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

That's really helpful, thanks a lot!

Iniciar sesión para comentar.

### Categorías

Más información sobre Image Arithmetic en Help Center y File Exchange.

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by