My filtering algorithm took in an image and a filter and applied the filter to each color layer of the image and then recombined them to construct the final image. First the image was padded symmetrically by half of the filter width minus one ((w-1)/2) in the x direction and half of the filter height minus one ((h-1)/2) in the y direction (see figure 2). The image was then separated into its RGB component matrices.
Each matrix was converted using im2col which rearranges image blocks into columns using sliding and the filter dimensions. This produces a matrix where each column i contains the pixels used to calculate the resulting image at index i in the image array (flattened matrix). To perform the filtering, the filter was reshaped into a row vector. This vector multiplied by im2col matrix performs the calculation for each pixel and results in the convolution of the filter with the image. Once the filtered RGB matrices are computed, they are recombined to form the layered image.
my_imfilter(image, filter):
padded = pad image with floor half of the filter dimensions
//Extract the layers and create sliding representation
R = im2col(red layer of padded, filter dimensions, 'sliding');
G = im2col(green layer of padded, filter dimensions, 'sliding');
B = im2col(blue layer of padded, filter dimensions, 'sliding');
flatFilter = filter as row vector
//convolve layer with filter and reshape resulting array
R' = reshape flatFilter*R to original image dimensions
G' = reshape flatFilter*G to original image dimensions
B' = reshape flatFilter*B to original image dimensions
return [R,G,B]
Below are the results of applying various filters to the an image using my implementation of filter.
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
From left to right: (Top) original image, padded image, identity filter, sobel filter. (Bottom) small box blur, gaussian blur, laplacian high pass, high pass by subtracting low frequencies.
Hybrid images are obtained by extracting the low frequencies of one image and the high frequencies of another image and combining the two. In this case the low frequencies were obtained using a gaussian blur. The high frequency image was obtained by subtracting the low frequencies, achieved with gaussian blur, from the original image. Then the two images were combined.
Below are the original, intermediate, and resulting images for creating a hybrid image.
![]() ![]() ![]() ![]() |
![]() ![]() |