溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java圖像處理之倒角距離變換的示例分析

發(fā)布時(shí)間:2021-08-23 10:53:50 來源:億速云 閱讀:187 作者:小新 欄目:編程語言

這篇文章主要介紹了java圖像處理之倒角距離變換的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

圖像處理中的倒角距離變換(Chamfer Distance Transform)在對(duì)象匹配識(shí)別中經(jīng)常用到,算法基本上是基于3x3的窗口來生成每個(gè)像素的距離值,分為兩步完成距離變換,第一步從左上角開始,從左向右、從上到下移動(dòng)窗口掃描每個(gè)像素,檢測(cè)在中心像素x的周圍0、1、2、3四個(gè)像素,保存最小距離與位置作為結(jié)果,圖示如下:

java圖像處理之倒角距離變換的示例分析

第二步從底向上、從右向左,對(duì)每個(gè)像素,檢測(cè)相鄰像素4、5、6、7保存最小距離與位置作為結(jié)果,如圖示所:

java圖像處理之倒角距離變換的示例分析

完成這兩步以后,得到的結(jié)果輸出即為倒角距離變換的結(jié)果。完整的圖像倒角距離變換代碼實(shí)現(xiàn)可以分為如下幾步:

1.對(duì)像素?cái)?shù)組進(jìn)行初始化,所有背景顏色像素點(diǎn)初始距離為無窮大,前景像素點(diǎn)距離為0

2.開始倒角距離變換中的第一步,并保存結(jié)果

3.基于第一步結(jié)果完成倒角距離變換中的第二步

4.根據(jù)距離變換結(jié)果顯示所有不同灰度值,形成圖像

最終結(jié)果顯示如下(左邊表示原圖、右邊表示CDT之后結(jié)果)

java圖像處理之倒角距離變換的示例分析

完整的二值圖像倒角距離變換的源代碼如下:

package com.gloomyfish.image.transform; 
 
import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.util.Arrays; 
 
import com.gloomyfish.filter.study.AbstractBufferedImageOp; 
 
public class CDTFilter extends AbstractBufferedImageOp { 
  private float[] dis; // nn-distances 
  private int[] pos; // nn-positions, 32 bit index 
  private Color bakcgroundColor; 
   
  public CDTFilter(Color bgColor) 
  { 
    this.bakcgroundColor = bgColor; 
  } 
 
  @Override 
  public BufferedImage filter(BufferedImage src, BufferedImage dest) { 
    int width = src.getWidth(); 
    int height = src.getHeight(); 
 
    if (dest == null) 
      dest = createCompatibleDestImage(src, null); 
 
    int[] inPixels = new int[width * height]; 
    pos = new int[width * height]; 
    dis = new float[width * height]; 
    src.getRGB(0, 0, width, height, inPixels, 0, width); 
    // 隨機(jī)生成距離變換點(diǎn) 
    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++; 
        } 
      } 
    } 
    final float d1 = 1; 
    final float d2 = (float) Math.sqrt(d1 * d1 + d1 * d1); 
    System.out.println(numOfFC); 
    float nd, nd_tmp; 
    int i, in, cols, rows, nearestPixel; 
 
    // 1 2 3 
    // 0 i 4 
    // 7 6 5 
    // first pass: forward -> L->R, T-B 
    for (rows = 1; rows < height - 1; rows++) { 
      for (cols = 1; cols < width - 1; cols++) { 
        i = rows * width + cols; 
 
        nd = dis[i]; 
        nearestPixel = pos[i]; 
        if (nd != 0) { // skip background pixels 
          in = i; 
 
          in += -1; // 0 
          if ((nd_tmp = d1 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          in += -width; // 1 
          if ((nd_tmp = d2 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          in += +1; // 2 
          if ((nd_tmp = d1 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          in += +1; // 3 
          if ((nd_tmp = d2 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          dis[i] = nd; 
          pos[i] = nearestPixel; 
        } 
      } 
    } 
 
    // second pass: backwards -> R->L, B-T 
    // exactly same as first pass, just in the reverse direction 
    for (rows = height - 2; rows >= 1; rows--) { 
      for (cols = width - 2; cols >= 1; cols--) { 
        i = rows * width + cols; 
 
        nd = dis[i]; 
        nearestPixel = pos[i]; 
        if (nd != 0) { 
          in = i; 
 
          in += +1; // 4 
          if ((nd_tmp = d1 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          in += +width; // 5 
          if ((nd_tmp = d2 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          in += -1; // 6 
          if ((nd_tmp = d1 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          in += -1; // 7 
          if ((nd_tmp = d2 + dis[in]) < nd) { 
            nd = nd_tmp; 
            nearestPixel = pos[in]; 
          } 
 
          dis[i] = nd; 
          pos[i] = nearestPixel; 
 
        } 
      } 
    } 
 
    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 gray = clamp((int) (dis[index])); 
          inPixels[index] = (255 << 24) | (gray << 16) | (gray << 8) 
              | gray; 
        } 
      } 
    } 
    setRGB(dest, 0, 0, width, height, inPixels); 
    return dest; 
  } 
 
  private int clamp(int i) { 
    return i > 255 ? 255 : (i < 0 ? 0 : i); 
  } 
 
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“java圖像處理之倒角距離變換的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI