ΤΕΧΝΟΛΟΓΙΚΟ ΕΚΠΑΙΔΕΥΤΙΚΟ ΙΔΡΥΜΑ ΚΡΗΤΗΣ ΔΠΜΣ ΠΡΟΗΓΜΕΝΑ ΣΥΣΤΗΜΑΤΑ ΠΑΡΑΓΩΓΗΣ ΑΥΤΟΜΑΤΙΣΜΟΥ ΚΑΙ ΡΟΜΠΟΤΙΚΗΣ ΕΡΓΑΣΙΑ 2 ADVANCES IN DIGITAL AND COMPUTER VISION ΣΠΟΥΔΑΣΤΕΣ: ΓΕΡΜΕΝΗΣ ΕΥΑΓΓΕΛΟΣ (Α.Μ.: ΜΗ77) ΠΑΠΑΔΟΠΟΥΛΟΣ ΓΕΩΡΓΙΟΣ (Α.Μ.:ΜΗ79) ΕΠΙΒΛΕΠΩΝ ΚΑΘΗΓΗΤΗΣ: ΑΛΕΞΑΝΔΡΟΣ ΜΑΚΡΗΣ ΗΡΑΚΛΕΙΟ 2016
Exercise 1 - Filters (20%) Experiment with the imfilter Matlab function using images of your choice. Present some (3-4) image filtering examples. For each example show which filter type you used and explain the purpose of filtering with that filter (e.g. smoothing, sharpening, edge). For each filter type use different parameter values (e.g. filter size, Gaussian filter standard deviation) and comment on how these parameters affect the result. To create the filter kernel creation you can use the built-in fspecial function or directly set the appropriate matrix. Χρησιμοποιήθηκαν τέσσερα διαφορετικά είδη φίλτρων για να γίνει κατανοητή η λειτουργία και η δομή τους. Σε πρώτη φάση κατασκευάσαμε φίλτρο box (Εικόνα 1.1) που δημιουργεί blur effect g = ones (5,5)/25 το οποίο εξομαλύνει της υψηλές μεταβολές στην φωτεινότητα παίρνοντας τον μέσο όρο των γειτονικών pixel. Έπειτα δημιουργήθηκε φίλτρο για Sharpening (Εικόνα 1.2) με διαστάσεις 9x9,κεντρικό pixel 10/9 και η γειτονία του 1/9. Εν συνεχεία εφαρμόστηκε ένα φίλτρο Gaussian [18x18] με σ=2.5 ώστε να δοκιμάσουμε την απόκριση του σε θόρυβο salt&pepper (Εικόνα 1.3) σε σχέση μ ένα median φίλτρο για τον ίδιο θόρυβο. Είναι φανερό ότι το median φίλτρο είναι ιδανικό για salt&pepper θόρυβο (Εικόνα 1.4) ενώ αντιθέτως στο Gaussian φίλτρο παραμένει λίγος θόρυβος. Συγχρόνως στην Gaussian εικόνα υπάρχει μεταβολή στην πληροφορία της και πιο συγκεκριμένα κάποια edges μπορεί να χαθούν. Παρακάτω αποδεικνύετε αυτό χρησιμοποιώντας ένα Sobel φίλτρο για να εντοπίσουμε τις ακμές για την ίδια εικόνα (Εικόνα 1.5) βλέποντας ότι χάνει κάποια features η Gaussian σε σχέση με τη median. Εν τέλει κάνουμε δοκιμή του Sobel φίλτρου (Εικόνα 1.6) για να δούμε ξεκάθαρα την λειτουργία του διότι ο sobel εντοπίζει ακμές ως προς Y ή ως προς Χ διεύθυνση αναλόγως την ακμή που θέλουμε να τονίσουμε κάθε φορά. 2
Αναλυτικά στον κώδικα: %---Blur filtro--- g=ones(5,5)/25; Imblur=imfilter(I,g); figure;imshow(imblur); title('blur Image Filter [box=ones(5,5)/25]'); %---sharp filter--- h=zeros(9,9); h(5,5)=10; h=h-ones(9)/9; sharp=imfilter(i,h); figure;imshow(sharp); title('sharp'); %---Gaussian/salt&pepper noise--- J = imnoise(i,'salt & pepper',0.02); & Noise creation h2=fspecial('gaussian',[18 18],2.5); Imgaus=imfilter(J,h2); figure;imshow(imgaus); title('salt&pepper noise / Gausian 18X18 σ=2.5'); %---Median/salt&pepper noise--- K = medfilt2(j); figure;imshowpair(j,k,'montage'); title('salt&pepper after median filter') %---sobel filter--- h3=fspecial('sobel'); Imsobx=imfilter(I3,h3); Imsoby=imfilter(I3,h3'); figure;imshowpair(imsobx,imsoby,'montage'); title('sobel Y Sobel X') %---Median Vs Gaus edges detection--- ImsobG=imfilter(Imgaus,h3'); ImsobK=imfilter(K,h3') figure;imshowpair(imsobk,imsobg,'montage'); title('median edges Vs Gaussian edges') 3
Εικόνα 1.1 4
Εικόνα 1.2 5
Εικόνα 1.3 6
Εικόνα 1.4 Εικόνα 1.5 7
Εικόνα 1.6 8
Exercise 2 Edge Detection (40%) The aim of this exercise is to detect the edges with a given direction in an image. To that end create a function [ E ] = oriented_edges( I, thr, a, da ) that takes as input a double grayscale image Ι, a threshold value thr, a direction a, and an angle da. The output of the fuction should be a binary image Ε where the pixels that meet the following requirements should have the value 1: The pixel intensity gradient is higher than thr. The gradient direction (in rad) is inside the interval: (a-da,a+da) For the gradient calculation use the imfilter with an appropriate filter (e.g. Sobel). Apply the function using different values for the parameters (thr, a, da) on images of your choice (e.g. <shapes.tiff>) and present the results along with comments on how the parameters affect the output. Δημιουργήσαμε συνάρτηση oriented_edges η οποία έχει ως input εικόνα Ι, ένα όριο threshold,διεύθυνση a σε μοίρες και ένα διάστημα ± da γύρω από τα a. Χρησιμοποιώντας το φίλτρο sobel υπολογίσαμε τις μερικές παραγώγους κατά (x,y) διευθύνσεις. Έπειτα υπολογίσαμε το μέτρο του Gradient (magnit) και την διεύθυνση του θ (Theta). Εν τέλει ως output της συνάρτησης παίρνουμε το [ Ε ],τις ακμές δηλαδή στην ζητούμενη διεύθυνση κάθε φορά. Αναλυτικά στον κώδικα: function [ E ] = oriented_edges(i,thr,a,da) I=double(I) % Μετατροπή σε double για μαθηματικές πράξεις sobel=fspecial('sobel'); % Δημιουργία sobel φίλτρου s=size(i); sobelx=imfilter(i,sobel); % Μερική παράγωγος ως προς Χ sobely=imfilter(i,sobel'); % Μερική παράγωγος ως προς Υ Theta=atan(sobelY./sobelX); % Υπολογισμός γωνίας θ Gradient magnit=sqrt((sobely.^2)+(sobelx.^2)) % Υπολογισμός μέτρου Gradient E=zeros(s(1),s(2)); % Δημιουργία νέας εικόνας Ε % degrees 2 rad a=deg2rad(a) da=deg2rad(da) E=((magnit>thr) & ((a-da)<theta) & ((a+da)>theta)); % Καθορισμός Ακμών End 9
Παρακάτω παρουσιάζονται δοκιμές για διάφορα threshold και a διευθύνσεις. [shapes.tiff] E=oriented_edges(I,20,45,45); figure;imshowpair(i,e,'montage') E=oriented_edges(I,300,45,45); figure;imshowpair(i,e,'montage') E=oriented_edges(I,200,15,15); figure;imshowpair(i,e,'montage') 10
E=oriented_edges(I,200,30,0.1*180); figure;imshowpair(i,e,'montage') E=oriented_edges(I,200,30,0.2*180); figure;imshowpair(i,e,'montage') Παρατηρούμε ότι για μεγάλο threshold η συνάρτηση θέτει αυστηρά όρια για την δημιουργία ακμών και έτσι φαίνεται ότι κάποια σχήματα χάθηκαν τελείως στη διεύθυνση. Στις τελευταίες περιπτώσεις αυξήσαμε σταδιακά το διάστημα da γύρω απ το a και πήραμε ακμές σταδιακά αλλά και από διαφορετικές διευθύνσεις. 11
Exercise 3 Dice (40%) The aim of this exercise is to detect dice of a known size and orientation in an image. The method should also be able to recognize the number on each detected dice. The assumption is that the dice in the image are almost parallel with the image axes (e.g. <dice_01.tiff>). For the recognition you are free to use corner and edge detection techniques, for instance you can use built-in Matlab functions. Edge detection will be useful to find the contour of each dice while corner detection on the appropriate scale will provide its number. Visualize the results by superimposing on the initial image the centers of each detected dice and its number. For the initial tests of the system use the simpler image provided: <dice_01.tiff>. Once the system's performance is verified with that image continue your tests with the rest of the provided images or other images of your choice containing dice and comment on the results. For each experiment provide and comment on the parameter values that you used to detect the dice. If your system fails try to explain why. The program should be properly structured using functions for each sub-problem. One possible strategy that you could use is the following: Edge detection to obtain the edge image Ε using the edge Matlab function. Creation of a filter kernel Β that if applied on E will output high response on the location of the possible dice centers. Filtering of the edge image E (after converting it to double) with the filter Β followed by thresholding to obtain a binary image, D, with the possible center locations. Localization of the centroid of each connected component of D using the function regionprops. The output S.Centroid are the possible dice center locations. To convert the output of regionprops to a matrix and visualize the result you can use the following lines of code: centroids = cat(1, S.Centroid); imshow(i); hold on; plot(centroids(:,1), centroids(:,2), 'g*'); hold off; Localization of the corners C at the appropriate scale using the built-in corner function (to adjust the scale change the function's parameter FilterCoefficients). The scale is correct if each dice dot is detected as a single corner.xercise 3 Σε πρώτη φάση υπολογίσαμε από την εικόνα το κατάλληλο μήκος πλευράς του ζαριού στην εικόνα dice_01.tiff. Έπειτα με την χρήση του αλγόριθμου Canny εντοπίσαμε της ακμές των ζαριών δημιουργώντας μια νέα εικόνα Ε. Στη συνέχεια κατασκευάσαμε το κατάλληλο τετραγωνικό (pxp) φίλτρο Β εισάγοντας την πληροφορία της πλευράς του ζαριού. Στο τετραγωνικό φίλτρο Β δημιουργήσαμε περιφέρεια = 1 και πάχος w = 2. Στο επόμενο βήμα δημιουργήσαμε την νέα φιλτραρισμένη D εικόνα και με την βοήθεια της regionprops με βάση την πληροφορία της D εικόνας και των centroids(κέντρα μαζών) εντοπίσαμε τα κέντρα των ζαριών στην πρώτη εικόνα Ι (dice_01.tiff). 12
Εν τέλει χρησιμοποιώντας τον αλγόριθμο Harris και εντοπίζοντας όλες τις πιθανές γωνίες (πίνακας c/ matlab) της εικόνας και συγχρόνως με την πληροφορία των συντεταγμένων των centroids κάναμε έλεγχο για γωνίες σε κοντινές περιοχές απ τα κέντρα μαζών. Έτσι καταλήξαμε στις ιδανικές αποστάσεις που μας δώσανε τον αριθμό γωνιών ίσο με το άθροισμα των αριθμών των ζαριών συγκρατώντας συγχρόνως και την πληροφορία με τις συντεταγμένες τους (πίνακας C / matlab). Παρουσιάσαμε με κόκκινο χρώμα όλες τις γωνίες πάνω στη Ι εικόνα και σ ένα array τον αριθμό του κάθε ζαριού ξεχωριστά. Αναλυτικά στον κώδικα: clear all close all clc p=55; %plevra zariou apo eikona w=2; % paxos filtrou I=imread('dice_01.tiff'); I=I(:,:,1); E=edge(I); %entopismos akmwn figure;imshow(e) B=zeros(p); % filtro diastasewn zariou ( p x p ) B(1:w,:)=1; B((p-(w-1)):p,:)=1;B(:,1:w)=1;B(:,(p-(w- 1)):p)=1;%Dimiourgia periferias filtrou + paxos w figure;imshow(b) E=double(E); D=imfilter(E,B); %Filtrarismenis eikona Edge me B filtro megethos zariou D=D>130;%Entopismos pithanwn kentrwn figure;imshow(d); s=regionprops(d); %--------Evresi Kentrou mazas------- centroids=cat(1,s.centroid); figure; imshow(i); hold on; plot(centroids(:,1),centroids(:,2),'g*'); hold off; %--------%entopismos gwniwn me algorithmo Harris------- c=corner(i,'harris','filtercoefficients',fspecial('gaussian',[9 1],2.5)); figure; imshow(i); hold on; 13
i=0; Xcorner=c(:,1); Ycorner=c(:,2); Xcenter=centroids(:,1); Ycenter=centroids(:,2); for q=1:length(centroids) n=0; for j=1:length(c) if (abs(xcorner(j)-xcenter(q))<20) & (abs(ycorner(j)- Ycenter(q))<20) %elegxos gwniwn mesa stin periferia tou zariou i=i+1; % taksinomitis sintetagmenon voulas kathe zariou C(i,1)=Xcorner(j); C(i,2)=Ycorner(j); n=n+1; %metrhths arithmou zariou end end Dice_number(q)=n; %arithmos zariwn (k) kai noumero kathe zariou (n) se array (1,k) end Dice_number plot(c(:,1), C(:,2), 'r*'); hold off; Παρακάτω βλέπουμε τα αποτελέσματα : Στη συνέχεια βλέπουμε όλα τα στάδια της εικόνας Ι 14
Εφαρμόσαμε την ίδια μέθοδο και στης υπόλοιπες εικόνες με ζάρια. Για την dice_02.jpg δοκιμάσαμε να περάσουμε ένα sharp φίλτρο για να βοηθήσουμε τον αλγόριθμο του Canny να βρει περισσότερα edges. Δώσαμε πλευρά ζαριού p=80, πάχος φίλτρου w=10 και διαφορετικά όρια για εντοπισμό γωνίας κοντά στα κέντρα έχουμε: Εντοπίζει τα 6 ζάρια από τα 7 της εικόνας αλλά λόγω του ότι είναι τοποθετημένα υπό γωνία δεν βοηθάει το φίλτρο που χρησιμοποιούμαι. Επίσης φαίνονται και άλλες πλευρές του ζαριού που σημαίνει ότι για κοντινές αποστάσεις απ τα κέντρα θα συμπεριλαμβάνονται γωνίες και από τις άλλες πλευρές. Άλλο ένα πρόβλημα είναι ότι τα black dots πάνω στα ζάρια βρίσκονται πολύ κοντά μεταξύ τους οπότε αναγνωρίζει πολλά corners. 15
Αποτελέσματα dice_02.jpg 16
Για την εικόνα dice_06.jpg το πρόβλημα χρειάζεται διαφορετικό τρόπο προσέγγισης όμως εντοπίστηκαν τα ζάρια με την τιμή 1 σε κατάλληλη ρύθμιση. Για p=14, πάχος φίλτρου w=2 και για αυστηρά όρια για εντοπισμό γωνίας κοντά στα κέντρα έχουμε: 17