hi, i'm trying to flip an image without any built in commands that is using for loops.
조회 수: 6 (최근 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???
채택된 답변
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
Walter Roberson
2017년 2월 12일
saisps hmm see the link I posted, https://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions .
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
2017년 2월 11일
편집: Chad Greene
2017년 2월 11일
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


