Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital...

48
Εργαστήριο ADICV2 Labs 2-6 Image filtering Κώστας Μαριάς

Transcript of Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital...

Page 1: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

Εργαστήριο ADICV2Labs 2-6

Image filtering

Κώστας Μαριάς

Page 2: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

Image Filtering

ADICV Kostas Marias TEI Crete 2017 2

Page 3: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

3Kostas Marias Digital Image Processing Lectures

Σκοπός εργαστηρίου

Θα φτιάξουμε ένα ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ (mean

FILTER) σε matlab

Στη συνέχεια θα το συγκρίνουμε με το έτοιμο

ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ που υπάρχει στη matlab.

Τέλος θα συγκρίνουμε τα δυο φίλτρα και θα

βρούμε πόσο αποκλίνουν υπολογίζοντας το

σφάλμα ανα pixel μεταξύ του δικού μας φλιτρου

και της matlab.

Basic Matlab

Page 4: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

4Kostas Marias Digital Image Processing Lectures

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

0 0

0 0

0 0

0 0 0 0 0

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

• f(x,y)=f(2,2)=11

• Νέα τιμή g(x,y)=T[f(x,y)] = 11/9 + 61/9 + 31/9 + 21/9 + 111/9 + 31/9

+ 51/9 + 101/9 + 61/9 = 47/9 = 5.222

5

Basic

Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα

Αρχική εικόνα: Εικόνα φιλτραρισμένη με

3x3 φίλτρο ομαλοποίησης

(x,y)

Page 5: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

5Kostas Marias Digital Image Processing Lectures

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

0 0

0 0

0 0

0 0 0 0 0

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

• Νέα τιμή = 11/9 + 61/9 + 31/9 + 21/9 + 111/9 + 31/9

+ 51/9 + 101/9 + 61/9 = 47/9 = 5.222

5

Basic

Αρχική εικόνα: Εικόνα φιλτραρισμένη με

3x3 φίλτρο ομαλοποίησης

Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα

Page 6: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

6Kostas Marias Digital Image Processing Lectures

Αρχική εικόνα:

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

Εικόνα φιλτραρισμένη με

3x3 φίλτρο ομαλοποίησης

0 0 0 0 0

0 0

0 0

0 0

0 0 0 0 0

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

• Νέα τιμή = 61/9 + 31/9 + 21/9 + 111/9 + 31/9 + 101/9

+ 101/9 + 61/9 + 91/9 = 60/9 = 6.667

5 7

Basic

Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα

Page 7: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

7Kostas Marias Digital Image Processing Lectures

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

0 0

0 0

0 0

0 0 0 0 0

5 7 5

5

5

5 6

64

• Στην επόμενη διαφάνεια θα δούμε την επίδραση

(ομαλοποίηση) του 3x3 φίλτρου αυτού σε μια εικόνα.

Basic

Αρχική εικόνα: Εικόνα φιλτραρισμένη με

3x3 φίλτρο ομαλοποίησης

Παράδειγμα Επεξεργασίας Εικόνας με χωρικά φίλτρα

Page 8: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

8Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ (mean FILTER)

Η λειτουργία του φίλτρου μέσης τιμής συνίσταται

με την αντικατάσταση της φωτεινότητας σε κάθε

εικονοστοιχείο με τη μέση φωτεινότητα σε μια

γειτονιά του.

Είναι Χαμηλοπερατά (lowpass) φίλτρα μιας και

αντικαθιστούμε τη τιμή του pixel με τη μέση τιμή

της γειτονιάς του οπότε και μειώνουμε βαθμιαία

απότομες αλλαγές στην ένταση των pixels.

Ενώ μειώνουμε των τυχαίο θόρυβο όμως χάνουμε

συνήθως ευκρίνεια στις ακμές τις εικόνας (edge

blurring – θόλωμα ακμών).

Page 9: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

9Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ (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 1 1

1 1 1

1 1 1

ή 1/9

Basic

Page 10: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

10Kostas Marias Digital Image Processing Lectures

Φιλτράρισμα στο Χωρικό πεδίο

Η αρχή γραμμικού φιλτραρίσματος στο χώρο παρουσιάζεται στο σχήμα:

(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)

w(- 1, - 1) w(- 1, 0) w(- 1, 1)

w(0, - 1) w(0,0) w(0, 1)

w( 1, - 1) w(1, 0) w( 1, 1)

Εικόνα f

Συντελεστές

του φίλτρου

Pixels εικόνας

f που θα

φιλτραριστούν

y →

x →

Basic

Page 11: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

11Kostas Marias Digital Image Processing Lectures

Φιλτράρισμα στο Χωρικό πεδίο

Η αρχή γραμμικού φιλτραρίσματος στο χώρο δίνεται από τη σχέση:

f(x - 1, y - 1) f(x - 1, y) f(x - 1, y + 1)

f(x, y - 1) f(x,y) f(x, y + 1)

f(x + 1, y - 1) f(x + 1, y) f(x + 1, y + 1)

w(- 1, - 1) w(- 1, 0) w(- 1, 1)

w(0, - 1) w(0,0) w(0, 1)

w( 1, - 1) w(1, 0) w( 1, 1)

Basic

Page 12: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

12Kostas Marias Digital Image Processing Lectures

Φιλτράρισμα στο Χωρικό πεδίο

Η αρχή γραμμικού φιλτραρίσματος στο χώρο δίνεται από τη σχέση συσχέτισης:

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)

f(x - 1, y - 1) f(x - 1, y) f(x - 1, y + 1)

f(x, y - 1) f(x,y) f(x, y + 1)

f(x + 1, y - 1) f(x + 1, y) f(x + 1, y + 1)

w(- 1, - 1) w(- 1, 0) w(- 1, 1)

w(0, - 1) w(0,0) w(0, 1)

w( 1, - 1) w(1, 0) w( 1, 1)

Basic

Page 13: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

13Kostas Marias Digital Image Processing Lectures

Basic 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;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');

E. Jebamalar Leavline, D. Asir Antony Gnana Singh, OnTeaching Digital Image Processing withMATLAB, American Journal of Signal Processing, Vol. 4No. 1, 2014, pp. 7-15. doi:10.5923/j.ajsp.20140401.02.

Εφαρμογή φίλτρου mean 3x3 αφού η

αρχική εικόνα επιμολυνθεί θόρυβο

Gauss. Χρησιμοποιούμε στη Matlab

τη συνάρτηση imfilter(I,h) όπου I και

h είναι πολυδιάστατοι πίνακες της

εικόνας εισόδου και του φίλτρου.

Το 3X3 φίλτρο μέσης τιμής δεν

ανταποκρίνεται όσο καλά όσο το 5x5

το οποίο απομακρύνει καλύτερα τον

θόρυβο με κόστος όμως το περαιτέρω

θόλωμα της εικόνας.

ΦΙΛΤΡΟ ΜΕΣΗΣ ΤΙΜΗΣ παράδειγμα με Matlab

Page 14: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

14Kostas Marias Digital Image Processing Lectures

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');

Page 15: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

15Kostas Marias Digital Image Processing Lectures

Φιλτράρισμα στο Χωρικό πεδίο 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

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)

end

End

Ο ΚΩΔΙΚΑΣ ΑΥΤΟΣ ΕΊΝΑΙ ΛΑΘΟΣ !!!!!!!!!!!!!!!!!! Γιατι όμως????

Basic Matlab

Page 16: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

16Kostas Marias Digital Image Processing Lectures

Φιλτράρισμα στο Χωρικό πεδίο Matlab

Για να φτιάξουμε ένα πρόγραμμα Matlab πρέπει

να μεταφέρουμε τις ‘θεωρητικές’ συντεταγμένες

της μάσκας 3x3 σε indexing ου είναι αποδεκτό

από τη matlab (δηλ. από (1,1) έως (3,3)):

w(- 1, - 1) w(- 1, 0) w(- 1, 1)

w(0, - 1) w(0,0) w(0, 1)

w( 1, - 1) w(1, 0) w( 1, 1)

w(1,1) w(1,2) w(1,3)

w(2,1) w(2,2) w(2,3)

w( 3, 1) w(3,2) w( 3,3)

Matlab

Page 17: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

17Kostas Marias Digital Image Processing Lectures

Φιλτράρισμα στο Χωρικό πεδίο

κώδικα 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,[])

Basic Matlab

Page 18: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

18Kostas Marias Digital Image Processing Lectures

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')

Basic Matlab

Page 19: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

19Kostas Marias Digital Image Processing Lectures

Page 20: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

20Kostas Marias Digital Image Processing Lectures

Σύγκριση των 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)

Basic Matlab

Page 21: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

21Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΑ GAUSS (Θόλωση Gauss)

Είναι φίλτρα θόλωσης εικόνας που χρησιμοποιούν τη

συνάρτηση Gauss (η οποία εκφράζει την κανονική κατανομή

στη στατιστική)για να υπολογίσει τους συντελεστές του

φίλτρου για τον μετασχηματισμό κάθε pixel:

Όπου x,y είναι οι αποστάσεις από την αρχή των αξόνων και σ

είναι η τυπική απόκλιση (standard deviation) της κατανομής

Gauss.

Στις 2 διαστάσεις η εξίσωση αυτή δίνε μια επιφάνεια της

οποία τα περιγράμματα είναι ομόκεντροι κύκλοι με

Γκαουσιανη κατανομή από το κεντρικό σημείο.

𝐺 𝑥, 𝑦 =1

2𝜋𝜎2𝑒−

𝑥2+𝑦2

2𝜎2

https://en.wikipedia.org/wiki/Gaussian_blur

Basic

Page 22: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

22Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΑ GAUSS (Θόλωση Gauss)

Μια προσέγγιση στο σχεδιασμό φίλτρων Gauss

είναι να υπολογίσουμε τα βάρη της μάσκας

απευθείας από την ασυνεχή κατανομή Gauss:

Προαιρετικά μπορούμε να χρησιμοποιήσουμε μια

είναι σταθερά κανονικοποίησης c:

𝐺 𝑖, 𝑗 =1

2𝜋𝜎2𝑒−

𝑖2+𝑗2

2𝜎2

𝐺(𝑖, 𝑗)

𝑐𝐺 𝑖, 𝑗 =

1

2𝜋𝜎2𝑒−

𝑖2+𝑗2

2𝜎2

Basic

Page 23: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

23Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΑ GAUSS στη Matlab

Η εντολή

h = fspecial('gaussian', hsize, sigma)

• Δίνει ένα περιφερειακά συμμετρικό φίλτρο Gauss

(lowpass filter) μεγέθους hsize και τυπικής

απόκλισης sigma (θετικός).

• Το hsize μπορεί να έιναι ένα διάνυσμα που να

καθορίζει τον αριθμό γραμμών/στηλών στο h, η

μπορεί να είναι βαθμωτη τιμή οπότε το h θα είναι

τετράγωνος πίνακας.

Η προεπιλεγμένη τιμή για το hsize είναι [3 3]και για

το sigma 0.5.

Basic Matlab

Page 24: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

24Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΑ 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

Basic Matlab

Page 25: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

25Kostas Marias Digital Image Processing Lectures

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');

Page 26: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

26Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΑ GAUSS στη MatlabMatlab

Page 27: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

27Kostas Marias Digital Image Processing Lectures

ΦΙΛΤΡΑ GAUSS στη MatlabMatlab

Page 28: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

28Kostas Marias Digital Image Processing Lectures

Lab 5

Filter a noisy image with different methods

Page 29: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

29Kostas Marias Digital Image Processing Lectures

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')

Page 30: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

30Kostas Marias Digital Image Processing Lectures

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)

Page 31: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

31Kostas Marias Digital Image Processing Lectures

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')

Page 32: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

32Kostas Marias Digital Image Processing Lectures

a=0.5

Page 33: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

33Kostas Marias Digital Image Processing Lectures

a=0.05

Page 34: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

34Kostas Marias Digital Image Processing Lectures

Lab 6

Page 35: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

35Kostas Marias Digital Image Processing Lectures

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')

Page 36: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

36Kostas Marias Digital Image Processing Lectures

Page 37: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

37Kostas Marias Digital Image Processing Lectures

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')

Page 38: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

38Kostas Marias Digital Image Processing Lectures

Page 39: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

39Kostas Marias Digital Image Processing Lectures

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')

Page 40: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

40Kostas Marias Digital Image Processing Lectures

Page 41: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

41Kostas Marias Digital Image Processing Lectures

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')

Page 42: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

42Kostas Marias Digital Image Processing Lectures

Page 43: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

43Kostas Marias Digital Image Processing Lectures

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.

Page 44: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

44Kostas Marias Digital Image Processing Lectures

Lab 7

Page 45: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

45Kostas Marias Digital Image Processing Lectures

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,[])

Page 46: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

46Kostas Marias Digital Image Processing Lectures

Page 47: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

47Kostas Marias Digital Image Processing Lectures

Blurring an Image and then sharpening it with Isotropic Laplacian Filter

Page 48: Εργαστήριο ADICV2 Labs 2-6 · ADICV Kostas Marias TEI Crete 2017 2. Kostas Marias Digital Image Processing Lectures 3 Σκοπός ργαηρίου ...

48Kostas Marias Digital Image Processing Lectures

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)