您的位置:首页 > 其它

拉普拉斯锐化处理

2016-04-22 09:08 260 查看
/**************************************************
* 功能: 设定指定位置的像素灰度
* 参数: imageBuf为目标图像 x,y为要设定像素的坐标
**************************************************/
void SetPixelXY(uchar** imageBuf1, int x, int y, int nc, int a)
{
if (nc == 1)
{
imageBuf1[y][x *nc] = a;
}
else if (nc == 3)
{
imageBuf1[y][x *nc] = a;
imageBuf1[y][x *nc + 1] = a;
imageBuf1[y][x *nc + 2] = a;
}
else if (nc == 4)
{
imageBuf1[y][x * nc] = a;
imageBuf1[y][x * nc + 1] = a;
imageBuf1[y][x * nc + 2] = a;
imageBuf1[y][x * nc + 3] = 255;
}
}

int GetAsh(uchar** imageBuf, int x, int y, int nc)
{
int clr = 0;
if (nc == 1)
{
clr = imageBuf[y][x * nc];
}
else
{
clr = (imageBuf[y][x * nc] + imageBuf[y][x * nc + 1] + imageBuf[y][x * nc + 2]) / 3;
}

return clr;
}

/********************************************************
* 把线形存储的像素转化为二维数组形式
* 参数: image 线形存储的像素, width,height 图象的长宽
********************************************************/
uchar** CreatImage(uchar * image, unsigned int width, unsigned int height, int nc)
{
int imgWidthStep = ((width*nc + 3) / 4) * 4;

uchar** imageBuf = (uchar**)malloc(sizeof(uchar*)*(height));
for (int y = 0; y < height; y++)
{
//使imageBuf中每个指针分别指向其下标表示的行的行首地址
imageBuf[y] = image + y*imgWidthStep;
}
return imageBuf;
}

/**************************************************
* 功能: 使用模板对彩色图邻域进行运算
* 参数: imageBuf为目标图像 w、h为图像大小
*       templt为模板 tw为邻域大小
*		x,y为要取得像素的坐标
*       cn为颜色分量编号 0为蓝色 1为绿色 2为红色
**************************************************/
int TempltExcuteCl(uchar** imageBuf0, int w, int h, int nc, int* templt, int tw, int x, int y, int cn)
{
int i, j;                        //循环变量
int m = 0;                      //用来存放加权和
int px, py;
//依次对邻域中每个像素进行运算
for (i = 0; i < tw; i++)
{
for (j = 0; j < tw; j++)
{
//计算对应模板上位置的像素在原图像中的位置
py = y - tw / 2 + i;
px = x - tw / 2 + j;
//加权求和
m += imageBuf0[py][px * nc + cn] * templt[i*tw + j];
}
}
return m;                     //返回结果
}

/******************************************************************
* 功能: 彩色图像的拉普拉斯锐化处理(scale = 3)
* 参数: image0为原图形,image1锐化结果,
*		w、h为图象的宽和高
******************************************************************/
void SharpLaplacianCl(uchar* image0, uchar* image1, unsigned int w, unsigned int h, int nc, float factor = 1.0)
{
//将图像转化为矩阵形式
uchar** imageBuf0 = CreatImage(image0, w, h, nc);
uchar** imageBuf1 = CreatImage(image1, w, h, nc);
//设定模板
//int templt[9] = { -1, -1, -1, -1, 8, -1, -1, -1, -1 };

int templt[25] = { 0, 0, -1, 0, 0,
0, -1, -2, -1, 0,
-1, -2, 16, -2, -1,
0, -1, -2, -1, 0,
0, 0, -1, 0, 0 };

int x, y, c;
int a;
//设定衰减因子

//依次对原图像的每个像素进行处理
for (y = 2; y < h - 2; y++)
{
for (x = 2; x < w/2  - 2; x++)
{
for (c = 0; c < nc; c++)
{
//利用拉普拉斯模板对邻域进行处理
a = TempltExcuteCl(imageBuf0, w, h, nc, templt, 5, x, y, c);
a = (int)((float)a / factor);
//对中心像素进行增强
a = imageBuf0[y][x * nc + c] + a;
//过限处理
a = a > 255 ? 255 : a;
a = a < 0 ? 0 : a;
imageBuf1[y][x * nc + c] = a;
}
}
}
//清理内存
free(imageBuf0);
free(imageBuf1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: