[5] | 1 | function im_update(arg1) |
---|
| 2 | % im_update(arg1) |
---|
| 3 | % |
---|
| 4 | % Auxiliary function for impaint.m It takes care for the callbacks when |
---|
| 5 | % the mouse button is clicked or released. |
---|
| 6 | % |
---|
| 7 | % See also impaint.m |
---|
| 8 | |
---|
| 9 | fig = gcbf; |
---|
| 10 | allUD = get(fig,'userdata'); |
---|
| 11 | UD=allUD.impaint; % our portion of UD |
---|
| 12 | |
---|
| 13 | switch lower(arg1) |
---|
| 14 | case 'fill' |
---|
| 15 | % Check if it is inside the image: |
---|
| 16 | set(fig,'units','normalized'); |
---|
| 17 | pos = get(fig,'currentpoint'); |
---|
| 18 | if pos(1)<UD.pos(1) | pos(1)>UD.pos(3) | pos(2)<UD.pos(2) | pos(2)>UD.pos(4) |
---|
| 19 | return |
---|
| 20 | end |
---|
| 21 | % Compute the position: |
---|
| 22 | pos(1) = round((pos(1)-UD.pos(1))*UD.W); |
---|
| 23 | pos(2) = round(UD.sz(1) - (pos(2)-UD.pos(2))*UD.H); |
---|
| 24 | |
---|
| 25 | switch get(fig,'SelectionType') |
---|
| 26 | case 'normal' % left click |
---|
| 27 | %disp('left click'); |
---|
| 28 | t=floor((UD.brushsz-1)/2); |
---|
| 29 | I = -t:t; |
---|
| 30 | % just paint it foreground: |
---|
| 31 | cdata = get(UD.msk_im,'cdata'); |
---|
| 32 | cdata(pos(2)+I,pos(1)+I,1) = UD.msk_fg(UD.currlab,1); |
---|
| 33 | cdata(pos(2)+I,pos(1)+I,2) = UD.msk_fg(UD.currlab,2); |
---|
| 34 | cdata(pos(2)+I,pos(1)+I,3) = UD.msk_fg(UD.currlab,3); |
---|
| 35 | set(UD.msk_im,'cdata',cdata); |
---|
| 36 | UD.labels(pos(2)+I,pos(1)+I) = UD.currlab; |
---|
| 37 | case 'alt' % right click |
---|
| 38 | %disp('right click'); |
---|
| 39 | % just paint it background: |
---|
| 40 | t=floor((UD.brushsz-1)/2); |
---|
| 41 | I = -t:t; |
---|
| 42 | cdata = get(UD.msk_im,'cdata'); |
---|
| 43 | cdata(pos(2)+I,pos(1)+I,1) = UD.msk_bg(1); |
---|
| 44 | cdata(pos(2)+I,pos(1)+I,2) = UD.msk_bg(2); |
---|
| 45 | cdata(pos(2)+I,pos(1)+I,3) = UD.msk_bg(3); |
---|
| 46 | set(UD.msk_im,'cdata',cdata); |
---|
| 47 | UD.labels(pos(2)+I,pos(1)+I) = 0; |
---|
| 48 | end |
---|
| 49 | % now I want to trace the cursor: |
---|
| 50 | set(fig,'WindowButtonMotionFcn','im_update(''fill'')'); |
---|
| 51 | |
---|
| 52 | case 'stopfill' |
---|
| 53 | % stop tracing the cursor: |
---|
| 54 | set(fig,'WindowButtonMotionFcn',[]); |
---|
| 55 | |
---|
| 56 | case 'keypress-hidden' |
---|
| 57 | t=get(fig,'currentchar'); |
---|
| 58 | if strcmp(UD.state,'off') & strcmp(t,'t') |
---|
| 59 | |
---|
| 60 | UD.state='on'; |
---|
| 61 | |
---|
| 62 | % show the overlay axes: |
---|
| 63 | set(UD.msk_im,'visible','on'); |
---|
| 64 | |
---|
| 65 | % use our impaint handlers (previous are stored) |
---|
| 66 | set(fig,'WindowButtonDownFcn','im_update(''fill'')'); |
---|
| 67 | set(fig,'WindowButtonUpFcn','im_update(''stopfill'')'); |
---|
| 68 | set(fig,'KeyPressFcn','im_update(''keypress'')'); |
---|
| 69 | |
---|
| 70 | % change the window title |
---|
| 71 | set(fig,'Name',sprintf('imprint on, brush: %d, class: %d',UD.brushsz,UD.currlab)); |
---|
| 72 | |
---|
| 73 | else |
---|
| 74 | % invoke old handler |
---|
| 75 | eval(UD.oldhandlers.keypress); |
---|
| 76 | end |
---|
| 77 | |
---|
| 78 | case 'keypress' |
---|
| 79 | t=get(fig,'currentchar'); |
---|
| 80 | if double(t)==46 |
---|
| 81 | % first allow decimal point |
---|
| 82 | UD.charbuffer=[UD.charbuffer '.']; |
---|
| 83 | else |
---|
| 84 | t2=str2num(t); % is is a number? |
---|
| 85 | if ~isempty(t2) |
---|
| 86 | % it's a diggit -> add to the buffer |
---|
| 87 | UD.charbuffer=[UD.charbuffer t]; |
---|
| 88 | else |
---|
| 89 | % it's a letter command: execute |
---|
| 90 | % get the current numerical content from buffer: |
---|
| 91 | num=str2num(UD.charbuffer); |
---|
| 92 | |
---|
| 93 | switch t |
---|
| 94 | case 't' % toggle the impaint on and off |
---|
| 95 | if strcmp(UD.state,'on') |
---|
| 96 | UD.state='off'; |
---|
| 97 | |
---|
| 98 | % hide the overlay axes: |
---|
| 99 | set(UD.msk_im,'visible','off'); |
---|
| 100 | |
---|
| 101 | % supply original handlers |
---|
| 102 | set(fig,'WindowButtonDownFcn',UD.oldhandlers.btndown); |
---|
| 103 | set(fig,'WindowButtonUpFcn',UD.oldhandlers.btnup); |
---|
| 104 | |
---|
| 105 | % keypress handler must allow to return back |
---|
| 106 | set(fig,'KeyPressFcn','im_update(''keypress-hidden'')'); |
---|
| 107 | |
---|
| 108 | % change the window title |
---|
| 109 | set(fig,'Name','imprint off'); |
---|
| 110 | |
---|
| 111 | set(fig,'UserData',[]); |
---|
| 112 | allUD.impaint=UD; |
---|
| 113 | set(fig,'UserData',allUD); |
---|
| 114 | return |
---|
| 115 | |
---|
| 116 | end |
---|
| 117 | case 'b' % change the brush size |
---|
| 118 | if isempty(num) |
---|
| 119 | fprintf(1,'impaint: use a numerical prefix followed by letter command!'); |
---|
| 120 | end |
---|
| 121 | if num>0 |
---|
| 122 | UD.brushsz=num; |
---|
| 123 | end |
---|
| 124 | case 'c' % class selector |
---|
| 125 | if isempty(num) |
---|
| 126 | fprintf(1,'impaint: use a numerical prefix followed by letter command!'); |
---|
| 127 | end |
---|
| 128 | if num>0 |
---|
| 129 | UD.currlab=num; |
---|
| 130 | end |
---|
| 131 | case 'a' % alpha level |
---|
| 132 | if isempty(num) |
---|
| 133 | fprintf(1,'impaint: use a numerical prefix followed by letter command!'); |
---|
| 134 | end |
---|
| 135 | if num>=0 & num<=1 |
---|
| 136 | msk2 = findobj(get(UD.msk_ax,'children'),'type','image'); |
---|
| 137 | set(msk2,'alphadata',num); |
---|
| 138 | end |
---|
| 139 | case 's' % save labels |
---|
| 140 | def='lab'; |
---|
| 141 | if ~isempty(num), def=[def num2str(num)]; end |
---|
| 142 | name=inputdlg('Enter label vector name','Assign labels to a workspace variable',1,{def}); |
---|
| 143 | if ~isempty(name) |
---|
| 144 | lab=UD.labels; |
---|
| 145 | lab=lab(1:UD.labsize(1),1:UD.labsize(2)); |
---|
| 146 | assignin('base',name{1},lab); |
---|
| 147 | end |
---|
| 148 | otherwise |
---|
| 149 | % fprintf(1,'unknown command'); |
---|
| 150 | end |
---|
| 151 | set(fig,'Name',sprintf('imprint on, brush: %d, class: %d',UD.brushsz,UD.currlab)); |
---|
| 152 | UD.charbuffer=''; % clear the buffer |
---|
| 153 | end |
---|
| 154 | end |
---|
| 155 | end |
---|
| 156 | |
---|
| 157 | set(fig,'UserData',[]); |
---|
| 158 | allUD.impaint=UD; |
---|
| 159 | set(fig,'UserData',allUD); |
---|
| 160 | |
---|
| 161 | % DXD it does not seem to work....: |
---|
| 162 | %set(UD.msk_im,'erasemode','normal'); |
---|
| 163 | |
---|
| 164 | return |
---|