Reputation: 19
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
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:
You can see this column flipping at the line:
newIm(r,c,p)= im(r,nc-c+1,p);
Where:
Upvotes: 1