hi, i'm trying to flip an image without any built in commands that is using for loops.

87 views (last 30 days)
hi, i'm trying to flip an image without any built in commands that is using for loops. here is my code:
org_img=imread('Fig219(rose1024).tif');
subplot(4,3,1), imshow(org_img);
title('original image');
%---------------------------------------------------------------------------%
M=size(org_img,1);
N=size(org_img,2);
for i=1:M
for j=1:N
n_img(i,j)=org_img(i,N-j+1)
subplot(4,3,2), imshow( n_img(i,j));
end
end
on executing the above code the output goes out of bound can you please tell me what i'm doing wrong???
  3 Comments
John BG
John BG on 14 Feb 2017
Edited: John BG on 14 Feb 2017
diagonal flipping, kein problem:
A=imread('hccca.jpg');
A=A(:,:,1);
figure(1);imshow(A);
A=A(:,[end:-1:1],:);
figure(2);imshow(A([end:-1:1],:,:)');
.
.
John BG

Sign in to comment.

Accepted Answer

John BG
John BG on 11 Feb 2017
Edited: John BG on 11 Feb 2017
To saisps hmm
to flip images vertically or horizontally, there is no need for for loops and indeed any advanced function, just invert indices, look:
A=imread('netmap1.jpg')
figure(1);imshow(A);
figure(2);imshow(A([end:-1:1],:,:))
figure(3);imshow(A(:,[end:-1:1],:))
figures 1 2 3, from left to right
1: original
2: vertical flip (around X axis) and
3: horizontal flip (around Y axis)
.
.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  11 Comments
Jan
Jan on 12 Feb 2017
Edited: Jan on 12 Feb 2017
Ah, the old "no built-in" problem: after using Matlab for 20 years it is hard to stop thinking in terms of built-in functions. The image does not define the transformation uniquely, because the data are symmetric to the main diagonal. See my answer.
Walter Roberson
Walter Roberson on 12 Feb 2017
Even indexing uses built-in commands.
Seriously: A(3,5) is not done by "just" indexing A at row 3, column 5: instead it is handled by calling
subsref(A, struct('type', '()', 'subs', {{3, 5}}) )
And this makes a difference in MATLAB, because it determines which subsref to call according to the data type of A. There are close to 50 different data types in MATLAB that define their own subsref, and you can define your own data types that use the () notation for special purposes.
There is almost nothing you can program in MATLAB without using a built-in function.

Sign in to comment.

More Answers (2)

Chad Greene
Chad Greene on 11 Feb 2017
Edited: Chad Greene on 11 Feb 2017
If you want to flip it vertically (equivalent to flipud), you'd only need to loop through each row.
I = imread('coins.png');
Nrows = size(I,1);
% Preallocate flipped version:
Iflip = NaN(size(I));
for k = 1:Nrows
Iflip(k,:) = I(Nrows-k+1,:);
end
figure
subplot(1,2,1)
image(I)
axis image
subplot(1,2,2)
image(Iflip)
axis image
colormap gray
To flip it horizontally (equivalent to fliplr) you'd loop through columns in the same way.
  4 Comments

Sign in to comment.


Jan
Jan on 12 Feb 2017
Edited: Jan on 12 Feb 2017
Mirroring on the minor diagonal:
img = rand(4, 4, 3);
imgT = cat(3, img(:,:,1).', img(:,:,2).', img(:,:,3).')
figure;
subplot(1,2,1);
image(img);
subplot(1,2,2);
image(imgT);
Or is the transpose and cat operation too built-in again? Note that the show code contains a built-in subsref and subsasgn also.
siz = size(img);
imgT = zeros(siz([2,1,3]), class(img));
c = siz(2) + 1;
for k = 1:siz(1)
imgT(:, c - k, :) = img(k, end:-1:1, :);
end

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!