How can I mirror a face image?
Show older comments
I want to mirror a face image by matlab, how can I do it? For example imagine that we split our face image by a vertical line and we want to mirror this 2 parts!what is the code?
Answers (1)
Image Analyst
on 21 Apr 2014
See code below to produce this image:

% Demo to mirror an image down the middle.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 22;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% 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
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, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get a flipped version
flippedRGBImage = fliplr(rgbImage);
subplot(2, 2, 2);
imshow(flippedRGBImage);
title('Mirrored Color Image', 'FontSize', fontSize);
% Replace right half with with right half of flipped image.
midColumn = ceil(columns/2);
outputImage = rgbImage; % Initialize
outputImage(:, midColumn:end, :) = flippedRGBImage(:, midColumn:end, :);
subplot(2, 2, 3);
imshow(outputImage);
title('Final Color Image', 'FontSize', fontSize);
Note that if you have an old version you're going to have to use a different function that fliplr() since that was only changed to handle color images with the R2014a version.
Feel free to adapt as needed to fit your situation.
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!