MATLAB Answers

How “size(x,3)>1” works on an image?

조회 수: 10(최근 30일)
Shaila parvin
Shaila parvin 11 Feb 2014
답변: Image Analyst 11 Feb 2014
x=imread('a.jpg');
if (size(x,3)>1)%if RGB image make gray scale
try
x=rgb2gray(x);%image toolbox dependent
catch
x=sum(double(x),3)/3;%if no image toolbox do simple sum
end
end
I don’t understand the if condition. How “size(x,3)>1” works on an image? Can anyone please help me?

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Thomas
Thomas 11 Feb 2014
size(x,3) - if it is greater than 1 than it is a color RGB image.
try size(x) and see - if you get two dimensions its grayscale if you get three dimensions its RGB and that is exactly what your code is looking for ( if its color RGB convert to grayscale)

  댓글 수: 0

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Image Analyst
Image Analyst 11 Feb 2014
I prefer the more explicit:
% Get the dimensions of the image.
% numberOfColorBands should be = 1 or 3 depending if image is grayscale or RGB.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
Here is how to check if you have the Image Processing Toolbox installed:
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Translated by