/// <summary> /// 膨胀 /// </summary> /// <param name="data">二维数据</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <param name="type">膨胀模板的类型</param> protected void delation(ref Byte[] data, Int32 width, Int32 height, FilterWindowType type, out Byte[] result) { result = new Byte[data.Length]; for (int i = 0; i < data.Length; i++) { result[i] = 255; } Int32[] set; Boolean hasZero; for (int i = 0; i < data.Length; i++) { hasZero = false; set = getFilterWindow(i, width, height, type); for (int j = 0; j < set.Length; j++) { if (data[set[j]] == 0) { hasZero = true; break; } } if (hasZero) { result[i] = 0; } } }
/// <summary> /// 获得滤波窗口 /// </summary> /// <param name="index">当前像素索引</param> /// <param name="width">二维数据宽度</param> /// <param name="height">二维数据高度</param> /// <param name="type">窗口类型</param> protected Int32[] getFilterWindow(Int32 index, Int32 width, Int32 height, FilterWindowType type) { Int32 row = index / width; Int32 col = index % width; Int32[] set = null; switch (type) { case FilterWindowType.Hori3: set = new Int32[3]; set[0] = row * width + System.Math.Abs(col - 1) % width; set[1] = index; set[2] = row * width + (col + 1) % width; break; case FilterWindowType.Vert3: set = new Int32[3]; set[0] = System.Math.Abs(row - 1) % height * width + col; set[1] = index; set[2] = (row + 1) % height * width + col; break; case FilterWindowType.Cros3: set = new Int32[5]; set[0] = System.Math.Abs(row - 1) % height * width + col; set[1] = row * width + System.Math.Abs(col - 1) % width; set[2] = index; set[3] = row * width + (col + 1) % width; set[4] = (row + 1) % height * width + col; break; case FilterWindowType.Rect3: set = new Int32[9]; set[0] = System.Math.Abs(row - 1) % height * width + System.Math.Abs(col - 1) % width; set[1] = System.Math.Abs(row - 1) % height * width + col; set[2] = System.Math.Abs(row - 1) % height * width + (col + 1) % width; set[3] = row * width + System.Math.Abs(col - 1) % width; set[4] = index; set[5] = row * width + (col + 1) % width; set[6] = (row + 1) % height * width + System.Math.Abs(col - 1) % width; set[7] = (row + 1) % height * width + col; set[8] = (row + 1) % height * width + (col + 1) % width; break; case FilterWindowType.Hori5: set = new Int32[5]; set[0] = row * width + System.Math.Abs(col - 2) % width; set[1] = row * width + System.Math.Abs(col - 1) % width; set[2] = index; set[3] = row * width + (col + 1) % width; set[4] = row * width + (col + 2) % width; break; case FilterWindowType.Vert5: set = new Int32[5]; set[0] = System.Math.Abs(row - 2) % height * width + col; set[1] = System.Math.Abs(row - 1) % height * width + col; set[2] = index; set[3] = (row + 1) % height * width + col; set[4] = (row + 2) % height * width + col; break; case FilterWindowType.Cros5: set = new Int32[9]; set[0] = System.Math.Abs(row - 2) % height * width + col; set[1] = System.Math.Abs(row - 1) % height * width + col; set[2] = row * width + System.Math.Abs(col - 2) % width; set[3] = row * width + System.Math.Abs(col - 1) % width; set[4] = index; set[5] = row * width + (col + 1) % width; set[6] = row * width + (col + 2) % width; set[7] = (row + 1) % height * width + col; set[8] = (row + 2) % height * width + col; break; case FilterWindowType.Rect5: set = new Int32[25]; set[0] = System.Math.Abs(row - 2) % height * width + System.Math.Abs(col - 2) % width; set[1] = System.Math.Abs(row - 2) % height * width + System.Math.Abs(col - 1) % width; set[2] = System.Math.Abs(row - 2) % height * width + col; set[3] = System.Math.Abs(row - 2) % height * width + (col + 1) % width; set[4] = System.Math.Abs(row - 2) % height * width + (col + 2) % width; set[5] = System.Math.Abs(row - 1) % height * width + System.Math.Abs(col - 2) % width; set[6] = System.Math.Abs(row - 1) % height * width + System.Math.Abs(col - 1) % width; set[7] = System.Math.Abs(row - 1) % height * width + col; set[8] = System.Math.Abs(row - 1) % height * width + (col + 1) % width; set[9] = System.Math.Abs(row - 1) % height * width + (col + 2) % width; set[10] = row * width + System.Math.Abs(col - 2) % width; set[11] = row * width + System.Math.Abs(col - 1) % width; set[12] = index; set[13] = row * width + System.Math.Abs(col + 1) % width; set[14] = row * width + System.Math.Abs(col + 2) % width; set[15] = (row + 1) % height * width + System.Math.Abs(col - 2) % width; set[16] = (row + 1) % height * width + System.Math.Abs(col - 1) % width; set[17] = (row + 1) % height * width + col; set[18] = (row + 1) % height * width + System.Math.Abs(col + 1) % width; set[19] = (row + 1) % height * width + System.Math.Abs(col + 2) % width; set[20] = (row + 2) % height * width + System.Math.Abs(col - 2) % width; set[21] = (row + 2) % height * width + System.Math.Abs(col - 1) % width; set[22] = (row + 2) % height * width + col; set[23] = (row + 2) % height * width + System.Math.Abs(col + 1) % width; set[24] = (row + 2) % height * width + System.Math.Abs(col + 2) % width; break; } return(set); }