图像处理之基于采样距离变换算法

简介: 图像处理之基于采样距离变换算法

图像处理之基于采样距离变换算法

算法是别人提出来的,感兴趣可以搜索《Distance Transforms of Sampled Functions》

这篇论文,网上也有很多实现的代码,但是结构不是很好,而且很分散不是一个完整的

算法。所以我整理了一下,写成一个单独的类,只要简单调用一下即可出结果图片。

至于算法原理什么的,我真很难解释清楚,大致的思想是基于能量最小化的,分别

进行行与列的1D距离变变换采样。

运行结果:


算法代码:

package com.gloomyfish.image.transform;
 
import java.awt.image.BufferedImage;
 
import com.gloomyfish.filter.study.GrayFilter;
/**
 * 
 * @author gloomyfish
 *
 */
public class FastDistanceTransformAlg extends GrayFilter {
  public final static double INF = 1E20;
  private int backgroundColor = 0; // default black
 
  public int getBackgroundColor() {
    return backgroundColor;
  }
 
  public void setBackgroundColor(int backgroundColor) {
    this.backgroundColor = backgroundColor;
  }
 
  @Override
  public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    int width = src.getWidth();
        int height = src.getHeight();
        dest = super.filter(src, null);
        
        //
        int[] inPixels = new int[width*height];
        float[] outPixels = new float[width*height];
        getRGB( dest, 0, 0, width, height, inPixels );
        int index = 0;
        for(int row=0; row<height; row++) {
          int tr = 0;
          for(int col=0; col<width; col++) {
            index = row * width + col;
                tr = (inPixels[index] >> 16) & 0xff;
                if(tr == backgroundColor)
                  outPixels[index] = (float)INF;
                else
                  outPixels[index] = 0;                 
          }
        }
        
        // transform along columns
        float[] f = new float[Math.max(width, height)];
        for(int col=0; col<width; col++) {
          for(int row=0; row<height; row++) {
            index = row * width + col;
                f[row] = outPixels[index];
          }
          float[] disColumns = distance1DTransform(f, height);          
          for(int row=0; row<height; row++) {
            index = row * width + col;
            outPixels[index] = disColumns[row];
          }
        }
        
        // transform along rows
        for (int row = 0; row < height; row++) {
          for (int col = 0; col < width; col++) {
          index = row * width + col;
            f[col] = outPixels[index];
          }
          float[] disColumns = distance1DTransform(f, width);      
          for (int col = 0; col < width; col++) {
          index = row * width + col;
          outPixels[index] = disColumns[col];
          }
        }
        
        // post sqrt calculation
        int[] result = new int[width*height];
        for(int row=0; row<height; row++) {
          for(int col=0; col<width; col++) {
            index = row * width + col;
            int pc = clamp(Math.sqrt(outPixels[index]));
            result[index] = (255 << 24) | (pc << 16) | (pc << 8) | pc;
          }
        }
        setRGB( dest, 0, 0, width, height, result );
        return dest;
  }
  
  public static int clamp(double c)
  {
    return c > 255 ? 255 : (c < 0 ? 0 : (int)c);
  }
  
  /**
   * 1D distance transform using squared distance
   * 
   * @param data
   * @param n
   * @return
   */
  private float[] distance1DTransform(float[] f, int n)
  {
      float[] d = new float[n];
      int[] v = new int[n];
      double[] z = new double[n+1];
      int k = 0;
      v[0] = 0;
      z[0] = -INF;
      z[1] = +INF;
      for (int q = 1; q <= n-1; q++) {
        double s  = ((f[q]+square(q))-(f[v[k]]+square(v[k])))/(2*q-2*v[k]);
        while (s <= z[k]) {
          k--;
          s  = ((f[q]+square(q))-(f[v[k]]+square(v[k])))/(2*q-2*v[k]);
        }
        k++;
        v[k] = q;
        z[k] = s;
        z[k+1] = +INF;
      }
 
      k = 0;
      for (int q = 0; q <= n-1; q++) {
        while (z[k+1] < q)
          k++;
        d[q] = (float)square(q-v[k]) + f[v[k]];
      }
      return d;
  }
  
  private double square(double v)
  {
    return v*v;
  }
  
}

相关文章
|
7月前
|
canal 算法 vr&ar
【图像处理】基于电磁学优化算法的多阈值分割算法研究(Matlab代码实现)
【图像处理】基于电磁学优化算法的多阈值分割算法研究(Matlab代码实现)
235 1
|
7月前
|
机器学习/深度学习 算法 PyTorch
【Pytorch框架搭建神经网络】基于DQN算法、优先级采样的DQN算法、DQN + 人工势场的避障控制研究(Python代码实现)
【Pytorch框架搭建神经网络】基于DQN算法、优先级采样的DQN算法、DQN + 人工势场的避障控制研究(Python代码实现)
206 1
|
8月前
|
算法 芯片 Python
使用变动和观察(Perturb and Observe)最大功率点跟踪(MPPT)算法控制升压变换器的MOSFET/IGBT(开关),以从光伏阵列中提取最大功率(Simulink仿真实现)
使用变动和观察(Perturb and Observe)最大功率点跟踪(MPPT)算法控制升压变换器的MOSFET/IGBT(开关),以从光伏阵列中提取最大功率(Simulink仿真实现)
370 1
|
7月前
|
机器学习/深度学习 算法 PyTorch
【DQN实现避障控制】使用Pytorch框架搭建神经网络,基于DQN算法、优先级采样的DQN算法、DQN + 人工势场实现避障控制研究(Matlab、Python实现)
【DQN实现避障控制】使用Pytorch框架搭建神经网络,基于DQN算法、优先级采样的DQN算法、DQN + 人工势场实现避障控制研究(Matlab、Python实现)
328 0
|
算法 数据安全/隐私保护 计算机视觉
基于sift变换的农田杂草匹配定位算法matlab仿真
本项目基于SIFT算法实现农田杂草精准识别与定位,运行环境为Matlab2022a。完整程序无水印,提供详细中文注释及操作视频。核心步骤包括尺度空间极值检测、关键点定位、方向分配和特征描述符生成。该算法通过特征匹配实现杂草定位,适用于现代农业中的自动化防控。
|
编解码 算法 数据安全/隐私保护
一维信号的小波变换与重构算法matlab仿真
本程序使用MATLAB2022A实现一维信号的小波变换与重构,对正弦测试信号进行小波分解和重构,并计算重构信号与原信号的误差。核心步骤包括:绘制分解系数图像、上抽取与滤波重构、对比原始与重构信号及误差分析。小波变换通过多分辨率分析捕捉信号的局部特征,适用于非平稳信号处理,在信号去噪、压缩等领域有广泛应用。
|
机器学习/深度学习 算法
基于BP神经网络和小波变换特征提取的烟草香型分类算法matlab仿真,分为浓香型,清香型和中间香型
```markdown 探索烟草香型分类:使用Matlab2022a中的BP神经网络结合小波变换。小波分析揭示香气成分的局部特征,降低维度,PCA等用于特征选择。BP网络随后处理这些特征,以区分浓香、清香和中间香型。 ```
|
算法
基于小波变换的图像自适应增强算法
基于小波变换的图像自适应增强算法
215 0
|
6月前
|
机器学习/深度学习 算法 机器人
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
629 0
|
6月前
|
数据采集 分布式计算 并行计算
mRMR算法实现特征选择-MATLAB
mRMR算法实现特征选择-MATLAB
410 2