# Gaussian Kernel with arbitrary size m x n

Is it possible to generate a Gaussian kernel with an arbitrary size m x n? How can I do that? I've looked into getGaussianKernel, however, it returns only squared kernels. In case I need to implement it by hand, any suggestions are welcome.

edit retag close merge delete

Sort by ยป oldest newest most voted

One may take advantage of kernel separatibility to generate such Gaussian Kernel.

// mu: mean, sigma: standard deviation
float gaussianDistribution(float x, float mu, float sigma) {
return std::exp( -(((x-mu)/(sigma))*((x-mu)/(sigma)))/2.0 ) / (cv::sqrt(2 * CV_PI) * sigma);
}

cv::Mat_<float> getGaussianMask(int nrows, int ncols) {

// compute a bidimensional gaussian
for (int x = 0; x < gaussianMask.cols; ++x) {
for (int y = 0; y < gaussianMask.rows; ++y) {
// taking advantage of the gaussian's kernel separatibility
}
}

// normalizing
cv::Scalar totalsum;
}


Resulting image for nrows = 128 and ncols = 64.

You may also vary the size of the standard deviation sigma to produce Gaussians with smaller or bigger radius. Here, I'm using gaussianMask.rows/4 and gaussianMask.cols/4. The mean mu determines the center of the Gaussian.

// ...


I used cv::applyColorMap(gaussianMask, gaussianMask, cv::COLORMAP_JET); to get this coloring.

more

Official site

GitHub

Wiki

Documentation