The median filter takes in the neighbourhood the median color (see Median filter)

(to test the function below, you can use these input and output solutions)

fcn medianFilter(img){  //-->new image
   var [const] window=[-2..2].walk(), edge=(window.len()/2);  // 5x5 window
 
   MX,MY,new := img.w,img.h,PPM(MX,MY);
   pixel,pixels:=List(),List();
   foreach x,y in ([edge..MX-edge-1],[edge..MY-edge-1]){
      pixels.clear();
      foreach ox,oy in (window,window){   // construct sorted list as pixels are read.
	 pixels.merge(pixel.clear(img[x+ox, y+oy]));    // merge sort two lists
      }
      new[x,y]=pixels[4];  // median value
   }
   new
}