最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java圖像處理之倒角距離變換

 更新時(shí)間:2018年01月19日 10:53:52   作者:gloomyfish  
這篇文章主要為大家詳細(xì)介紹了java圖像處理之倒角距離變換的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

完成這兩步以后,得到的結(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é)果)

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

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); 
  } 
 
} 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用Java Apache POI 生成Word文檔示例代碼

    利用Java Apache POI 生成Word文檔示例代碼

    本篇文章主要介紹了利用Java Apache POI 生成Word文檔示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java使用遞歸解決算法問題的實(shí)例講解

    Java使用遞歸解決算法問題的實(shí)例講解

    遞歸算法的實(shí)質(zhì)是把問題分解成規(guī)模縮小的同類問題的子問題,然后遞歸調(diào)用方法來表示問題的解,這里我們就來看幾個(gè)Java使用遞歸解決算法問題的實(shí)例講解
    2016-06-06
  • Mybatis之Select Count(*)的獲取返回int的值操作

    Mybatis之Select Count(*)的獲取返回int的值操作

    這篇文章主要介紹了Mybatis之Select Count(*)的獲取返回int的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸

    SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸

    這篇文章主要介紹了SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java語言實(shí)現(xiàn)最大堆代碼示例

    Java語言實(shí)現(xiàn)最大堆代碼示例

    這篇文章主要介紹了Java語言實(shí)現(xiàn)最大堆代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Springboot中@Value注解的場景用法及可能遇到的問題詳解

    Springboot中@Value注解的場景用法及可能遇到的問題詳解

    這篇文章主要給大家介紹了關(guān)于Springboot中@Value注解的場景用法及可能遇到問題的相關(guān)資料, @Value通常用于注入外部化屬性,即外部配置屬性的注入,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • java調(diào)用接口返回亂碼問題及解決

    java調(diào)用接口返回亂碼問題及解決

    這篇文章主要介紹了java調(diào)用接口返回亂碼問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 盤點(diǎn)SpringBoot中@Async注解的遇到的坑點(diǎn)及解決辦法

    盤點(diǎn)SpringBoot中@Async注解的遇到的坑點(diǎn)及解決辦法

    SpringBoot是一個(gè)流行的Java開發(fā)框架,在異步編程方面,Spring Boot提供了@Async注解,它能夠讓方法異步執(zhí)行,然而,在使用@Async注解時(shí),有一些潛在的坑需要注意,本文將深入探討Spring Boot中使用@Async注解時(shí)可能遇到的8大坑點(diǎn),并提供相應(yīng)的解決方案
    2024-03-03
  • Spring Boot如何支持嵌入式Servlet容器

    Spring Boot如何支持嵌入式Servlet容器

    這篇文章主要介紹了Spring Boot如何支持嵌入式Servlet容器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源示例

    Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源,結(jié)合實(shí)例形式分析了Spring使用ClassPathResource加載xml資源的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

聂荣县| 宁明县| 台北市| 扎赉特旗| 吴旗县| 台南市| 凤阳县| 陈巴尔虎旗| 崇仁县| 黔东| 常熟市| 长垣县| 柏乡县| 宜兰县| 从江县| 黄山市| 孟村| 弥勒县| 罗定市| 信阳市| 宝鸡市| 林西县| 大新县| 布拖县| 中宁县| 茌平县| 万宁市| 广丰县| 巍山| 晋中市| 全椒县| 张家口市| 云梦县| 宜春市| 玛多县| 武功县| 保定市| 麟游县| 克拉玛依市| 监利县| 宁安市|