Editing images using MATLAB

103 views (last 30 days)
Light
Light on 14 Apr 2012
Moved: DGM on 12 Feb 2023
Hi, I found an interesting exercise to do photo editing using matlab but I don't know how to write the convenient script. Here are the steps:
a- Find or take a picture of yourself with a plain background such as a green screen, using the JPEG image format. It would be a good idea not to wear the color of the background.
b- Find a JPEG image of the place you want to go and decide on the rectangle in that scene where your image should appear. Save the width and height of the rectangle and the row and column of its top left corner.
c- Re-size your image to be the width and height of the rectangle.
d- Use the color masking technique to copy your image without the green screen into the selected rectangle of your dream scene.
Thanks a lot!

Accepted Answer

Image Analyst
Image Analyst on 14 Apr 2012
But isn't the fun of it doing it yourself? Well, I have a demo that does most of it for you. You just have to draw the box, resize to the box and figure out how to shift the mask. But this is a good place to start:
[ Edit] Now it does all that for you.
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\Light\Documents\Temporary';
baseFileName = 'awrdd5.jpg';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Read image from disk.
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Read in second image.
baseFileName = 'man.jpg';
fullFileName = fullfile(folder, baseFileName);
rgbImage2 = imread(fullFileName);
% Display the second color image.
subplot(2, 3, 2);
imshow(rgbImage2, []);
axis on;
title('Second Color Image', 'FontSize', fontSize);
% Ask user for location using imrect() or rbbox().
subplot(2, 3, 2); % Switch back to first image.
promptMessage = sprintf('Draw a box over the original (first) image');
button = questdlg(promptMessage, 'Draw box', 'OK', 'Cancel', 'OK');
if strcmp(button, 'Cancel')
return;
end
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
% Plot the mask as an outline over the image.
hold on;
plot(xCoords, yCoords, 'linewidth', 2);
% Resize rgbImage2
% Call to imresize()
% Here, I'll just resize to size of the box.
newRows = abs(y2-y1)+1;
newColumns = abs(x2-x1)+1
rgbImage2 = imresize(rgbImage2, [newRows newColumns]);
[rows2 columns2 numberOfColorBands2] = size(rgbImage);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Same, but this time for the second image.
redChannel2 = rgbImage2(:, :, 1);
greenChannel2 = rgbImage2(:, :, 2);
blueChannel2 = rgbImage2(:, :, 3);
% Get the mask - it's where the red channel is brighter than 55.
mask = greenChannel2 < 171; % A logical image.
% Fill in holes
mask = imfill(mask, 'holes');
% Display the mask image.
subplot(2, 3, 3);
imshow(mask, []);
title('Binary Mask', 'FontSize', fontSize);
axis on;
% Multiply the mask by the images to get just the football
% and not the background.
maskedRed = redChannel2 .* uint8(mask);
maskedGreen = greenChannel2 .* uint8(mask);
maskedBlue = blueChannel2 .* uint8(mask);
% Stack all the color channels back into the 3D true color image.
maskedRgbImage = cat(3, maskedRed, maskedGreen, maskedBlue);
% Display the masked second color image.
subplot(2, 3, 4);
imshow(maskedRgbImage, []);
axis on;
title('Masked Color Image', 'FontSize', fontSize);
% Make a large mask for the original picture.
bigMask = false(size(redChannel));
% Now insert the small mask into it.
bigMask(y1:y2, x1:x2) = mask;
% Insert rgbImage2 into rgbImage.
% Assign only image pixels, not background pixels.
redChannel(bigMask) = redChannel2(mask);
greenChannel(bigMask) = greenChannel2(mask);
blueChannel(bigMask) = blueChannel2(mask);
rgbImage3 = cat(3, redChannel, greenChannel, blueChannel);
% Display the second color image.
subplot(2, 3, 5);
imshow(rgbImage3, []);
axis on;
title('Output Color Image', 'FontSize', fontSize);
msgbox('Done with demo. Thanks ImageAnalyst!');
  12 Comments
Tahreen Isha
Tahreen Isha on 8 Jun 2018
Edited: Tahreen Isha on 8 Jun 2018
I get the following error while extracting the green and blue channels:
Index exceeds matrix dimensions.
Error in editor (line 101)
rgbImage(:, :, 2)
UPDATE: I was using .tif images. Using .jpg gives no error.
Walter Roberson
Walter Roberson on 8 Jun 2018
Whatever image you were dealing with at the time was not an RGB image.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!