MATLAB: “size(I, 3)” when ran on an RGB image returns ans as “1” when that is not the case.

digital image processingMATLAB

function mainfunctionGaus(MyFileName)
if size(MyFileName,3) == 3
disp('RGB')
MyConvRGB(MyFileName);
else
disp('GSCale')
MyConvGreyTest(MyFileName);
end
end
The above is my code and the image stored in "MyFileName" is this:
im1.jpg
What should be happening is the function "MyConvRGB" should be ran by the if statement I put in but since size is giving me an incorrect answer it puts it through my greyscale code.

Best Answer

Shawn - the input to your function is a parameter called MyFileName. The name suggests that it is a file name and not an image. If, for example, the file name is
MyFileName = 'someImageFile.jpg'
then
size(MyFileName, 3)
will be 1. You need to read the image (given the file name) and then take the size of that
myImage = imread(MyFileName);
if size(myImage,3) == 3
disp('RGB')
MyConvRGB(myImage);
else
disp('GSCale')
MyConvGreyTest(myImage);
end