Hybrid image of Cesear and Andy Serkis from Rise of the Planet of the Apes (2011).
In this project, an image filtering function is implemented to create hybrid images. Hybrid images are static images that change in interpretation as a function of the viewing distance. The basic idea is that high frequency tends to dominate perception when it is available, but, at a distance, only the low frequency (smooth) part of the signal can be seen. By blending the high frequency portion of one image with the low-frequency portion of another, you get a hybrid image that leads to different interpretations at different distances.
The things have been done within this project are listed in the following;
Here is the details of my_imfilter() function.
if nargin < 3
bound_opt = 'zero';
end
if ~( strcmpi(bound_opt,'zero') || strcmpi(bound_opt,'symmetric') || ...
strcmpi(bound_opt,'replicate') || strcmpi(bound_opt,'circular') )
warning(['Invalid boundary option "' bound_opt '". Using "zero" boundary option.']);
bound_opt='zero';
end
if any(mod(size(filter),2)==0)
error(['Invalid filter size: ' num2str(size(filter))]); % Terminate the code.
end
p_image=pad_image(image,filter,bound_opt); % padded image
im_size=size(image);
output=zeros(im_size); % filtered image
if numel(im_size)==3
filter= cat(3,filter,filter,filter);
end
[f_y,f_x]=size(filter);
for i=1:im_size(1)
for j=1:im_size(2)
output(i,j,:)=sum( sum( p_image(i:i+f_y-1,j:j+f_x-1,:).*filter ) );
end
end
This function pads any given image according to any given filter in different size. The funtion provides four different boundary padding options such as default 'zero' padding, 'symmetric', 'replicate' and 'circular' paddings.
f_y=(size(filter,1)-1)/2; % padding size at the top and bottom of the given image.
f_x=(size(filter,2)-1)/2; % padding size at the left and right of the given image.
% create padded image with respect to given image's color dimension.
im_size=size(image);
if numel(im_size)==2
p_image=zeros( im_size(1)+2*f_y, im_size(2)+2*f_x);
p_image(f_y+1:end-f_y,f_x+1:end-f_x)=image;
else
p_image=zeros( im_size(1)+2*f_y, im_size(2)+2*f_x,3);
p_image(f_y+1:end-f_y,f_x+1:end-f_x,:)=image;
end
switch bound_opt
case 'symmetric'
p_image(1:f_y,:,:)=flipud( p_image(f_y+1:2*f_y,:,:) );
p_image(end-f_y+1:end,:,:)=flipud( p_image(end-2*f_y+1:end-f_y,:,:) );
p_image(:,1:f_x,:)=fliplr( p_image(:,f_x+1:2*f_x,:) );
p_image(:,end-f_x+1:end,:)=fliplr( p_image(:,end-2*f_x+1:end-f_x,:) );
case 'replicate'
p_image(1:f_y,:,:)= repmat(p_image(f_y+1,:,:),[f_y 1]);
p_image(end-f_y+1:end,:,:)= repmat(p_image(end-f_y,:,:),[f_y 1]);
p_image(:,1:f_x,:)= repmat(p_image(:,f_x+1,:),[1 f_x]);
p_image(:,end-f_x+1:end,:)= repmat(p_image(:,end-f_x,:),[1 f_x]);
case 'circular'
p_image(1:f_y,:,:)=p_image(end-2*f_y+1:end-f_y,:,:);
p_image(end-f_y+1:end,:,:)=p_image(f_y+1:2*f_y,:,:);
p_image(:,1:f_x,:)=p_image(:,end-2*f_x+1:end-f_x,:);
p_image(:,end-f_x+1:end,:)=p_image(:,f_x+1:2*f_x,:);
end
As stated above, the basic idea is that high frequency tends to dominate perception when it is available, but, at a distance, only the low frequency (smooth) part of the signal can be seen. By blending the high frequency portion of one image with the low-frequency portion of another, you get a hybrid image that leads to different interpretations at different distances.
Here is some example hybrid images created with given data samples. In each examples, first image pairs are given. Then, low frequencies of left image and high frequencies of right image are shown in left to right order. Finally, hybrid of two images in different scales are presented in second row of image table. For each example cutoff frequencies are presented. Also, a distance between Matlab imfilter() and my_imfilter() metric is calculated by summing absulute values of pixel by pixel differences in all color dimensions of both hybrid images. Hybrid images generated by using only my_imfilter() and Matlab's imfilter() functions. Results show that my_imfilter() function is imitating Matlab's imfilter() function very closely. Symmetric padding is choosen for all of the filtering operations.
Example 1: cutoff-frequency= 7, distance to Matlab imfilter()= 0.0062057 pixels.
Example 2: cutoff-frequency= 4, distance to Matlab imfilter()= 0.002752 pixels.
Example 3: cutoff-frequency= 3, distance to Matlab imfilter()= 0.016734 pixels.
Example 4: cutoff-frequency= 8, distance to Matlab imfilter()= 0.0040283 pixels.
Example 5: cutoff-frequency= 7, distance to Matlab imfilter()= 0.0056374 pixels.
Example 6: cutoff-frequency= 6, distance to Matlab imfilter()= 0.0015248 pixels.
Cesear and Andy Serkis from Rise of the Planet of the Apes (2011).
Example 7: cutoff-frequency= 7, distance to Matlab imfilter()= 0.0021846 pixels.
Grayscale version of Example 1.