Εργαστήριο ADICV2 Labs 2-6 Image filtering Κώστας Μαριάς
Image Filtering ADICV Kostas Marias TEI Crete 2017 2
Basic Matlab Σκοπός εργαστηρίου Θα φτιάξουμε ένα ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ (mean FILTER) σε matlab Στη συνέχεια θα το συγκρίνουμε με το έτοιμο ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ που υπάρχει στη matlab. Τέλος θα συγκρίνουμε τα δυο φίλτρα και θα βρούμε πόσο αποκλίνουν υπολογίζοντας το σφάλμα ανα pixel μεταξύ του δικού μας φλιτρου και της matlab. 3
Basic Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα Αρχική εικόνα: Εικόνα φιλτραρισμένη με 3x3 φίλτρο ομαλοποίησης 1/9 1 1/9 6 1/9 3 2 9 (x,y) 1/9 2 11 1/9 1/9 3 10 0 1/9 5 10 1/9 1/9 6 9 7 3 1 0 2 8 4 4 2 9 10 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 f(x,y)=f(2,2)=11 Νέα τιμή g(x,y)=t[f(x,y)] = 1 1/9 + 6 1/9 + 3 1/9 + 2 1/9 + 11 1/9 + 3 1/9 + 5 1/9 + 10 1/9 + 6 1/9 = 47/9 = 5.222 4
Basic Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα Αρχική εικόνα: Εικόνα φιλτραρισμένη με 3x3 φίλτρο ομαλοποίησης 1 1/9 6 1/9 3 1/9 2 9 2 1/9 11 1/9 3 1/9 10 0 5 1/9 10 1/9 6 1/9 9 7 3 1 0 2 8 4 4 2 9 10 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 Νέα τιμή = 1 1/9 + 6 1/9 + 3 1/9 + 2 1/9 + 11 1/9 + 3 1/9 + 5 1/9 + 10 1/9 + 6 1/9 = 47/9 = 5.222 5
Basic Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα Αρχική εικόνα: Εικόνα φιλτραρισμένη με 3x3 φίλτρο ομαλοποίησης 1 6 1/9 3 1/9 2 1/9 9 2 11 1/9 3 1/9 10 1/9 0 5 10 1/9 6 1/9 9 1/9 7 3 1 0 2 8 4 4 2 9 10 0 0 0 0 0 5 7 0 0 0 0 0 0 0 0 0 0 0 Νέα τιμή = 6 1/9 + 3 1/9 + 2 1/9 + 11 1/9 + 3 1/9 + 10 1/9 + 10 1/9 + 6 1/9 + 9 1/9 = 60/9 = 6.667 6
Basic Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα Αρχική εικόνα: Εικόνα φιλτραρισμένη με 3x3 φίλτρο ομαλοποίησης 1 6 3 2 9 2 11 3 10 0 5 10 6 9 7 3 1 0 2 8 4 4 2 9 10 0 0 0 0 0 5 7 5 0 0 5 6 0 0 4 5 0 0 0 0 0 0 0 5 6 Στην επόμενη διαφάνεια θα δούμε την επίδραση (ομαλοποίηση) του 3x3 φίλτρου αυτού σε μια εικόνα. 7
ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ (mean FILTER) Η λειτουργία του φίλτρου μέσης τιμής συνίσταται με την αντικατάσταση της φωτεινότητας σε κάθε εικονοστοιχείο με τη μέση φωτεινότητα σε μια γειτονιά του. Είναι Χαμηλοπερατά (lowpass) φίλτρα μιας και αντικαθιστούμε τη τιμή του pixel με τη μέση τιμή της γειτονιάς του οπότε και μειώνουμε βαθμιαία απότομες αλλαγές στην ένταση των pixels. Ενώ μειώνουμε των τυχαίο θόρυβο όμως χάνουμε συνήθως ευκρίνεια στις ακμές τις εικόνας (edge blurring θόλωμα ακμών). 8
Basic ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ (mean FILTER) Η γειτονιά Ν είναι συνήθως καθορισμένη για κάθε επεξεργασία και συνήθως αντιστοιχεί σε τετράγωνες μάσκες. Έτσι για ακτίνα ίση με ένα έχουμε ουσιαστικά μια γειτονιά διαστάσεων 3 3. Ένα 3 3 φίλτρο μέσης τιμής μπορεί πρακτικά να υλοποιηθεί με μια μάσκα της μορφής: 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 ή 1/9 1 1 1 1 1 1 1 1 1 9
Basic Φιλτράρισμα στο Χωρικό πεδίο Η αρχή γραμμικού φιλτραρίσματος στο χώρο παρουσιάζεται στο σχήμα: y Εικόνα f w(- 1, - 1) w(- 1, 0) w(- 1, 1) x w(0, - 1) w(0,0) w(0, 1) w( 1, - 1) w(1, 0) w( 1, 1) (x - 1, y - 1) (x - 1, y) (x - 1, y + 1) (x, y - 1) (x,y) (x, y + 1) Συντελεστές του φίλτρου (x + 1, y - 1) (x + 1, y) (x + 1, y + 1) Pixels εικόνας f που θα φιλτραριστούν 10
Basic Φιλτράρισμα στο Χωρικό πεδίο Η αρχή γραμμικού φιλτραρίσματος στο χώρο δίνεται από τη σχέση: f(x - 1, y - 1) f(x - 1, y) f(x - 1, y + 1) w(- 1, - 1) w(- 1, 0) w(- 1, 1) f(x, y - 1) f(x,y) f(x, y + 1) w(0, - 1) w(0,0) w(0, 1) f(x + 1, y - 1) f(x + 1, y) f(x + 1, y + 1) w( 1, - 1) w(1, 0) w( 1, 1) 11
Basic Φιλτράρισμα στο Χωρικό πεδίο Η αρχή γραμμικού φιλτραρίσματος στο χώρο δίνεται από τη σχέση συσχέτισης: f(x - 1, y - 1) f(x - 1, y) f(x - 1, y + 1) w(- 1, - 1) w(- 1, 0) w(- 1, 1) f(x, y - 1) f(x,y) f(x, y + 1) w(0, - 1) w(0,0) w(0, 1) f(x + 1, y - 1) f(x + 1, y) f(x + 1, y + 1) w( 1, - 1) w(1, 0) w( 1, 1) g(x, y) = w(-1, -1) f(x - 1, y - 1)+ w(-1, 0) f(x - 1, y) + w(-1, 1)f(x - 1, y+1) + w(0, -1) f(x, y - 1) + w(0, 0) f(x, y) + w(0, 1) f(x, y+1) + w(1,-1) f(x+1, y - 1) + w(1, 0) f(x+1, y) + w(1, 1) f(x +1, y+1) 12
Basic Matlab ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ παράδειγμα με Matlab Εφαρμογή φίλτρου mean 3x3 αφού η αρχική εικόνα επιμολυνθεί θόρυβο Gauss. Χρησιμοποιούμε στη Matlab τη συνάρτηση imfilter(i,h) όπου I και h είναι πολυδιάστατοι πίνακες της εικόνας εισόδου και του φίλτρου. Το 3X3 φίλτρο μέσης τιμής δεν ανταποκρίνεται όσο καλά όσο το 5x5 το οποίο απομακρύνει καλύτερα τον θόρυβο με κόστος όμως το περαιτέρω θόλωμα της εικόνας. E. Jebamalar Leavline, D. Asir Antony Gnana Singh, On Teaching Digital Image Processing with MATLAB, American Journal of Signal Processing, Vol. 4 No. 1, 2014, pp. 7-15. doi: 10.5923/j.ajsp.20140401.02. % Teaching gaussian noise removal using a simple 3X3 %average filter clc;clear all; close all; Im = imread('cameraman.tif');i = imnoise(im,'gaussian'); h1 = ones(3,3) / 9;h2 = ones(5,5) / 25; I1 = imfilter(i,h1);i2 = imfilter(i,h2); subplot(2,2,1);imshow(im,[ ]);title('original Image'); subplot(2,2,2);imshow(i,[ ]);title('image with Gaussian noise'); subplot(2,2,3);imshow(i1,[ ]); title('filtered Image with 3X3 average filter'); subplot(2,2,4);imshow(i2,[ ]); title('filtered Image with 5X5 average filter'); 13
Basic Matlab ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ παράδειγμα με Matlab % Teaching gaussian noise removal using a simple 3X3 %average filter clc;clear all; close all; Im = imread('cameraman.tif');i = imnoise(im,'gaussian'); h1 = ones(3,3) / 9; I1 = imfilter(i,h1); subplot(2,2,1);imshow(im,[ ]);title('original Image'); subplot(2,2,2);imshow(i,[ ]);title('image with Gaussian noise'); subplot(2,2,3);imshow(i1,[ ]); title('filtered Image with 3X3 average filter'); subplot(2,2,4);imshow(im-i1,[ ]); title( Diafora anamesa se arxikh kai filtrarismenh'); 14
Basic Matlab Φιλτράρισμα στο Χωρικό πεδίο Matlab Θα φτιάξουμε κώδικα matlab για να φίλτρο μέσης τιμής 3x3 : w=zeros(3,3); w(-1, -1) =1/9; w(-1, 0)=1/9; w(-1, 1)=1/9; w(0, -1) =1/9; w(0, 0)=1/9; w(0, 1) =1/9; w(1,-1)=1/9; w(1, 0)=1/9; w(1, 1) =1/9; f = imread('cameraman.tif'); [m,n]=size(f); g=zeros(m,n); for x=1:m for y=1:n end End g(x, y) = w(-1, -1) f(x - 1, y - 1)+ w(-1, 0) f(x - 1, y) + w(-1, 1)f(x - 1, y+1) + w(0, -1) f(x, y - 1) + w(0, 0) f(x, y) + w(0, 1) f(x, y+1) + w(1,-1) f(x+1, y - 1) + w(1, 0) f(x+1, y) + w(1, 1) f(x +1, y+1) Ο ΚΩΔΙΚΑΣ ΑΥΤΟΣ ΕΊΝΑΙ ΛΑΘΟΣ!!!!!!!!!!!!!!!!!! Γιατι όμως???? 15
Matlab Φιλτράρισμα στο Χωρικό πεδίο Matlab Για να φτιάξουμε ένα πρόγραμμα Matlab πρέπει να μεταφέρουμε τις θεωρητικές συντεταγμένες της μάσκας 3x3 σε indexing ου είναι αποδεκτό από τη matlab (δηλ. από (1,1) έως (3,3)): w(- 1, - 1) w(- 1, 0) w(- 1, 1) w(1,1) w(1,2) w(1,3) w(0, - 1) w(0,0) w(0, 1) w(2,1) w(2,2) w(2,3) w( 1, - 1) w(1, 0) w( 1, 1) w( 3, 1) w(3,2) w( 3,3) 16
Basic Matlab Φιλτράρισμα στο Χωρικό πεδίο κώδικα matlab για να φίλτρο μέσης τιμής 3x3 : w=zeros(3,3); w(:,:)=1/9; f = imread('cameraman.tif'); [m,n]=size(f); g=uint8(zeros(m,n)); for x=2:m-1 for y=2:n-1 g(x,y)=w(1,1) *f(x-1, y-1)+ w(1,2)* f(x-1, y)+w(1,3)*f(x-1,y+1)+w(2,1)*f(x, y-1) + w(2,2)*f(x, y) + w(2,3)*f(x, y+1)+w(3,1)*f(x+1, y-1) + w(3,2)*f(x+1, y) + w(3,3)*f(x+1, y+1); end end % Η εικόνα g είναι τώρα η φιλτραρισμένη εικόνα f με φίλτρο 3x3 μέσης τιμής figure, imshow(g,[]) 17
Basic Matlab Building a basic filtering code f = imread('cameraman.tif'); w=(1/9)*ones(3,3); g=zeros(size(f)); for x=2:size(f,1)-1 for y=2:size(f,2)-1 g(x,y)=w(1,1) *f(x-1, y-1)+ w(1,2)* f(x-1, y)+w(1,3)*f(x-1,y+1)+w(2,1)*f(x, y-1) + w(2,2)*f(x, y) + w(2,3)*f(x, y+1)+w(3,1)*f(x+1, y-1) + w(3,2)*f(x+1, y) + w(3,3)*f(x+1, y+1); end end subplot(1,2,1), imshow(f), title('original image') subplot(1,2,2), imshow(uint8(g)), title('filtered image') 18
19
Basic Matlab Σύγκριση των 2 φίλτρων subplot(2,2,1);imshow(f,[]), title('original Image'); subplot(2,2,2);imshow(i,[]), title('image with Gaussian noise '); subplot(2,2,3);imshow(i1,[]), title('image filtered with Matlab s mean filter '); subplot(2,2,4);imshow(g,[]), title('filtered Image with our method'); figure, imshow(i1-g,[]), title('difference Image between the two methods'); sum(sum(i1-g))/(m*n) 20
Basic ΦΙΛΤΡΑ GAUSS (Θόλωση Gauss) Είναι φίλτρα θόλωσης εικόνας που χρησιμοποιούν τη συνάρτηση Gauss (η οποία εκφράζει την κανονική κατανομή στη στατιστική)για να υπολογίσει τους συντελεστές του φίλτρου για τον μετασχηματισμό κάθε pixel: G x, y = 1 x 2 +y 2 2πσ 2 e 2σ 2 Όπου x,y είναι οι αποστάσεις από την αρχή των αξόνων και σ είναι η τυπική απόκλιση (standard deviation) της κατανομής Gauss. Στις 2 διαστάσεις η εξίσωση αυτή δίνε μια επιφάνεια της οποία τα περιγράμματα είναι ομόκεντροι κύκλοι με Γκαουσιανη κατανομή από το κεντρικό σημείο. https://en.wikipedia.org/wiki/gaussian_blur 21
Basic ΦΙΛΤΡΑ GAUSS (Θόλωση Gauss) Μια προσέγγιση στο σχεδιασμό φίλτρων Gauss είναι να υπολογίσουμε τα βάρη της μάσκας απευθείας από την ασυνεχή κατανομή Gauss: G i, j = 1 2πσ 2 e i 2 +j 2 2σ 2 Προαιρετικά μπορούμε να χρησιμοποιήσουμε μια είναι σταθερά κανονικοποίησης c: G(i, j) c G i, j = 1 i 2 +j 2 2πσ 2 e 2σ 2 22
Basic Matlab ΦΙΛΤΡΑ GAUSS στη Matlab Η εντολή h = fspecial('gaussian', hsize, sigma) Δίνει ένα περιφερειακά συμμετρικό φίλτρο Gauss (lowpass filter) μεγέθους hsize και τυπικής απόκλισης sigma (θετικός). Το hsize μπορεί να έιναι ένα διάνυσμα που να καθορίζει τον αριθμό γραμμών/στηλών στο h, η μπορεί να είναι βαθμωτη τιμή οπότε το h θα είναι τετράγωνος πίνακας. Η προεπιλεγμένη τιμή για το hsize είναι [3 3]και για το sigma 0.5. 23
Basic Matlab ΦΙΛΤΡΑ GAUSS στη Matlab h=fspecial('gaussian', [100 100],2); figure, imshow(h,[]) figure, imagesc(h,[]) h=fspecial('gaussian', [100 100],7); figure, imagesc(h), colormap jet figure, surf(h), shading interp, colormap jet 24
Build 2 Gaussian masks a)5x5και σ=1.5, b)μια7x7και σ=1.5 and filter Image I. What happens? % Teaching gaussiannoise removal using a simple 3X3 Gaussian filter clc;clearall; close all; Im= imread('cameraman.tif');i = imnoise(im,'gaussian'); h1 = fspecial('gaussian'); h2 = fspecial('gaussian', [3 3],1.5); I1 = imfilter(i,h1); I2 = imfilter(i,h2); subplot(2,2,1);imshow(im,[ ]);title('original Image'); subplot(2,2,2);imshow(i,[ ]);title('image with Gaussian noise'); subplot(2,2,3);imshow(i1,[ ]); title('filtered Image with 3X3 Gaussian filter σ=0.5'); subplot(2,2,4);imshow(i2,[ ]); title('filtered Image with 3X3 Gaussian filter σ=1.5'); 25
Matlab ΦΙΛΤΡΑ GAUSS στη Matlab 26
Matlab ΦΙΛΤΡΑ GAUSS στη Matlab 27
Lab 5 Filter a noisy image with different methods 28
Im = imread('cameraman.tif'); %f = imnoise(im,'gaussian'); %This is the noisy image I want to filter/smooth f=imnoise(im,'salt & pepper', 0.05); % A Filter the image with our own filter w=(1/9)*ones(3,3); g=zeros(size(f)); for x=2:size(f,1)-1 for y=2:size(f,2)-1 g(x,y)=w(1,1) *f(x-1, y-1)+ w(1,2)* f(x-1, y)+w(1,3)*f(x-1,y+1)+w(2,1)*f(x, y-1) + w(2,2)*f(x, y) + w(2,3)*f(x, y+1)+w(3,1)*f(x+1, y-1) + w(3,2)*f(x+1, y) + w(3,3)*f(x+1, y+1); end end subplot(1,2,1), imshow(f), title('original image') subplot(1,2,2), imshow(uint8(g)), title('filtered image') % The second choice is Gaussian filtering w=fspecial('gaussian', [7 7],2); g=imfilter(f,w); subplot(1,2,1), imshow(f), title('original image') subplot(1,2,2), imshow(uint8(g)), title('filtered image') 29
It is not possible to remove salt and pepper noise with mean or Gaussian filters. The way to do it is with median filter (as in your assignment) 30
Log Filter g = double(imread('cameraman.tif')); a=0.5; b=255/(log(1+255*a)); Tg=b*log(1+a*g); subplot(1,2,1), imshow(uint8(g)), title('original image') subplot(1,2,2), imshow(uint8(tg)), title('log Transformed image') 31
a=0.5 32
a=0.05 33
Lab 6 34
Use the exp filter in the Office Image g = double(imread('office.jpg')); g=g(:,:,1); a=0.3; b=255/(log(1+255*a)); Tg=(1/a)*(exp(g/b)-1); Tg=mat2gray(Tg)*255; subplot(1,2,1), imshow(uint8(g)), title('original image') subplot(1,2,2), imshow(uint8(tg)), title('exp Transformed image') 35
36
g = double(imread('office.jpg')); g=g(:,:,1); a=0.3; b=255/(log(1+255*a)); Tg=(1/a)*(exp(g/b)-1); Tg=mat2gray(Tg)*255; subplot(2,2,1), imshow(uint8(g)), title('original image') subplot(2,2,2), imshow(uint8(tg)), title('exp Transformed image') subplot(2,2,3), imhist(uint8(g)), title('original image histogram') subplot(2,2,4), imhist(uint8(tg)), title('exp Transformed image histogram') 37
38
g = double(imread('office.jpg')); g=g(:,:,1); a=0.3; b=255/(log(1+255*a)); Tg=(1/a)*(exp(g/b)-1); Tg=mat2gray(Tg)*255; subplot(2,2,1), imshow(uint8(g)), title('original image') subplot(2,2,2), imshow(uint8(tg)), title('exp Transformed image') subplot(2,2,3), imhist(uint8(g)), title('original image histogram') subplot(2,2,4), imhist(uint8(tg)), title('exp Transformed image histogram') 39
40
Histogram Equalisation g = double(imread('office.jpg')); g=g(:,:,1); a=0.3; b=255/(log(1+255*a)); Tg=(1/a)*(exp(g/b)-1); Tg=mat2gray(Tg)*255; subplot(2,2,1), imshow(uint8(g)), title('original image') subplot(2,2,2), imshow(uint8(tg)), title('exp Transformed image') subplot(2,2,3), imhist(uint8(g)), title('original image histogram') subplot(2,2,4), imhist(uint8(tg)), title('exp Transformed image histogram') %Let's try to HE the initial image g figure subplot(2,2,1), imshow(uint8(g)), title('original image') subplot(2,2,2), imshow(uint8(255*histeq(g))), title('histogram Equalised image') subplot(2,2,3), imhist(uint8(g)), title('original image histogram') subplot(2,2,4), imhist(uint8(255*histeq(g))), title('histogram Equalised Image histogram') 41
42
Notice that although Histogram Equalization gives good results there is a great amount of pixilation (i.e. areas with the same values). This can be explained by observing the sparsity of the resulting HE image. 43
Lab 7 44
Finding the Gradient of an Image I=imread('moon.tiff'); sobelx=[-1-2 -1; 0 0 0; 1 2 1]; %This is gx mask in the class notes Ix=filter2(sobelx,I); %The x derivative image of I sobely=sobelx'; %This is gy mask in the class notes Iy=filter2(sobely,I);%The y derivative image of I figure, imshow(mat2gray(sqrt(ix.^2+iy.^2))) figure, imshow(mat2gray(abs(ix)+abs(iy))) figure, imshow(ix,[]) figure, imshow(iy,[]) 45
46
Blurring an Image and then sharpening it with Isotropic Laplacian Filter 47
Blurring an Image and then sharpening it with Isotropic Laplacian Filter I=imread('moon.tiff'); BL=ones(3)/9;BI=uint8(filter2(BL,I)); figure, imshow(bi); %BI is a blurred version of the original image L=ones(3);L(2,2)=-8; LI=filter2(L,BI); %The Laplacian Image of the BLURRED IMAGE RI=mat2gray(mat2gray(BI)-mat2gray(LI)); %Is the sharp image by substructing BI-LI figure, imshow(ri) 48