USM锐化算法

USM锐化算法

1、算法原理

①假设原图为f(i,j)以半径r计算高斯模糊图g(i,j);
②计算像素位置(i,j),计算dif(i,j) = f(i,j)-g(i,j) (保留高频部分)
③根据dif(i,j)计算掩膜: m a s k ( i , j ) = { 255 , abs(dif)>threshold 0 , otherwise mask(i,j)=\begin{cases} 255, & \text{abs(dif)>threshold}\\ 0,& \text{otherwise} \end{cases} mask(i,j)={255,0,abs(dif)>thresholdotherwise

④以r为半径对mask进行高斯滤波,得到alpha
⑤锐化效果图为usm:
K = f ( i , j ) + a m o u n t ∗ d i f ( i , j ) u s m ( i , j ) = K ∗ a l p h a ( i , j ) + f ( i , j ) ∗ ( 255 − a l p h a ( i , j ) ) 255 K = f(i,j) + amount * dif(i,j)\\ usm(i,j) = \frac{K*alpha(i,j)+f(i,j)*(255-alpha(i,j))}{255} K=f(i,j)+amountdif(i,j)usm(i,j)=255Kalpha(i,j)+f(i,j)(255alpha(i,j))

2、参考代码

int f_USM(unsigned char* srcData,int width, int height,int stride,int radius, int amount, int threshold)
{
	int ret = 0;
    if(radius == 0)
		return ret;
	radius = CLIP3(radius, 0, 100);
	amount = CLIP3(amount, 0,500);
	threshold = CLIP3(threshold, 0,255);
	unsigned char* gaussData = (unsigned char*)malloc(sizeof(unsigned char) * height * stride);
	memcpy(gaussData, srcData, sizeof(unsigned char) * height * stride);
	f_FastGaussFilter(gaussData, width, height, stride, radius);
	int i, j, r, g, b, offset;
	offset = stride - width * 3;
	amount = amount * 128 / 100;
	unsigned char* pSrc = srcData;
	unsigned char* pDst = gaussData;
	unsigned char* maskData = (unsigned char*)malloc(sizeof(unsigned char) * height * stride);
	unsigned char* pMask = maskData;
	for(j = 0; j < height; j++)
	{
		for(i = 0; i < width; i++)
		{
			pMask[0] = abs(pSrc[0] - pDst[0]) < threshold ? 0 : 128;
			pMask[1] = abs(pSrc[1] - pDst[1]) < threshold ? 0 : 128;
			pMask[2] = abs(pSrc[2] - pDst[2]) < threshold ? 0 : 128;
			pDst += 3;
			pSrc += 3;
			pMask += 3;
		}
		pDst += offset;
		pSrc += offset;
		pMask += offset;
	}
	pDst = gaussData;
	pSrc = srcData;
	pMask = maskData;
	f_FastGaussFilter(maskData, width, height, stride, radius);
	for(j = 0; j < height; j++)
	{
		for(i = 0; i < width; i++)
		{
			b = pSrc[0] - pDst[0];
			g = pSrc[1] - pDst[1];
			r = pSrc[2] - pDst[2];
 
			b = (pSrc[0] + ((b * amount) >> 7));
			g = (pSrc[1] + ((g * amount) >> 7));
			r = (pSrc[2] + ((r * amount) >> 7));
			
			b = (b * pMask[0] + pSrc[0] * (128 - pMask[0])) >> 7;
			g = (g * pMask[1] + pSrc[1] * (128 - pMask[1])) >> 7;
			r = (r * pMask[2] + pSrc[2] * (128 - pMask[2])) >> 7;
 
			pSrc[0] = CLIP3(b, 0, 255);
			pSrc[1] = CLIP3(g, 0, 255);
			pSrc[2] = CLIP3(r, 0, 255);
			pSrc += 3;
			pDst += 3;
			pMask += 3;
		}
		pSrc += offset;
		pDst += offset;
		pMask += offset;
	}
	free(gaussData);
	free(maskData);
	return ret;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听风者868

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值