convert grayscale to red image in matlab

To convert a grayscale image to a red image in MATLAB, you can first create a matrix of zeros with the same dimensions as the grayscale image, and then assign the grayscale image as the red channel of the new matrix. Here's an example code:

main.m
% read grayscale image
gray_img = imread('input_gray_img.png');

% create red image with the same dimensions as grayscale image
red_img = zeros(size(gray_img, 1), size(gray_img, 2), 3);

% assign grayscale image as red channel of red image
red_img(:,:,1) = gray_img;

% display red image
imshow(red_img);
305 chars
12 lines

In the above code, we first read the grayscale image using the imread function. We then create a new matrix red_img using the zeros function, with the same number of rows and columns as the grayscale image and 3 channels (corresponding to RGB color space). Finally, we assign the grayscale image as the red channel of the new matrix, and display the resulting red image using imshow.

Note that in the resulting red image, the green and blue channels will have values of zero, resulting in a red-only image.

gistlibby LogSnag