형태학적 처리 C# 소스 코드
형태학적 처리 부분 소스 코드
0. 메뉴를 다음과 같이 추가한다.
1. 형태_침식연산 메뉴를 더블클릭한 후 private void 형태침식연산ToolStripMenuItem_Click() 함수 위쪽에 침식(f_Errosion) 함수와 팽창(f_Dilation)를 추가한다.
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 |
private byte[] f_Errosion(int width, int height, byte[] data) { int x, y; byte min; byte[] newdata = new byte[data.Length]; for (y = 1; y < height - 1; y++) { for (x = 1; x < width - 1; x++) { min = 255; if (data[(y - 1) * height + x - 1] < min) min = data[(y - 1) * height + x - 1]; if (data[(y - 1) * height + x] < min) min = data[(y - 1) * height + x]; if (data[(y - 1) * height + x + 1] < min) min = data[(y - 1) * height + x + 1]; if (data[(y) * height + x - 1] < min) min = data[y * height + x - 1]; if (data[(y) * height + x] < min) min = data[y * height + x]; if (data[(y) * height + x + 1] < min) min = data[y * height + x + 1]; if (data[(y + 1) * height + x - 1] < min) min = data[(y + 1) * height + x - 1]; if (data[(y + 1) * height + x] < min) min = data[(y + 1) * height + x]; if (data[(y + 1) * height + x + 1] < min) min = data[(y + 1) * height + x + 1]; newdata[y * width + x] = min; // 최소값을 결과 영상에 저장 } } return newdata; } |
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 |
private byte[] f_Dilation(int width, int height, byte[] data) { int x, y; byte max; byte[] newdata = new byte[data.Length]; for (y = 1; y < height - 1; y++) { for (x = 1; x < width - 1; x++) { max = 0; if (data[(y - 1) * height + x - 1] > max) max = data[(y - 1) * height + x - 1]; if (data[(y - 1) * height + x] > max) max = data[(y - 1) * height + x]; if (data[(y - 1) * height + x + 1] > max) max = data[(y - 1) * height + x + 1]; if (data[(y) * height + x - 1] > max) max = data[y * height + x - 1]; if (data[(y) * height + x] > max) max = data[y * height + x]; if (data[(y) * height + x + 1] > max) max = data[y * height + x + 1]; if (data[(y + 1) * height + x - 1] > max) max = data[(y + 1) * height + x - 1]; if (data[(y + 1) * height + x] > max) max = data[(y + 1) * height + x]; if (data[(y + 1) * height + x + 1] > max) max = data[(y + 1) * height + x + 1]; newdata[y * width + x] = max; // 최대값을 결과 영상에 저장 } } return newdata; } |
2. [형태_침식연산(배경=0)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 |
///////// Start Image Processing //////////////// data = f_Errosion(bmp.Width, bmp.Height, data); ///////// End Image Processing //////////////// |
3. [형태_팽창연산(배경=0)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 |
///////// Start Image Processing //////////////// data = f_Dilation(bmp.Width, bmp.Height, data); ///////// End Image Processing //////////////// |
4. [형태_열림연산(배경=0)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 |
///////// Start Image Processing //////////////// data = f_Errosion(bmp.Width, bmp.Height, data); data = f_Errosion(bmp.Width, bmp.Height, data); data = f_Errosion(bmp.Width, bmp.Height, data); data = f_Dilation(bmp.Width, bmp.Height, data); data = f_Dilation(bmp.Width, bmp.Height, data); data = f_Dilation(bmp.Width, bmp.Height, data); ///////// End Image Processing //////////////// |
5. [형태_닫힘연산(배경=0)] 메뉴를 더블클릭한 후 다음코드를 완성한다.
1 2 3 4 5 6 7 8 |
///////// Start Image Processing //////////////// data = f_Dilation(bmp.Width, bmp.Height, data); data = f_Dilation(bmp.Width, bmp.Height, data); data = f_Dilation(bmp.Width, bmp.Height, data); data = f_Errosion(bmp.Width, bmp.Height, data); data = f_Errosion(bmp.Width, bmp.Height, data); data = f_Errosion(bmp.Width, bmp.Height, data); ///////// End Image Processing //////////////// |
6 [형태_세포개수 카운트(배경=255)] 메뉴를 더블클릭한 후 다음코드를 완성한다.