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

6 Ansichten (letzte 30 Tage)
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 Kommentare
John BG
John BG am 14 Feb. 2017
Bearbeitet: John BG am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John BG
John BG am 11 Feb. 2017
Bearbeitet: John BG am 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 Kommentare
Jan
Jan am 12 Feb. 2017
Bearbeitet: Jan am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Chad Greene
Chad Greene am 11 Feb. 2017
Bearbeitet: Chad Greene am 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 Kommentare

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 12 Feb. 2017
Bearbeitet: Jan am 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

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by