MATLAB⿏标事件应⽤(记录)
前记:
⼈机交互的⽅式--键盘开关、⿏标、触摸屏、体感传感器(Kinect、leap motion)等的使⽤⼤⼤促进⼈与机器的交流过程。
这⾥记录⿏标事件与MATLAB的结合,为后期机器⼈仿真控制打下基础---如⿏标直接拖动机器⼈运动(像很多机器⼈仿真软件⾥的作⽤⼀样,如Robotstudio、soldworks、proe\core、Adams等)。
1、⿏标画矩形
代码功能:运⾏代码在出现的figure窗⼝,点击⿏标画出矩形
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
hold on
axis manual
plot(x,y)
2、⿏标拖动直线
function main
f =figure;
aH=axes('Xlim',[0 1],'Ylim',[0 1]);
h=line([0.5 0.5],[0 1],...
'color','red',...
'linewidth',4,...
'ButtonDownFcn',@startDragFcn);
set(f,'WindowButtonUpFcn',@startDragFcn);
function startDragFcn(varargin)
set(f,'WindowButtonMotionFcn',@draggingFcn); end
function draggingFcn(varargin)
pt=get(aH,'CurrentPoint');
set(h,'Xdata',pt(1)*[1 1]);
end
function stopDragFcn(varargin)
set(f,'WindowButtonMotionFcn','');
end
end
3、显⽰坐标
function ff
x = 5:30;
y = x.^2-40.*x+400;
plot(x,y);
axis([5,30,-50,250]);
tb = text;
set(gcf, 'WindowButtonMotionFcn', @callback);
function callback(hObject, event)
loc = get(gca, 'CurrentPoint');
loc = loc([1 3]);
set(tb, 'string', num2str(loc), 'position', loc);
end
end
4、画轨迹并保存点坐标
function MouseDraw(action)
% MouseDraw 本例展⽰如何以Handle Graphics来设定滑⿏事件
% (MouseDraw Events)的反应指令(Callbacks)
% 本程序在⿏标移动⾮常快时,不会造成画“断线”
% global不能传矩阵
global InitialX InitialY FigHandle
if nargin == 0, action = 'start';
end
switch(action)
%%开启图形视窗
case 'start'
FigHandle = figure('WindowButtonDownFcn','MouseDraw down');
axis([-600 600 -600 600]); % 设定图轴范围
% axis off;
grid on;
box on; % 将图轴加上图框
title('⼿写体输⼊窗');
% % fprintf('start');
%%设定滑⿏按钮被按下时的反应指令为「MouseDraw down」
% set(gcf, 'WindowButtonDownFcn', 'MouseDraw down');
dlmwrite('', 10, 'delimiter', '\t', 'precision', 10);
dlmwrite('', 10, 'delimiter', '\t', 'precision', 10);
%%滑⿏按钮被按下时的反应指令
case 'down'
if strcmp(get(FigHandle, 'SelectionType'), 'normal') %如果是左键 set(FigHandle,'pointer','hand');
CurPiont = get(gca, 'CurrentPoint');
InitialX = CurPiont(1,1);
InitialY = CurPiont(1,2);
dlmwrite('', InitialX, '-append', 'delimiter', '\t', 'precision', 10); dlmwrite('', InitialY, '-append', 'delimiter', '\t', 'precision', 10); % 列印「MouseDraw down!」讯息
% % fprintf('MouseDraw down!\n');
% 设定滑⿏移动时的反应指令为「MouseDraw move」
set(gcf, 'WindowButtonMotionFcn', 'MouseDraw move');
set(gcf, 'WindowButtonUpFcn', 'MouseDraw up');
elseif strcmp(get(FigHandle, 'SelectionType'), 'alt') % 如果是右键 set(FigHandle, 'Pointer', 'arrow');
set( FigHandle, 'WindowButtonMotionFcn', '')
set(FigHandle, 'WindowButtonUpFcn', '')
fprintf('MouseDraw right button down!\n');
ImageX = importdata('');
ImageY = importdata('');
InputImage = ones(imSize);
roundX = round(ImageX);
roundY = round(ImageY);
for k = 1:size(ImageX,1)
if 0<roundX(k) && roundX(k)<imSize && 0<roundY(k) && roundY(k)<imSize InputImage(roundX(k)-1:roundX(k)+2, roundY(k)-1:roundY(k)+2) = 0;
end
end
InputImage = imrotate(InputImage,180); % 图像旋转
figure(2);
imshow(InputImage);
end
%%滑⿏移动时的反应指令
case 'move'
CurPiont = get(gca, 'CurrentPoint');
X = CurPiont(1,1);
Y = CurPiont(1,2);
% 当⿏标移动较快时,不会出现离散点。
% 利⽤y=kx+b直线⽅程实现。
x_gap = 1; % 定义x⽅向增量
y_gap = 1; % 定义y⽅向增量
if X > InitialX
step_x = x_gap;
else
step_x = -x_gap;
end
if Y > InitialY
step_y = y_gap;
else
step_y = -y_gap;
end
% 定义x,y的变化范围和步长
if abs(X-InitialX) < 0.1 % 线平⾏于y轴,即斜率不存在时
iy = InitialY:step_y:Y;
ix = X.*ones(1,size(iy,2));
else
ix = InitialX:step_x:X ; % 定义x的变化范围和步长
% 当斜率存在,即k = (Y-InitialY)/(X-InitialX) ~= 0
iy = (Y-InitialY)/(X-InitialX).*(ix-InitialX)+InitialY;
end
ImageX = [ix, X];
ImageY = cat(2, iy, Y);
line(ImageX,ImageY, 'marker', '.', 'markerSize',1, ...
'LineStyle', '-', 'LineWidth', 2, 'Color', 'Blue');
dlmwrite('', ImageX, '-append', 'delimiter', '\t', 'precision', 10);
dlmwrite('', ImageY, '-append', 'delimiter', '\t', 'precision',10);
InitialX = X; %记住当前点坐标
InitialY = Y; %记住当前点坐标
% 列印「MouseDraw is moving!」及滑⿏现在位置
% fprintf('MouseDraw is moving! Current location = (%g, %g)\n', ...
% CurPiont(1,1), CurPiont(1,2));
% % fprintf('MouseDraw move!\n');
% 设定滑⿏按钮被释放时的反应指令为「MouseDraw up」
% set(gcf, 'WindowButtonUpFcn', 'MouseDraw up');
%%滑⿏按钮被释放时的反应指令
case 'up'
% 清除滑⿏移动时的反应指令
set(gcf, 'WindowButtonMotionFcn', '');
% 清除滑⿏按钮被释放时的反应指令
set(gcf, 'WindowButtonUpFcn', '');
fprintf作用% 列印「MouseDraw up!」
% % fprintf('MouseDraw up!\n');
end
end
要的⾃⼰下)。
以上为⽹上资源的收集,主要是供⾃⼰学习。如果碰巧你需要,甚幸~.~ //
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论