Matlab 利用FFT和IFFT对彩色图像进行低通滤波
我正在尝试将FFT应用于彩色图像。我提取三个分量:红色、绿色和蓝色,然后分别对每个分量应用Matlab 利用FFT和IFFT对彩色图像进行低通滤波,matlab,image-processing,colors,fft,Matlab,Image Processing,Colors,Fft,我正在尝试将FFT应用于彩色图像。我提取三个分量:红色、绿色和蓝色,然后分别对每个分量应用fft2,然后在每个平面应用高斯滤波器。现在,我试图显示模糊后的红色、绿色和蓝色组件。之后,我应用ifft2得到结果 我的问题是我在每个组件中看到一个灰色图像。我试图仅显示颜色平面,但我的代码不起作用。此外,我想将这三个组件组合在一起,以返回到全彩图像。我在下面编写了以下代码。谁能告诉我我做错了什么 % Calculate FFT for R , G , B images I = imread ('len
fft2
,然后在每个平面应用高斯滤波器。现在,我试图显示模糊后的红色、绿色和蓝色组件。之后,我应用ifft2
得到结果
我的问题是我在每个组件中看到一个灰色图像。我试图仅显示颜色平面,但我的代码不起作用。此外,我想将这三个组件组合在一起,以返回到全彩图像。我在下面编写了以下代码。谁能告诉我我做错了什么
% Calculate FFT for R , G , B images
I = imread ('lena.jpg');
% Extract three images
Red = I (: , : , 1);
Green = I (: , : , 2);
Blue = I(: , : , 3);
f_r = fftshift (Red);
F_r = fft2 (f_r);
f_g = fftshift (Green);
F_g = fft2 (f_g);
f_b = fftshift (Blue);
F_b = fft2 (f_b);
% Calculate the gaussian filter then find its FFT
h = fspecial( 'gaussian', [512 512] , 3.0 );
h = fftshift (h);
H = fft2(h); % Fourier Transform of 2D Gaussian
FF_R = H .* F_r ;
FF_G = H .* F_g;
FF_B = H .* F_b;
% This is to get red, green and blue images
b = zeros(512, 512);
% Inverse IFFT _RED
Ir = ifftshift(FF_R);
Irr= ifft2 (Ir);
I_R = fftshift (Irr);
IFF_R = 1 + log (abs(I_R));
figure , imshow (IFF_R , [ ]);
Con1 = im2uint8(IFF_R);
just_red_2 = cat(3, Con1, b, b);
figure, imshow (just_red_2);
% Inverse IFFT _Green
Ig = ifftshift(FF_G);
Igg= ifft2 (Ig);
I_G = fftshift (Igg);
figure , imshow (1+ log(abs(I_G)), [ ]);
just_green_2 = cat(3, b, I_G, b);
%figure, imshow (1 + log(abs(just_green_2)) );
% Inverse IFFT Blue
Ib = ifftshift(FF_B);
Ibb= ifft2 (Ib);
I_B = fftshift (Ibb);
figure , imshow (1+ log(abs(I_B)), [ ]);
just_blue_2 = cat(3, b,b, I_B);
%figure, imshow (1 + log(abs(just_blue_2)) );
%Combine the three component togather
%full_image2 = cat (3, FF_R , FF_G , FF_B);
full_image2 = cat (3, just_red_2 (:,:,1) , just_green_2(:,:,2) , just_blue_2(:, :, 3));
%full_image2 (: , : , 1) = FF_R (: , : , 1);
%full_image2 (: , : , 2) = FF_G (: , : , 2);
%full_image2 (: , : , 3) = FF_B (: , : , 3);
Full = ifft2 ( ifftshift(full_image2));
figure, imshow (Full , [ ])
Final = fftshift(Full);
figure , imshow ( full_image2 )
你做了很多不必要的计算。单独过滤平面后,可以立即合并它们。此外,您的红色组件正在执行
log
转换,而其他颜色通道没有执行此转换。此外,变换图像后,实际上需要执行fftshift
,以便将光谱居中。您首先执行了fft换档
,这是不正确的。同样的事情也需要应用到过滤器定义中。过滤图像后,必须小心地反转操作。正向转换包括执行fft2
,然后执行fftshift
。反向操作要求您先ifftshift
,然后ifft2
。你的行动方向与此相反
我需要强调的一点是,您需要将图像平面转换为双倍,以保持计算精度不变。您不需要这样做,因此所有的计算都是在uint8
中完成的
同样值得注意的是,在执行ifft2
之后,可能会有一些剩余的虚值,因此最好使用real
来消除虚值分量。根据您的评论,我制作了一个图形,显示了红色、绿色和蓝色组件,其色调保持不变,以及最终的模糊图像,显示在一个2x2窗格中
有了这些,下面是您的代码修改,以适应我的评论。您的帖子中也没有包含您的图片,但我使用了维基百科上的Lena版本:
现在,请记住,为了实现您的目标,我删除了很多代码:
% Calculate FFT for R , G , B images
I = imread ('https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png');
I = double(I); %// Change - cast to double
% Extract three images
Red = I (: , : , 1);
Green = I (: , : , 2);
Blue = I(: , : , 3);
% // Change - Transform, then shift
f_r = fft2(Red);
F_r = fftshift(f_r);
f_g = fft2(Green);
F_g = fftshift(f_g);
f_b = fft2(Blue);
F_b = fftshift(f_b);
% Calculate the gaussian filter then find its FFT
h = fspecial( 'gaussian', [512 512] , 3.0 );
%// Change - Filter, then FFT shift
H = fft2(h); % Fourier Transform of 2D Gaussian
H = fftshift(H);
% // Now filter
FF_R = H .* F_r ;
FF_G = H .* F_g;
FF_B = H .* F_b;
%// Change - perform ifftshift, then ifft2, then cast to real
% Inverse IFFT _RED
Ir = ifftshift(FF_R);
Irr = fftshift(real(ifft2(Ir)));
% Inverse IFFT _Green
Ig = ifftshift(FF_G);
Igg = fftshift(real(ifft2(Ig)));
% Inverse IFFT _Blue
Ib = ifftshift(FF_B);
Ibb = fftshift(real(ifft2(Ib)));
%// Visualize the red, green and blue components
b = zeros(512, 512, 'uint8');
image_red = cat(3,Irr, b, b);
image_green = cat(3, b, Igg, b);
image_blue = cat(3, b, b, Ibb);
%Combine the three component together
%// Change - Removed fluff
b = uint8(cat(3, Irr, Igg, Ibb));
%// NEW - Display each component as well as the final image in a new figure
figure;
subplot(2,2,1);
imshow(image_red);
subplot(2,2,2);
imshow(image_green);
subplot(2,2,3);
imshow(image_blue);
subplot(2,2,4);
imshow(b);
这是我得到的数字:
感谢您的重播。。。。实际上,这是要求应用FFT的高斯和3个图像分别然后做乘法,然后返回到颜色。。。如果我切换步骤有问题吗?@seeen2004-好的,你可以先对图像执行
ifftshift
,然后应用fft
,将频谱移到左上角。我不明白你所说的“切换步骤”是什么意思。当我尝试你的代码时,我没有得到正确的图像。。。它需要再次转换。。。我看到绿色是最美的!另外,我尝试在高斯和FFT@rayryengOops之后显示红色、绿色和蓝色。你说得对。我在绿色通道上犯了个错误。我在我的电脑上更正了这个,但不是在这里。我打了一个不必要的电话。请重试我的代码。