How to Perform Random Pseudo Coloring in Grayscale Image Using MATLAB?


In digital image processing, random pseudo coloring is a technique in which pixels of a grayscale image are assigned a random color. In this tutorial, I am going to explain the process of performing random pseudo coloring in a grayscale image using MATLAB.

What is Random Pseudo Coloring in Grayscale Image?

In digital image processing, "random pseudo coloring" is a way of converting a gray scale image to a random colored image. In this technique, the intensity value of each pixel of the gray scale image is assigned a random color value.

Random pseudo coloring in grayscale image is widely used to visually represent data by creating artistic images.

The process of random pseudo coloring in a gray scale image is explained below.

  • Firstly, we take a grayscale image in which each pixel has only one intensity value representing black (minimum pixel intensity) and white (maximum pixel intensity).

  • Then, each pixel intensity value is mapped with a random color intensity value.

  • Finally, the randomly color mapped pixel values are applied to the grayscale image to obtain the final random pseudo color image.

Random pseudo coloring is used in various image processing applications, such as artistic images, creative graphics, visualization of different scientific data with different colors, etc.

Perform Random Pseudo Coloring in Grayscale Image Using MATLAB

MATLAB is a powerful tool to process digital images. We can also use MATLAB to perform random pseudo coloring of a gray scale image. For this, we map the intensity values of each pixel in a gray scale image with a random color value.

The step-by-step process to perform random pseudo coloring in grayscale image using MATLAB is explained below.

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

  • Step (2) − Convert the input image to grayscale if necessary. For this, use the "rgb2gray" function.

  • Step (3) − Generate random color. For this, use the "rand" function.

  • Step (4) − Create a random pseudo color image by multiplying the pixel values of the gray image with the randomly generated colors.

  • Step (5) − Display the random pseudo color image using the "imshow" function.

We can follow these steps to perform random pseudo coloring in a gray scale image using MATLAB.

Example

Let us now take an example to understand the MATLAB code implementation to perform random pseudo coloring of a grayscale image.

% MATLAB code to perform random pseudo coloring in a grayscale image
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale if necessary
if size(img, 3) == 3	% Checking for grayscale
   gray_img = rgb2gray(img);	% Convert to grayscale
else
   gray_img = img;	% If already in grayscale
end

% Create random colors
random_color = rand(1, 3);  % Generating random RGB colors

% Perform random pseudo coloring of gray image
pseudo_color_img = cat(3, gray_img * random_color(1), gray_img * random_color(2), gray_img * random_color(3));

% Display the gray- and pseudo-colored images
figure;
subplot(1, 2, 1);
imshow(gray_img);
title('Gray Image');

subplot(1, 2, 2);
imshow(pseudo_color_img);
title('Pseudo Colored Image');

Output

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

First Time Run

Second Time Run

Third Time Run

Explanation

In this MATLAB code, we start by reading the input image using the "imread" function. Then, we check whether the image is already in grayscale or not. If it is not, then we use the "rgb2gray" function to convert it into grayscale, else we use it directly.

Then, we use the "rand" function to create three random colors. After that we multiply these randomly generated color values with the pixel values of the grayscale image to create a random pseudo color image. Finally, we use the "imshow" function to display the gray image and the random pseudo color image.

Conclusion

In conclusion, random pseudo coloring is a technique of applying a random color to a grayscale image. In this tutorial, I explained the step-by-step process of performing random pseudo coloring in a gray scale image with the help of an example in MATLAB.

Updated on: 06-Oct-2023

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements