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

5 次查看(过去 30 天)
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 个评论
John BG
John BG 2017-2-14
编辑:John BG 2017-2-14
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

请先登录,再进行评论。

采纳的回答

John BG
John BG 2017-2-11
编辑:John BG 2017-2-11
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 个评论
Jan
Jan 2017-2-12
编辑:Jan 2017-2-12
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 2017-2-12
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.

请先登录,再进行评论。

更多回答(2 个)

Chad Greene
Chad Greene 2017-2-11
编辑:Chad Greene 2017-2-11
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 个评论

请先登录,再进行评论。


Jan
Jan 2017-2-12
编辑:Jan 2017-2-12
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

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by