How can I change the background of an image with green screen background?

53 visualizaciones (últimos 30 días)
Hi guys, I have an image with green screen background and I would like to know how can I change it for a random background.
What I want to know is how to change the background of an image with green screen for random backgrounds.
Being the original (this green screen background is RGB(0,255,0)) like this:
And what I want is to switch between random backgrounds like this:
  2 comentarios
KSSV
KSSV el 27 de Oct. de 2021
Attach your image and show us your expectations.
Tito
Tito el 27 de Oct. de 2021
Thank you, I edited the question. Let me know if you need something more specific.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 27 de Oct. de 2021
Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'keys.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the image.
subplot(2, 3, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
%-----------------------------------------------------------------------------------------------------------------------------------
% Get the green mask
[r, g, b] = imsplit(rgbImage);
greenMask = r == 0 & g == 255 & b == 0;
keysMask = ~greenMask;
% Display the image.
subplot(2, 3, 2);
imshow(keysMask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Non Pure Green Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
keysImage = bsxfun(@times, rgbImage, cast(keysMask, 'like', rgbImage));
% Get the keys
% first crop the keys mask to make the image smaller so it will be easier to place on the background anywhere without going outside of it.
keysMask2 = imfill(keysMask, 'holes');
props = regionprops(keysMask2, 'BoundingBox');
keysImage = imcrop(keysImage, props.BoundingBox);
keysMask2 = imcrop(keysMask2, props.BoundingBox);
% Display the image.
subplot(2, 3, 3);
imshow(keysImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Keys Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
subplot(2, 3, 4);
imshow(keysMask2, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Keys Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Read in a background image.
backgroundImage = imread('peppers.png');
% Display the image.
subplot(2, 3, 5);
imshow(backgroundImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Background Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
uiwait(helpdlg('Point to the upper left corner of the keys bounding box.'))
[x, y] = ginput(1);
x = round(x);
y = round(y);
[rows, columns, numberOfColorChannels] = size(keysImage)
[rowsb, columnsb, numberOfColorChannelsb] = size(rgbImage)
% Initialize output image.
outputImage = backgroundImage;
% Paste the keys onto the background.
for col = 1 : columns
targetCol = col + x - 1;
if targetCol > columnsb
continue; % Can't go outsize image.
end
for row = 1 : rows
targetRow = row + x - 1;
if targetRow > rowsb
continue; % Can't go outsize image.
end
if keysMask2(row, col)
outputImage(targetRow, targetCol, :) = keysImage(row, col, :);
end
end
end
% Display the image.
subplot(2, 3, 6);
imshow(outputImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Background Image With Keys', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
msgbox('Done!');

Más respuestas (1)

yanqi liu
yanqi liu el 28 de Oct. de 2021
Editada: yanqi liu el 28 de Oct. de 2021
clc; clear all; close all
im = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/781108/image.png');
jm = rgb2hsv(im);
s = mat2gray(jm(:,:,2));
bw = ~im2bw(s, 0.8);
bw = imclose(bw, strel('disk', 1));
% target image
zm = imread('cameraman.tif');
if ndims(zm) == 2
zm = cat(3,zm,zm,zm);
end
zm = imresize(zm, size(bw), 'bilinear');
im1 = im(:,:,1); im2 = im(:,:,2); im3 = im(:,:,3);
zm1 = zm(:,:,1); zm2 = zm(:,:,2); zm3 = zm(:,:,3);
zm1(bw) = im1(bw); zm2(bw) = im2(bw); zm3(bw) = im3(bw);
zt = cat(3,zm1,zm2,zm3);
figure; imshow(zt, []);

Community Treasure Hunt

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

Start Hunting!

Translated by