umair ahmed
umair ahmed

Reputation: 13

How to flip image in matlab without using built in functions?

Write a MATLAB code that reads a gray scale image and generates the flipped image of original image.enter image description here i am trying this code but is not giving me correct flipped image.Help will be much appreciated.Thankyou

clear all
clc
a=imread('pout.tif');
[r,c]=size(a);
for i=r:-1:1
  k=1;
for j=1:1:c 
    temp=a(k,j);
    result(k,j)=a(i,j);
    result(i,j)=temp;
     k=k+1;
  end
end
 subplot(1,2,1), imshow(a)
 subplot(1,2,2),imshow(result) 

Upvotes: 1

Views: 984

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60635

You can use basic indexing to flip a matrix. 2D case (gray-scale image):

a = a(:,end:-1:1); % horizontal flip
a = a(end:-1:1,:); % vertical flip
a = a(end:-1:1,end:-1:1); % flip both: 180 degree rotation

For the 3D case (color image) add a 3rd index ::

a = a(:,end:-1:1,:); % horizontal flip

Upvotes: 1

Michael
Michael

Reputation: 534

What you're doing with indices is kind of unclear. You should also pre-allocate memory for the result.

clear all
clc
a=imread('pout.tif');
[r,c]=size(a);
result = a; % preallocate memory for result
for i=1:r
    for j=1:c
        result(r-i+1,j)=a(i,j);
    end
end
subplot(1,2,1), imshow(a)
subplot(1,2,2),imshow(result)

Upvotes: 2

Related Questions