document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. %{
  2. -------------------------------
  3. 2D Median Filter
  4. -------------------------------
  5. 1.find the greyscale of an image
  6. 2.add noise to it using imnoise()
  7. 3.use median filter to see the effect
  8. %}
  9.  
  10. %%reading the image and getting the grayscale image
  11. image=imread(\'x.jpg\');
  12. gray=rgb2gray(image);
  13. figure,imshow(image),title(\'Original\');
  14. figure,imshow(gray),title(\'Gray\');
  15.  
  16. %%adding noise to the image
  17. noisy=imnoise(gray,\'salt & pepper\',0.02);
  18. figure,imshow(noisy),title(\'Noise Added\');
  19. noisy1=noisy ; %%copy of the noisy image
  20. noisy2=noisy; %%for testing with medfilt()
  21. %%implementing the median blur on the noisy image
  22. [row,col] = size(noisy);
  23. %%these are created for the padding
  24. arr_up=noisy1(1,:); %top padding
  25. arr_lt=noisy1(:,1); %left left padding
  26. arr_rt=noisy1(:,col); %right padding
  27. arr_dn=noisy1(row,:); %down padding
  28.  
  29. %%corner values
  30. top_lt=noisy1(1,1);
  31. top_rt=noisy1(1,col);
  32. bot_lt=noisy1(row,1);
  33. bot_rt=noisy1(row,col);
  34.  
  35. lt_pad=[top_lt;arr_lt;bot_lt];
  36. rt_pad=[top_rt;arr_rt;bot_rt];
  37. %%adding the pads
  38. pad1=[arr_up;noisy1;arr_dn];
  39. final=[lt_pad,pad1,rt_pad];
  40. %%now final consists of the padded noisy image
  41. final1=final;
  42. [r,c]=size(final1);
  43.  
  44. for i=2:r-1
  45.     for j=2:c-1
  46.          array1=[ final1(i-1,j-1) final1(i-1,j) final1(i,j+1) final1(i,j-1) final1(i,j) final1(i,j+1) final1(i+1,j-1) final1(i+1,j) final1(i+1,j+1) ];
  47.          final1(i,j)=median(array1);
  48.     end
  49. end
  50. figure,imshow(final1),title(\'After Applying Median Blur\');
  51. mim=medfilt2(noisy2);
  52. figure,imshow(mim),title(\'Medfilt()\');
');