Download - Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

Transcript
Page 1: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

Laboratorio di Immagini

Esercitazione 1:

Introduzione a MATLAB

Mauro Zucchelli09/03/2016

Page 2: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

Cos’è MATLAB?

Page 3: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

Cos’è MATLAB?

● MATLAB non è un linguaggio di programmazione

Page 4: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

Cos’è MATLAB?

● MATLAB non è un linguaggio di programmazione

● MATLAB è un ambiente per il calcolo numerico, al cui

interno contiene un linguaggio di programmazione

proprietario

Page 5: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

Cos’è MATLAB?

● MATLAB non è un linguaggio di programmazione

● MATLAB è un ambiente per il calcolo numerico, al cui

interno contiene un linguaggio di programmazione

proprietario

● MATLAB è disponibile sia per sistemi Unix (Mac Os e

Linux) sia per Windows

Page 6: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

● MATLAB è la contrazione delle parole MATrix

LABoratory

Page 7: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

● MATLAB è la contrazione delle parole MATrix

LABoratory

● Come si evince dal nome, MATLAB è stato progettato

per il calcolo matriciale ed è particolarmente efficiente

nello svolgimento di questi calcoli

Page 8: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

L’interfaccia di MATLAB

Page 9: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

L’interfaccia di MATLAB

COMMAND WINDOW

Page 10: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

L’interfaccia di MATLAB

WORKSPACE

Page 11: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

L’interfaccia di MATLAB

COMMAND HISTORY

Page 12: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

Dichiarazione di una variabile:

Le variabili in MATLAB non vanno dichiarate con il “tipo”

Page 13: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

MATLAB è tipizzato implicitamente (a volte è più un problema che un vantaggio)

Page 14: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

La stessa variabile può essere riciclata

Page 15: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

Operazioni base:

Page 16: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

Operazioni base:

Il punto e virgola sopprime l’output!

Page 17: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

Costrutto “if”

Page 18: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi base

Costrutto “for”

Page 19: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

In MATLAB ci sono vari modi per dichiarare un array

Page 20: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

● In MATLAB gli array partono da 1● Per accedere si usano le parentesi tonde

Page 21: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

● L’operatore “:” permette di accedere a più valori

Page 22: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

● Operazioni tra array (controllate la dimensione)

Page 23: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

● L’apice “traspone” gli array (e le matrici)

Page 24: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

● L’operatore ‘*’ di default indica il prodotto vettoriale!

1 X 3 * 3 X 1 = 1 X 1

3 X 1 * 1 X 3 = 3 X 3

Page 25: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: array

● Usate “ .* ” per il prodotto tra elementi

Page 26: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: matrici

Page 27: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: matrici

● Per MATLAB le matrici funzionano esattamente come gli array

● Di fatto gli array sono matrici 1XN (o NX1 )

Page 28: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: matrici

Vale sempre la regola del prodotto: l’operatore “ * “ indica il prodotto di matrici.

Page 29: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi utili

● il comando “length” ritorna la lunghezza di un vettore o il numero di righe di una matrice

● Il comando “size” ritorna il numero di righe e colonne

Page 30: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi utili

Page 31: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi utili

MATLAB ha già implementato praticamente tutte le funzioni matematiche

Page 32: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: comandi utili

MATLAB ha già implementato praticamente tutte le funzioni matematiche

Page 33: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: funzioni

In MATLAB possiamo definire le nostre funzioni in file con l’estensione .m

Page 34: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: funzioni

In MATLAB possiamo definire le nostre funzioni in file con l’estensione .m

STESSO NOME

Page 35: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: funzioni

In MATLAB possiamo definire le nostre funzioni in file con l’estensione .m

RETURN IMPLICITO

Page 36: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: attenzione

MATLAB è super-ottimizzato per le operazioni matriciali, non usiamo i clicli se non strettamente necessario!

Page 37: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: attenzione

MATLAB è super-ottimizzato per le operazioni matriciali, non usiamo i clicli se non strettamente necessario!

La funzione sum di MATLAB è 72 volte più veloce di my_sum

Page 38: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: plot

Page 39: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: plot

Page 40: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: multiple plots: “hold on”

Page 41: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: multiple plots: “subplot”

Page 42: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: histograms

Page 43: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: histograms

Page 44: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB

LE IMMAGINI

Page 45: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

● MATLAB vede le immagini come matrici:

○ Immagini in bianco e nero (scala di grigi) come

matrici NXM;

○ Immagini a colori come matrici NXMX3 (RGB);

Page 46: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

● MATLAB vede le immagini come matrici:

○ Immagini in bianco e nero (scala di grigi) come

matrici NXM;

○ Immagini a colori come matrici NXMX3 (RGB);

Page 47: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

Page 48: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

Page 49: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

Page 50: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

Page 51: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

Page 52: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: le immagini

Page 53: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: compiti per oggi

Disegnare in una figure 4 immagini 512X512

rappresentanti:

● Un quadrato rosso su sfondo bianco

● Un quadrato blu su sfondo nero

● Un quadrato verde su sfondo nero

● Un quadrato nero su sfondo giallo

Page 54: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: compiti per oggi

Page 55: Esercitazione 1: Introduzione a MATLABprofs.scienze.univr.it/~zucchelli/Esercitazione_1/Es_lab_img_01.pdf · Introduzione a MATLAB Mauro Zucchelli 09/03/2016. MATLAB Cos’è MATLAB?

MATLAB: compiti per oggi