■ 중간값 필터 회선을 적용하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#region 중간값 필터 회선 적용하기 - ConvolveMedianValueFilter(sourceArray, windowWidth, windowHeight) /// <summary> /// 중간값 필터 회선 적용하기 /// </summary> /// <param name="sourceArray">소스 배열</param> /// <param name="windowWidth">윈도우 너비</param> /// <param name="windowHeight">윈도우 높이</param> /// <returns>중간값 필터 회선 적용 배열</returns> public int[,] ConvolveMedianValueFilter(int[,] sourceArray, int windowWidth, int windowHeight) { int sourceWidth = sourceArray.GetUpperBound(1) + 1; int sourceHeight = sourceArray.GetUpperBound(0) + 1; int[,] resultArray = new int[sourceHeight, sourceWidth]; int xPadding = windowWidth / 2; int yPadding = windowHeight / 2; int windowSize = windowHeight * windowWidth; int[] targetArray = new int[windowSize]; int index; for(int y = 0; y < sourceHeight - 2 * yPadding; y++) { for(int x = 0; x < sourceWidth - 2 * xPadding; x++) { index = 0; for(int r = 0; r < windowHeight; r++) { for(int c = 0; c < windowWidth; c++) { targetArray[index++] = sourceArray[y + r, x + c]; } } resultArray[y + yPadding, x + xPadding] = GetMedianValue(targetArray, windowSize); } } for(int y = 0; y < yPadding; y++) { for(int x = xPadding; x < sourceWidth - xPadding; x++) { resultArray[y, x] = resultArray[yPadding, x]; resultArray[sourceHeight - 1 - y, x] = resultArray[sourceHeight - 1 - yPadding, x]; } } for(int x = 0; x < xPadding; x++) { for(int y = 0; y < sourceHeight; y++) { resultArray[y, x] = resultArray[y, xPadding]; resultArray[y, sourceWidth - 1 - x] = resultArray[y, sourceWidth - 1 - xPadding]; } } return resultArray; } #endregion #region 중간값 구하기 - GetMedianValue(targetArray, targetLength) /// <summary> /// 중간값 구하기 /// </summary> /// <param name="targetArray">타겟 배열</param> /// <param name="targetLength">타겟 길이</param> /// <returns>중간값</returns> private int GetMedianValue(int[] targetArray, int targetLength) { int value; for(int i = 0; i < targetLength - 1; i++) { for(int j = i + 1; j < targetLength; j++) { if(targetArray[i] > targetArray[j]) { value = targetArray[i]; targetArray[i] = targetArray[j]; targetArray[j] = value; } } } return (targetArray[targetLength / 2]); } #endregion |