数字图像算法研究---PS USM锐化算法详解

图像USM锐化是图像处理软件中常见的功能，而PHOTOSHOP中的USM锐化相对而言效果较好，网上有很多揭秘PhotoShop USM锐化算法的文章，但是，算法效果与PS相比，差距较大，今天本人针对PS的USM锐化，给出一个效果比较接近的算法，跟大家分享一下，有助于大家走出误区。

int f_USM(unsigned char* srcData,int width, int height,int stride,int radius, int amount, int threshold)
{
int ret = 0;
return ret;
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);
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);
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;
}
pDst += offset;
pSrc += offset;
}
pDst = gaussData;
pSrc = srcData;
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;
}
pSrc += offset;
pDst += offset;
}
free(gaussData);
return ret;
};