图像处理之基于阈值模糊

简介: 图像处理之基于阈值模糊 算法思想: 实现一个高斯卷积模糊但是只运用与周围的像素值与中心像素值差值小于阈值。两个 像素值之间的距离计算可以选用向量距离即曼哈顿距离或者欧几里德距离。高斯模糊 采用先XY方向一维高斯模糊完成目的是为了减小计算量。

图像处理之基于阈值模糊

算法思想:

实现一个高斯卷积模糊但是只运用与周围的像素值与中心像素值差值小于阈值。两个

像素值之间的距离计算可以选用向量距离即曼哈顿距离或者欧几里德距离。高斯模糊

采用先XY方向一维高斯模糊完成目的是为了减小计算量。

程序效果:


关键代码解释:

分别完成XY方向的一维高斯模糊

thresholdBlur( kernel, inPixels, outPixels, width, height, true );
thresholdBlur( kernel, outPixels, inPixels, height, width, true );
计算像素距离,完成像素高斯卷积代码如下:

int d;
if(euclid) {
	d = (int)Math.sqrt(a1*a1-a2*a2);
} else {
	d = a1-a2;
}
if ( d >= -threshold && d <= threshold ) {
    a += f * a2;
    af += f;
}
if(euclid) {
	d = (int)Math.sqrt(r1*r1-r2*r2);
} else {
	d = r1-r2;
}
if ( d >= -threshold && d <= threshold ) {
    r += f * r2;
    rf += f;
}
if(euclid) {
	d = (int)Math.sqrt(g1*g1-g2*g2);
} else {
	d = g1-g2;
}
if ( d >= -threshold && d <= threshold ) {
    g += f * g2;
    gf += f;
}
if(euclid) {
	d = (int)Math.sqrt(b1*b1-b2*b2);
} else {
	d = b1-b2;
}
if ( d >= -threshold && d <= threshold ) {
    b += f * b2;
    bf += f;
}
滤镜完整代码如下:

package com.gloomyfish.filter.study;

import java.awt.image.BufferedImage;

public class SmartBlurFilter extends AbstractBufferedImageOp {

	private int hRadius = 5;
	private int threshold = 50;
	private boolean euclid = false;
	
    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];
        int[] outPixels = new int[width*height];
        getRGB( src, 0, 0, width, height, inPixels );

        // generate the Gaussian kernel data
        float[] kernel = makeKernel(hRadius);
        
        // do Gaussian X and Y direction with kernel data.
        // this way will proceed quickly
		thresholdBlur( kernel, inPixels, outPixels, width, height, true );
		thresholdBlur( kernel, outPixels, inPixels, height, width, true );

		// set back result data to destination image
        setRGB( dest, 0, 0, width, height, inPixels );
        return dest;
    }
    
	/**
	 * Convolve with a Gaussian matrix consisting of one row float data
	 */
	public void thresholdBlur(float[] matrix, int[] inPixels, int[] outPixels, int width, int height, boolean alpha) {
		int cols = matrix.length;
		int cols2 = cols/2;

		for (int y = 0; y < height; y++) {
			int ioffset = y*width; // index to correct row here!!
            int outIndex = y;
			for (int x = 0; x < width; x++) {
				float r = 0, g = 0, b = 0, a = 0;
				int moffset = cols2;

                int rgb1 = inPixels[ioffset+x];
                int a1 = (rgb1 >> 24) & 0xff;
                int r1 = (rgb1 >> 16) & 0xff;
                int g1 = (rgb1 >> 8) & 0xff;
                int b1 = rgb1 & 0xff;
				float af = 0, rf = 0, gf = 0, bf = 0;
                for (int col = -cols2; col <= cols2; col++) {
					float f = matrix[moffset+col];

					if (f != 0) {
						int ix = x+col;
						if (!(0 <= ix && ix < width))
							ix = x;
						int rgb2 = inPixels[ioffset+ix];
                        int a2 = (rgb2 >> 24) & 0xff;
                        int r2 = (rgb2 >> 16) & 0xff;
                        int g2 = (rgb2 >> 8) & 0xff;
                        int b2 = rgb2 & 0xff;

						int d;
						if(euclid) {
							d = (int)Math.sqrt(a1*a1-a2*a2);
						} else {
							d = a1-a2;
						}
                        if ( d >= -threshold && d <= threshold ) {
                            a += f * a2;
                            af += f;
                        }
                        if(euclid) {
							d = (int)Math.sqrt(r1*r1-r2*r2);
						} else {
							d = r1-r2;
						}
                        if ( d >= -threshold && d <= threshold ) {
                            r += f * r2;
                            rf += f;
                        }
                        if(euclid) {
							d = (int)Math.sqrt(g1*g1-g2*g2);
						} else {
							d = g1-g2;
						}
                        if ( d >= -threshold && d <= threshold ) {
                            g += f * g2;
                            gf += f;
                        }
                        if(euclid) {
							d = (int)Math.sqrt(b1*b1-b2*b2);
						} else {
							d = b1-b2;
						}
                        if ( d >= -threshold && d <= threshold ) {
                            b += f * b2;
                            bf += f;
                        }
					}
				}
                // normalization process here
                a = af == 0 ? a1 : a/af; 
                r = rf == 0 ? r1 : r/rf;
                g = gf == 0 ? g1 : g/gf;
                b = bf == 0 ? b1 : b/bf;
                
                // return result pixel data
				int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
				int ir = PixelUtils.clamp((int)(r+0.5));
				int ig = PixelUtils.clamp((int)(g+0.5));
				int ib = PixelUtils.clamp((int)(b+0.5));
				outPixels[outIndex] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
                outIndex += height;
			}
		}
	}

	public void setHRadius(int hRadius) {
		this.hRadius = hRadius;
	}
	
	public void setThreshold(int th) {
		this.threshold = th;
	}
	
    public void setEuclid(boolean apply) {
    	this.euclid = apply;
    }

}




目录
相关文章
|
2天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
13天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1286 5
|
12天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1318 87
|
1天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
175 82
2025年阿里云域名备案流程(新手图文详细流程)
|
1天前
|
自然语言处理 前端开发
基于Electron38+Vite7.1+Vue3+Pinia3+ElementPlus电脑端admin后台管理模板
基于最新版跨平台框架Electron38整合Vite7+Vue3+ElementPlus搭建轻量级客户端中后台管理系统解决方案。
152 86