Swami
Swami

Reputation: 81

Discrete Approximation to Gaussian smoothing

I am trying to find a discrete approximation to the Gaussian smoothing operation, as shown in the link :

http://bit.ly/1cSgkwt

G is some discrete smoothing kernel, a Gaussian in this case and * is the convolution operation. Basically, I want to apply a smoothing kernel to each pixel in the image. I am doing this in MATLAB and using the following code to create the matrix G, which is naive and hence painfully slow :

z = rgb2gray(imread('train_02463_1.bmp'));
im_sz = size(z);
ksize = 5;

% Gaussian kernel of size ksize*ksize
gw_mat = g_sigma(1,2*ksize+1)'*g_sigma(1,2*ksize+1);

G = sparse(length(ksize+1:im_sz(1)-ksize),prod(im_sz));
for i = ksize+1:im_sz(1)-ksize
    for j = ksize+1:im_sz(2)-ksize

    [x,y] = meshgrid(i-ksize:i+ksize,j-ksize:j+ksize);
    row_num = sub2ind(im_sz,i,j);
    colnums = sub2ind(im_sz,x,y);
    G(row_num,colnums(:)) = gw_mat(:)';

    end
end

Is there a more efficient way to do this ?

EDIT: I should apologize for not complete specifying the problem. Most of the answers below are valid ones, but the issue here is the above approximation is part of a optimization objective where the variable is z. The whole problem looks something like this :

http://goo.gl/rEE02y

Thus, I have to pre-generate a matrix G which approximates a smoothing function in order to feed this objective to a solver. I am using cvx, if that helps.

Upvotes: 0

Views: 2218

Answers (3)

Royi
Royi

Reputation: 4953

The fastest way to apply Spatially Invariant Filter on an image using MATLAB would be using imfilter.
imfilter will automatically notice "Seperable" Kernel and will apply it in 2 steps (Vertically / Horizontally).

For creating some known and useful kernels you may look at fspecial.

Upvotes: 0

deltheil
deltheil

Reputation: 16121

You can use approximate Gaussian smoothing via box filters, e.g see integgaussfilt.m:

This function approximates Gaussian filtering by repeatedly applying averaging filters. The averaging is performed via integral images which results in a fixed and very low computational cost that is independent of the Gaussian size.

Upvotes: 0

Vlad
Vlad

Reputation: 4515

Typically people do it by applying two 1D Gaussian functions sequentially instead of one 2D function (idea of separable filters). You can approximate 1D horizontal Gaussian pretty fast, apply it to each pixel, save result in temp, and then apply a vertical Gaussian to temp. The expected speed up is quite significant: O(ksize*ksize) -> O(ksize+ksize)

Upvotes: 1

Related Questions