当前位置: 首页 > news >正文

图像处理之USM锐化

一:基本原理

图像卷积处理实现锐化有一种常用的算法叫做Unsharpen Mask方法,这种锐化的方法就是对原图像先做一个高斯模糊,然后用原来的图像减去一个系数乘以高斯模糊之后的图像,然后再把值Scale到0~255的RGB像素值范围之内。基于USM锐化的方法可以去除一些细小的干扰细节和噪声,比一般直接使用卷积锐化算子得到的图像锐化结果更加真实可信。USM锐化公式表示如下:

(源图像– w*高斯模糊)/(1-w);其中w表示权重(0.1~0.9),默认为0.6

 

二:实现步骤

1.      读入图像的像素数据

2.      对图像像素数据实现高斯模糊,

3.      根据输入参数w,对图像上的每个像素点,使用USM锐化公式计算每个像素点锐化之后的像素

4.      构建一张新的输出图像,返回显示

 

三:运行效果


四:代码实现

高斯模糊代码如下:

int width = image.getWidth();
		int height = image.getHeight();
		// generateKerneal();
		generateKerneal2D();
		int radius = (int)(this.sigma*2);
		// 二维高斯模糊
		int[] pixels = new int[width*height];
		int[] outPixels = new int[width*height];
		getRGB(image, 0, 0 , width, height, pixels);
		int r=0, g=0, b=0;
		int r1=0, g1=0, b1=0;
		for(int row=0; row<height; row++) {
			int offset = row*width;
			for(int col=1; col<width-1; col++) {
				double sr=0, sg=0, sb=0; 
				// 二维高斯窗口
				for(int i=-radius; i<=radius; i++) {
					int roffset = row + i;
					if(roffset < 0) {
						roffset = 0;
					}
					if(roffset >= height) {
						roffset = height-1;
					}
					int offset1 = roffset*width;
					for(int j=-radius; j<=radius; j++) {
						int coffset = j+col;
						if(coffset < 0 ) {
							coffset = 0;
						}
						if(coffset >= width) {
							coffset = width-1;
						}
						r1 = (pixels[offset1+coffset]>>16)&0xff;
						g1 = (pixels[offset1+coffset]>>8)&0xff;
						b1 = (pixels[offset1+coffset]&0xff);
						sr += kernel2D[i+radius][j+radius]*r1;
						sg += kernel2D[i+radius][j+radius]*g1;
						sb += kernel2D[i+radius][j+radius]*b1;
						
					}
				}
				r = (int)sr;
				g = (int)sg;
				b = (int)sb;
				outPixels[offset+col]=(0xff<<24) | (r<<16) | (g << 8) | b;
			}
		}

基于高斯模糊的代码是USM锐化的代码如下:

		int width = image.getWidth();
		int height = image.getHeight();

		int[] pixels1 = new int[width*height];
		int[] pixels2 = new int[width*height];
		getRGB(image, 0, 0 , width, height, pixels1);
		
		// 高斯模糊
		BufferedImage blurImage = super.process(image);
		getRGB(blurImage, 0, 0 , width, height, pixels2);
		
		// USM 锐化
		int[] output = new int[width*height];
		int r=0, g=0, b=0;
		int r1=0, g1=0, b1=0;
		int r2=0, g2=0, b2=0;
		for(int i=0; i<pixels1.length; i++) {
			r1 = (pixels1[i] >> 16)&0xff;
			g1 = (pixels1[i] >> 8)&0xff;
			b1 = pixels1[i]&0xff;
			
			r2 = (pixels2[i] >> 16)&0xff;
			g2 = (pixels2[i] >> 8)&0xff;
			b2 = pixels2[i]&0xff;
			
			r = (int)((r1-weight*r2)/(1-weight));
			g = (int)((g1-weight*g2)/(1-weight));
			b = (int)((b1-weight*b2)/(1-weight));
			
			output[i]=(0xff<<24) | (clamp(r)<<16) | (clamp(g) << 8) | clamp(b);
		}
		
		BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		setRGB(dest, 0, 0, width, height, output);
		return dest;
其中BufferedImage blurImage = super.process(image);调用的代码为高斯模糊


相关文章:

  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • POJ 3384 Feng Shui
  • Android环境下hanlp汉字转拼音功能的使用介绍
  • 轻松搭建docker应用的mesos集群
  • Android Studio发布Release版本之坑--Unknown host 'd29vzk4ow07wi7.cloudfront.net'
  • Spring—Quartz定时调度CronTrigger时间配置格式说明与实例
  • 一步步教你用 CSS 为 SVG 添加过滤器
  • js学习笔记之日期倒计时(天,时,分,秒)
  • iOS app和Extension数据共享DB时候遇到的坑 NSFileManager共享数据的坑
  • ASP.NET MVC学习之路由篇(2)
  • 用Go语言写Android应用 (2) - 从Android的Java调用Go代码
  • RootMe--HTTP - Open redirect
  • SerializeDeserialize
  • Unity3dShader边缘发光效果
  • 利用python jieba库统计政府工作报告词频
  • download使用浅析
  • gitlab-ci配置详解(一)
  • HTTP中的ETag在移动客户端的应用
  • js作用域和this的理解
  • overflow: hidden IE7无效
  • Redis的resp协议
  • socket.io+express实现聊天室的思考(三)
  • Swoft 源码剖析 - 代码自动更新机制
  • TypeScript迭代器
  • 复杂数据处理
  • 给Prometheus造假数据的方法
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 后端_MYSQL
  • 小程序01:wepy框架整合iview webapp UI
  • #if #elif #endif
  • $.ajax,axios,fetch三种ajax请求的区别
  • (14)Hive调优——合并小文件
  • (3)选择元素——(17)练习(Exercises)
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (python)数据结构---字典
  • (办公)springboot配置aop处理请求.
  • (笔试题)合法字符串
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (三分钟)速览传统边缘检测算子
  • (顺序)容器的好伴侣 --- 容器适配器
  • (四)图像的%2线性拉伸
  • (未解决)macOS matplotlib 中文是方框
  • (转)IOS中获取各种文件的目录路径的方法
  • (转)VC++中ondraw在什么时候调用的
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .NET CORE 第一节 创建基本的 asp.net core
  • .NET Core 2.1路线图
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .net 发送邮件
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .NET面试题(二)
  • .NET中 MVC 工厂模式浅析
  • .NET中的十进制浮点类型,徐汇区网站设计