图像锐化——高斯—拉普拉斯算子

/**************************************************
 * 功能: 设定指定位置的像素灰度
 * 参数: 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);
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小新识图

你的鼓励是我最大的分享动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值