Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.14 KB | None | 0 0
  1. clearvars;
  2. close all;
  3. clc;
  4.  
  5. load('MR_data.mat');
  6.  
  7. img0 = I_noisefree;
  8. img1 = I_noisy1;
  9. img2 = I_noisy2;
  10. img3 = I_noisy3;
  11. img4 = I_noisy4;
  12.  
  13. %drawImages(img0, 0.1);
  14. %drawImages(img0, 0.6);
  15. drawImages(img1, 0.1);
  16. drawImages(img1, 14);
  17. %drawImages(img2, 5);
  18. %drawImages(img3, 0.1);
  19. %drawImages(img4, 0.2);
  20.  
  21.  
  22. function data_filtered = convolution(data, local_window)
  23.     FUNCTION = @(data_, local_window_)convolution_local(data_, local_window_);
  24.     data_filtered = colfilt(data, local_window, 'sliding', FUNCTION, local_window);
  25. end
  26.  
  27. function data_filtered = convolution_local(data, local_window)
  28.     Nx = size(data,2);
  29.     h = fspecial('gaussian', local_window, 25);
  30.     for i=1:Nx
  31.         patch = reshape(data(:,i),local_window);
  32.         data_filtered(i) = sum(sum(patch .* h)) ;
  33.     end
  34. end
  35.  
  36. function data_filtered = bilateral(data,local_window, sigma)
  37.     FUNCTION = @(data_,local_window_)bilateral_local(data_,local_window_, sigma);
  38.     data_filtered = colfilt(data,local_window, 'sliding', FUNCTION,local_window);
  39. end
  40.  
  41. function data_filtered = bilateral_local(data,local_window, sigma)
  42. Nx = size(data,2);
  43. Ncy = ceil(local_window(1)/2);
  44. Ncx = ceil(local_window(2)/2);
  45. h = fspecial('gaussian',local_window,25);
  46.     for i=1:Nx  
  47.         window = reshape(data(:,i),local_window);
  48.         [rows, cols] = find(window == window);
  49.         rows = reshape(rows, local_window);
  50.         cols = reshape(cols,local_window);
  51.         dist = sqrt((Ncy - rows).^2 + (Ncx - cols).^2);
  52.         psi = dist .* h;
  53.         dist_anty = abs(window - window(Ncy,Ncx));
  54.         gamma = exp((-1)*(dist_anty .* dist_anty)/(2*sigma*sigma));
  55.         %gamma = exp(((-1)*(dist_anty).^2)/2*(sigma)^2);
  56.         Wn = sum(sum(psi .* gamma));
  57.         data_filtered(i) = sum(sum(psi .* gamma .* window))/Wn;
  58.     end
  59. end
  60.  
  61.  
  62. function drawImages(image, sigma)
  63.     figure();
  64.     subplot(1,3,1);
  65.     imshow (image,[]);
  66.     title('oryginał');
  67.     subplot(1,3,2);
  68.     conv = convolution(image,[5 5]);
  69.     imshow(conv,[]);
  70.     title('convoution');
  71.     subplot(1,3,3);
  72.     bil = bilateral(image,[5 5], sigma);
  73.     imshow(bil,[]);
  74.     title("bilateral " + sigma);
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement