Create Mirror Image using MATLAB
Prerequisite: Image representation in MATLAB
In MATLAB, Images are stored in matrices, in which each element of the matrix corresponds to a single discrete pixel of the image. We can get the mirror image of the given image if we reverse the order of the pixels (elements of the matrix) in each row.

Code #1: Using MATLAB Library function
% Read the target image fileimg = imread('leaf.png'); % Reverse the element in each rowmirror_image = flip(img, 2); % Display the mirror imageimshow(mirror_img); title('Mirror image'); |
Code #2: Using matrix manipulation
% Read the target image fileimg = imread('leaf.png'); % Flip the columns left to rightmirror_image = img(:, end :, -1:, 1, :); % Display the mirror imageimshow(mirror_img); title('Mirror image'); |
Code #3: Using matrix manipulation (Using loops)
Approach:
- Read the source image file in MATLAB environment
- Get the Dimensions of the image matrix
- Reverse the order of the elements of each row in every image plane
- Display the mirror image
Below is the implementation of the above approach:
% Read the target image fileimg = imread('leaf.png'); % Get the dimensions of the image[x, y, z] =size(img); % Reverse elements of each row% in every image planefor plane = 1: z for i = 1 : x len = y; for j = 1 : y % To reverse the order of the element % of a row we can swap the % leftmost element of the row with % its rightmost element if j < y/2 temp = img(i, j, plane); img(i, j, plane) = img(i, len, plane); img(i, len, plane) = temp; len = len - 1; end end endend % Display the mirror imageimshow(img); title('Mirror image'); |
Input Image: leaf.png
Output:


