How to Change the Background of an Image with Green Screen Background in MATLAB?


In this article, we learn how to change the green background of an image to some other background using MATLAB programming.

The concept of changing a green screen background to another background involves the use of a technology called “chroma key”. This technology detects the absolute green color in the image and omits it from the image. Then, we can apply another image or color as the background to the image.

Algorithm

The step−by−step process of changing the green screen background of an image with another background in MATLAB is explained below:

Step (1) − Read the input image with a green screen background.

Step (2) − Read the new background image that has to be inserted into the image as background in the place of green screen.

Step (3) − Convert the input images to RGB format, if required.

Step (4) − Remove the green screen background from the image.

Step (5) − Set the new background image.

Step (6) − Display the final result image.

Now, let us understand the implementation and execution of these six steps in MATLAB programming.

Example

% MATLAB Code for changing green screen background in an image
% Read the input image with green screen background
in_img = imread('https://ak.picdn.net/shutterstock/videos/2322320/thumb/1.jpg');

% Read the new background image
bg_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425323.jpg');

% Convert the images into the RGB format with double precision for calculations
in_img = im2double(in_img);
bg_img = im2double(bg_img);

% Resize the new background image to match the size of the input image
bg_img = imresize(bg_img, [size(in_img, 1), size(in_img, 2)]);

% Remove the green screen background from the input image
x = 0.65;    % Adjust the value based on your image’s requirements
a = in_img(:, :, 2);    % Specify the green channel of the image
m = a > x;

% Change the green screen background with the new background image
out_img = in_img;
out_img(repmat(m, [1, 1, 3])) = bg_img(repmat(m, [1, 1, 3]));

% Display the original image, background image, and the output image
subplot(1, 3, 1); imshow(in_img); title('Original Image');
subplot(1, 3, 2); imshow(bg_img); title('New Background Image');
subplot(1, 3, 3); imshow(out_img); title('Output Image');

Output

Code Explanation

In this MATLAB, we start by reading the green screen input image and the new background image using the ‘imread’ function, and store them in variables ‘in_img’ and ‘bg_img’ respectively. Then, we convert both images to double precision RGB format for better calculations by using the ‘im2double’ function. Then, we resize the background image to match the size of the green screen image.

After that we remove the green screen background from the input image. For this we define a threshold value ‘x’ to determine the green pixel that we want to remove from the image, you should adjust this value as per your image.

Next, we extract a green channel ‘a’ of the green screen image by using the formula ‘in_img(:, :, 2)’, and create a binary mask ‘m’. In this binary mask, the value of each pixel is set to 1 when the value of the green channel ‘a’ is greater than the threshold value ‘x’.

After that we replace the green screen background of the ‘in_img’ with the background image ‘bg_img’. To perform this replacement, we use the ‘repmat’ function. The result is then stored in a variable ‘out_img’.

Finally, we call the ‘imshow’ function to display the green screen image, background image, and the output image.

Hence, in this article, we have explained how to change the green screen background of an image to another background using MATLAB coding.

Updated on: 07-Aug-2023

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements