How to Create a Partially Colored Gray Image in MATLAB?


We can use MATLAB to create a partially colored gray image, which we will discuss in this tutorial. But before that, let us first get an overview of what a partially colored gray image is used for.

What is a Partially Colored Gray Image?

A digital image in which some parts or objects are colored while the rest parts are in gray scale is called a partially colored gray image.

In simple terms, a digital image which combines both color and gray scale objects in a single image area is called a partially colored gray image.

Partially colored gray images are used to enhance the visual quality of an image and is widely used for artistic and creative imaging purposes.

Let us now understand how we can use MATLAB to create a partially colored gray image.

Create Partially Colored Gray Image in MATLAB

We can utilize MATLAB to create partially colored gray images. The step-by-step process of creating a partially colored gray image is explained below.

  • Step (1) − Read the input image using the "imread" function.

  • Step (2) − Separate the RGB color components from the input image.

  • Step (3) − Create separate grayscale channels for coloring.

  • Step (4) − Read a mask image which specifies the regions of the gray image that have to be colored.

  • Step (5) − Convert the mask image to grayscale.

  • Step (6) − Create a binary mask by thresholding the mask image.

  • Step (7) − Apply the colors to the masked regions by using the binary mask.

  • Step (8) − Combine all the RGB channels to create the final partially colored gray image.

  • Step (9) − Display the partially colored gray image.

We can follow these steps to create a partially colored gray image using MATLAB.

Example

Let us see an example to understand how to write codes in MATLAB to create a partially colored gray image.

% MATLAB code to create a partially colored gray image
% Read the input image
I = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');	% Replace with your input image

% Separate the RGB color channels
R = I(:,:,1);	% Red channel
G = I(:,:,2);	% Green channel
B = I(:,:,3);	% Blue channel

% Convert the input image to grayscale image
gray_img = rgb2gray(I);

% Create separate grayscale channels for coloring
R1 = gray_img;
G1 = gray_img;
B1 = gray_img;

% Read the mask image
mask_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');	% Replace URL with masked image URL

% Convert the mask image to grayscale
mask_gray = rgb2gray(mask_img);

% Create a binary mask by thresholding the gray mask image
binary_mask = imbinarize(mask_gray);

% Determine the indices where the mask is black
A = find(binary_mask == 0);

% Apply colors from original image to the masked regions
R1(A) = R(A);
G1(A) = G(A);
B1(A) = B(A);

% Create the partially colored RGB image
colored_img = cat(3, R1, G1, B1);

% Convert the image to uint8 format to display
colored_img = uint8(colored_img);

% Display the original image and partially colored image
figure;
subplot(2, 1, 1);
imshow(I);
title('Original Image');

subplot(2, 1, 2);
imshow(colored_img);
title('Partially Colored Image');

Output

When you run this code, it will produce the following output −

Explanation

In this example, we start by reading the input image using the "imread" function. Then, we isolate the RGB color channels from the image and convert it into gray scale using the "rgb2gray" function.

After that, we create separate grayscale channels as R1, G1, and B1 for coloring the image. Then, we read mask image using the "imread" function and convert it to grayscale for processing.

Next, we create a binary mask by thresholding the gray mask image using the "imbinarize" function. Then, we determine the indices where the mask is black (we can also check for white mask by replacing 0 with 1).

After that, we apply the colors from the original image to the masked regions. Finally, we combine all the color channels to create the partially colored gray image.

At last, we use the "imshow" function to display the original and partially colored gray images.

Conclusion

This is all about creating a partially colored gray image using MATLAB. In this tutorial, I explained the step-by-step process with the help of an example to create a partially colored gray image in MATLAB. In conclusion, a partially colored gray image is one that combines both color and gray scale effect in a single image.

Updated on: 05-Oct-2023

33 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements