Die Entfernungstransformation in der Bildverarbeitung wird häufig bei der Erkennung von Objektanpassungen verwendet. Der Algorithmus erzeugt im Grunde genommen den Entfernungswert jedes Pixels basierend auf einem 3x3 -Fenster und ist in zwei Schritte unterteilt, um die Entfernungsumwandlung zu vervollständigen. Der erste Schritt beginnt von der oberen linken Ecke, scannt jedes Pixel von links nach rechts von oben nach unten und erkennt vier Pixel um das mittlere Pixel X und spart den Mindestabstand und die Position als Ergebnis. Die Abbildung lautet wie folgt:
Der zweite Schritt besteht darin, den Mindestabstand und die Position der benachbarten Pixel 4, 5, 6, 7 von unten nach rechts nach links zu erfassen, als Ergebnis für jedes Pixel:
Nach Abschluss dieser beiden Schritte ist die Ergebnisausgabe das Ergebnis der Abstandsumwandlung. Die vollständige Implementierung der Abstands -Transformation des Bildungsabstands kann in die folgenden Schritte unterteilt werden:
1. Initialisieren Sie das Pixel -Array, der Anfangsabstand aller Hintergrundfarbenpixel ist unendlich und der Abstand der Vordergrundpixel ist 0.
2. Starten Sie den ersten Schritt in der Abstandsumwandlung und speichern Sie das Ergebnis
A
4. Zeigen Sie alle verschiedenen Graustufenwerte gemäß den Entfernungstransformationsergebnissen an, um ein Bild zu bilden
Das Endergebnis wird wie folgt angezeigt (die linke zeigt das Originalbild an und das rechte zeigt das Ergebnis nach CDT an).
Der Quellcode für die vollständige Transformation der Binärbildmangel lautet wie folgt:
Paket com.gloomyfish.image.transform; Import Java.awt.Color; Import Java.awt.image.BuffenedImage; Import Java.util.Arrays; import com.gloomyfish.filter.study.abstractBuffenedImageop; Public Class CDTFilter erweitert AbstractBuffenedImageop {private float [] dis; // nn-distances private int [] pos; // nn-positionen, 32 Bit Index Private Color BakcgroundColor; public CDTFilter (Farbe bgcolor) {this.bakcgroundColor = bgcolor; } @Override public bufferedImage filter (bufferedImage src, bufferedimage dest) {int width = src.getWidth (); int height = src.getheight (); if (dest == null) dest = createCompatibledStimage (SRC, NULL); int [] inpixels = new int [width * Höhe]; pos = new int [width * Höhe]; dis = neuer float [Breite * Höhe]; src.getRGB (0, 0, Breite, Höhe, Inpixel, 0, Breite); // zufällig erzeugte Distanztransformationspunkte int Index = 0; Arrays.fill (dis, float.max_value); int numoffc = 0; für (int row = 0; Zeile <Höhe; Zeile ++) {für (int col = 0; col <width; col ++) {index = row * width+col; if (inpixels [index]! pos [index] = index; Numoffc ++; }}} endgültig float d1 = 1; endgültig float d2 = (float) math.sqrt (d1 * d1 + d1 * d1); System.out.println (Numoffc); float nd, nd_tmp; int i, in, cols, Reihen, nächstesPixel; // 1 2 3 // 0 i 4 // 7 6 5 // Erstpass: Vorwärts -> l-> r, tb für (rows = 1; Zeilen <Höhe - 1; Zeilen ++) {für (cols = 1; cols <breit - 1; cols ++) {für (cols = 1; cols <width - 1; nd = dis [i]; nächstesPixel = pos [i]; if (nd! = 0) {// Hintergrundpixel in = i; in += -1; // 0 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += -width; // 1 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += +1; // 2 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += +1; // 3 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } dis [i] = nd; pos [i] = nächstesPixel; }}} // zweiter Pass: rückwärts-> r-> l, bt // genau wie der erste Durchgang, nur in umgekehrte Richtung für (Zeilen = Höhe-2; Zeilen> = 1; Zeilen--) {für (cols = width-2; cols> = 1; cols-) {i = Zeilen * width + cols; nd = dis [i]; nächstesPixel = pos [i]; if (nd! = 0) {in = i; in += +1; // 4 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += +width; // 5 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += -1; // 6 if ((nd_tmp = d1 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } in += -1; // 7 if ((nd_tmp = d2 + dis [in]) <nd) {nd = nd_tmp; nächstesPixel = pos [in]; } dis [i] = nd; pos [i] = nächstesPixel; }}} für (int row = 0; Zeile <Höhe; Zeile ++) {für (int col = 0; col <width; col ++) {index = row * width+col; if (float.max_value! = dis [index]) {int Gray = clamp ((int) (dis [index])); Inpixel [index] = (255 << 24) | (Gray << 16) | (Gray << 8) | grau; }}} setRGB (Dest, 0, 0, Breite, Höhe, Inpixel); Rückkehrend; } private Int -Clamp (int i) {return i> 255? 255: (i <0? 0: i); }}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.