Trying to flip image without using the built in function

조회 수: 7 (최근 30일)
panacean
panacean 2018년 2월 8일
답변: shivam singh 2019년 7월 30일
Trying to write a function that reads in an image and then flips the image so that its appearance is a mirror image of the original image. Here is my code for my attempt. Not quite working, if has anyone could provide some suggestions, it would be appreciated.

채택된 답변

Stephen23
Stephen23 2018년 2월 8일
function B = myflipped_image_lr()
A = imread('horse.jpg');
figure(1)
imshow(A);
B = A(:,end:-1:1,:);
figure(2)
imshow(B);
end
  댓글 수: 2
panacean
panacean 2018년 2월 8일
Just for my own understanding, could you explain how B = A(:,end:-1:1,:); is flipping the image. I confused about the logic. Thanks
Walter Roberson
Walter Roberson 2018년 2월 8일
For example if there were 5 columns, then end would be 5, and end:-1:1 would be 5:-1:1 which would be [5 4 3 2 1]. The code would then be equivalent to A(:, [5 4 3 2 1], :) which extracts the data with the columns in reverse order.
Historically, fliplr() used exactly this logic of end:-1:1 (but has a different internal implementation now, calling a built-in routine)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 2월 8일
Hint:
Replace
[ii, jj] = size(A);
with
[ii, jj, numberOfColorChannels] = size(A);

shivam singh
shivam singh 2019년 7월 30일
% To create the mirror image of an image without using pre defined function
clc;
clear all;
close all;
A = imread('C:\Users\Student\Desktop\shivam_1069.jpg');
subplot(2,2,1);
imshow(A);
B = A(:,end:-1:1,:);
subplot(2,2,2);
imshow(B);

Community Treasure Hunt

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

Start Hunting!

Translated by