The goal of this project was to implement an image filtering algorithm such that a filter could be applied to an image. Once this algorithm was implemented, it could be used to isolate the high frequencies or the low frequencies from an image. By combining the high frequencies from one image with the low frequenceis from another, "hybrid images" can be formed, which contain details from both images that change depending on the size at which the image is displayed.
The algorithm for applying filters is relatively straightforward. Images in MATLAB are represented as a 3-dimensional array; the first two dimensions represent the image's Y and X coordinates respectively, and the third dimension represents the color channels (the red, blue, and green channels respectively). Each color channel is operated on independently of the others.
filter_y = size(filter,1);
filter_x = size(filter,2);
pad_y = filter_y/2 - .5;
pad_x = filter_x/2 - .5;
filter_vec = reshape(filter, 1, numel(filter));
for i = 1:size(image,3)
image_channel = image(:,:,i);
image_channel = padarray(image_channel, ...
[pad_y, pad_x], ...
'symmetric');
channel_vecs = im2col(image_channel, [filter_y, filter_x], 'sliding');
channel_vals = filter_vec * channel_vecs;
new_channel = reshape(channel_vals, size(image,1), size(image,2));
output(:,:,i) = new_channel;
end
After calculating the size of the the filter and the ammount of padding necessary
for the image, the code does the following for each channel:
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
The above are some examples of hybrid images created using the image filtering algorithm described above. Low frequencies were obtained by filtering the images using a Gaussian filter, and high frequencies were obtained by filtering the image using a Laplacian filter (i.e. subtracting the filtered image from the original). When combined, they tend to have the overall form, colors, etc. of the low-frequency image but have the details of the high-frequency image. An interesting phenomenon is that when images are scaled down to smaller sizes or viewed from further away, they appear to resemble the low-frequency image more than when viewed at a larger size or from closer.