Non-cyclic smoothing of a 2D image I was wondering if there was a simple approach to smooth images, e.g using a Gaussian Kernel without introducing boundary effects which are also present when using Discrete Fourier Transforms?
For both approaches, the missing pixels at the left-hand side will be taken from the right-hand side and vice versa.

Right now I have a 5x5 Gaussian Kernel which I move iteratively over my MxN image. Up to now, in case I reach the last pixel of the image, the operator automatically uses  the first pixel and so on. Since my picture is not periodic I get artifacts even if I pad my picture on all sides with the intensity values of the corresponding adjacent column or row.

Do you gus have any suggestions?

Thanks!
Pad image and smooth:




Make image of value 1 same size as original. Pad and smooth:




Divide original image by '1' image:



And crop:



MATLAB demo code:

I = double(imread('lena.tif'));
g = fspecial('gaussian',60,10);
Ipad = padarray(I,[10,10]);
imagesc(Ipad); colormap gray(256); pause;
Ipadg = imfilter(Ipad,g);
imagesc(Ipadg); colormap gray(256); pause;
A = ones(size(I));
Apad = padarray(A,[10,10]);
imagesc(Apad); colormap gray(256); pause;
Apadg = imfilter(Apad,g);
imagesc(Apadg); colormap gray(256); pause;
IA = Ipadg./Apadg;
imagesc(IA); colormap gray(256); pause;
I = IA(11:end-10,11:end-10);
imagesc(I); colormap gray(256); pause;


*Note: * MATLAB imfilter already does zero padding. The pad array step is not needed, it is just for demonstration. Actual code would be:

I = double(imread('lena.tif'));
g = fspecial('gaussian',60,10);
Ig = imfilter(I,g);
A = ones(size(I));
Ag = imfilter(A,g);;
Ismooth = Ig./Ag;