Advertisement
tsounakis

1.3 Signal Processing (RGB)

Nov 5th, 2021 (edited)
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.03 KB | None | 0 0
  1. function one_3_rgb()
  2.     ft = find_spectrum("im-4.png");
  3.     reconstruct(ft, 4);
  4.    
  5.     figure(2);
  6.     ft = find_spectrum("im-4.png");
  7.     reconstruct(ft, 8);
  8. end
  9.  
  10. function ft = find_spectrum(im)
  11.     image = imread(im);      
  12.     show_image(image, "RGB image (original)", 1, false);
  13.  
  14.     R = image(:, :, 1);
  15.     G = image(:, :, 2);
  16.     B = image(:, :, 3);
  17.     fourier_R = fftshift(fft2(R));
  18.     fourier_G = fftshift(fft2(G));
  19.     fourier_B = fftshift(fft2(B));
  20.    
  21.     log_transR = log(1 + abs(fourier_R));
  22.     log_transG = log(1 + abs(fourier_G));
  23.     log_transB = log(1 + abs(fourier_B));
  24.     show_image(log_transR, "Spectrum (R-channel)", 1, true);
  25.     show_image(log_transG, "Spectrum (G-channel)", 2, true);
  26.     show_image(log_transB, "Spectrum (B-channel)", 3, true);
  27.    
  28.     ft = cat(3, fourier_R, fourier_G, fourier_B);
  29.   end
  30.  
  31.  
  32. function reconstruct(ft, k)    
  33.     N = size(ft(:,:,1), 1);
  34.     M = size(ft(:,:,1), 2);
  35.    
  36.     new_fR = zeros(N, M);
  37.     new_fG = zeros(N, M);
  38.     new_fB = zeros(N, M);
  39.     new_fR(round(N/2 - N/(2*k)):round(N/2 + N/(2*k)), round(M/2 - M/(2*k)):round(M/2 + M/(2*k))) = ft(round(N/2 - N/(2*k)):round(N/2 + N/(2*k)), round(M/2 - M/(2*k)):round(M/2 + M/(2*k)),1);
  40.     new_fG(round(N/2 - N/(2*k)):round(N/2 + N/(2*k)), round(M/2 - M/(2*k)):round(M/2 + M/(2*k))) = ft(round(N/2 - N/(2*k)):round(N/2 + N/(2*k)), round(M/2 - M/(2*k)):round(M/2 + M/(2*k)),2);
  41.     new_fB(round(N/2 - N/(2*k)):round(N/2 + N/(2*k)), round(M/2 - M/(2*k)):round(M/2 + M/(2*k))) = ft(round(N/2 - N/(2*k)):round(N/2 + N/(2*k)), round(M/2 - M/(2*k)):round(M/2 + M/(2*k)),3);
  42.    
  43.     recon = abs(ifft2(cat(3, new_fR, new_fG, new_fB)));
  44.    
  45.     show_image(recon, "Reconstructed image after RGB (3-channel) N/" + k + " F.T.", 3, false);
  46. end
  47.  
  48. function show_image(im, tl, r, isFourier)
  49.     if isFourier == false
  50.         subplot(2,3,r);
  51.         imshow(uint8(im));
  52.         title(tl);  
  53.     else
  54.         subplot(2,3,3+r);
  55.         imagesc(im);
  56.         colormap("jet");
  57.         title(tl);  
  58.         colorbar
  59.     end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement