1. 程式人生 > >高斯濾波——影象處理

高斯濾波——影象處理

</pre><pre name="code" class="cpp">void GaussianFilter(unsigned char* data, int width, int height)
{
	int				i,j,index,sum;
	unsigned char	*tmpdata;
	int		templates[9]={1,2,1,
		2,4,2,
		1,2,1};
	tmpdata = (unsigned char*)malloc(sizeof(unsigned char) * width * height);
	memcpy(tmpdata, data, sizeof(unsigned char) * width * height);
	for (i=1; i<height-1; i++)
	{
		for (j=1; j<width-1; j++)
		{
			index = 0;
			sum = 0;
			for (int m=i-1; m<i+2; m++)
			{
				for (int n=j-1; n<j+2; n++)
				{
					sum += tmpdata[m*width+n]*templates[index++];
				}
			}
			data[i*width+j] = sum/16;
		}
	}
	free(tmpdata);

}