图像锐化算法

对3通道8位图像锐化:

A.每个通道的9宫格内的像素值进行Laplacian锐化,代码如下:

void hello::LinearSharpen(unsigned char* lpImage, int nWidth, int nHeight, unsigned char* SharpenImage)
{
 // 遍历图象的纵坐标
	int y;

 // 遍历图象的横坐标
	int x;

	double * pdGrad ;
 
 // 设置模板系数,定义a方便参数改动
	double a;
	a=0.3;
	static double nWeight[3][3] = {{-a,-a,-a},{-a,8*a,-a},{-a,-a,-a}};

 
 //这些变量用来表示Laplacian算子象素值
	double rTmp[3][3];
	double gTmp[3][3];
	double bTmp[3][3];
 
 // 临时变量
	 double rGrad,gGrad,bGrad;
 
 // 模板循环控制变量
	int yy ;
	int xx ;
 
	pdGrad = (double *)malloc(3*nWidth*nHeight*sizeof(double));
 
 // 初始化为0
	memset(pdGrad, 0, 3*nWidth*nHeight*sizeof(double));
 
	for(y=1; y 255)
				temp1 = 255;
			else if(temp1 < 0)
				temp1 = 0;
			if(temp2 > 255)
				temp2 = 255;
			else if(temp2 < 0)
				temp2 = 0;
			if(temp3 > 255)
				temp3 = 255;
			else if(temp3 < 0)
				temp3 = 0;

			SharpenImage[y*nWidth*3+x  ] = (unsigned char)temp1;
			SharpenImage[y*nWidth*3+x+1] = (unsigned char)temp2;
			SharpenImage[y*nWidth*3+x+2] = (unsigned char)temp3;
 
		}
	}
	free(pdGrad) ;
	pdGrad = NULL   ;
}

B.对每一点的3通道像素值进行平均,并对平均的值进行卷积,然后锐化:

void hello::LinearSharpen_Avg(unsigned char* lpImage, int nWidth, int nHeight, unsigned char* SharpenImage)
{
 // 遍历图象的纵坐标
	int y;
 
 // 遍历图象的横坐标
	int x;

	double * pdGrad ;
 
 // 设置模板系数
	double a;
	a=0.2;
	static double nWeight[3][3] = {{-a,-a,-a},{-a,8*a,-a},{-a,-a,-a}};
 
 //这个变量用来表示Laplacian算子象素值
	double rTmp[3][3];

 
 // 临时变量
	 double rGrad;//,gGrad,bGrad;
 
 // 模板循环控制变量
	int yy ;
	int xx ;
 
	pdGrad = (double *)malloc(3*nWidth*nHeight*sizeof(double));
 
 // 初始化为0
	memset(pdGrad, 0, 3*nWidth*nHeight*sizeof(double));
 
	for(y=1; y 255)
				temp1 = 255;
			else if(temp1 < 0)
				temp1 = 0;
			if(temp2 > 255)
				temp2 = 255;
			else if(temp2 < 0)
				temp2 = 0;
			if(temp3 > 255)
				temp3 = 255;
			else if(temp3 < 0)
				temp3 = 0;

			SharpenImage[y*nWidth*3+x] = (unsigned char)temp1;
			SharpenImage[y*nWidth*3+x+1] = (unsigned char)temp2;
			SharpenImage[y*nWidth*3+x+2] = (unsigned char)temp3;
 
		}
	}
	free(pdGrad) ;
	pdGrad = NULL   ;
}

Main函数如下:

void hello::LinearMain(){
	cout<<"please input the path of picture;"<depth,3);
	unsigned char* temp_IpImage=&((unsigned char*)IpImage->imageData)[0];
	unsigned char* temp_out=&((unsigned char*)out->imageData)[0];
	hello::LinearSharpen(temp_IpImage,cvGetSize(IpImage).width,cvGetSize(IpImage).height,temp_out);

	cvShowImage("Linear-in",IpImage);
	cvShowImage("Linear-out",out);

	cvWaitKey(0);
	cvReleaseImage(&IpImage);
	cvReleaseImage(&out);
}


备注:这是把卷积算法简单实现了下,但没有考虑边缘像素点,opencv中cvFilter2D() 和 cvCopyMakeBorder() 两个函数可以实现卷积及扩大边界。



你可能感兴趣的:(IMAGE,算法,null,string,input,path)