Related Articles

Related Articles

Create Mirror Image using MATLAB
  • Last Updated : 21 Nov, 2019

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.

Original-Mirror-Image

Code #1: Using MATLAB Library function

filter_none

edit
close

play_arrow

link
brightness_4
code

% Read the target image file
img = imread('leaf.png');
  
% Reverse the element in each row
mirror_image = flip(img, 2);
  
% Display the mirror image
imshow(mirror_img); 
title('Mirror image');

chevron_right


 
Code #2: Using matrix manipulation



filter_none

edit
close

play_arrow

link
brightness_4
code

% Read the target image file
img = imread('leaf.png');
  
% Flip the columns left to right
mirror_image = img(:, end :, -1:, 1, :);
  
% Display the mirror image
imshow(mirror_img); 
title('Mirror image');          

chevron_right


 
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:

filter_none

edit
close

play_arrow

link
brightness_4
code

% Read the target image file
img = imread('leaf.png');
  
% Get the dimensions of the image
[x, y, z] =size(img);
  
  
% Reverse elements of each row
% in every image plane
for 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
    end
end
  
  
% Display the mirror image
imshow(img); 
title('Mirror image');

chevron_right


Input Image: leaf.png
Leaf

Output:
Leaf Mirror image

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :