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
相關文章
相關標籤/搜索