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 view (last 30 days)
Show older comments
a=imread(a.jpg)
R=a(:,:,1)
G=a(:,:,2)
B=a(:,:,3)
%if a= a gray image what will happen??
0 Comments
Answers (1)
Walter Roberson
on 18 Oct 2016
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 Comments
Walter Roberson
on 18 Oct 2016
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
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!