color image can be divided into individual color channels. if i give a gray input to the same program what will be the output??

1 次查看(过去 30 天)
a=imread(a.jpg)
R=a(:,:,1)
G=a(:,:,2)
B=a(:,:,3)
%if a= a gray image what will happen??

回答(1 个)

Walter Roberson
Walter Roberson 2016-10-18
Grayscale .jpg images are quite uncommon -- I think I have only ever encountered one, myself.
The result of reading in a grayscale .jpg image would be an array in which the third dimension was only depth 1. Accessing a(:,:,2) would then generate an error because at most a(:,:,1) would exist.
There are also .jpg images which are color images but which visually look grey. If they are true grey images, then they would be RGB images in which all of the color channels would be equal, so isequal(R,G) would be true and isequal(R,B) would also be true.
  2 个评论
anna ann
anna ann 2016-10-18
编辑:Walter Roberson 2016-10-18
ok if a true gray image is used in this case what will be the output?is it error or r,g,b with equal values??
actually i want a single algorithm able to process both color image and gray scale image. so separating into rgb channels and analysing its values, can algorithm distinguish color and gray images??
Walter Roberson
Walter Roberson 2016-10-18
tempimg = imread('cameraman.tif');
tfile = tempname;
filename_gs = [tfile, 'gs.jpg'];
imwrite(tempimg, filename_gs, 'mode', 'lossless');
filename_rgb = [tfile, 'rgb.jpg'];
tempimg_rgb = tempimg(:,:,[1 1 1]);
imwrite(tempimg_rgb, filename_rgb, 'mode', 'lossless');
subplot(1,2,1)
img_gs = imread(filename_gs);
imshow(img_gs);
title('grayscale 2D image');
disp('image size for 2D grayscale image is')
disp(size(img_gs))
disp('number of dimensions for 2D grayscale image is')
disp(ndims(img_gs))
try
r_gs = img_gs(:,:,1);
g_gs = img_gs(:,:,2);
b_gs = img_gs(:,:,3);
disp('We WERE able to extract three planes from 2D grayscale image')
catch
disp('We were NOT able to extract three planes from 2D grayscale image')
end
subplot(1,2,2)
img_rgb = imread(filename_rgb);
imshow(img_rgb);
title('rgb image that happens to be gray')
disp('image size for RGB image that happens to be gray is')
disp(size(img_rgb))
disp('number of dimensions for RGB image that happens to be gray is')
disp(ndims(img_rgb))
try
r_rgb = img_rgb(:,:,1);
g_rgb = img_rgb(:,:,2);
b_rgb = img_rgb(:,:,3);
disp('We WERE able to extract three planes from RGB image that happens to be gray')
catch
disp('We were NOT able to extract three planes from RGB image that happens to be gray')
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by