2012-09-21 41 views
6

Quiero tener un programa en matlab con GUI, al ejecutar el programa, el usuario puede dibujar cualquier cosa con el mouse sobre los ejes en la GUI, y quiero guardar la imagen creada en una matriz. ¿Cómo puedo hacer esto?Dibujando con el mouse en la GUI en matlab

Respuesta

8

fin encuentro un buen código y he cambiado algunas partes de personalización para mí. con esta forma, el usuario puede dibujar anythings en los ejes con el ratón:

function userDraw(handles) 
%F=figure; 
%setptr(F,'eraser'); %a custom cursor just for fun 

A=handles.axesUserDraw; % axesUserDraw is tag of my axes 
set(A,'buttondownfcn',@start_pencil) 

function start_pencil(src,eventdata) 
coords=get(src,'currentpoint'); %since this is the axes callback, src=gca 
x=coords(1,1,1); 
y=coords(1,2,1); 

r=line(x, y, 'color', [0 .5 1], 'LineWidth', 2, 'hittest', 'off'); %turning  hittset off allows you to draw new lines that start on top of an existing line. 
set(gcf,'windowbuttonmotionfcn',{@continue_pencil,r}) 
set(gcf,'windowbuttonupfcn',@done_pencil) 

function continue_pencil(src,eventdata,r) 
%Note: src is now the figure handle, not the axes, so we need to use gca. 
coords=get(gca,'currentpoint'); %this updates every time i move the mouse 
x=coords(1,1,1); 
y=coords(1,2,1); 
%get the line's existing coordinates and append the new ones. 
lastx=get(r,'xdata'); 
lasty=get(r,'ydata'); 
newx=[lastx x]; 
newy=[lasty y]; 
set(r,'xdata',newx,'ydata',newy); 

function done_pencil(src,evendata) 
%all this funciton does is turn the motion function off 
set(gcf,'windowbuttonmotionfcn','') 
set(gcf,'windowbuttonupfcn','') 
+0

¿Cómo uso esta función para dibujar? – mikeglaz

3

La función ginput obtiene las coordenadas de moueclicks dentro de una figura. Puede usarlos como puntos de una línea, polígono, etc.

Si esto no se ajusta a sus necesidades, debe indicar exactamente qué espera que dibuje el usuario.

dibujo a mano alzada Para esto podría ser útil:

http://www.mathworks.com/matlabcentral/fileexchange/7347-freehanddraw

+0

i han desarrollando un programa para detectar el carácter que el dibujo del usuario, por lo que el usuario debe ser capaz de dibujo de caracteres alfanuméricos. –

+0

Ver mi edición superior. –

Cuestiones relacionadas