2
$\begingroup$

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(:));

I = mean(double(imread('image.png')),3);
GaussBlur = conv2(I,gfilter);
%%plot images side by side
subplot(1,2,1);
imshow(I, []);
subplot(1,2,2);
imshow(GaussBlur, []);
$\endgroup$
1
  • $\begingroup$ I have updated my previous incomplete answer $\endgroup$ Jan 1, 2017 at 10:59

1 Answer 1

3
$\begingroup$

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

Filters low-pass to high-pass in 1D

The adaptation in 2D follows:

Filters low-pass to high-pass in 2D

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))));
$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Fred
    Nov 27, 2016 at 18:02
  • $\begingroup$ Try conv2 with options, perhaps 'same' to keep coherent size. Convolution with Dirac does not need implementation. Use the image instead $\endgroup$ Nov 27, 2016 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.