How do I change image background to red

I have an image with black background, which I got from imfreehand function. I want to change that black background to red. Following is link for image.

 Accepted Answer

background = grayImage == 0; % Find black pixels.
redChannel = grayImage;
redChannel(background) = 255; % Set black pixels to max value.
% Create true color image.
rgbImage = cat(3, redChannel, grayImage, grayImage);
imshow(rgbImage); % Display it.

5 Comments

Thanks a lot. I got the output. I have one more question for the same image. For further processing I want to convert the gray ROI to binary by keeping the same red background. And from that I want to calculate the black and white ratio of ROI.
I don't know what that means. "background is the mask", or ROI, array - it's a binary (true/false) image. I don't know what the black and white ratio of the ROI is. Do you mean the ratio of black/false mask pixels to white/true pixels? What good would that be? Or do you want to examine the gray scale pixels within the ROI or background?
In My binary ROI image I would like to identify how many white pixels and black pixels are there in actual ROI. In a binary image the background pixels get mixed with my ROI image black pixels. So I would not able to identify which are background pixels and which are actual ROI pixels. So for ar Binary image I would like to get red background just shown in following figure.
https://plus.google.com/photos/111799079095235591256/albums/5982533848683720257?hl=en_US&partnerid=gplp0
numberOfBackgroundPixels = sum(background(:)); % Red/white pixels in background.
numberOfROIPixels = numel(background) - numberOfBackgroundPixels;
I got gray scale Image with red background as per your code. In next step of Image processing I want to binarize only region of Interest keeping red background as it is. but when I apply im2bw all image convert into black and white, even red background. So what is the solution for keeping red background as it is and convert gray image into binary

Sign in to comment.

More Answers (2)

Hi, you can use following MATLAB code to change the color scheme of a gray image.
gray_image = imread('cameraman.tif');
map = colormap('autumn');
rgb_image = ind2rgb(gray_image,map);
You can get different color schemes under 'colormap' function in MATLAB.
hf fh
hf fh on 19 Sep 2018
Please help me I have an image and I want to make the background color white I used the previous method but did not good out with me

5 Comments

I have 3.png image , I do not know what to do with
a black and white image A grayscale I want to change the background color from black to white what should I do I am a new user on Matlab
Why do you want or need the background to be white? I don't see what purpose it would serve? It seems totally unnecessary to me. Please justify why you think you need it to be white.
To make sure there is no noise in the image and also make sure the edges of the image
Making the surround white does not make sure there is no noise in the image. And you didn't finish the second part of the sentence about making sure the edges.....are what?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!