The project report contains the following
Filtering is a fundamental operation in image processing. In this report filtering has been interpreted in both, spatial and frequency domain of the image. Filtering in the spatial domain is a mathematical operation on a neighbourhood of pixels around a given pixel. Filtering in the frequency domain can be interpreted as modification of frequencies in the image
In the spatial domain, while implementing image filtering, a few considerations have to be made -
Filtering on color images is handled by performing the same filtering operation on individual color channels. The outermost loop in the code iterates over all the channels in the image.
Filtering is done by performing mathematical operations to calculate the value of a pixel based on its neighbouring pixel. Since boundary pixels don't have the complete neighbourhood, we use padding to handle the boundary. The simplest option is to pad the image with rows and columns of 0s according to the size of the filter. However this affects the filtered version of the image with darkening around the boundaries. To overcome this, padding can be replaced with mirroring of pixels around the boundaries (Figure 3). This is done using padarray() function of matlab. The thickness of the padding is calculated using the size of the filter.
The implementation handles dynamic filter sizes as long as dimensions are odd. The size of the filter defines the neighbourhood of pixels on which mathematical operations need to be performed. Once the boundary has been identified, the value of each pixel in the filtered image is computed as an element wise dot product of the filter and the neighbourhood.
%filtering code
filtered_image = [];
for channel=1:size(image,3)
image_channel = image(:,:,channel);
%% padding the image
[m,n] = size(image_channel);
[fm,fn] = size(filter);
padded_rows = floor(fm/2);
padded_cols = floor(fn/2);
%padded_image = padarray(image_channel,[padded_rows,padded_cols]);
padded_image = padarray(image_channel,[padded_rows,padded_cols],'symmetric');
%% filtering the image
filtered_image_channel = [];
%pixel wise filtering
for i=padded_rows:m+padded_rows-1
for j=padded_cols:n + padded_cols-1
filtered_image_channel(i-padded_rows+1,j-padded_cols+1)...
= sum(sum(padded_image(i-padded_rows+1:i-padded_rows+fm,j-padded_cols+1:j-padded_cols+fn) .* filter));
end
end
filtered_image(:,:,channel) = filtered_image_channel;
end
Figure 1. Blurring and edge detection using filtering
Figure 2. (Left)Darkening issue with zero padding, (Right)Resolved using symmetric padding
Hybrid images are a combination of images in low frequencies and high frequencies. Due to the difference in frequencies and the nature of human vision, interpretation of the images is dependant on the viewing distance.
%% Filtering and Hybrid Image construction
high_pass_frequency = 7;
low_pass_frequency = 4;
high_pass_filter = fspecial('Gaussian', high_pass_frequency*4+1, high_pass_frequency);
low_pass_filter = fspecial('Gaussian', low_pass_frequency*4+1, low_pass_frequency);
%% Create low frequency image
low_frequencies = my_imfilter(image1,low_pass_filter);
%% Create high frequency image
high_frequencies = image2 - my_imfilter(image2, high_pass_filter);
%% Combine to create hybrid image
hybrid_image = low_frequencies + high_frequencies;
Image pair | High pass frequency | Low pass frequency |
---|---|---|
Cat - Dog | 7 | 7 |
Einstein - Marilyn | 7 | 4 |
Plane - Bird | 5 | 4 |
Bicycle - Motorcycle [Grayscale] | 8 | 5 |
Submarine - Fish | 7 | 4 |
Figure 3. (Left) A ground level image of high rise buildings (Right) Batman's batarang
Figure 4. Hybrid image viewed as buildings from close (high frequencies) and batarang from far(low frequencies)
Figure 5. (Left) Aerial view of New York city. Credits : Michael Chinnici + PHOTOWORKSHOPADVENTURES.COM. Note the presence of similar lighting around all buildings in the centre of the image (Right) Batarang with high frequencies around the edges
Figure 6. Significant halo of the blurred image due to presence of high frequencies and a slight misalignment with the lighting in the original image causes this to be unsatisfactory. The batarang is visible even at a close viewing distance