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???
  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 件のコメント
saisps hmm
saisps hmm 2017 年 2 月 12 日
thanks, it worked!!!
MUYIDEEN MOMOH
MUYIDEEN MOMOH 2017 年 6 月 28 日
your solution has solved my problem

サインインしてコメントする。


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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by