如何在不使用内置函数的情况下在 matlab 中翻转图像?

How to flip image in matlab without using built in functions?

编写读取灰度图像并生成原始图像翻转图像的 MATLAB 代码。enter image description here 我正在尝试这段代码,但没有给我正确的翻转 image.Help 会很多 appreciated.Thankyou

clear all
clc
a=imread('pout.tif');
[r,c]=size(a);
for i=r:-1:1
  k=1;
for j=1:1:c 
    temp=a(k,j);
    result(k,j)=a(i,j);
    result(i,j)=temp;
     k=k+1;
  end
end
 subplot(1,2,1), imshow(a)
 subplot(1,2,2),imshow(result) 

您对索引所做的事情有点不清楚。您还应该为结果预分配内存。

clear all
clc
a=imread('pout.tif');
[r,c]=size(a);
result = a; % preallocate memory for result
for i=1:r
    for j=1:c
        result(r-i+1,j)=a(i,j);
    end
end
subplot(1,2,1), imshow(a)
subplot(1,2,2),imshow(result)

您可以使用基本索引来翻转矩阵。二维案例(灰度图):

a = a(:,end:-1:1); % horizontal flip
a = a(end:-1:1,:); % vertical flip
a = a(end:-1:1,end:-1:1); % flip both: 180 degree rotation

对于 3D 情况(彩色图像)添加第三个索引 ::

a = a(:,end:-1:1,:); % horizontal flip