PDI Aula de Suporte a MATLAB
description
Transcript of PDI Aula de Suporte a MATLAB
Tópicos Integradores (PDI) Suporte a MATLAB
Prof. João Marcelo M. da Silva
2015.2
Sumário
• Comandos (ginput, plot e save)
• Sistema de Janelas no MATLAB
Sumário
• Comandos (ginput, plot e save)
• Sistema de Janelas no MATLAB
• Deseja-se um programa que:
• adquira pontos da tela pelo mouse;
• mostre o local onde os pontos foram
adquiridos;
• salve estes pontos.
Objetivo
• Resultado esperado.
Resultado
• ginput – Graphical input from mouse.
• [x,y] = ginput(n) enables you to select n
points from the current axes and returns the x- and y-coordinates in the column vectors x and
y, respectively. Press the Return key to
terminate the input before entering n points.
Comando: ginput
• ginput – Graphical input from mouse.
• [x,y] = ginput gathers an unlimited
number of points until you press the Return
key.
• [x,y,button] = ginput(...) returns the
x-coordinates, the y-coordinates, and the button or key designation. button is a vector
of integers indicating which mouse buttons you
pressed (1 for left, 2 for middle, 3 for right), or
ASCII numbers indicating which keys on the
keyboard you pressed.
Comando: ginput
• Exemplo:
I = imread('eight.tif');
imshow(I);
[x,y] = ginput;
Comando: ginput
• plot – 2-D line plot.
• plot(Y) plots the columns of Y versus their
index.
• plot(X1,Y1,...) plots all lines defined by
Xn versus Yn pairs.
• plot(X1,Y1,LineSpec,...) plots all lines
defined by the Xn,Yn,LineSpec triples, where
LineSpec is a line specification that
determines line type, marker symbol, and color
of the plotted lines.
Comando: plot
• LineSpec – Line specification string syntax.
Comando: plot
Specifier Line Style - Solid line (default)
-- Dashed line : Dotted line -. Dash-dot line
Specifier Marker Type
+ Plus sign
o Circle
* Asterisk
. Point
x Cross
'square' or s Square
'diamond' or d Diamond
^ Upward-pointing triangle
v Downward-pointing triangle
> Right-pointing triangle
< Left-pointing triangle
'pentagram' or p Five-pointed star (pentagram)
'hexagram' or h Six-pointed star (hexagram)
Specifier Color r Red
g Green b Blue c Cyan
m Magenta y Yellow
k Black w White
• Exemplo: I = imread('eight.tif');
imshow(I);
[x,y] = ginput;
%exibição dos contornos
x(end+1)=x(1);
y(end+1)=y(1);
hold on
plot(x,y,'-b');
plot(x,y,'xr');
Comando: plot
• save filename stores all variables in the
current workspace in the file filename.
• save filename content options
stores only those variables specified by content in file filename, also applying
the specified options.
• Example: save 'pts_xy.txt' x y -ASCII;
save('pts_xy.txt','x','y','-ASCII');
Comando: save
• Format Options:
Comando: save
MAT-file format Options How Data Is Stored
-ascii Save data in 8-digit ASCII format.
-ascii -tabs Save data in 8-digit ASCII format delimited with tabs.
-ascii -double Save data in 16-digit ASCII format.
-ascii -double -tabs Save data in 16-digit ASCII format delimited with tabs.
-mat Binary MAT-file form (default).
• Exemplo:
clear, clc
disp('=========================')
disp('<-- PROGRAMA CONTORNO -->')
disp('========================='), disp(' ')
nome=input('Entre com o nome da imagem (ex:
eight.tif) --> ','s'); disp(' ')
I = imread(nome); imshow(I);
disp('Marque os pontos na imagem e para
finalizar, pressione a tecla "Esc" ...'),
disp(' ')
Comandos: ginput, plot e save
• Exemplo (continuação):
hold on, k=0; b=0;
while b~=27 %tecla “Esc”
k=k+1;
[x(k),y(k),b]=ginput(1);
if b~=27
plot(x(k),y(k),'+r');
end
end
x(k)=x(1); y(k)=y(1);
plot(x,y,'-b'), hold off;
Comandos: ginput, plot e save
• Exemplo (continuação):
x(k)=[]; y(k)=[];
pts_xy = 'pts_xy.txt';
save 'pts_xy.txt' x y -ASCII;
disp(['Pontos salvos em ' '"' pts_xy
'"']), disp(' ')
disp(' <-- FIM -->')
disp('=========================')
Comandos: ginput, plot e save
• Exercícios:
• Sabe-se que o centróide de um polígono é
calculado pelo valor médio das coordenadas.
Calcule o centróide dos polígonos gerados.
• Calcule o perímetro do polígono.
Comandos: ginput, plot e save
Sumário
• Comandos (ginput, plot e save)
• Sistema de Janelas no MATLAB
Programas com “Menu”
• Estrutura: Menu main_handle = figure('numbertitle','off','name','HistDoc','menubar','none');
imshow('hist_doc.jpg');
file = uimenu('label','File');
uimenu(file,'label','Open Image','callback','open_im','Accelerator','O');
uimenu(file,'label','Save Image','callback','save_im','Accelerator','S');
uimenu(file,'label','Save as','callback','save_as');
...
edit = uimenu('label','Edit');
uimenu(edit,'label','Undo/Redo','callback','undo_redo_im','Accelerator','Z');
...
transformation = uimenu('label','Transformation');
uimenu(transformation,'label','RGB to Gray','callback','rgb_gray');
...
help_histdoc = uimenu('label','Help');
uimenu(help_histdoc,'label','Help','callback','help_histdoc');
uimenu(help_histdoc,'label','About','callback','about_histdoc');
Programas com “Menu”
• Estrutura: Toolbar [icon1,map1] = imread('zoomicon.gif'); icon1 = ind2rgb(icon1,map1);
[icon2,map2] = imread('zoomouticon.gif'); icon2 = ind2rgb(icon2,map2);
[icon3,map3] = imread('panicon.gif'); icon3 = ind2rgb(icon3,map3);
[icon4,map4] = imread('openicon.gif'); icon4 = ind2rgb(icon4,map4);
[icon5,map5] = imread('saveicon.gif'); icon5 = ind2rgb(icon5,map5);
[icon6,map6] = imread('undoicon.gif'); icon6 = ind2rgb(icon6,map6);
TOOLbar = uitoolbar(main_handle);
uipushtool(TOOLbar,'CData',icon4,'TooltipString','Open','ClickedCallback','open_im');
uipushtool(TOOLbar,'CData',icon5,'TooltipString','Save','ClickedCallback','save_im');
...
clear icon1 icon2 icon3 icon4 icon5 icon6 map1 map2 map3 map4 map5 map6;
Programas com “Menu”
• open_im.m
Abrir Imagens
• open_im.m function open_im
[fname,pname] = uigetfile( ...
{'*.jpg;*.bmp','IMAGE Files (*.jpg,*.bmp)';
'*.jpg', 'JPEG (*.jpg)'; ...
'*.bmp','BMP (*.bmp)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select an Image');
if size(fname,2)>1 && size(pname,2)>1
global main_handle
close(main_handle),HistDoc
global main_handle fname_in pname_in
fname_in = fname;
pname_in = pname;
global I I_AUX
I=imread([pname_in,fname_in]);
...
imshow(I), set(main_handle,'name',['HistDoc - ' fname_in]);
warning on all
end
Abrir Imagens
• save_as.m
Salvar Imagens
• save_as.m function save_as
global fname_in pname_in I main_handle
if prod(size(I))~=0
[fname, pname, filterindex] = uiputfile( ...
{'*.jpg;*.bmp','IMAGE Files (*.jpg,*.bmp)';
'*.jpg', 'JPEG (*.jpg)'; ...
'*.bmp','BMP (*.bmp)'; ...
'*.*', 'All Files (*.*)'}, ...
'Save as', fname_in);
if size(fname,2)>1 && size(pname,2)>1
imwrite(I,[pname,fname]);
fname_in = fname;
pname_in = pname;
set(main_handle,'name',['HistDoc - ' fname_in]);
end
end
Salvar Imagens
• rgb_gray.m
Converte: RGB – NC
• rgb_gray.m function rgb_gray
global I I_AUX
if prod(size(I))~=0
I_AUX = I;
I = rgb2gray(I);
hold on, imshow(I), hold off
end
Converte: RGB – NC
• help_histdoc.m
“Ajuda”
• help_histdoc.m function help_histdoc
help_string = sprintf(['We are sory, but the HistDoc\n' ...
'tool is under construction' ...
'.']);
help_box = 'HistDoc Tool - Help';
[A,Amap] = imread('histdoclogo.gif');
msgbox(help_string,help_box,'custom',A,Amap)
open('HistDoc.pdf');
“Ajuda”
Obs.: o exemplo foi feito com PDF, mas
também pode ser feito em HTML para
ser visualizado no browser do MATLAB
HELP.
• about_histdoc.m
“Sobre”
• about_histdoc.m function about_histdoc
help_string = sprintf(['HistDoc v1.0\n\n' ...
'HistDoc is a tool to process ' ...
'historical document images.\n\n' ...
'Developed by DA SILVA, J.M.M and LINS, R.D.\n' ...
'Copyright %c 2007'],169);
help_box = 'HistDoc Tool - About';
[A,Amap] = imread('histdoclogo.gif');
msgbox(help_string,help_box,'custom',A,Amap)
“Sobre”