In this project, we implement a function comparable to MATLAB's 'imfilter' function. The function is passed an image (with two spatial dimensions and an arbitrary number of channels) and a filter (two-dimensional) which we wish to apply to each channel independently. The algorithm must perform the following steps:
To pad the image, we employ MATLAB's 'padarray' function, with the 'symmetric' option which mirrors the image across its boundaries. The amount of padding necessary depends on the size of the filter; each direction will need enough padding for the convolution to be well defined. In this case, if the filter is W by H (both odd), we need (W-1)/2 more elements on each side and (H-1)/2 more elements on both top and bottom.
The convolution is done by separately considering each pixel in the output image, which will have the same dimensions as the original image. A filter-sized rectangle centered at each pixel is multiplied element-wise with the filter itself, which is then summed twice (once in each direction) to achieve the value of the output image at that pixel. This is identical in form to the theoretical summation explored in the notes.
The code may be observed in the accompanying file 'my_imfilter.m'.
We may test our function by convolving a sample image with some common filters:
![]() |
![]() |
![]() |
![]() |
Example Image |
Sobel Filter [-1 0 1; -2 0 2; -1 0 1] (added intensity) |
Box Filter [1 1 1; 1 1 1; 1 1 1] |
Laplacian Filter [0 1 0; 1 -4 1; 0 1 0] (added intensity) |
To create hybrid images, we merge two images of identical dimensions. Both images are filtered: one with a low-pass, the other with a high-pass; thus the resulting image has the low frequencies of one image but the high frequencies of another. In practice, we need only define one low-pass filter L (e.g., a Gaussian). The desired low-frequency image is hit with L; the high-frequency is hit with the operator (I-L), where I is the identity (i.e., we subtract out the low frequencies). As the high-frequency image is zero-mean, we may simply add it to the low-frequency image to obtain a hybrid image.
The code may be observed in the accompanying file 'proj1.m'.
The low-pass filter L is defined as Gaussian with customizable standard deviation, which is specified in the variable cutoff_frequency (we use 7 for these images). The results of hybridizing various image pairs appear below:
![]() |
![]() |
![]() |
![]() |
![]() | |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() | |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() | |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() | |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() | |
![]() |