Complete the code to read an image file into a variable.
img = imread([1]);The imread function requires the filename as a string, so it must be enclosed in quotes.
Complete the code to convert a color image to grayscale.
gray_img = rgb2gray([1]);The rgb2gray function converts a color image stored in img to grayscale.
Fix the error in the code to check if an image is RGB by checking its size.
is_rgb = (size([1], 3) == 3);
The variable img holds the original image. Checking the third dimension size of img tells if it is RGB.
Fill both blanks to create a grayscale image of the same size as the original RGB image.
gray_img = zeros(size([1], [2]), size([1], 2));
To create a 2D grayscale image matching the height and width of the original RGB image, use zeros(size(img, 1), size(img, 2)). The first blank is the image variable img and the second blank is the dimension 1 (rows).
Fill all three blanks to create an RGB image with all pixels set to red.
red_img = zeros(size([1])); red_img(:, :, [2]) = [3];
We create a zero matrix with the same size as the original image img. The red channel is the first channel, so we set red_img(:, :, 1) to 255 to make all pixels red.
