Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.34 KB | None | 0 0
  1. function [newImg] = myMedian(srcimg, rows, cols)
  2. %TODO - pad the image
  3. imageWidth = size(srcimg, 1);
  4. imageHeight = size(srcimg, 2);
  5.  
  6. disp(imageHeight);
  7. disp(imageWidth);
  8.  
  9. pad = max(floor(rows / 2), floor(cols / 2));
  10. paddedmat = zeros(size(srcimg) + pad);
  11.  
  12.          for x=pad + 1:size(srcimg,1)
  13.             for y=pad + 1:size(srcimg,2)
  14.                 paddedmat(x,y)=srcimg(x - pad + 1, y - pad + 1);
  15.             end
  16.         end
  17. srcimg = paddedmat;
  18.  
  19. outputimg = zeros(size(srcimg));
  20. outputimg = double(outputimg);
  21.  
  22. windowHeight = rows;
  23. windowWidth = cols;
  24.  
  25. window = zeros(1, windowHeight * windowWidth);
  26.  
  27. edgex = floor(windowHeight / 2);
  28. edgey = floor(windowWidth / 2);
  29.  
  30. helperHeight = floor((windowHeight / 2));
  31. helperWidth = floor((windowHeight / 2));
  32.  
  33. for x = edgex + 1:(size(srcimg, 1) - pad)
  34.     for y = edgey + 1:(size(srcimg, 2) - pad)
  35.         %disp("x: " + x + ", y: " + y);
  36.         %disp("edgex: " + edgex);
  37.         %disp("edgey: " + edgey);
  38.         %disp("pad: " + pad);
  39.        k = srcimg(x - helperHeight:x + helperHeight, y - helperWidth:y + helperWidth);
  40.        window = reshape(k,1,[]);
  41.        
  42.        window = sort(window);
  43.        outputimg(x, y) = window( floor((windowWidth * windowHeight) / 2));
  44.     end
  45. end
  46. %disp(pad);
  47. newImg = outputimg(1 + pad:size(outputimg, 1),1 + pad:size(outputimg, 2));%zeros(0);
  48.  
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement