图像处理之倒角距离变换

简介: 图像处理之倒角距离变换图像处理中的倒角距离变换(Chamfer Distance Transform)在对象匹配识别中经常用到,算法基本上是基于3x3的窗口来生成每个像素的距离值,分为两步完成距离变换,第一步从左上角开始,从左向右、从上到下移动窗口扫描每个像素,检测在中心像素x的周围0、1、2、3四个像素,保存最小距离与位置作为结果,图示如下:第二步从底向上、从右向左,对每个像素,检测相邻像素4、5、6、7保存最小距离与位置作为结果,如图示所:完成这两步以后,得到的结果输出即为倒角距离变换的结果。

图像处理之倒角距离变换

图像处理中的倒角距离变换(Chamfer Distance Transform)在对象匹配识别中经常用到,

算法基本上是基于3x3的窗口来生成每个像素的距离值,分为两步完成距离变换,第一

步从左上角开始,从左向右、从上到下移动窗口扫描每个像素,检测在中心像素x的周

围0、1、2、3四个像素,保存最小距离与位置作为结果,图示如下:


第二步从底向上、从右向左,对每个像素,检测相邻像素4、5、6、7保存最小距离与

位置作为结果,如图示所:


完成这两步以后,得到的结果输出即为倒角距离变换的结果。完整的图像倒角距离变

换代码实现可以分为如下几步:

1.      对像素数组进行初始化,所有背景颜色像素点初始距离为无穷大,前景像素点距

  离为0

2.      开始倒角距离变换中的第一步,并保存结果

3.      基于第一步结果完成倒角距离变换中的第二步

4.      根据距离变换结果显示所有不同灰度值,形成图像

最终结果显示如下(左边表示原图、右边表示CDT之后结果)


完整的二值图像倒角距离变换的源代码如下:

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);
		// 随机生成距离变换点
		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);
	}

}
转载请注明出处!!
目录
相关文章
|
算法 计算机视觉
图像处理之距离变换
图像处理之距离变换
351 8
|
芯片 SoC
FinFET工作原理、结构和应用特性介绍
FinFET的全称是Fin Field-Effect Transistor。它是一种新型互补金属氧化物半导体晶体管。FinFET 的名称是基于晶体管和鳍片形状的相似性。
14252 0
FinFET工作原理、结构和应用特性介绍
|
3月前
|
算法 搜索推荐 API
API让电商“活”起来:动态定价策略的革新力量
在电商竞争中,动态定价策略通过API实时调整价格,响应市场变化,提升利润与竞争力。本文解析其原理、技术实现与应用,探讨API如何重塑电商生态。
158 1
|
应用服务中间件 nginx
安装nginx-rtmp-module模块与配置
安装nginx-rtmp-module模块与配置
|
算法 API 计算机视觉
图像处理之角点检测与亚像素角点定位
图像处理之角点检测与亚像素角点定位
223 1
|
数据安全/隐私保护 智能硬件
智能家居系统入门指南
随着科技的飞速发展,智能家居系统已不再是遥不可及的梦想。本文将带你走进智能生活的世界,从基础概念到实用设备,再到搭建步骤和常见问题解答,全方位解析如何打造一个舒适、便捷、高效的智能居家环境。让我们一起探索,如何通过简单的操作,实现家居生活的智能化升级。
|
大数据 UED 开发者
实战演练:利用Python的Trie树优化搜索算法,性能飙升不是梦!
【7月更文挑战第19天】Trie树,又称前缀树,是优化字符串搜索的高效数据结构。通过利用公共前缀,Trie树能快速插入、删除和查找字符串。
291 2
|
Prometheus 监控 Cloud Native
Gin 集成 prometheus 客户端实现注册和暴露指标
Gin 集成 prometheus 客户端实现注册和暴露指标
421 0
|
存储 开发工具 git
我为什么选择 Wiki.js 记笔记?
我为什么选择 Wiki.js 记笔记?