The image filtering algorithm moves the filter across the image matrix like a sliding window. It computes a value for each local neighborhood. Three for loops were used to loop through each of the three dimensions of the image: height, width, and color. As the algorithm looks at each individual pixel value, it takes a slice of the image with the same dimensions of the filter - this is the local neighborhood. The slice is centered around the current pixel, and the dot product of the slice and the filter is stored in the new image matrix (representing the new image produced) at the relative position. Problems with slicing the image matrix out of bounds along the edges were avoided by zero padding in relation to the height and width of the filter. While the algorithm is slicing into a new image matrix with slightly altered dimensions, the new image matrix has dimensions reflecting those of the original, unpadded image matrix.
[height, width, color] = size(image);
[filterH, filterW] = size(filter);
padHeight = floor(filterH / 2);
padWidth = floor(filterW / 2);
C = image;
B = padarray(image, [padHeight padWidth], 0);
for h = (padHeight + 1):height + padHeight - 1
for w = (padWidth + 1):width + padWidth - 1
for c = 1:color
current = sum(dot(B(h-padHeight:h+padHeight, w-padWidth:w+padWidth, c), filter));
C(h - padHeight,w - padWidth,c) = current;
end
end
end
output = C;
The below results represent the use of my_imfilter procedure on the test_image with the following filters, respectively: identity, small blur, large blur, sobel, laplacian, and high pass.
Gaussian blurring is highly effective at removing high frequencies. Observe it here, on this image of a dog.
Using a Gaussian blur again, the low frequencies of an image can be removed. However, to get this high frequency image, subtract the blurred image from the original. The difference between the two images is everything but the low frequency image - just the high frequencies.
After combining the two images, it becomes apparent that certain frequencies within the resultant image are more visible at different sizes. The low frequency image is dominant at smaller scales, and the high frequency image is more visible at larger scales.