The column in the middle is the image with gaussian blur and the right column is the image with median filter – top amount = 2 and bottom amount = 4. The gaussian blur seems to be better at removing points of noise although I like the effect the median filter creates.
for (int i = 0; i < width; i++){ for (int j = 0; j < height; j++){ int pixIndex = j * width + i; int i_m_1 = MAX(0, i-1); int i_p_1 = MIN(width-1, i+1); int j_m_1 = MAX(0, j-1); int j_p_1 = MIN(height-1, j+1); int ne = (j_m_1 * width + i_m_1); int n_ = (j_m_1 * width + i ); int nw = (j_m_1 * width + i_p_1); int _e = (j * width + i_m_1); int me = (j * width + i ); int _w = (j * width + i_p_1); int se = (j_p_1 * width + i_m_1); int s_ = (j_p_1 * width + i ); int sw = (j_p_1 * width + i_p_1); //place the values in the array sortArray[0] = pixelsMedian1[ne]; sortArray[1] = pixelsMedian1[n_]; sortArray[2] = pixelsMedian1[nw]; sortArray[3] = pixelsMedian1[_e]; sortArray[4] = pixelsMedian1[me]; sortArray[5] = pixelsMedian1[_w]; sortArray[6] = pixelsMedian1[se]; sortArray[7] = pixelsMedian1[s_]; sortArray[8] = pixelsMedian1[sw]; qsort(sortArray, 9, sizeof(int), compare); int newValue = sortArray[4]; if (newValue < 0) newValue = 0; if (newValue > 255) newValue = 255; pixelsMedian1[pixIndex] = newValue; } } texture3.loadData(pixelsMedian1, width, height, GL_LUMINANCE);