opencv学习笔记—入门(27)之高斯核单、双维的构造函数

CvMat* GaussianKernel2D(int dim, float sigma)   
{
	CvMat* mat=cvCreateMat(dim, dim, CV_32FC1);
	CvMat* mat1=cvCreateMat(dim, dim, CV_32FC1);  
	CvMat* mat2=cvCreateMat(dim, dim, CV_32FC1);  
	#define Mat2(ROW,COL) ((float *)(mat2->data.fl + mat2->step/sizeof(float) *(ROW)))[(COL)]  
	#define Mat1(ROW,COL) ((float *)(mat1->data.fl + mat1->step/sizeof(float) *(ROW)))[(COL)] 
	#define Mat(ROW,COL) ((float *)(mat->data.fl + mat->step/sizeof(float) *(ROW)))[(COL)] 
	float s2 = sigma * sigma; 
	float m= 1.0/(sqrt(2.0 * CV_PI) * sigma); 
	if ((dim % 2) != 0)   //dim is a  odd
	{
		int c = dim / 2;  
		for (int i = 0; i < dim; i++)   
		{  
			 for (int j = 0; j < dim; j++)   
			 {  
	     		printf("%d %d %d/n", c, i, j);  
	     		float v = m * exp(-(1.0*i*i + 1.0*j*j) / (2.0 * s2));  
				//float v = exp(-(1.0*i*i + 1.0*j*j) / s2); 
			    Mat(c+i,c+j) =v;  
			    Mat(c-i,c+j) =v;  
		        Mat(c+i,c-j) =v;  
			    Mat(c-i,c-j) =v;  
			 }  
		}  
	}
	else   //dim is a even
	{
		for(int i = 0; i < dim; i++)
		{
			for(int j = 0; j < dim; j++)
			{
				Mat1(i, j) = i;
				Mat2(j, i) = i;
			}
		}
		for(int i = 0; i < dim; i++)
		{
			for(int j = 0; j < dim; j++)
			{	
				Mat(i,j) =m * exp(-(((Mat1(i,j) - (float)(dim-1)/2)*(Mat1(i,j) - (float)(dim-1)/2) + (Mat2(i,j) \
					      - (float)(dim-1)/2)*(Mat2(i,j) - (float)(dim-1)/2)) / (2.0 * s2)));	
			}
		}
	}
	// normalizeMat(mat);  
	return mat;  
}  

你可能感兴趣的:(c,float)