Applying a temporal Gaussian filter to a series of images
I was wondering how I would go about applying a Gaussian filter between two images in MATLAB. I understand for a spatial Gaussian filter on one image, you would do something like the following:
gauss_filter = fspecial('gaussian', [3 3], 1.5); processed = imfilter(image, gauss_filter, 'replicate');
But the algorithm I am trying to implement for optical flow (from the Trucco and Verri text) describes applying a Gaussian filter with sigma = 1.5 frames across the temporal dimension in a sequence of frames.
Is there a way to do this in MATLAB? I think maybe a 3d Gaussian filter would work, but I'm not sure if that is the correct way of doing this or how I would go about creating/applying such a filter.
You can store images in [width x height x frames] array and apply filter across 3rd dimention
img_array(:,:,1) = img1; img_array(:,:,2) = img2; img_array(:,:,3) = img3; %... sigma = 1.5; fsize = 10; x = linspace(-fsize / 2, fsize / 2, fsize); gauss = exp(-x .^ 2 / (2 * sigma ^ 2)); gauss = gaussFilter / sum (gauss); for i=1:width for j=1:height array_filtered(i,j,:) = conv(img_array(i,j,:), gaussFilter, 'same'); end end
As @s.bandara suggested, you can use gauss = normpdf(x,0,sigma) instead of
gauss = exp(-x .^ 2 / (2 * sigma ^ 2)); gauss = gaussFilter / sum (gauss);