fft3d,交换切片,需要转置?
fft3d, swapped slice, need to transpose?
我需要使用 fftw 库在 C++ 中执行两步 ifft3d。
1st step: a one dimensional ifft across third dimension, creating a "hybrid_kspace"
...
[some operations with hybrid_kspace]
...
2nd step: a two dimensional ifft over horizontal slices of hybrid_kspace.
根据这个 (https://cmb.ornl.gov/members/z8g/csproject-report.pdf) 我必须执行某种转置操作,但我不太明白。
所以我试图在 Matlab 中研究这个问题,但发生了一些相当奇怪的事情(见代码)。 Clown3D 图像的第一片和最后片交换了,我不明白为什么。
load clown;
Z=5;
Clown3D=repmat(X,[1,1,Z]);
white=max(Clown3D(:));
Clown3D(100,:,1)=white; %1 white row
Clown3D([70,130],:,2)=white; %2 white row
Clown3D([50 100 150],:,3)=white; %3 white row
Clown3D([40 80 120 160],:,4)=white; %4 white row
Clown3D([30 60 90 120 150],:,5)=white; %5 white row
%Original 3d image
for ii=1:Z
figure, imagesc(Clown3D(:,:,ii)), colormap gray
end
%into fourier space
Kspace=fftshift(fftn(fftshift(Clown3D)));
%2-step ifft3d
K_hybrid3D = fftshift(ifft(fftshift(Kspace,3),[],3),3);
ReconClown3D=zeros(size(Clown3D));
for ii=1:Z
B=squeeze(K_hybrid3D(:,:,ii));
A = fftshift(ifftn(fftshift(B)));
ReconClown3D(:,:,ii)=A;
end
for ii=1:Z
figure, imagesc(abs(ReconClown3D(:,:,ii))), colormap gray
end
%classic ifft3d
ReconClown3DCalssic=fftshift(ifftn(fftshift(Kspace)));
for ii=1:Z
figure, imagesc(abs(ReconClown3DCalssic(:,:,ii))), colormap gray
end
由于您使用了 fftshift
而没有使用 ifftshift
,因此正在进行交换。我建议阅读 fftshift
documentation here and ifftshift
documentation here. ifftshift
is the inverse shifting operation for an fftshift
. If you shift a vector or matrix with fftshift
, you should be using ifftshift
to unshift it or the output of an ifft
of it, and vice versa. There is an excellent discussion of the correct way to do this in this MathWorks thread.
每次调用 ifft
时,您应该先使用 fftshift
移动参数,然后使用 ifftshift
移动输出以获得正确的结果。当使用 fft
时,您应该使用 ifftshift
移动输入并使用 fftshift
.
移动输出
如果输入是偶数大小,这种区别并不重要,因为在这种情况下 fftshift
和 ifftshift
是它们自己的倒数。但是,您输入的维度为 5,这就是导致问题的原因。
由于 fft
的不同维度的混合,这对于您正在实施“两步”ifft3d 的部分来说有点冒险。看起来您正在依次进行 3D fft
、1D ifft
和 2D ifft
。我在代码中也包含了一个 'fix',它不遵循之前的建议。相反,它使用 fftshift
作为 3D fft
.
的输入和输出
要修复您的代码,您应该按照前面所述修改涉及移位的行。结果代码如下:
load clown;
Z=5;
Clown3D=repmat(X,[1,1,Z]);
white=max(Clown3D(:));
Clown3D(100,:,1)=white; %1 white row
Clown3D([70,130],:,2)=white; %2 white row
Clown3D([50 100 150],:,3)=white; %3 white row
Clown3D([40 80 120 160],:,4)=white; %4 white row
Clown3D([30 60 90 120 150],:,5)=white; %5 white row
%Original 3d image
for ii=1:Z
figure, imagesc(Clown3D(:,:,ii)), colormap gray
end
%into fourier space
%this line is kept as is; modifying it causes swapping.
Kspace=fftshift(fftn(fftshift(Clown3D)));
%2-step ifft3d
%Modified to include ifftshift, although it shouldn't matter here
K_hybrid3D = ifftshift(ifft(fftshift(Kspace,3),[],3),3);
ReconClown3D=zeros(size(Clown3D));
for ii=1:Z
B=squeeze(K_hybrid3D(:,:,ii));
%Modified to include ifftshift, although it shouldn't matter here
A = ifftshift(ifftn(fftshift(B)));
ReconClown3D(:,:,ii)=A;
end
for ii=1:Z
figure, imagesc(abs(ReconClown3D(:,:,ii))), colormap gray
end
%classic ifft3d
%Modified to include ifftshift, and it DOES matter here.
ReconClown3DCalssic=ifftshift(ifftn(fftshift(Kspace)));
for ii=1:Z
figure, imagesc(abs(ReconClown3DCalssic(:,:,ii))), colormap gray
end
我需要使用 fftw 库在 C++ 中执行两步 ifft3d。
1st step: a one dimensional ifft across third dimension, creating a "hybrid_kspace"
...
[some operations with hybrid_kspace]
...
2nd step: a two dimensional ifft over horizontal slices of hybrid_kspace.
根据这个 (https://cmb.ornl.gov/members/z8g/csproject-report.pdf) 我必须执行某种转置操作,但我不太明白。
所以我试图在 Matlab 中研究这个问题,但发生了一些相当奇怪的事情(见代码)。 Clown3D 图像的第一片和最后片交换了,我不明白为什么。
load clown;
Z=5;
Clown3D=repmat(X,[1,1,Z]);
white=max(Clown3D(:));
Clown3D(100,:,1)=white; %1 white row
Clown3D([70,130],:,2)=white; %2 white row
Clown3D([50 100 150],:,3)=white; %3 white row
Clown3D([40 80 120 160],:,4)=white; %4 white row
Clown3D([30 60 90 120 150],:,5)=white; %5 white row
%Original 3d image
for ii=1:Z
figure, imagesc(Clown3D(:,:,ii)), colormap gray
end
%into fourier space
Kspace=fftshift(fftn(fftshift(Clown3D)));
%2-step ifft3d
K_hybrid3D = fftshift(ifft(fftshift(Kspace,3),[],3),3);
ReconClown3D=zeros(size(Clown3D));
for ii=1:Z
B=squeeze(K_hybrid3D(:,:,ii));
A = fftshift(ifftn(fftshift(B)));
ReconClown3D(:,:,ii)=A;
end
for ii=1:Z
figure, imagesc(abs(ReconClown3D(:,:,ii))), colormap gray
end
%classic ifft3d
ReconClown3DCalssic=fftshift(ifftn(fftshift(Kspace)));
for ii=1:Z
figure, imagesc(abs(ReconClown3DCalssic(:,:,ii))), colormap gray
end
由于您使用了 fftshift
而没有使用 ifftshift
,因此正在进行交换。我建议阅读 fftshift
documentation here and ifftshift
documentation here. ifftshift
is the inverse shifting operation for an fftshift
. If you shift a vector or matrix with fftshift
, you should be using ifftshift
to unshift it or the output of an ifft
of it, and vice versa. There is an excellent discussion of the correct way to do this in this MathWorks thread.
每次调用 ifft
时,您应该先使用 fftshift
移动参数,然后使用 ifftshift
移动输出以获得正确的结果。当使用 fft
时,您应该使用 ifftshift
移动输入并使用 fftshift
.
如果输入是偶数大小,这种区别并不重要,因为在这种情况下 fftshift
和 ifftshift
是它们自己的倒数。但是,您输入的维度为 5,这就是导致问题的原因。
由于 fft
的不同维度的混合,这对于您正在实施“两步”ifft3d 的部分来说有点冒险。看起来您正在依次进行 3D fft
、1D ifft
和 2D ifft
。我在代码中也包含了一个 'fix',它不遵循之前的建议。相反,它使用 fftshift
作为 3D fft
.
要修复您的代码,您应该按照前面所述修改涉及移位的行。结果代码如下:
load clown;
Z=5;
Clown3D=repmat(X,[1,1,Z]);
white=max(Clown3D(:));
Clown3D(100,:,1)=white; %1 white row
Clown3D([70,130],:,2)=white; %2 white row
Clown3D([50 100 150],:,3)=white; %3 white row
Clown3D([40 80 120 160],:,4)=white; %4 white row
Clown3D([30 60 90 120 150],:,5)=white; %5 white row
%Original 3d image
for ii=1:Z
figure, imagesc(Clown3D(:,:,ii)), colormap gray
end
%into fourier space
%this line is kept as is; modifying it causes swapping.
Kspace=fftshift(fftn(fftshift(Clown3D)));
%2-step ifft3d
%Modified to include ifftshift, although it shouldn't matter here
K_hybrid3D = ifftshift(ifft(fftshift(Kspace,3),[],3),3);
ReconClown3D=zeros(size(Clown3D));
for ii=1:Z
B=squeeze(K_hybrid3D(:,:,ii));
%Modified to include ifftshift, although it shouldn't matter here
A = ifftshift(ifftn(fftshift(B)));
ReconClown3D(:,:,ii)=A;
end
for ii=1:Z
figure, imagesc(abs(ReconClown3D(:,:,ii))), colormap gray
end
%classic ifft3d
%Modified to include ifftshift, and it DOES matter here.
ReconClown3DCalssic=ifftshift(ifftn(fftshift(Kspace)));
for ii=1:Z
figure, imagesc(abs(ReconClown3DCalssic(:,:,ii))), colormap gray
end