Mastering Matlab II Esempi ed esercizi 22/04/2010 C.Guerrini 1 Esempio 1 Questo esempio crea un montaggio da una serie di immagini contenute in 10 file. Il montaggio ha due righe e 10 colonne. Si possono usare I parametri riguardanti Il DisplayRange per evidenziare le strutture nell immagine. filefolder = fullfile(matlabroot,'toolbox','images','imdemos'); diroutput = dir(fullfile(filefolder,'at3_1m4_*.tif')); filenames = {diroutput.name}' montage(filenames, 'Size', [2 5]); figure, montage(filenames, 'Size', [2 5],... 'DisplayRange', [75 200]); 22/04/2010 C.Guerrini 2 Mastering Matlab II 1
Creazione di un movie da immagini tif %% crea un movie leggo delle immagini tif % presenti nel toobox image/imdemos filefolder = fullfile(matlabroot,'toolbox','images','imdemos'); diroutput = dir(fullfile(filefolder,'at3_1m4_*.tif')); filenames = {diroutput.name}' [m,n]=size(filenames); for i=1:m simage(:,:,i)=imread(filenames{i} ); %% Record the movie for j = 1:m imshow(simage(:,:,j)) F(j) = getframe; [h, w, p] = size(f(1).cdata); % use 1st frame to get dimensions hf = figure; %% resize figure based on frame's w x h, and place at (150, 150) set(hf, 'position', [150 150 w h]); axis off %% tell movie command to place frames at bottom left movie(hf,f,10,30,[0 0 0 0]); 22/04/2010 C.Guerrini 3 Esempio 2 L esempio sottostante mostra come personalizzare il numero di immagini Per creare un montaggio. % Create a default montage. load mri montage(d, map) % Create a new montage containing only the first 9 images. figure montage(d, map, 'Indices', 1:9); 22/04/2010 C.Guerrini 4 Mastering Matlab II 2
GCF (get current figure) Gestione della figura corrente, in questo ambiente si fa largo uso delle strutture e dei cell-array axes; set(gca,'color','b') Con la seguente istruzione si cambiano i colori delle linee in black. plot(peaks) set(findobj('type','line'),'color','k') 22/04/2010 C.Guerrini 5 Si possono definire delle proprietà in una struttura per organizzare meglio il codice. Le seguenti istruzioni definiscono una struttura chiamata active, che contiene un insieme di definizioni di proprietà utilizzate dal uicontrol objects in una particolare figura. Quando questa figura diventa figura corrente MATLAB cambia i colori e abilita il controllo active.backgroundcolor = [.7.7.7]; active.enable = 'on'; active.foregroundcolor = [0 0 0]; if gcf == control_fig_handle set(findobj(control_fig_handle,'type','uicontrol'),active) 22/04/2010 C.Guerrini 6 Mastering Matlab II 3
Lettura di un file qcif % qcif_read clear all;close all; % parameters filename='suzie.qcif'; frames=150; col=176; row=144; UV_ratio=2; UV_col=col/UV_ratio; UV_row=row/UV_ratio; Y=zeros(row,col,frames); % open file Y_size=col*row; U_size=UV_col*UV_row; V_size=UV_col*UV_row; buffer_size=y_size+u_size+v_size; [fid,message]=fopen(filename,'r'); for ith_frame=1:1:frames [raw_yuv,count]=fread(fid,buffer_size,'uint8'); temp_y_frame=reshape(uint8(raw_yuv(1:y_size)),[col row])'; temp_u_frame=reshape(uint8(raw_yuv(y_size+1:y_size+u_size)),[uv_col UV_row])'; temp_v_frame=reshape(uint8(raw_yuv(y_size+u_size+1:y_size+u_size+v_size)),[uv_col UV_row])'; subplot(3,1,1); imshow(temp_y_frame,[0 255]); title('y'); subplot(3,1,2); imshow(temp_u_frame,[0 255]); title('u'); subplot(3,1,3); imshow(temp_v_frame,[0 255]); title('v'); xlabel(strcat(num2str(ith_frame),'th frame. Press any key to show next')); Y(:,:,ith_frame)=temp_Y_frame; pause status=fclose(fid); 22/04/2010 C.Guerrini 7 Utilizzate il sito Stanford Center for Image Systems Engineering http://scien.stanford.edu/labsite/scien_test_images_videos.php Per recuperare un video-sequence memorizzato in formato raw Concatenato con UV componenti sottocampionate (formato qcif) (utilizza la function qcif_read.m) Esercizio sull uso delle strutture leggere un filmato qcif scritto in formato YUV E convertirlo in RGB Utilizzando la tabella di conversione che puoi trovare in internet per esempio in http://www.fourcc.org/fccyvrgb.php Costruisci un video a colori. 22/04/2010 C.Guerrini 8 Mastering Matlab II 4
Creazione di un filmato % prova movieric.m % Record the movie [m,n,mz]=size(y); for j = 1:mz imshow(uint8(y(:,:,j)),[]); F(j) = getframe; % Play the movie two times movie(f,2) 22/04/2010 C.Guerrini 9 Mastering Matlab II 5