이미지 처리의 모따기 거리 변환은 종종 객체 일치 인식에 사용됩니다. 알고리즘은 기본적으로 3x3 창을 기반으로 각 픽셀의 거리 값을 생성하고 거리 변환을 완료하기 위해 두 단계로 나뉩니다. 첫 번째 단계는 왼쪽 상단 모서리에서 시작하여 각 픽셀을 왼쪽에서 오른쪽으로, 위에서 아래로 스캔하고 중앙 픽셀 X 주위에 4 개의 픽셀을 감지하고 최소 거리와 위치를 결과적으로 저장합니다. 그림은 다음과 같습니다.
두 번째 단계는 각 픽셀에 대해 인접한 픽셀 4, 5, 6, 7의 최소 거리 및 위치를 탐지하는 것입니다.
이 두 단계를 완료 한 후 결과 출력은 모따기 거리 변환의 결과입니다. 완전한 이미지 촬영 거리 변환 코드 구현은 다음 단계로 나눌 수 있습니다.
1. 픽셀 배열을 초기화하고 모든 배경 컬러 픽셀의 초기 거리는 무한대이며 전경 픽셀의 거리는 0입니다.
2. 모따기 거리 변환의 첫 번째 단계를 시작하고 결과를 저장하십시오.
3. 첫 번째 단계의 결과를 기반으로 모따기 거리 변환의 두 번째 단계를 완료합니다.
4. 거리 변환 결과에 따라 모든 다른 회색도 값을 표시하여 이미지를 형성합니다.
최종 결과는 다음과 같이 표시됩니다 (왼쪽은 원본 이미지를 나타내고 오른쪽은 CDT 이후 결과를 나타냅니다).
완전한 바이너리 이미지 모따기 거리 변환의 소스 코드는 다음과 같습니다.
패키지 com.gloomyfish.image.transform; import java.awt.color; java.awt.image.bufferedimage import; import java.util.arrays; com.gloomyfish.filter.study.AbstractBufferedImageop import; 공개 클래스 cdtfilter는 AbstractBufferedImageop {private float [] dis; // nn-distances private int [] pos; // nn-positions, 32 비트 인덱스 개인 색상 BakcgroundColor; public cdtfilter (컬러 bgcolor) {this.bakcgroundcolor = bgcolor; } @override public bufferedimage 필터 (bufferedImage src, bufferedImage dest) {int width = src.getWidth (); int height = src.getheight (); if (dest == null) dest = createCompatibledStimage (src, null); int [] inpixels = new int [너비 * 높이]; pos = new int [너비 * 높이]; dis = 새 플로트 [너비 * 높이]; src.getrgb (0, 0, 너비, 높이, 인 픽셀, 0, 너비); // 무작위로 생성 된 거리 변환 지점 int index = 0; arrays.fill (dis, float.max_value); int numoffc = 0; for (int row = 0; row <height; row ++) {for (int col = 0; col <width; col ++) {index = row * width+col; if (inpixels [index]! = bakcgroundcolor.getrgb ()) {dis [index] = 0; pos [index] = index; numoffc ++; }}} 최종 플로트 d1 = 1; 최종 플로트 d2 = (float) math.sqrt (d1 * d1 + d1 * d1); System.out.println (numoffc); float nd, nd_tmp; int i, in, cols, 행, 가장 가까운 픽셀; // 1 2 3 // 0 i 4 // 7 6 5 // 첫 번째 패스 : Forward-> l-> r, tb for (rows = 1; rows <높이 -1; rows ++) {for (cols = 1; cols <width -1; cols ++) {(cols = 1; cols <width -1; cols ++) {i = Rows * Rows * Rows * Rows; nd = dis [i]; 가장 가까운 pixel = pos [i]; if (nd! = 0) {// 배경 픽셀을 건너 뜁니다. in = i; += -1; // 0 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += -width; // 1 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += +1; // 2 if ((nd_tmp = d1 + dis [in] <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += +1; // 3 if ((nd_tmp = d2 + dis [in] <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } dis [i] = nd; pos [i] = 가장 가까운 픽셀; }}} // 두 번째 패스 : 뒤로-> r-> l, bt // 첫 번째 패스와 정확히 동일합니다. nd = dis [i]; 가장 가까운 pixel = pos [i]; if (nd! = 0) {in = i; += +1; // 4 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += +너비; // 5 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } in += -1; // 7 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; 가장 가까운 pixel = pos [in]; } dis [i] = nd; pos [i] = 가장 가까운 픽셀; }}} for (int row = 0; row <height; row ++) {for (int col = 0; col <width; col ++) {index = row * width+col; if (float.max_value! = dis [index]) {int Grey = clamp ((int) (dis [index]); inpixels [index] = (255 << 24) | (회색 << 16) | (회색 << 8) | 회색; }}} setrgb (dest, 0, 0, 너비, 높이, 인 픽셀); 반환 데스트; } private int clamp (int i) {return i> 255? 255 : (i <0? 0 : i); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.