2014-11-04 207 views
2

我正試圖在彩色圖像上應用FFT。我提取了三個分量:紅色,綠色和藍色,然後我分別應用fft2,然後我在每個平面上應用高斯濾波器。現在我試圖在模糊後顯示紅色,綠色和藍色的組件。之後,我申請ifft2以獲得結果。使用FFT和IFFT對彩色圖像進行低通濾波

我的問題是我看到每個組件中的灰色圖像。我試圖只顯示顏色平面,但我的代碼不起作用。另外,我想將這三個組件結合在一起以返回到全綵色圖像。我在下面寫了下面的代碼。誰能告訴我我做錯了什麼?

% 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) 

回答

3

你正在做很多不必要的計算。一旦你分開過濾飛機,你可以立即將它們組合起來。此外,您的紅色組件正在執行log轉換,而其他顏色通道沒有執行此操作。另外,一旦轉換圖像,您實際上需要執行fftshift,以便您可以對光譜進行居中。你先做了fftshift,這是不正確的。同樣的事情需要應用到你的過濾器定義。一旦你過濾圖像,你必須仔細扭轉你的操作。正向轉換包括做fft2,然後是fftshift。反向操作要求您輸入ifftshift,然後ifft2之後。你走向與你的行動相反的方向。

我需要強調的一件事是,你需要鑄造你的圖像飛機加倍,以保持計算的精確性完好無損。你不這樣做,所以所有的計算都在uint8中完成。

在執行ifft2之後可能會有一些殘留虛數值,因此最好使用real消除虛部。根據您的評論,我製作了一個圖形,顯示紅色,綠色和藍色組件的色調完整,以及2 x 2窗格中的最終模糊圖像。

有了這個,這裏是你的代碼修改,以適應我的意見。您還沒有包括你的形象與您的文章,但我用維基百科上一個版本莉娜:

現在,請記住,我刪除了很多你的代碼來實現你的目標:

% 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); 

這是數字我得到:

enter image description here

+0

感謝您的重播....其實這是要求申請FFT分別對高斯和3個圖像進行乘法運算然後返回到顏色! ...如果我切換步驟,是否有任何問題? – Seereen2004 2014-11-04 22:05:13

+0

@ Seereen2004 - 那麼,您可以先對圖像執行一次「ifftshift」,然後在此之後應用「fft」將光譜帶到左上角。我不明白「切換步驟」的含義。 – rayryeng 2014-11-04 22:06:21

+0

當我嘗試你的代碼我沒有得到正確的圖像! ......它需要再次移動......而我看到綠色是最多的! – Seereen2004 2014-11-04 22:14:26