Saturday, 26 March 2016

HW 3 (Before the midterm)

Due: 4 April 2016, Monday, 17:00
Drop the HWs on my desk. My office no is A004, next to the students affairs.

We will have midterm on Tuesday, at the lecture hour.
Download the HW3 here:

https://drive.google.com/file/d/0B2gHM0DbKAwpMnhscmk1MkJqZW8/view?usp=sharing

Wednesday, 9 March 2016

HW2 - Image Filtering in Frequency Domain - Anti-Aliasing


Due: 23:59 19 March 2016, Saturday  
Where to upload: Use canvas
  • Resize the given image to one-third of its original size 942x942 by downsampling. 
  • Observe the aliasing on his blazer after the downsampling.
  • To prevent the aliasing, apply a gaussian filter in frequency domain with an appropriate \(\sigma\). (That is, Do not make the image too blurry and do not have aliasing after the downsampling. )
You can use MATLAB functions such as fspecial to obtain a gaussian, but do not use imresize, imgaussfilt or so. 

BONUS +50%:
  • Find the best (\sigma\) for the gaussian filter. (read explanations below)
Some notes: Recall that downsampling the image by two expands the frequency domain representation of the image by two. So, you can guess what happens in frequency domain when we downsample by three. Considering this fact, you should be able to find out the best possible boundaries of our low pass filter in order to prevent overlapping. You can assume that the gaussian function is effectively zero after (2\sigma\) (see three sigma rule).

The image is taken from Prof. Enis Çetin's DSP lecture notes book. You can find a free copy from here.




Prof. Cevdet Aykanat (Image courtesy of Prof. Enis Çetin http://kilyos.ee.bilkent.edu.tr/~ee424/Lecture%20Notes.htm)

My results:

Matlab DFT demonstration

We did a demonstration of fft2 function of MATLAB.

clear
clc
Irgb = imread('bricks.png');
% figure,imshow(Irgb)
Id = rgb2gray(Irgb);
Ig = rgb2gray(Irgb);
% figure,imshow(Ig)
Id = im2double(Ig);
figure,imshow(Id)
figure,imshow(Id(1:2:end,1:2:end))
figure,imshow(Id(1:3:end,1:3:end))
%figure,imshow(Id)
figure,imshow(Id(1:10:end,1:10:end))
%figure,imshow(Id)
%Lets create an synthetic image
%This part is taken from the pdf (I will provide it on the blog)
x = linspace(-pi, pi, 201);
% If you pass meshgrid only one vector, it uses that vector for both the x
% and the y coordinates.
[xx,yy] = meshgrid(x);
A = 10;
Icircles = sin(A*(xx.^2 + yy.^2));
% End of synthetic image creation
figure,imshow(Icircles)
figure,imshow(Icircles(1:3:end,1:3:end))
%imshow(Icircles)
Fcircles = fftshift(abs(fft2(Icircles)));
figure,imshow(Fcircles)
figure,imshow(Fcircles,[0,200])
FcirclesShifted = fftshift(fft2(Icircles));
%imshow(abs(Fcircles),[0,200])
size(Fcircles)
LowPassRectangle = zeros(size(Fcircles));
LowPassRectangle(69:132,69:132) = 1;
imshow(LowPassRectangle)
%imshow(LowPassRectangle.*Fcircles)
imshow(LowPassRectangle.*Fcircles,[0,200])
FcirclesLow = LowPassRectangle.*FcirclesShifted;
figure,imshow(abs(FcirclesLow),[0,200])
%IcirclesLow = ifft2(fftshift(FcirclesLow));
IcirclesLow = abs(ifft2(fftshift(FcirclesLow)));
figure,imshow(IcirclesLow)
figure,imshow(Icircles(1:3:end,1:3:end))
figure,imshow(IcirclesLow(1:3:end,1:3:end))
figure,imshow(IcirclesLow(1:2:end,1:2:end))
figure,imshow(Icircles(1:2:end,1:2:end))
Iparrot = im2double(imread('parrotsGrayscale.jpg'));
figure,imshow(Iparrot)
figure,imshow(Iparrot(1:5:end,1:5:end))
Fparrot = fft2(Iparrot);
%figure, imshow(abs(fftshift(Fparrot)))
figure, imshow(abs(fftshift(Fparrot)),[0,200])
LowPassRectangle = zeros(size(Fparrot));
%LowPassRectangle(342:382,221:258)=1;
%figure,imshow(LowPassRectangle)
%figure, imshow(abs(fftshift(Fparrot)),[0,200])
%figure,imshow(LowPassRectangle)
LowPassRectangle = zeros(size(Fparrot));
LowPassRectangle(221:257,342:382)=1;
figure,imshow(LowPassRectangle)
figure,imshow(abs(LowPassRectangle.*fftshift(Fparrot)),[0,200])
IparrotLow = abs(ifft2(fftshift(LowPassRectangle.*fftshift(Fparrot))));
figure,imshow(IparrotLow)
figure,imshow(IparrotLow(1:5:end,1:5:end))
Warning: Image is too big to fit on screen; displaying at 50%

ans =


   201   201






















Tuesday, 1 March 2016

DFT insights


Matlab demonstration codes - DFT

I made a demonstration in MATLAB today. Here is My command History and also the m file which has a simple low pass filter:

%-- 1/3/16 11:18 AM --%
t=linspace(0,1,100);
plot(cos(2*pi*t))
stem(cos(2*pi*t))
f = cos(2*pi*t);
F = fft(f);
stem(F)
stem(abs(F))
F(0)
F(1)
stem(abs(F))
stem(abs(fftshift(F)))
c = cos(2*pi*t);
c = cos(2*pi*t*30);
c30 = cos(2*pi*t*30);
c = cos(2*pi*t);
stem(c)
stem(c30)
c10 = cos(2*pi*t*10);
stem(c10)
f = c + 0.5*c10 + 0.1*c30;
stem(f)
F = fft(f);
stem(f)
stem(abs(fftshift(F)))
Fdn = zeros(F);
Fdn = zeros(size(F));
Fdn(50:52) = F(50:52);
fdn = ifft(Fdn);
stem(fdn)
stem(abs(fftshift(F)))
stem(abs(F))
stem(abs(fftshif(fftshift(F))))
stem(abs(fftshift(fftshift(F))))
stem(abs((fftshift(F))))
stem(abs(Fdn))
stem(abs(fftshift(Fdn)))
fdn = fftshift(Fdn);
fdn = ifft(fftshift(Fdn));
stem(fdn)
stem(f)
stem(fdn)
figure,stem(f)
stem(f)
stem(lowpass(f))

%%end of history

%%begining of lowpass.m file
function fl = lowpass(f)
    F = fft(f);
    F(6:end-5) = 0;
    fl = ifft(F);

end