Borrar filtros
Borrar filtros

What is the meaning of if size(I,3)>1 I=rgb2gray(I);

5 visualizaciones (últimos 30 días)
D Joseph
D Joseph el 13 de Ag. de 2015
Comentada: 鸿 惊 el 11 de Mzo. de 2021
I dont understand why the condition if size(I,3)>1 means and why it is need to convert I=rgb2gray(I);
  2 comentarios
Kanwal Kaur
Kanwal Kaur el 17 de Nov. de 2016
Editada: Walter Roberson el 17 de Nov. de 2016
if (size(I,3) == 3)
I=rgb2gray(I);
end
here, what will be the use of 3 in (I,3), plz explain
Walter Roberson
Walter Roberson el 17 de Nov. de 2016
See my description below. The 3 in size() refers to the third dimension. The second 3 being compared to corresponds to the three R, G, B values.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Ag. de 2015
grayscale images are represented as 2 dimensional matrices, Height by Width.
RGB ("truecolor") images are represented as 3 dimensional matrices, Height by Width by ColorPlane .
In MATLAB, if you take the size() of the 3rd dimension of a 2 dimensional object, the result is 1. Height by Width is the same as Height by Width by 1.
So to test whether the array represents a grayscale image or represents an RGB image, you can test whether the third dimension is size greater than 1 -- because it will be 1 for 2 dimensional arrays and it will be 3 for RGB arrays.
If the image is already grayscale there is no need to convert it again, so the rgb2gray() is being done only if the image is RGB.
rgb2gray() happens to be defined to leave 2 dimensional matrices unchanged, so you can just call rgb2gray() on the image whether it is grayscale or RGB. It is common, though, for programmers not to know about to be the case, or not to trust that it will always be the case, and so it is common for programmers to test the dimensions to see if the call needs to be made. Also size() is "nearly free", a built-in operation that has little to do internally, and it is more costly to call rgb2gray, so it is faster to do the test than to leave it up to rgb2gray()
Eventually you may see programs where the internal convention has become that 2 dimensional images are always double() but 3 dimensional images are often uint8(). In those programs, testing the dimensions would also be used to determine whether im2double() should be called along with rgb2gray()

Más respuestas (0)

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by