■ 회선을 적용하는 방법을 보여준다.
▶ 회선 적용하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 |
double[,] maskArray = new double[,] { { 0.0, -1.0, 0.0 }, {-1.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0 } }; int[,] resultArray = Convolve(sourceArray, maskArray, 128); |
▶ 회선 적용하기 (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 |
using System; #region 회선 적용하기 - Convolve(sourceArray, maskArray, biasValue) /// <summary> /// 회선 적용하기 /// </summary> /// <param name="sourceArray">소스 배열</param> /// <param name="maskArray">마스크 배열</param> /// <param name="biasValue">바이어스 값</param> /// <returns>회선 적용 배열</returns> public int[,] Convolve(int[,] sourceArray, double[,] maskArray, int biasValue) { int sourceWidth = sourceArray.GetUpperBound(1) + 1; int sourceHeight = sourceArray.GetUpperBound(0) + 1; int maskWidth = maskArray.GetUpperBound(1) + 1; int maskHeight = maskArray.GetUpperBound(0) + 1; int[,] resultArray = new int[sourceHeight, sourceWidth]; int xPadding = maskWidth / 2; int yPadding = maskHeight / 2; double summary; for(int y = 0; y < sourceHeight - 2 * yPadding; y++) { for(int x = 0; x < sourceWidth - 2 * xPadding; x++) { summary = 0.0; for(int r = 0; r < maskHeight; r++) { for(int c = 0; c < maskWidth; c++) { summary += sourceArray[y + r, x + c] * maskArray[r, c]; } } summary += biasValue; resultArray[y + yPadding, x + xPadding] = (int)Math.Max(0.0, Math.Min(255.0, summary)); } } 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 |