title: What is exact difference between mean filter and median filter
body: <p>I am trying to understand the c code of mean filter and median filter .
  I understood the mean filter and implemented too .But for the median filter I am not able to understand the below logic used in one of the reference code for median filter .
  I want to know why there is calculation of min and max in below example of median filter :</p>
  
  <pre><code>#define PAD_LINES 2
  void ExecuteMedianFilterReference(cl_uint* p_input, cl_uint* p_output, cl_int width, cl_uint height)
  {
      memset(p_output, 0,   width * (height+4));
  
      // do the Median
      for(cl_uint y = 0; y &lt; height; y++)        // rows loop
      {
          int iOffset = (y+PAD_LINES) * width;
          int iPrev = iOffset - width;
          int iNext = iOffset + width;
  
          for(int x = 0; x &lt; width; x++)        // columns loop
          {
              unsigned uiRGBA[9];
  
              //get pixels within aperture
              uiRGBA[0] = p_input[iPrev + x - 1];
              uiRGBA[1] = p_input[iPrev + x];
              uiRGBA[2] = p_input[iPrev + x + 1];
  
              uiRGBA[3] = p_input[iOffset + x - 1];
              uiRGBA[4] = p_input[iOffset + x];
              uiRGBA[5] = p_input[iOffset + x + 1];
  
              uiRGBA[6] = p_input[iNext + x - 1];
              uiRGBA[7] = p_input[iNext + x];
              uiRGBA[8] = p_input[iNext + x + 1];
  
              unsigned uiMin = c4min(uiRGBA[0], uiRGBA[1]);
              unsigned uiMax = c4max(uiRGBA[0], uiRGBA[1]);
              uiRGBA[0] = uiMin;
              uiRGBA[1] = uiMax;
  
              uiMin = c4min(uiRGBA[3], uiRGBA[2]);
              uiMax = c4max(uiRGBA[3], uiRGBA[2]);
              uiRGBA[3] = uiMin;
              uiRGBA[2] = uiMax;
  
              uiMin = c4min(uiRGBA[2], uiRGBA[0]);
              uiMax = c4max(uiRGBA[2], uiRGBA[0]);
              uiRGBA[2] = uiMin;
              uiRGBA[0] = uiMax;
  
              uiMin = c4min(uiRGBA[3], uiRGBA[1]);
              uiMax = c4max(uiRGBA[3], uiRGBA[1]);
              uiRGBA[3] = uiMin;
              uiRGBA[1] = uiMax;
  
              uiMin = c4min(uiRGBA[1], uiRGBA[0]);
              uiMax = c4max(uiRGBA[1], uiRGBA[0]);
              uiRGBA[1] = uiMin;
              uiRGBA[0] = uiMax;
  
              uiMin = c4min(uiRGBA[3], uiRGBA[2]);
              uiMax = c4max(uiRGBA[3], uiRGBA[2]);
              uiRGBA[3] = uiMin;
              uiRGBA[2] = uiMax;
  
              uiMin = c4min(uiRGBA[5], uiRGBA[4]);
              uiMax = c4max(uiRGBA[5], uiRGBA[4]);
              uiRGBA[5] = uiMin;
              uiRGBA[4] = uiMax;
  
              uiMin = c4min(uiRGBA[7], uiRGBA[8]);
              uiMax = c4max(uiRGBA[7], uiRGBA[8]);
              uiRGBA[7] = uiMin;
              uiRGBA[8] = uiMax;
  
              uiMin = c4min(uiRGBA[6], uiRGBA[8]);
              uiMax = c4max(uiRGBA[6], uiRGBA[8]);
              uiRGBA[6] = uiMin;
              uiRGBA[8] = uiMax;
  
              uiMin = c4min(uiRGBA[6], uiRGBA[7]);
              uiMax = c4max(uiRGBA[6], uiRGBA[7]);
              uiRGBA[6] = uiMin;
              uiRGBA[7] = uiMax;
  
              uiMin = c4min(uiRGBA[4], uiRGBA[8]);
              uiMax = c4max(uiRGBA[4], uiRGBA[8]);
              uiRGBA[4] = uiMin;
              uiRGBA[8] = uiMax;
  
              uiMin = c4min(uiRGBA[4], uiRGBA[6]);
              uiMax = c4max(uiRGBA[4], uiRGBA[6]);
              uiRGBA[4] = uiMin;
              uiRGBA[6] = uiMax;
  
              uiMin = c4min(uiRGBA[5], uiRGBA[7]);
              uiMax = c4max(uiRGBA[5], uiRGBA[7]);
              uiRGBA[5] = uiMin;
              uiRGBA[7] = uiMax;
  
              uiMin = c4min(uiRGBA[4], uiRGBA[5]);
              uiMax = c4max(uiRGBA[4], uiRGBA[5]);
              uiRGBA[4] = uiMin;
              uiRGBA[5] = uiMax;
  
              uiMin = c4min(uiRGBA[6], uiRGBA[7]);
              uiMax = c4max(uiRGBA[6], uiRGBA[7]);
              uiRGBA[6] = uiMin;
              uiRGBA[7] = uiMax;
  
              uiMin = c4min(uiRGBA[0], uiRGBA[8]);
              uiMax = c4max(uiRGBA[0], uiRGBA[8]);
              uiRGBA[0] = uiMin;
              uiRGBA[8] = uiMax;
  
              uiRGBA[4] = c4max(uiRGBA[0], uiRGBA[4]);
              uiRGBA[5] = c4max(uiRGBA[1], uiRGBA[5]);
  
              uiRGBA[6] = c4max(uiRGBA[2], uiRGBA[6]);
              uiRGBA[7] = c4max(uiRGBA[3], uiRGBA[7]);
  
              uiRGBA[4] = c4min(uiRGBA[4], uiRGBA[6]);
              uiRGBA[5] = c4min(uiRGBA[5], uiRGBA[7]);
  
              // convert and copy to output
              p_output[(y+PAD_LINES) * width + x] = c4min(uiRGBA[4], uiRGBA[5]);
          }
      }
  }
  </code></pre>