# How to convert a low-pass Gaussian filter to a high pass filter

I have the following code for a applying a Gaussian filter to an image. From what I understand this is a low pass filter. Now I have to convert this into a high-pass filter, and from what we were told from the instructions, one difference between Gaussian low and high pass filters is that for a high-pass, the sum of the elements in the filter kernel should sum up to zero as opposed to one like for the-low pass filter. How should I go about doing this?

%%horizontal and vertical coordinates
coordinates = -floor(N/2) : floor(N/2);
[X Y] = meshgrid(coordinates, coordinates);
N = 12;
sigma=2;

gfilter = exp(-(X.^2 + Y.^2)/(2*sigma.^2));
gfilter = gfilter/sum(gfilter(:));

GaussBlur = conv2(I,gfilter);
%%plot images side by side
subplot(1,2,1);
imshow(I, []);
subplot(1,2,2);
imshow(GaussBlur, []);

• I have updated my previous incomplete answer – Laurent Duval Jan 1 '17 at 10:59

The simplest option is to remove the low-pass signal from the data. Just subtract the outcome of your code from the original image. This amounts to computing:

$$I_\textrm{high} = \delta \ast I - g\ast I = (\delta -g) \ast I\,.$$

The term $\delta \ast I$ corresponds to the convolution of a Dirac delta to the image, which is an invariant operation. As the sum of the coefficients of the $\delta$ filter is one, like $g$, the resulting filter $\delta -g$ has a zero sum. Warning though: a filter whose coefficients sum to one is not necessarily an high-pass filter. It is a low-cut, and can be for instance a band-pass.

A more convincing method consists in using the modulation property: you can multiply the coefficients of the low-pass filter pointwise by alternating $1$ and $-1$ (alternating signs are discussed here: why the sum of masks should be one?). The following code does it in 1D:

b = [1 4 6 4 1];b=b/sum(b) ;a=1;
c = b.* (-1).^(1:length(b));
[hb,w] = freqz(b,a,1024);
[hc,w] = freqz(c,a,1024);
figure(1);plot(w,abs([hb,(hc)]));axis tight


The adaptation in 2D follows:

N =13;
%%horizontal and vertical coordinates
coordinates = -floor(N/2) : floor(N/2);
[X Y] = meshgrid(coordinates, coordinates);
sigma=2;

gfilter = exp(-(X.^2 + Y.^2)/(2*sigma.^2));
gfilter = gfilter/sum(gfilter(:));

gridInv = (-1).^(X+Y);
hfilter = gfilter.*gridInv;

subplot(2,2,1);
mesh(gfilter);
subplot(2,2,2);
mesh(hfilter);

subplot(2,2,3);
imagesc(abs(fftshift(fft2(gfilter))));
subplot(2,2,4);
imagesc(abs(fftshift(fft2(hfilter))));

• Thanks. I've tried subtracting the outcome from the original image but I only get the error "Matrix dimensions must agree". Also, how do i generate a dirac delta? Is it enough with using the dirac(x) function? – Fred Nov 27 '16 at 18:02
• Try conv2 with options, perhaps 'same' to keep coherent size. Convolution with Dirac does not need implementation. Use the image instead – Laurent Duval Nov 27 '16 at 21:39