opencv生成高斯核

[cpp] view plain copy print ?
  1. Matlab:  
  2. K = fspecial('gaussian',m,sigma);  

Mat gaussian_kernal(int dim,int sigma)
{
	int c = dim / 2;
	Mat K(dim, dim, CV_32FC1);
	//生成二维高斯核
	float s2 = 2.0 * sigma * sigma;
	for(int i = (-c); i <= c; i++)
	{
		int m = i + c;
		for (int j = (-c); j <= c; j++)
		{
			int n = j + c;
			float v = exp(-(1.0*i*i + 1.0*j*j) / s2);
			K.ptr<float>(m)[n] = v;
		}
	}
	Scalar all = sum(K);
	Mat gaussK;
	K.convertTo(gaussK, CV_32FC1, (1/all[0]));
	all = sum(gaussK);
	return gaussK;
}

你可能感兴趣的:(opencv生成高斯核)