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

简介: 图像处理之基于采样距离变换算法 算法是别人提出来的,感兴趣可以搜索《Distance Transforms of Sampled Functions》 这篇论文,网上也有很多实现的代码,但是结构不是很好,而且很分散不是一个完整的 算法。

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

算法是别人提出来的,感兴趣可以搜索《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;
	}
	
}
2013年的最后一篇!谢谢一直关注的各位!
目录
相关文章
|
3月前
|
算法 计算机视觉
图像处理之积分图应用四(基于局部均值的图像二值化算法)
图像处理之积分图应用四(基于局部均值的图像二值化算法)
514 0
|
3月前
|
监控 算法 图计算
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
59 0
|
1月前
|
算法
基于小波变换的图像自适应增强算法
基于小波变换的图像自适应增强算法
11 0
|
3月前
|
机器学习/深度学习 算法
基于BP神经网络和小波变换特征提取的烟草香型分类算法matlab仿真,分为浓香型,清香型和中间香型
```markdown 探索烟草香型分类:使用Matlab2022a中的BP神经网络结合小波变换。小波分析揭示香气成分的局部特征,降低维度,PCA等用于特征选择。BP网络随后处理这些特征,以区分浓香、清香和中间香型。 ```
|
3月前
|
算法 Java 计算机视觉
图像处理之积分图算法
图像处理之积分图算法
38 2
|
3月前
|
资源调度 算法 计算机视觉
图像处理之积分图应用二(快速边缘保留滤波算法)
图像处理之积分图应用二(快速边缘保留滤波算法)
26 0
|
3月前
|
算法 BI 计算机视觉
图像处理之积分图应用一(半径无关的快速模糊算法)
图像处理之积分图应用一(半径无关的快速模糊算法)
28 0
|
16天前
|
算法 BI Serverless
基于鱼群算法的散热片形状优化matlab仿真
本研究利用浴盆曲线模拟空隙外形,并通过鱼群算法(FSA)优化浴盆曲线参数,以获得最佳孔隙度值及对应的R值。FSA通过模拟鱼群的聚群、避障和觅食行为,实现高效全局搜索。具体步骤包括初始化鱼群、计算适应度值、更新位置及判断终止条件。最终确定散热片的最佳形状参数。仿真结果显示该方法能显著提高优化效率。相关代码使用MATLAB 2022a实现。
|
16天前
|
算法 数据可视化
基于SSA奇异谱分析算法的时间序列趋势线提取matlab仿真
奇异谱分析(SSA)是一种基于奇异值分解(SVD)和轨迹矩阵的非线性、非参数时间序列分析方法,适用于提取趋势、周期性和噪声成分。本项目使用MATLAB 2022a版本实现从强干扰序列中提取趋势线,并通过可视化展示了原时间序列与提取的趋势分量。代码实现了滑动窗口下的奇异值分解和分组重构,适用于非线性和非平稳时间序列分析。此方法在气候变化、金融市场和生物医学信号处理等领域有广泛应用。
|
17天前
|
资源调度 算法
基于迭代扩展卡尔曼滤波算法的倒立摆控制系统matlab仿真
本课题研究基于迭代扩展卡尔曼滤波算法的倒立摆控制系统,并对比UKF、EKF、迭代UKF和迭代EKF的控制效果。倒立摆作为典型的非线性系统,适用于评估不同滤波方法的性能。UKF采用无迹变换逼近非线性函数,避免了EKF中的截断误差;EKF则通过泰勒级数展开近似非线性函数;迭代EKF和迭代UKF通过多次迭代提高状态估计精度。系统使用MATLAB 2022a进行仿真和分析,结果显示UKF和迭代UKF在非线性强的系统中表现更佳,但计算复杂度较高;EKF和迭代EKF则更适合维数较高或计算受限的场景。