Result of a hybrid image.
The goal of this assignment is to understand the process of 2D convolution and use the iamge filtering function wrote by myself to create hybrid image. Hybrid image is generated by combining two different images, one of which provides high frequency portion and the other provides low frequency portion.
The assignment contains several parts:
In this assignment, the image filtering function is based on 2D convolution in spatial domain. First, the image matrix need to be zero padded according to the size of filter. Second, the filter matrix should be reversed and shifted for the convenience of further calculation. Third, calculate the result of filtered imaged using 2D convolution formula.
In this assignment, the image filtering function is based on 2D convolution in spatial domain. First, the image matrix need to be zero padded according to the size of filter. Second, the filter matrix should be reversed and shifted for the convenience of further calculation. Third, calculate the result of filtered imaged using 2D convolution formula.
%Input image and filter infomation
[M,N,n] = size(image);
[a,b] = size(filter);
M_zp = M+(a-1);
N_zp = N+(b-1);
fil_cov = zeros(a,b);
output = zeros(M,N,n);
image_zp = zeros(M_zp,N_zp,n);
%zero padding
for i = 1:n
image_zp((a-1)/2+1:(a-1)/2+M,(b-1)/2+1:(b-1)/2+N,i) = image(:,:,i);
end
%% Reverse and shift the filter vectors.
for i = 1:a
for j = 1:b
fil_cov(i,j) = filter(-i+a+1,-j+b+1);
end
end
%% Calculate the result of 2D convolution
for k = 1:n
for i = 1:M
for j = 1:N
sum = 0;
for i_cov = 1:a
for j_cov = 1:b
sum = sum + image_zp(i+i_cov-1,j+j_cov-1,k)*fil_cov(i_cov,j_cov);
end
end
output(i,j,k) = sum;
end
end
end
The code shown above is my_imfilter.m and it can filter both grayscale and color images with any arbitrary shaped filters. Here shown below are the test results.
Identity image |
Blur image |
Large blur image |
Sobel image |
Laplacian image |
High pass image |
%Set cutoff frequency of low pass Gaussian filter.
cutoff_frequency = 7;
filter = fspecial('Gaussian', cutoff_frequency*8+1, cutoff_frequency);
%Generate low frequency part.
low_frequencies = my_imfilter(image1,filter);
%Generate high frequency part by remove low frequency part.
filter2 = fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency);
high_frequencies = ifft2(fft2(image2)-fft2(my_imfilter(image2,filter2)));
%Generate hybrid image by combine high and low frequency parts.
hybrid_image = ifft2(fft2(high_frequencies)+fft2(low_frequencies));
Low frequency |
High frequency |
Hybrid image |
Scaled hybrid images |
Low frequency |
High frequency |
Hybrid image |
Scaled hybrid images |
Low frequency |
High frequency |
Hybrid image |
Scaled hybrid images |