How to convert multiple images to grayscale whilst utilizing the right conversion type

3 views (last 30 days)
Hello, everyone!
The instructions to my assignment are:
if true
% code
end
1) Open a new figure
2) Read the images 01.jpg, 02.jpg, and 03.jpg into variables im1, im2, and
im3 respectively.
3) The jpg images that you will have loaded in step 2) are color images. In Matlab
those are represented as 3D matrices, where two of the dimensions correspond to
the spatial dimensions of the image and the third describes the color, which is
encoded as a mix between red, green, and blue (RGB). Convert the color images
from 2) into gray value images and store the resulting matrices in the variables
im1_g, im2_g, and im3_g respectively.
4) Compute the arithmetic average of the three images and store it in the variable
I_avg.
5) Display the averaged image using a gray colormap and making sure that the
aspect ratio of the original image is preserved
I read up on the documentation on the rgb2gray feature with regards to Matlab, and it's still murky to me.
I wrote my code, and everything's fine except an error somewhere I made in the beginning of my code. I am not converting im1, im2, and im3 to grayscale properly, and the error message is "Integers can only be combined with integers of the same class, or scalar doubles" but I am not sure what the intermediary step is to do the proper conversion. My code is:
if true
figure;
im1 = imread('01.jpg');
im2 = imread('02.jpg');
im3 = imread('03.jpg');
im1_g = rgb2gray(im1);
im2_g = rgb2gray(im2);
im3_g = rgb2gray(im3);
im1_g = double(im1_g);
im2_g = double(im2_g);
im3_g = double(im3_g);
I_avg = ((im1_g + im2_g + im3_g)/3);
y = imagesc(I_avg);
axis image;
colormap(gray);
end
Thank you!(Squashed the code to make it fit)

Answers (0)

Categories

Find more on Convert Image Type 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!