영역 기반 영상 처리 C# 소스 코드
영역 기반 처리 부분 소스 코드
용어 : 회선(convolution) : 디지털 영상에서 각각의 픽셀을 본래 픽셀과 그 주변 픽셀의 조합으로 대체하는 동작,
0. 메뉴를 다음과 같이 추가한다.
1. 영역_선명화 메뉴를 더블클릭한 후 private void 영역선명화ToolStripMenuItem_Click() 함수 위쪽에 회선(convolution) 함수를 추가한다.
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 |
private byte[] f_Convolve(int width, int height, int bias, byte[] data, double[] mask) { int depth = data.Length / width / height; byte[] newdata = new byte[data.Length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double sum = 0; for (int yy = 0; yy < 3; yy++) { for (int xx = 0; xx < 3; xx++) { if (y + yy - 1 < 0 || y + yy - 1 >= height || x + xx - 1 < 0 || x + xx - 1 >= width) { sum += 0; } else { sum += data[(y + yy - 1) * width + (x + xx - 1)] * mask[yy * 3 + xx]; } } } sum += bias; if (sum > 255) sum = 255; if (sum < 0) sum = 0; newdata[y * width + x] = (byte)sum; } } return newdata; } |
2. [영역_선명화] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 |
///////// Start Image Processing //////////////// double[] mask = new double[9]{ 0, -1, 0, -1, 5, -1, 0, -1, 0 }; data = f_Convolve(bmp.Width, bmp.Height, 0, data, mask); ///////// End Image Processing //////////////// |
3. [영역_흐리게하기] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 |
///////// Start Image Processing //////////////// double[] mask = new double[9]{ 1/9.0, 1/9.0, 1/9.0, 1/9.0, 1/9.0, 1/9.0, 1/9.0, 1/9.0, 1/9.0 }; data = f_Convolve(bmp.Width, bmp.Height, 0, data, mask); ///////// End Image Processing //////////////// |
4. [영역_엠보싱] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 |
///////// Start Image Processing //////////////// double[] mask = new double[9]{ -1, 0, 0, 0, 0, 0, 0, 0, 1 }; data = f_Convolve(bmp.Width, bmp.Height, 0, data, mask); ///////// End Image Processing //////////////// |
5. RegionEdge 함수를 추가한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private byte[] f_RegionEdge(int width, int height, byte[] data, double[] mask1, double[] mask2) { byte[] newdata = new byte[data.Length]; byte[] Er = f_Convolve(width, height, 0, data, mask1); byte[] Ec = f_Convolve(width, height, 0, data, mask2); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double sum = Math.Sqrt((double)Er[y * width + x] * Er[y * width + x] + Ec[y * width + x] * Ec[y * width + x]); if (sum > 255) sum = 255; if (sum < 0) sum = 0; newdata[y * width + x] = (byte)sum; } } return newdata; } |
6. [영역_경계선검출(Prewitt)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
///////// Start Image Processing //////////////// double[] mask1 = new double[9]{ 1, 0, -1, 1, 0, -1, 1, 0, -1 }; double[] mask2 = new double[9]{ -1, -1, -1, 0, 0, 0, 1, 1, 1 }; data = f_RegionEdge(bmp.Width, bmp.Height, data, mask1, mask2); ///////// End Image Processing //////////////// |
7. [영역_경계선검출(Roberts)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
///////// Start Image Processing //////////////// double[] mask1 = new double[9]{ -1, 0, 0, 0, 1, 0, 0, 0, 0 }; double[] mask2 = new double[9]{ 0, 0, -1, 0, 1, 0, 0, 0, 0 }; data = f_RegionEdge(bmp.Width, bmp.Height, data, mask1, mask2); ///////// End Image Processing //////////////// |
8. [영역_경계선검출(Sobel)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
///////// Start Image Processing //////////////// double[] mask1 = new double[9]{ 1, 0, -1, 2, 0, -2, 1, 0, -1 }; double[] mask2 = new double[9]{ -1, -2, -1, 0, 0, 0, 1, 2, 1 }; data = f_RegionEdge(bmp.Width, bmp.Height, data, mask1, mask2); ///////// End Image Processing //////////////// |
9. [영역_잡음제거(중간값필터)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
///////// Start Image Processing //////////////// int width = bmp.Width; int height = bmp.Height; byte[] newdata = new byte[data.Length]; for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { double[] n = new double[9]; n[0] = data[(y - 1) * width + x - 1]; n[1] = data[(y - 1) * width + x]; n[2] = data[(y - 1) * width + x + 1]; n[3] = data[y * width + x - 1]; n[4] = data[y * width + x]; n[5] = data[y * width + x + 1]; n[6] = data[(y + 1) * width + x - 1]; n[7] = data[(y + 1) * width + x]; n[8] = data[(y + 1) * width + x + 1]; Array.Sort(n); newdata[y * width + x] = (byte)n[4]; // 중간값을 결과 영상에 저장 } } ///////// End Image Processing //////////////// |