20140728 matlab用户交互部分

涉及鼠标的回调函数,参数适用全局变量来记录。
注意:1. 全局变量时需先声明;2. 左右键分别表明一类; 3. 计算涉及的区域占据的superpixels函数

global bg; %  全局变量 记录鼠标划过的背景像素
global fg; %  全局变量 记录鼠标划过的前景景像素  
global bg_num ;
global fg_num ;
bg_num = 0;
fg_num = 0;

img = imread('img_view.jpg');
figure;
imshow(img); hold on

set(gcf,'WindowButtonDownFcn',@ButtonDownFcn); % 鼠标按下的响应
set(gcf,'WindowButtonUpFcn',@ButtonUpFcn); % 鼠标放下的响应

a=input('Any character end'); % 等待输入完成


function ButtonDownFcn(~,~)
    set(gcf,'WindowButtonMotionFcn',@ButtonMotionFcn); %  设置鼠标移动响应
end

function ButtonUpFcn(src,event)
    set(gcf, 'WindowButtonMotionFcn', ''); %  取消鼠标移动响应
end

function [x,y] = ButtonMotionFcn(~,~)
    global bg;
    global fg;
    global bg_num;
    global fg_num;
    left = 0;
    right = 0;
    pt = get(gca,'CurrentPoint');
    if strcmp(get(gcf,'SelectionType'),'alt')
       left = 1;
    elseif  strcmp(get(gcf,'SelectionType'),'normal')
       right = 1;
    end
    x = pt(1,1);
    y = pt(1,2);
    if left == 1
       line(x, y, 'marker', '.', 'EraseMode', 'normal','Color',[.6 0 0]);    
       bg_num = bg_num +1;
       bg(bg_num,:) = [x,y];
    elseif right == 1
       line(x, y, 'marker', '.', 'EraseMode', 'normal');   
       fg_num = fg_num +1;
       fg(fg_num,:) = [x,y];
    end  
end
相关文章
相关标签/搜索