/// <summary> /// 3×3中值滤波除杂,yuanbao,2007.10 /// </summary> /// <param name="dgGrayValue"></param> public void ClearNoise(int dgGrayValue) { int x, y; byte[] p = new byte[9]; //最小处理窗口3*3 byte s; //byte[] lpTemp=new BYTE[nByteWidth*nHeight]; int i, j; //--!!!!!!!!!!!!!!下面开始窗口为3×3中值滤波!!!!!!!!!!!!!!!! for (y = 1; y < bmpobj.Height - 1; y++) //--第一行和最后一行无法取窗口 { for (x = 1; x < bmpobj.Width - 1; x++) { //取9个点的值 p[0] = bmpobj.GetPixel(x - 1, y - 1).R; p[1] = bmpobj.GetPixel(x, y - 1).R; p[2] = bmpobj.GetPixel(x + 1, y - 1).R; p[3] = bmpobj.GetPixel(x - 1, y).R; p[4] = bmpobj.GetPixel(x, y).R; p[5] = bmpobj.GetPixel(x + 1, y).R; p[6] = bmpobj.GetPixel(x - 1, y + 1).R; p[7] = bmpobj.GetPixel(x, y + 1).R; p[8] = bmpobj.GetPixel(x + 1, y + 1).R; //计算中值 for (j = 0; j < 5; j++) { for (i = j + 1; i < 9; i++) { if (p[j] > p[i]) { s = p[j]; p[j] = p[i]; p[i] = s; } } } // if (bmpobj.GetPixel(x, y).R < dgGrayValue) bmpobj.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4])); //给有效值付中值 } } }
求改为5*5的滑动窗口