user3362481
user3362481

Reputation: 19

Flipping images using Matlab

I am familiarizing myself with the software called 'MATLAB' and I have the file called 'flipLtRt.m' with following code:

function newIm = flipLtRt(im)
% newIm is impage im flipped from left to right

[nr,nc,np]= size(im);    % dimensions of im
newIm= zeros(nr,nc,np);  % initialize newIm with zeros
newIm= uint8(newIm);     % Matlab uses unsigned 8-bit int for color values


for r= 1:nr
    for c= 1:nc
        for p= 1:np
            newIm(r,c,p)= im(r,nc-c+1,p);
        end
    end
end

By applying this code:

I = imread('Dog.jpg');  
Dog = flipLtRt(I);  
imshow(Dog);  

my dog image is mirrored.

It is easy for us to use this file 'flipLtRt.m' to mirror an image without actually understanding the code. If someone tells me to mirror an image, I can simply apply the code (second section) but if someone tells me to actually explain how each line of code do, I won't be able to.

Would anyone be able to explain what each line of the code actually do?
It's the 'for' part that I cannot understand properly. And what is 'np'? New position?

Upvotes: 1

Views: 1394

Answers (1)

Alceu Costa
Alceu Costa

Reputation: 9899

The code iterate over the rows of the image im, which is represented by a matrix with nr rows and np columns, flipping:

  • The 1st column with the last;
  • The 2nd column with the penultimate;
  • The c-th column with the nc-(c-1)th column;

You can see this column flipping at the line:

newIm(r,c,p)= im(r,nc-c+1,p);

Where:

  • r is the row index, nr is the number of rows;
  • c is the column index nc is the number of columns;
  • p is color channel index (e.g.: 1=Red, 2=Green, 3=Blue);
  • np is the number of color channels;

Upvotes: 1

Related Questions