1 function Segment_demo (Action, Arguments, varargin)
\r
3 global All_vars Biomed_on;
\r
4 if size(All_vars, 2) > 0
\r
5 eval (['global ', All_vars]);
\r
7 % regions are represented as structures of:
\r
9 % Number - region number
\r
10 % Color - the original color to draw the region
\r
11 % Line - the actual points as a line object
\r
12 % Active - whether it was merged into another region
\r
13 % From - which regions it was obtained from (by merging or cutting)
\r
15 % Boundary (Boundary) given as set of points
\r
16 % Area - total area of the regions, since computed anyway
\r
17 % Inner point - telling where the interior of region is (Inner_pts)
\r
18 % Shifts - a matrix of size 2 by 2 giving min, max x and y vals for boundary
\r
19 % Region_matr - the filled with pixels boundary for various operations
\r
20 % user defined chars:
\r
24 Common_vars =[' Gray_image Image Luv_image Image_is_gray ', ...
\r
25 ' Region_colors Bndred_regions Curr_pt Curr_pt_line ',...
\r
26 ' Slices Centers Store_mouse_pts ', ...
\r
27 ' Regions Region_menu_added Min_region_size ', ...
\r
28 ' Region_menu Main_figure Main_axis Marker_size ', ...
\r
29 ' Region_left Region_right Show_all_regions ', ...
\r
30 ' Instance Active_instances Biomed_on '];
\r
32 Biomed_vars = [' Amnt_rot Window_regs_chosen Window1 Window2 '];
\r
34 All_vars = [Common_vars, Biomed_vars];
\r
35 eval (['global ', All_vars]);
\r
36 Action = 'Initialize';
\r
39 if strcmp(Action, 'Initialize')
\r
40 if nargin < 2 % the very first call to it
\r
41 Msg_box_hndl = msgbox('Welcome to the system ...', 'Welcome');
\r
42 Child = get(Msg_box_hndl, 'Children'); % the OK button
\r
43 set(Child(length(Child)), 'Style', 'Text', 'String', '');
\r
44 Image_menu = uimenu(Msg_box_hndl, 'Label','&Image');
\r
45 uimenu(Image_menu, 'Label', '&Open...', ...
\r
46 'Callback', 'close; Segment_demo(''Read image'')', 'Accelerator', 'O');
\r
47 uimenu(Image_menu, 'Label', '&Quit', ...
\r
48 'Callback', 'close', 'Separator','on', 'Accelerator', 'Q');
\r
49 Screen_size = get(0, 'ScreenSize');
\r
50 Marker_size = round(max(Screen_size)/200);
\r
52 I = varargin{length(varargin)}; % last argument always
\r
53 if length(size(Arguments)) == 3 % color image
\r
54 Image_is_gray{I} = 0;
\r
55 Image{I} = Arguments;
\r
57 Image_is_gray{I} = 1;
\r
58 Image{I} = Arguments;
\r
59 Gray_image{I} = double(Arguments);
\r
62 if not(varargin{1}) % does not need new window
\r
63 Kids = get(Main_axis{I}, 'Children');
\r
64 for i=1:length(Kids)
\r
65 if strcmp(get(Kids(i), 'Type'), 'line')
\r
66 set(Kids(i), 'XData', 0, 'YData', 0, 'Visible', 'off');
\r
69 Segment_demo('Refresh image', 0, I);
\r
73 Bndred_regions{I} = 0;
\r
74 Region_left{I} = 0; Region_right{I} = 0;
\r
75 Store_mouse_pts{I} = 0; Curr_pt_line{I} = 0;
\r
76 Show_all_regions{I} = 0; % will show only active regions by default
\r
78 % Biomed vars go here
\r
80 Window_regs_chosen = 0;
\r
84 % the list of colors
\r
85 Region_colors{1, 1} = 'magenta';
\r
86 Region_colors{1, 2} = 'yellow';
\r
87 Region_colors{1, 3} = 'green';
\r
88 Region_colors{1, 4} = 'red';
\r
89 Region_colors{1, 5} = 'cyan';
\r
90 Region_colors{1, 6} = 'blue';
\r
91 Region_colors{1, 7} = 'black';
\r
93 if varargin{1} % needs new window
\r
94 Region_menu_added{I} = 0;
\r
95 ButtonDown = sprintf('Segment_demo(''%s'',%d,%d)', 'Down', 0, I);
\r
96 CloseReq = sprintf('Segment_demo(''%s'',%d,%d)', 'Exit', 0, I);
\r
97 set(gcf, 'WindowButtonDownFcn', ButtonDown, 'MenuBar', 'none', ...
\r
98 'CloseRequestFcn', CloseReq);
\r
99 Main_figure{I} = gcf;
\r
100 Main_axis{I} = gca;
\r
101 Image_menu = uimenu('Label','&Image');
\r
102 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Read image', 0, I);
\r
103 uimenu(Image_menu, 'Label', '&Open...', ...
\r
104 'Callback', Callback, 'Accelerator', 'O');
\r
105 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Smooth prompt', 0, I);
\r
106 uimenu(Image_menu, 'Label', 's&Mooth...', ...
\r
107 'Callback', Callback, 'Accelerator', 'M');
\r
108 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Segment prompt', 0, I);
\r
109 uimenu(Image_menu, 'Label', 'se&Gment...', ...
\r
110 'Callback', Callback, 'Accelerator', 'G');
\r
111 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Crop', 0, I);
\r
112 uimenu(Image_menu, 'Label', '&Crop...', ...
\r
113 'Callback', Callback, 'Accelerator', 'C');
\r
114 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Refresh image', 0, I);
\r
115 uimenu(Image_menu, 'Label', 'r&Efresh!', ...
\r
116 'Callback', Callback, 'Accelerator', 'E');
\r
117 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Label prompt', 0, I);
\r
118 uimenu(Image_menu, 'Label', 'l&abel', ...
\r
119 'Callback', Callback, 'Accelerator', 'A');
\r
120 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Save image', 0, I);
\r
121 uimenu(Image_menu, 'Label', '&Save as...', ...
\r
122 'Callback', Callback, 'Accelerator', 'S');
\r
123 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Exit', 0, I);
\r
124 uimenu(Image_menu, 'Label', '&Quit', ...
\r
125 'Callback', Callback, 'Separator','on', 'Accelerator', 'Q');
\r
126 Segment_demo('Show image', 1, I); % needs window size setting
\r
128 Segment_demo('Show image', 0, I); % no window size setting
\r
134 %-----------------------------------------
\r
135 % exiting if desired, after warning
\r
136 if strcmp(Action, 'Exit')
\r
137 I = varargin{length(varargin)}; % last argument always
\r
138 set(Main_figure{I}, 'CloseRequestFcn', 'closereq');
\r
140 Active_instances = Active_instances-1;
\r
141 if Active_instances == 0
\r
142 eval (['clear global ', All_vars]);
\r
145 %-----------------------------------------
\r
146 % keeping track of the pixel clicked onto by mouse
\r
147 elseif strcmp(Action, 'Down') % request to pick region follows
\r
148 I = varargin{length(varargin)}; % last argument always
\r
149 Curr_pt_get{I} = get(gca, 'CurrentPoint');
\r
150 Curr_pt_get{I} = round(Curr_pt_get{I}(1, 1:2));
\r
151 Curr_pt_get{I} = [Curr_pt_get{I}(2); Curr_pt_get{I}(1)]; % switching x and y
\r
152 if Store_mouse_pts{I}
\r
153 Curr_pt{I} = [Curr_pt{I}, Curr_pt_get{I}];
\r
155 Curr_pt{I} = Curr_pt_get{I};
\r
158 set(Curr_pt_line{I}, 'XData',Curr_pt{I}(2, :), 'YData',Curr_pt{I}(1, :));
\r
160 Curr_pt_line{I} = ...
\r
161 line('XData', Curr_pt{I}(2, :), 'YData', Curr_pt{I}(1, :), ...
\r
162 'Marker', '.', 'Color', 'yellow', 'EraseMode', 'none');
\r
166 %-----------------------------------------
\r
167 % segmenting the image into a number of subregions by the subroutine
\r
168 elseif strcmp(Action, 'Segment prompt')
\r
169 I = varargin{length(varargin)}; % last argument always
\r
171 'Window size (small window => small variation)', ...
\r
172 'Color components to use (fewer => quicker)', ...
\r
173 'Color group size (small size => fewer colors left)', ...
\r
174 'Min region size (small size => more small regions)', ...
\r
175 'Would you like to see separate layers?'};
\r
176 if Image_is_gray{I}
\r
177 Default = {20, 1, 200, 20, 'No'};
\r
179 Default = {30, 2, 200, 20, 'No'};
\r
181 Title = 'Segmentation parameters';
\r
183 Segment_input = My_inputdlg(Prompt, Title, Line_number, Default);
\r
184 if size(Segment_input, 2) > 0
\r
185 Segment_demo ('Initialize', Image{I}, 0, I); % does not need new wind
\r
186 Segment_demo ('Segment', Segment_input, I);
\r
190 %-----------------------------------------
\r
191 % segmenting the image into a number of subregions by the subroutine
\r
192 elseif strcmp(Action, 'Segment')
\r
193 I = varargin{length(varargin)}; % last argument always
\r
194 Segment_input = Arguments;
\r
195 Window_radius = base2dec(Segment_input{1}, 10);
\r
196 Components = base2dec(Segment_input{2}, 10);
\r
197 % Components = round(max(Components, 2)); % for a while
\r
198 Color_group = base2dec(Segment_input{3}, 10);
\r
199 Min_region_size{I} = base2dec(Segment_input{4}, 10);
\r
200 Plot_slices = length(findstr(lower(Segment_input{5}), 'y'));
\r
201 if Image_is_gray{I}
\r
202 [Segmented, Centers] = ...
\r
203 Gray_segment (Gray_image{I}, Window_radius, Color_group);
\r
205 Luv_image{I} = Rgb_to_luv(Image{I}, 'Image');
\r
206 Gray_image{I} = squeeze(Luv_image{I}(:, :, 1));
\r
207 Lhs = '[Segmented, Centers] = ';
\r
208 Rhs = [sprintf('Color_segment_svd%d', Components), ...
\r
209 '(Luv_image{I}, Window_radius, Color_group);'];
\r
213 Segments = size(Centers, 2);
\r
214 Slices{I} = zeros(size(Gray_image{I}));
\r
217 figure; % plotting the resulting regions
\r
218 Square_side = ceil(sqrt(Segments));
\r
222 Slice = (Segmented == i);
\r
223 % some image processing - taken from morphology file
\r
224 Slice = bwmorph(Slice, 'majority');
\r
225 Slice = (Slice == 0); % negation
\r
226 Slice = bwmorph(Slice, 'majority');
\r
227 Slice = (Slice == 0); % negation
\r
228 % Slice = bwmorph(Slice, 'thicken', 2); % no thickening for a while
\r
229 Slices{I}(:, :, i) = bwlabel(Slice);
\r
231 subplot(Square_side, Square_side, i);
\r
232 image((Slices{I}(:, :, i)>0)*255); axis off;
\r
233 colormap(gray(255));
\r
237 figure(Main_figure{I});
\r
240 % appending the new submenu
\r
241 if not(Region_menu_added{I})
\r
242 Region_menu{I} = uimenu('Label', '&Region');
\r
243 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Find boundary', 0, I);
\r
244 uimenu(Region_menu{I}, 'Label', '&Find boundary', ...
\r
245 'Callback', Callback, 'Accelerator', 'F');
\r
246 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Select left region', 0, I);
\r
247 uimenu(Region_menu{I}, 'Label', '(un)select &1-st region', 'Enable', 'off', ...
\r
248 'Callback', Callback, 'Accelerator', '1');
\r
249 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Select right region', 0, I);
\r
250 uimenu(Region_menu{I}, 'Label', '(un)select &2-nd region', 'Enable', 'off', ...
\r
251 'Callback', Callback, 'Accelerator', '2');
\r
252 Callback=sprintf('Segment_demo(''%s'',%d,%d)','Save boundary prompt',0,I);
\r
253 uimenu(Region_menu{I}, 'Label', 'sa&Ve as...', 'Enable', 'off', ...
\r
254 'Callback', Callback, 'Accelerator', 'V');
\r
255 Compare_menu = uimenu(Region_menu{I}, 'Label', '&Compare to...', ...
\r
257 Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
\r
258 'Compare disk regions prompt', 0, I);
\r
259 uimenu(Compare_menu, 'Label', '&Disk region...', ...
\r
260 'Callback', Callback, 'Accelerator', 'D');
\r
261 Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
\r
262 'Compare image regions prompt', 0, I);
\r
263 uimenu(Compare_menu, 'Label', '&Image region...', ...
\r
264 'Callback', Callback, 'Accelerator', 'I');
\r
265 Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
\r
266 'Compare window regions prompt', 0, I);
\r
267 uimenu(Compare_menu, 'Label', '&Window region...', ...
\r
268 'Callback', Callback, 'Accelerator', 'W');
\r
269 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Merge prompt', 0, I);
\r
270 uimenu(Region_menu{I}, 'Label', 'me&Rge...', 'Enable', 'off', ...
\r
271 'Callback', Callback, 'Accelerator', 'R');
\r
272 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Cut prompt', 0, I);
\r
273 uimenu(Region_menu{I}, 'Label', 'c&Ut...', 'Enable', 'off', ...
\r
274 'Callback', Callback, 'Accelerator', 'U');
\r
275 Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
\r
276 'Intensity 3-d prompt', 0, I);
\r
277 uimenu(Region_menu{I},'Label', 'intensity &3-d...', 'Enable','off', ...
\r
278 'Callback', Callback, 'Accelerator', '3');
\r
279 Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
\r
280 'Background region prompt',0, I);
\r
281 uimenu(Region_menu{I}, 'Label', 'bac&Kground...', 'Enable', 'off', ...
\r
282 'Callback', Callback, 'Accelerator', 'K');
\r
283 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Color prompt', 0, I);
\r
284 uimenu(Region_menu{I}, 'Label', 'co&Lor...', 'Enable', 'off', ...
\r
285 'Callback', Callback, 'Accelerator', 'L');
\r
286 Callback=sprintf('global Marker_size; Marker_size = Marker_size + 2;');
\r
287 uimenu(Region_menu{I}, 'Label', 'Thicken next line', 'Enable', 'off', ...
\r
288 'Callback', Callback);
\r
289 Callback=sprintf('global Marker_size; Marker_size = Marker_size - 2;');
\r
290 uimenu(Region_menu{I}, 'Label', 'Thin next line', 'Enable', 'off', ...
\r
291 'Callback', Callback);
\r
292 Region_menu_added{I} = 1;
\r
296 %-----------------------------------------
\r
297 elseif strcmp(Action, 'Crop')
\r
298 I = varargin{length(varargin)}; % last argument always
\r
299 msgbox('Please crop a subregion', 'Cropping');
\r
300 waitforbuttonpress;
\r
301 Point1 = get(gca,'CurrentPoint'); % button down detected
\r
302 Final_rect = rbbox; % return Figure units
\r
303 Point2 = get(gca,'CurrentPoint'); % button up detected
\r
304 Point1 = round(Point1(1,1:2)); % extract x and y
\r
305 Point2 = round(Point2(1,1:2));
\r
306 P1 = min(Point1,Point2); % calculate locations
\r
307 Offset = abs(Point1-Point2); % and dimensions
\r
308 Rect_x = [P1(1) P1(1)+Offset(1) P1(1)+Offset(1) P1(1) P1(1)];
\r
309 Rect_y = [P1(2) P1(2) P1(2)+Offset(2) P1(2)+Offset(2) P1(2)];
\r
312 plot(Rect_x, Rect_y);
\r
314 Button = questdlg('Is it suitable?', 'Crop/Undo cropping','Yes','No','Yes');
\r
315 if strcmp(Button,'Yes')
\r
316 Left_x = min(Rect_y); Right_x = max(Rect_y); % again shift
\r
317 Low_y = min(Rect_x); High_y = max(Rect_x); % again shift
\r
318 if Image_is_gray{I}
\r
319 Image_cropped = Gray_image{I}(Left_x:Right_x, Low_y:High_y);
\r
321 Image_cropped = Image{I}(Left_x:Right_x, Low_y:High_y, :);
\r
324 % creating a window for the newly cropped image
\r
325 figure; % creating one more window
\r
326 set (gcf, 'Name', [get(Main_figure{I}, 'Name'), ' cropped']);
\r
327 if length(Instance) > 0 % was defined
\r
328 Instance = Instance+1;
\r
329 Active_instances = Active_instances+1;
\r
332 Active_instances = 1;
\r
334 Segment_demo ('Initialize', Image_cropped, 1, Instance); %needs new window
\r
336 Segment_demo('Refresh image', 0, I);
\r
340 %-----------------------------------------
\r
341 elseif strcmp(Action, 'Smooth prompt')
\r
342 I = varargin{length(varargin)}; % last argument always
\r
343 Scr_sz = get(0, 'ScreenSize');
\r
344 New_wind = dialog('Name', 'Which filter to use?', 'Position', ...
\r
345 [Scr_sz(3)*0.3 Scr_sz(4)*0.4 Scr_sz(3)*0.3 Scr_sz(4)*0.1]);
\r
346 Choice = uicontrol('Parent', New_wind);
\r
347 Callback = sprintf('Segment_demo(''Smooth'', %18.16f, %d)', Choice, I);
\r
348 set(Choice, 'Style', 'Popup', ...
\r
349 'String', {'Circle-5', 'Symlet', 'Avg-3'}, ...
\r
350 'Units', 'Normalized', 'Position', [0 0.5 1 0.5]);
\r
351 Cancel_button = uicontrol('Parent', New_wind, ...
\r
352 'String', 'Cancel', 'Style', 'Pushbutton', 'Callback', 'close', ...
\r
353 'Units', 'Normalized', 'Position', [0.6 0.3 0.3 0.4]);
\r
354 Ok_button = uicontrol('Parent', New_wind, ...
\r
355 'String', 'OK', 'Style', 'Pushbutton', 'Callback', Callback, ...
\r
356 'Units', 'Normalized', 'Position', [0.1 0.3 0.3 0.4]);
\r
359 %----------------------------------------
\r
360 elseif strcmp(Action, 'Smooth')
\r
361 I = varargin{length(varargin)}; % last argument always
\r
362 Choice = get(Arguments, 'Value');
\r
363 close; % closing the prompt window
\r
366 Filter = 'Symlets';
\r
369 Filter = Filter/sum(sum(Filter));
\r
372 Filter(1:2, 1) = zeros(2, 1); Filter(4:5, 1) = zeros(2, 1);
\r
373 Filter(1:2, 5) = zeros(2, 1); Filter(4:5, 5) = zeros(2, 1);
\r
374 Filter(1, 2) = 0; Filter(5, 2) = 0;
\r
375 Filter(2, 5) = 0; Filter(5, 4) = 0;
\r
376 Filter = Filter/sum(sum(Filter));
\r
380 if size(Filter, 2) > 1
\r
381 if strcmp(Filter, 'Symlets')
\r
382 if Image_is_gray{I}
\r
383 Image_filtered = Smooth_wv(Gray_image{I});
\r
386 Image_filtered(:, :, i) = Smooth_wv(Image{I}(:, :, i));
\r
390 if Image_is_gray{I}
\r
391 Image_filtered = filter2(Filter, Gray_image{I});
\r
394 Image_filtered(:, :, i) = filter2(Filter, Image{I}(:, :, i));
\r
398 Segment_demo ('Initialize', Image_filtered, 0, I); % no new window
\r
402 %-----------------------------------------
\r
403 % now saves both in low-quality and high quality formats in jpeg/tif/bmp and
\r
404 % in eps format with a standard size
\r
405 elseif strcmp(Action, 'Save image')
\r
406 I = varargin{length(varargin)}; % last argument always
\r
407 % Filter = '*.jpeg; *.jpg; *.tif; *.tiff; *.bmp; *.eps; *.ps';
\r
408 Filter = '*.jpeg; *.jpg; *.tif; *.bmp; *.eps';
\r
409 [File_name, Path_name] = uiputfile(Filter, 'Save to file ...');
\r
410 [Screen, Screen_map] = capture(Main_figure{I});
\r
411 % splitting file name into extension and file itself
\r
412 for i=length(File_name):-1:1
\r
413 if File_name(i) == '.'
\r
414 File_type = File_name(i+1:length(File_name));
\r
415 File_name = File_name(1:i-1);
\r
420 if File_name(1) > 0
\r
421 if (strcmp(File_type, 'jpeg') | strcmp(File_type, 'jpg'))
\r
422 % low-level file first
\r
423 imwrite(Screen, Screen_map, [Path_name, File_name, '_low.', File_type], ...
\r
424 File_type, 'Quality', 80);
\r
425 print_string = sprintf('%s %s', 'print -djpeg ', ...
\r
426 [Path_name, File_name, '_high.', File_type]);
\r
427 eval(print_string);
\r
428 msgbox(['Image was saved in ', File_name, '_low.', File_type, ...
\r
429 ' and ', File_name, '_high.', File_type], 'Files saved');
\r
431 if (strcmp(File_type, 'tiff') | strcmp(File_type, 'tif'))
\r
432 % low-level file first
\r
433 imwrite(Screen, Screen_map, [Path_name, File_name, '_low.', File_type], ...
\r
435 print_string = sprintf('%s %s', 'print -dtiff ', ...
\r
436 [Path_name, File_name, '_high.', File_type]);
\r
437 eval(print_string);
\r
438 msgbox(['Image was saved in ', File_name, '_low.', File_type, ...
\r
439 ' and ', File_name, '_high.', File_type], 'Files saved');
\r
441 if strcmp(File_type, 'bmp')
\r
442 % low-level file first
\r
443 imwrite(Screen, Screen_map, [Path_name, File_name, '.', File_type], ...
\r
445 print_string = sprintf('%s %s', 'print -djpeg ', ...
\r
446 [Path_name, File_name, '.jpg']);
\r
447 eval(print_string);
\r
448 msgbox(['Image was saved in ', File_name, '.', File_type, ...
\r
449 ' and ', File_name, '.jpg'], 'Files saved');
\r
451 if (strcmp(File_type, 'eps') | strcmp(File_type, 'ps'))
\r
452 print_string = sprintf('%s %s', 'print -depsc -loose ', ...
\r
453 [Path_name, File_name, '.', File_type]);
\r
454 eval(print_string);
\r
455 msgbox(['Image was saved in ', File_name, '.', File_type], ...
\r
461 %-----------------------------------------
\r
462 elseif strcmp(Action, 'Read image')
\r
463 Filter = '*.jpeg; *.jpg; *.tif; *.tiff; *.bmp';
\r
464 [File_name, Path_name] = uigetfile(Filter, 'Open file');
\r
465 Image_name = File_name;
\r
466 File_name = [Path_name, File_name];
\r
467 if File_name(1) > 0 % not zeros there
\r
468 [Im_read, Map] = imread(File_name);
\r
469 if length(Map) > 0 % not gray or RGB, but could be made such
\r
470 Im_read = ind2rgb(Im_read, Map);
\r
471 Im_read = 256*Im_read;
\r
473 Im_read = double(Im_read);
\r
474 if length(size(Im_read)) == 3 % color image
\r
476 Img(:,:,i) = min(Im_read(:,:,i)+ones(size(Im_read(:,:,i))), ...
\r
477 255*ones(size(Im_read(:,:,i))));
\r
480 Img = min(Im_read+ones(size(Im_read)),255*ones(size(Im_read)));
\r
482 figure; % creating one more window
\r
483 set (gcf, 'Name', Image_name);
\r
484 if length(Instance) > 0 % was defined
\r
485 Instance = Instance+1;
\r
486 Active_instances = Active_instances+1;
\r
489 Active_instances = 1;
\r
491 Segment_demo('Initialize', Img, 1, Instance); % needs new window
\r
495 %-----------------------------------------
\r
496 elseif strcmp(Action, 'Refresh image')
\r
497 I = varargin{length(varargin)}; % last argument always
\r
498 if length(Curr_pt_line) >= I
\r
499 if Curr_pt_line{I} > 0
\r
500 set(Curr_pt_line{I}, 'EraseMode', 'normal');
\r
501 set(Curr_pt_line{I}, 'Visible', 'off');
\r
502 set(Curr_pt_line{I}, 'Visible', 'on');
\r
503 set(Curr_pt_line{I}, 'EraseMode', 'none');
\r
507 % deleting rectangles if present - they have few points
\r
508 Kids = get(Main_axis{I}, 'Children');
\r
509 for i=1:length(Kids)
\r
510 if strcmp(get(Kids(i), 'type'), 'line') & ...
\r
511 length(get(Kids(i), 'XData')) == 5 % rectangle
\r
512 set(Kids(i), 'XData', 0, 'YData', 0, 'Visible', 'off');
\r
517 %-----------------------------------------
\r
518 % labeling the image at a chosen place
\r
519 elseif strcmp(Action, 'Label prompt')
\r
520 I = varargin{length(varargin)}; % last argument always
\r
521 % determining whether near an existing label
\r
522 Curr_pt_local = get(gca, 'CurrentPoint');
\r
523 Curr_pt_local = round(Curr_pt_local(1, 1:2));
\r
524 kids = get(gca, 'children');
\r
525 for i=1:length(kids)
\r
526 if strcmp(get(kids(i), 'type'), 'text') & ...
\r
527 strcmp(get(kids(i), 'visible'), 'on') & ...
\r
528 length(get(kids(i), 'string')) > 0
\r
529 text_pos = get(kids(i), 'position');
\r
530 text_pos = text_pos(1:2);
\r
531 if norm(Curr_pt_local-text_pos)<8
\r
532 set(kids(i), 'editing', 'on');
\r
538 Prompt = {'Label', 'Font size', 'Font type', 'Font color', 'Rotated'};
\r
539 Default = {'Region ', 10, 'Courier', 'black', 0};
\r
540 Title = 'Labeling image';
\r
542 Label_input = My_inputdlg(Prompt, Title, Line_number, Default);
\r
543 if size(Label_input, 2) > 0
\r
544 new_text = text(Curr_pt_local(1), Curr_pt_local(2), Label_input{1});
\r
545 set(new_text, 'fontsize', base2dec(Label_input{2}, 10), ...
\r
546 'fontname', Label_input{3}, 'color', Label_input{4}, ...
\r
547 'rotation', base2dec(Label_input{5}, 10));
\r
548 Button = questdlg('Is it suitable?', 'Labeling', 'Yes', 'No', 'Yes');
\r
549 if strcmp(Button, 'No')
\r
550 set(new_text, 'visible', 'off');
\r
554 %-----------------------------------------
\r
555 elseif strcmp(Action, 'Show image') % for internal purposes only
\r
556 I = varargin{length(varargin)}; % last argument always
\r
557 Rows = size(Image{I}, 1);
\r
558 Cols = size(Image{I}, 2);
\r
559 if Arguments % needs size setting
\r
560 Max_rc = max(Rows, Cols);
\r
561 Scr_sz = get(0, 'ScreenSize');
\r
562 % "tiling" the images on the screen into 3 by 4 grid
\r
563 Grid_v = mod(floor((I-1)/4), 3); % finding row
\r
564 Grid_h = mod(I+3, 4); % finding column
\r
565 New_position(1:2) = [Scr_sz(3)*(0.04+0.24*Grid_h) ...
\r
566 Scr_sz(4)*(0.08+0.31*Grid_v)];
\r
567 New_position(3) = Scr_sz(3)*0.22*Cols/Max_rc;
\r
568 New_position(4) = Scr_sz(4)*0.22*Rows/Max_rc;
\r
569 set(Main_figure{I}, 'Position', New_position);
\r
571 Axis_children = get(Main_axis{I}, 'Children');
\r
573 for i=length(Axis_children):-1:1
\r
574 if strcmp(get(Axis_children(i), 'Type'), 'image')
\r
575 Image_handle = Axis_children(i);
\r
579 if Image_is_gray{I}
\r
580 if not(Image_handle)
\r
581 Image_handle = image(Gray_image{I});
\r
582 set(Image_handle, 'Parent', Main_axis{I});
\r
583 axis([1, Cols, 1, Rows]);
\r
585 set(Image_handle, 'CData', Gray_image{I});
\r
586 axis([1, Cols, 1, Rows]);
\r
588 colormap(gray(255));
\r
590 if not(Image_handle)
\r
591 Image_handle = image(uint8(floor(double(Image{I})))); % no maps
\r
592 set(Image_handle, 'Parent', Main_axis{I});
\r
593 axis([1, Cols, 1, Rows]);
\r
595 set(Image_handle, 'CData', uint8(floor(double(Image{I}))));
\r
596 axis([1, Cols, 1, Rows]);
\r
599 set(Main_axis{I}, 'Position', [0 0 1 1], 'XTick', [], 'YTick', []);
\r
602 %----------------------------------------
\r
603 elseif strcmp(Action, 'Enable region')
\r
604 I = varargin{length(varargin)}; % last argument always
\r
605 if Arguments == 0 % call from interactive input
\r
607 Arguments = Region_left{I};
\r
608 Region_left{I} = 0;
\r
610 Regions{I}{Arguments}.Private.Active = 1;
\r
611 Segment_demo('Draw boundary', Arguments, I);
\r
614 %-----------------------------------------
\r
615 elseif strcmp(Action, 'Background region prompt')
\r
616 I = varargin{length(varargin)}; % last argument always
\r
617 Operation_prompt; % the common piece
\r
619 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Background region', 0, I);
\r
621 set(Ok_button, 'Callback', Callback);
\r
624 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Background region', 1, I);
\r
629 %----------------------------------------
\r
630 elseif strcmp(Action, 'Background region')
\r
631 I = varargin{length(varargin)}; % last argument always
\r
632 if Arguments == 0 % from interactive
\r
635 [Rows, Cols] = size(Regions{I}{Region_left{I}}.Public.Region_matr);
\r
636 Shift = Regions{I}{Region_left{I}}.Public.Shifts(1:2, 1);
\r
637 if Image_is_gray{I}
\r
640 if Regions{I}{Region_left{I}}.Public.Region_matr(i, j)
\r
641 Image{I}(i+Shift(1)-1, j+Shift(2)-1) = 0;
\r
642 Gray_image{I}(i+Shift(1)-1, j+Shift(2)-1) = 0;
\r
649 if Regions{I}{Region_left{I}}.Public.Region_matr(i, j)
\r
650 Image{I}(i+Shift(1)-1, j+Shift(2)-1, :) = [0, 0, 0];
\r
655 Segment_demo('Draw boundary', Region_left{I}, 'none', I);
\r
656 Region_left{I} = 0;
\r
657 Segment_demo('Show image', 0, I);
\r
660 %-----------------------------------------
\r
661 elseif strcmp(Action, 'Color prompt')
\r
662 I = varargin{length(varargin)}; % last argument always
\r
663 Operation_prompt; % the common piece
\r
665 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region color prompt', 0, I);
\r
667 set(Ok_button, 'Callback', Callback);
\r
670 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region color prompt', 1, I);
\r
675 %-----------------------------------------
\r
676 elseif strcmp(Action, 'Region color prompt')
\r
677 I = varargin{length(varargin)}; % last argument always
\r
678 if Arguments == 0 % from interactive
\r
681 Scr_sz = get(0, 'ScreenSize');
\r
682 New_wind = dialog('Name', 'Choose region color', 'Position', ...
\r
683 [Scr_sz(3)*0.3 Scr_sz(4)*0.4 Scr_sz(3)*0.3 Scr_sz(4)*0.1]);
\r
684 set(New_wind, 'WindowStyle', 'normal');
\r
685 Choice_color = uicontrol('Parent', New_wind);
\r
686 set(Choice_color, 'Style', 'Popup', 'String', Region_colors, ...
\r
687 'Units', 'Normalized', 'Position', [0 0.5 0.45 0.5]);
\r
688 Cancel_button = uicontrol('Parent', New_wind, ...
\r
689 'String', 'Cancel', 'Style', 'Pushbutton', 'Callback', 'close', ...
\r
690 'Units', 'Normalized', 'Position', [0.6 0.3 0.3 0.4]);
\r
691 Callback=sprintf('Segment_demo(''%s'',%18.16f,%d)', 'Region color', Choice_color, I);
\r
692 Ok_button = uicontrol('Parent', New_wind, ...
\r
693 'String', 'OK', 'Style', 'Pushbutton', 'Callback', Callback, ...
\r
694 'Units', 'Normalized', 'Position', [0.1 0.3 0.3 0.4]);
\r
697 %-----------------------------------------
\r
698 elseif strcmp(Action, 'Region color')
\r
699 I = varargin{length(varargin)}; % last argument always
\r
700 Index_color = get(Arguments, 'Value');
\r
701 Index_string = get(Arguments, 'String');
\r
702 close; % input window
\r
703 Regions{I}{Region_left{I}}.Private.Color = Index_string{Index_color};
\r
704 Segment_demo('Draw boundary', Region_left{I}, I);
\r
705 Region_left{I} = 0;
\r
708 %-----------------------------------------
\r
709 elseif strcmp(Action, 'Select left region')
\r
710 I = varargin{length(varargin)}; % last argument always
\r
711 Curr_pt_local = get(gca, 'CurrentPoint');
\r
712 Curr_pt_local = round(Curr_pt_local(1, 1:2));
\r
713 Curr_pt_local = Curr_pt_local(2:-1:1);
\r
714 for i=Bndred_regions{I}:-1:1
\r
715 Min_x = Regions{I}{i}.Public.Shifts(1, 1);
\r
716 Min_y = Regions{I}{i}.Public.Shifts(2, 1);
\r
717 Max_x = Regions{I}{i}.Public.Shifts(1, 2);
\r
718 Max_y = Regions{I}{i}.Public.Shifts(2, 2);
\r
719 if Curr_pt_local(1) >= Min_x & Curr_pt_local(1) <= Max_x
\r
720 if Curr_pt_local(2) >= Min_y & Curr_pt_local(2) <= Max_y
\r
721 if Regions{I}{i}.Public.Region_matr(...
\r
722 Curr_pt_local(1)-Min_x+1, Curr_pt_local(2)-Min_y+1) > 0
\r
723 % then the region has been found
\r
724 if Region_left{I} % was assigned some value
\r
725 if not(Region_left{I} == Region_right{I})
\r
726 Segment_demo('Draw boundary', Region_left{I}, I);
\r
729 if Region_left{I} == i % this was selected, now is deselected
\r
730 Region_left{I} = 0;
\r
732 Region_left{I} = i;
\r
733 Segment_demo('Draw boundary', Region_left{I}, 'white', I);
\r
742 %-----------------------------------------
\r
743 elseif strcmp(Action, 'Select right region')
\r
744 I = varargin{length(varargin)}; % last argument always
\r
745 Curr_pt_local = get(gca, 'CurrentPoint');
\r
746 Curr_pt_local = round(Curr_pt_local(1, 1:2));
\r
747 Curr_pt_local = Curr_pt_local(2:-1:1);
\r
748 for i=Bndred_regions{I}:-1:1
\r
749 Min_x = Regions{I}{i}.Public.Shifts(1, 1);
\r
750 Min_y = Regions{I}{i}.Public.Shifts(2, 1);
\r
751 Max_x = Regions{I}{i}.Public.Shifts(1, 2);
\r
752 Max_y = Regions{I}{i}.Public.Shifts(2, 2);
\r
753 if Curr_pt_local(1) >= Min_x & Curr_pt_local(1) <= Max_x
\r
754 if Curr_pt_local(2) >= Min_y & Curr_pt_local(2) <= Max_y
\r
755 if Regions{I}{i}.Public.Region_matr(...
\r
756 Curr_pt_local(1)-Min_x+1, Curr_pt_local(2)-Min_y+1) > 0
\r
757 % then the region has been found
\r
758 if Region_right{I} % was assigned some value
\r
759 if not(Region_left{I} == Region_right{I})
\r
760 Segment_demo('Draw boundary', Region_right{I}, I);
\r
763 if Region_right{I} == i % this was selected, now is deselected
\r
764 Region_right{I} = 0;
\r
766 Region_right{I} = i;
\r
767 Segment_demo('Draw boundary', Region_right{I}, 'white', I);
\r
775 %----------------------------------------
\r
776 elseif strcmp(Action, 'Disable region')
\r
777 I = varargin{length(varargin)}; % last argument always
\r
778 if Arguments == 0 % call from interactive input
\r
780 Arguments = Region_left{I};
\r
781 Region_left{I} = 0;
\r
783 Regions{I}{Arguments}.Private.Active = 0;
\r
784 Segment_demo('Draw boundary', Arguments, 'none', I);
\r
785 Segment_demo('Refresh image', 0, I);
\r
788 %-----------------------------------------
\r
789 elseif strcmp(Action, 'Find boundary')
\r
790 I = varargin{length(varargin)}; % last argument always
\r
791 Rows = size(Image{I}, 1);
\r
792 Cols = size(Image{I}, 2);
\r
793 if length(Curr_pt{I}) > 0 % was defined
\r
794 if ((Curr_pt{I}(1) > 0) & (Curr_pt{I}(2) > 0) & ...
\r
795 (Curr_pt{I}(1) < Rows) & (Curr_pt{I}(2) < Cols))
\r
796 for i=1:size(Slices{I}, 3)
\r
797 if Slices{I}(Curr_pt{I}(1), Curr_pt{I}(2), i) > 0 % in this slice
\r
798 [Boundary, Area] = Extract_region_reg(Slices{I}(:, :, i) == ...
\r
799 Slices{I}(Curr_pt{I}(1),Curr_pt{I}(2), i), Min_region_size{I});
\r
805 if exist('Boundary', 'var')
\r
806 if size(Boundary, 2) > 1
\r
807 Bndred_regions{I} = Bndred_regions{I}+1;
\r
808 Regions{I}{Bndred_regions{I}}.Private.Number = Bndred_regions{I};
\r
809 Regions{I}{Bndred_regions{I}}.Public.Boundary = Boundary;
\r
810 Min_x = min(Boundary(1, :)); Max_x = max(Boundary(1, :));
\r
811 Min_y = min(Boundary(2, :)); Max_y = max(Boundary(2, :));
\r
812 Regions{I}{Bndred_regions{I}}.Public.Shifts = ...
\r
813 [Min_x, Max_x; Min_y, Max_y];
\r
814 Regions{I}{Bndred_regions{I}}.Public.Inner_pts = ...
\r
815 [Curr_pt{I}(1); Curr_pt{I}(2)];
\r
816 Regions{I}{Bndred_regions{I}}.Public.Region_matr = ...
\r
817 Region_matrix(Regions{I}{Bndred_regions{I}}.Public);
\r
818 Regions{I}{Bndred_regions{I}}.Private.Color = ...
\r
819 Region_colors{1, 1+rem(Bndred_regions{I}-1, size(Region_colors, 2))};
\r
820 % Regions{I}{Bndred_regions{I}}.Private.Color = ...
\r
822 Regions{I}{Bndred_regions{I}}.Private.Line = 0;
\r
823 Regions{I}{Bndred_regions{I}}.Private.From = [];
\r
824 % setting up variables for region info values
\r
825 Regions{I}{Bndred_regions{I}}.Public.Area = -99;
\r
826 Regions{I}{Bndred_regions{I}}.Public.Volume= -99;
\r
827 Regions{I}{Bndred_regions{I}}.Public.Avg_intensity = -99;
\r
828 Menu_children = get(Region_menu{I}, 'Children');
\r
829 for i=1:length(Menu_children)
\r
830 set (Menu_children(i), 'Enable', 'on');
\r
832 Segment_demo('Draw boundary first time', Bndred_regions{I}, I);
\r
833 Segment_demo('Enable region', Bndred_regions{I}, I);
\r
839 %-----------------------------------------
\r
840 elseif strcmp(Action, 'Save boundary prompt')
\r
841 I = varargin{length(varargin)}; % last argument always
\r
842 Operation_prompt; % the common piece
\r
844 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Save boundary', 0, I);
\r
846 set(Ok_button, 'Callback', Callback);
\r
849 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Save boundary', 1, I);
\r
854 %----------------------------------------
\r
855 elseif strcmp(Action, 'Save boundary')
\r
856 I = varargin{length(varargin)}; % last argument always
\r
857 if Arguments == 0 % from interactive
\r
861 [File_name, Path_name] = uiputfile(Filter, 'Save to ...');
\r
862 if File_name(1) > 0
\r
863 File_name = [Path_name, File_name];
\r
864 fid = fopen(File_name, 'w');
\r
865 fprintf(fid, '%d %d\n', Regions{I}{Region_left{I}}.Public.Inner_pts(1:2));
\r
866 fprintf(fid, '%d %d\n', 0, 0); % the separation for Inner pts
\r
867 fprintf (fid, '%d %d\n', Regions{I}{Region_left{I}}.Public.Boundary);
\r
870 Segment_demo('Draw boundary', Region_left{I}, I);
\r
871 Region_left{I} = 0;
\r
874 %-----------------------------------------
\r
875 elseif strcmp(Action, 'Compare window regions prompt')
\r
876 I = varargin{length(varargin)}; % last argument always
\r
877 if not(Window_regs_chosen) % need to open one more window
\r
878 Operation_prompt; % the common piece
\r
880 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare window regions', 0, I);
\r
882 set(Ok_button, 'Callback', Callback);
\r
885 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare window regions', 1, I);
\r
889 Operation_prompt_right;
\r
891 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare window regions', 0, I);
\r
893 set(Ok_button, 'Callback', Callback);
\r
896 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare window regions', 1, I);
\r
901 %-----------------------------------------
\r
902 elseif strcmp(Action, 'Compare window regions')
\r
903 I = varargin{length(varargin)}; % last argument always
\r
904 if Arguments == 0 % interactive input
\r
907 if not(Window_regs_chosen) % need to open one more window
\r
908 msgbox('Please choose another region', 'Another');
\r
910 Window_regs_chosen = 1;
\r
913 % computing chars if needed
\r
914 if Regions{Window1}{Region_left{Window1}}.Public.Area < 0
\r
915 Segment_demo('Intensity 3-d', Region_left{Window1}, Window1);
\r
917 if Regions{Window2}{Region_right{Window2}}.Public.Area < 0
\r
918 Segment_demo('Intensity 3-d', Region_right{Window2}, Window2);
\r
920 [Bdry_dist, Reg_dist] = ...
\r
921 Svd_dist_reg(Regions{Window1}{Region_left{Window1}}.Public, ...
\r
922 Regions{Window2}{Region_right{Window2}}.Public);
\r
923 Msg = [sprintf('Boundary difference: %6.2f\n\n', Bdry_dist), ...
\r
924 sprintf('Region difference: %6.2f\n\n', Reg_dist), ...
\r
925 sprintf('Areas ratio: %6.2f\n\n', ...
\r
926 Regions{Window1}{Region_left{Window1}}.Public.Area/...
\r
927 Regions{Window2}{Region_right{Window2}}.Public.Area), ...
\r
928 sprintf('Volumes ratio: %6.2f\n\n', ...
\r
929 Regions{Window1}{Region_left{Window1}}.Public.Volume/...
\r
930 Regions{Window2}{Region_right{Window2}}.Public.Volume), ...
\r
931 sprintf('Avg intensities ratio: %6.2f', ...
\r
932 Regions{Window1}{Region_left{Window1}}.Public.Avg_intensity/...
\r
933 Regions{Window2}{Region_right{Window2}}.Public.Avg_intensity)
\r
935 Wind_name = sprintf('%d and %d regions differences and ratios', ...
\r
936 Region_left{Window1}, Region_right{Window2});
\r
937 msgbox(Msg, Wind_name);
\r
939 Segment_demo('Draw boundary', Region_left{Window1}, Window1);
\r
940 Segment_demo('Draw boundary', Region_right{Window2}, Window2);
\r
941 Region_left{Window1} = 0; Region_right{Window2} = 0;
\r
942 Window_regs_chosen = 0;
\r
946 %-----------------------------------------
\r
947 elseif strcmp(Action, 'Compare image regions prompt')
\r
948 I = varargin{length(varargin)}; % last argument always
\r
949 Operation_prompt; % the common piece
\r
950 Right_button; % one more button here if Interactive
\r
952 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare image regions', 0, I);
\r
954 set(Ok_button, 'Callback', Callback);
\r
957 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare image regions', 1, I);
\r
961 %-----------------------------------------
\r
962 elseif strcmp(Action, 'Compare image regions')
\r
963 I = varargin{length(varargin)}; % last argument always
\r
964 if Arguments == 0 % interactive call
\r
967 if Region_right{I} == 0
\r
970 [Bdry_dist, Reg_dist] = ...
\r
971 Svd_dist_reg(Regions{I}{Region_left{I}}.Public, ...
\r
972 Regions{I}{Region_right{I}}.Public);
\r
973 % computing chars if needed
\r
974 if Regions{I}{Region_left{I}}.Public.Area < 0
\r
975 Segment_demo('Intensity 3-d', Region_left{I}, I);
\r
977 if Regions{I}{Region_right{I}}.Public.Area < 0
\r
978 Segment_demo('Intensity 3-d', Region_right{I}, I);
\r
980 [Bdry_dist, Reg_dist] = ...
\r
981 Svd_dist_reg(Regions{I}{Region_left{I}}.Public, ...
\r
982 Regions{I}{Region_right{I}}.Public);
\r
983 Msg = [sprintf('Boundary difference: %6.2f\n\n', Bdry_dist), ...
\r
984 sprintf('Region difference: %6.2f\n\n', Reg_dist), ...
\r
985 sprintf('Areas ratio: %6.2f\n\n', ...
\r
986 Regions{I}{Region_left{I}}.Public.Area/...
\r
987 Regions{I}{Region_right{I}}.Public.Area), ...
\r
988 sprintf('Volumes ratio: %6.2f\n\n', ...
\r
989 Regions{I}{Region_left{I}}.Public.Volume/...
\r
990 Regions{I}{Region_right{I}}.Public.Volume), ...
\r
991 sprintf('Avg intensities ratio: %6.2f', ...
\r
992 Regions{I}{Region_left{I}}.Public.Avg_intensity/...
\r
993 Regions{I}{Region_right{I}}.Public.Avg_intensity)
\r
995 Wind_name = sprintf('%d and %d regions differences and ratios', ...
\r
996 Region_left{I}, Region_right{I});
\r
997 msgbox(Msg, Wind_name);
\r
999 Segment_demo('Draw boundary', Region_left{I}, I);
\r
1000 Segment_demo('Draw boundary', Region_right{I}, I);
\r
1001 Region_left{I} = 0; Region_right{I} = 0;
\r
1004 %-----------------------------------------
\r
1005 elseif strcmp(Action, 'Compare disk regions prompt')
\r
1006 I = varargin{length(varargin)}; % last argument always
\r
1007 Operation_prompt; % the common piece
\r
1009 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare disk regions', 0, I);
\r
1011 set(Ok_button, 'Callback', Callback);
\r
1014 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Compare disk regions', 1, I);
\r
1018 %-----------------------------------------
\r
1019 elseif strcmp(Action, 'Compare disk regions')
\r
1020 I = varargin{length(varargin)}; % last argument always
\r
1021 if Arguments == 0 % interactive input
\r
1025 [File_name, Path_name] = uigetfile(Filter, 'Compare to ...');
\r
1026 if File_name(1) > 0
\r
1027 File_name = [Path_name, File_name];
\r
1028 fid = fopen(File_name, 'r');
\r
1030 Pt_scanned(1:2, 1) = fscanf(fid, '%d', [2, 1]);
\r
1031 while sum(Pt_scanned) > 0
\r
1033 Region2.Public.Inner_pts(:, k) = Pt_scanned;
\r
1034 Pt_scanned(1:2, 1) = fscanf(fid, '%d', [2, 1]);
\r
1036 Region2.Public.Boundary = fscanf(fid, '%d', [2, inf]);
\r
1037 Min_x = min(Region2.Public.Boundary(1, :));
\r
1038 Max_x = max(Region2.Public.Boundary(1, :));
\r
1039 Min_y = min(Region2.Public.Boundary(2, :));
\r
1040 Max_y = max(Region2.Public.Boundary(2, :));
\r
1041 Region2.Public.Shifts = [Min_x, Max_x; Min_y, Max_y];
\r
1043 [Bdry_dist, Reg_dist] = ...
\r
1044 Svd_dist_reg(Regions{I}{Region_left{I}}.Public, Region2.Public);
\r
1045 Msg = [sprintf('Boundary difference is %6.2f\n\n', Bdry_dist), ...
\r
1046 sprintf('Region difference is %6.2f', Reg_dist)];
\r
1047 msgbox(Msg, 'Difference');
\r
1049 Segment_demo('Draw boundary', Region_left{I}, I);
\r
1050 Region_left{I} = 0;
\r
1053 %-----------------------------------------
\r
1054 elseif strcmp(Action, 'Intensity 3-d prompt')
\r
1055 I = varargin{length(varargin)}; % last argument always
\r
1056 Operation_prompt; % the common piece
\r
1058 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Intensity 3-d', 0, I);
\r
1060 set(Ok_button, 'Callback', Callback);
\r
1063 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Intensity 3-d', -1, I);
\r
1067 %-----------------------------------------
\r
1068 elseif strcmp(Action, 'Intensity 3-d')
\r
1069 I = varargin{length(varargin)}; % last argument always
\r
1070 if Arguments == 0 % interactive call
\r
1071 Region_number = Region_left{I};
\r
1073 elseif Arguments == -1 % was selected not by menu
\r
1074 Region_number = Region_left{I};
\r
1076 Region_number = Arguments;
\r
1080 [Rows, Cols] = size(Regions{I}{Region_number}.Public.Region_matr);
\r
1081 To_display = zeros(Cols, Rows);
\r
1082 Shift = Regions{I}{Region_number}.Public.Shifts(1:2, 1);
\r
1085 if Regions{I}{Region_number}.Public.Region_matr(i, j)
\r
1086 To_display(j, i) = Gray_image{I}(i+Shift(1)-1, j+Shift(2)-1);
\r
1087 Volume = Volume+To_display(j, i);
\r
1092 Min = min(To_display(find(To_display > 0)));
\r
1093 To_display = max(To_display, ones(size(To_display))*Min);
\r
1096 set(gca, 'XTick', [], 'YTick', [], 'view', [77, 67]); % a better viewpoint
\r
1097 zlabel('Intensity');
\r
1098 Wind_name = sprintf('Region %d Intensity 3-d', Region_number);
\r
1099 set(gcf, 'MenuBar', 'none', 'Name', Wind_name);
\r
1100 Rotate_menu = uimenu('Label','&Rotate');
\r
1101 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate left', 0, I);
\r
1102 uimenu (Rotate_menu, 'Label', 'Left', ...
\r
1103 'Callback', Callback, 'Accelerator', 'L');
\r
1104 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate right', 0, I);
\r
1105 uimenu (Rotate_menu, 'Label', 'Right', ...
\r
1106 'Callback', Callback, 'Accelerator', 'R');
\r
1107 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate up', 0, I);
\r
1108 uimenu (Rotate_menu, 'Label', 'Up', ...
\r
1109 'Callback', Callback, 'Accelerator', 'U');
\r
1110 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate down', 0, I);
\r
1111 uimenu (Rotate_menu, 'Label', 'Down', ...
\r
1112 'Callback', Callback, 'Accelerator', 'D');
\r
1113 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Increase rotation', 0, I);
\r
1114 uimenu (Rotate_menu, 'Label', 'Increase', ...
\r
1115 'Callback', Callback, 'Accelerator', 'A');
\r
1116 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Decrease rotation', 0, I);
\r
1117 uimenu (Rotate_menu, 'Label', 'Decrease', ...
\r
1118 'Callback', Callback, 'Accelerator', 'V');
\r
1120 Max = max(max(To_display));
\r
1122 Regions{I}{Region_number}.Public.Area = Area; % storing area
\r
1123 Area_string = sprintf('Region area\t%10.2f', Area);
\r
1124 Regions{I}{Region_number}.Public.Volume = Volume; % storing volume
\r
1125 Volume_string = sprintf('Volume\t%10.2f', Volume); % compute avg intens
\r
1126 Avg_intensity = Volume/Area;
\r
1127 Regions{I}{Region_number}.Public.Avg_intensity = Avg_intensity;
\r
1128 Avg_intensity_string = sprintf('Average intensity\t%10.2f', Avg_intensity);
\r
1129 Intensity_normed_volume_string = ...
\r
1130 sprintf('Intensity normalized volume\t%10.2f', Volume/Max);
\r
1132 Values_menu = uimenu('Label', '&Values');
\r
1133 uimenu (Values_menu, 'Label', Area_string);
\r
1134 uimenu (Values_menu, 'Label', Volume_string);
\r
1135 uimenu (Values_menu, 'Label', Avg_intensity_string);
\r
1136 uimenu (Values_menu, 'Label', Intensity_normed_volume_string);
\r
1138 if Arguments <= 0 % interactive
\r
1139 Segment_demo('Draw boundary', Region_left{I}, I);
\r
1140 Region_left{I} = 0;
\r
1143 %-----------------------------------------
\r
1144 elseif strcmp(Action, 'Rotate right')
\r
1145 I = varargin{length(varargin)}; % last argument always
\r
1146 Az_el = get(gca, 'View');
\r
1147 set(gca, 'View', [Az_el(1)-Amnt_rot{I}, Az_el(2)]);
\r
1149 %-----------------------------------------
\r
1150 elseif strcmp(Action, 'Rotate left')
\r
1151 I = varargin{length(varargin)}; % last argument always
\r
1152 Az_el = get(gca, 'View');
\r
1153 set(gca, 'View', [Az_el(1)+Amnt_rot{I}, Az_el(2)]);
\r
1155 %-----------------------------------------
\r
1156 elseif strcmp(Action, 'Rotate up')
\r
1157 I = varargin{length(varargin)}; % last argument always
\r
1158 Az_el = get(gca, 'View');
\r
1159 set(gca, 'View', [Az_el(1), Az_el(2)-Amnt_rot{I}]);
\r
1161 %-----------------------------------------
\r
1162 elseif strcmp(Action, 'Rotate down')
\r
1163 I = varargin{length(varargin)}; % last argument always
\r
1164 Az_el = get(gca, 'View');
\r
1165 set(gca, 'View', [Az_el(1), Az_el(2)+Amnt_rot{I}]);
\r
1167 %-----------------------------------------
\r
1168 elseif strcmp(Action, 'Increase rotation')
\r
1169 I = varargin{length(varargin)}; % last argument always
\r
1170 Amnt_rot{I} = Amnt_rot{I}*2;
\r
1172 %-----------------------------------------
\r
1173 elseif strcmp(Action, 'Decrease rotation')
\r
1174 I = varargin{length(varargin)}; % last argument always
\r
1175 Amnt_rot{I} = Amnt_rot{I}/2;
\r
1178 %-----------------------------------------
\r
1179 elseif strcmp(Action, 'Cut prompt')
\r
1180 I = varargin{length(varargin)}; % last argument always
\r
1181 Operation_prompt; % the common piece
\r
1183 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region cut prompt', 0, I);
\r
1185 set(Ok_button, 'Callback', Callback);
\r
1188 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region cut prompt', 1, I);
\r
1192 %-----------------------------------------
\r
1193 elseif strcmp(Action, 'Region cut prompt')
\r
1194 I = varargin{length(varargin)}; % last argument always
\r
1195 if Arguments == 0 % interactive input
\r
1198 Scr_sz = get(0, 'ScreenSize');
\r
1199 New_wind = dialog('Name', 'Draw cutting line', 'Position', ...
\r
1200 [Scr_sz(3)*0.3 Scr_sz(4)*0.4 Scr_sz(3)*0.3 Scr_sz(4)*0.1]);
\r
1201 set(New_wind, 'WindowStyle', 'normal');
\r
1202 Cancel_button = uicontrol('Parent', New_wind, ...
\r
1203 'String', 'Cancel', 'Style', 'Pushbutton', 'Callback', 'close', ...
\r
1204 'Units', 'Normalized', 'Position', [0.6 0.3 0.3 0.4]);
\r
1205 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Cut', 0, I);
\r
1206 Ok_button = uicontrol('Parent', New_wind, ...
\r
1207 'String', 'OK', 'Style', 'Pushbutton', 'Callback', Callback, ...
\r
1208 'Units', 'Normalized', 'Position', [0.1 0.3 0.3 0.4]);
\r
1209 Store_mouse_pts{I} = 1;
\r
1210 Curr_pt{I}= zeros(2, 0);
\r
1212 %-----------------------------------------
\r
1213 elseif strcmp(Action, 'Cut')
\r
1214 I = varargin{length(varargin)}; % last argument always
\r
1215 close; % the dialogue window
\r
1216 Dense_pts = Fit_spline(Curr_pt{I});
\r
1218 [Bdry{1}, Inner{1}, Bdry{2}, Inner{2}] = ...
\r
1219 Cut_boundary_reg(Regions{I}{Region_left{I}}.Public, Dense_pts);
\r
1220 if (size(Bdry{1}, 2) > 1) & (size(Bdry{2}, 2) > 1) % detected boundaries
\r
1222 Bndred_regions{I} = Bndred_regions{I}+1;
\r
1223 Regions{I}{Bndred_regions{I}}.Public.Boundary = Bdry{i};
\r
1224 Regions{I}{Bndred_regions{I}}.Public.Inner_pts = Inner{i};
\r
1225 Min_x = min(Regions{I}{Bndred_regions{I}}.Public.Boundary(1, :));
\r
1226 Max_x = max(Regions{I}{Bndred_regions{I}}.Public.Boundary(1, :));
\r
1227 Min_y = min(Regions{I}{Bndred_regions{I}}.Public.Boundary(2, :));
\r
1228 Max_y = max(Regions{I}{Bndred_regions{I}}.Public.Boundary(2, :));
\r
1229 Regions{I}{Bndred_regions{I}}.Public.Shifts = ...
\r
1230 [Min_x, Max_x; Min_y, Max_y];
\r
1231 Regions{I}{Bndred_regions{I}}.Private.Number = Bndred_regions{I};
\r
1232 Regions{I}{Bndred_regions{I}}.Public.Region_matr = ...
\r
1233 Region_matrix(Regions{I}{Bndred_regions{I}}.Public);
\r
1234 Regions{I}{Bndred_regions{I}}.Private.Color = ...
\r
1235 Region_colors{1, 1+rem(Bndred_regions{I}-1,size(Region_colors, 2))};
\r
1236 Regions{I}{Bndred_regions{I}}.Private.From = [Region_left{I}];
\r
1237 Regions{I}{Bndred_regions{I}}.Public.Area = -99;
\r
1238 Regions{I}{Bndred_regions{I}}.Public.Volume= -99;
\r
1239 Regions{I}{Bndred_regions{I}}.Public.Avg_intensity = -99;
\r
1240 Segment_demo('Draw boundary first time', Bndred_regions{I}, I);% Line
\r
1241 Segment_demo('Enable region', Bndred_regions{I}, I);
\r
1243 Segment_demo('Disable region', Region_left{I}, I);
\r
1246 Region_left{I} = 0;
\r
1247 Store_mouse_pts{I} = 0;
\r
1248 set(Curr_pt_line{I}, 'EraseMode', 'normal');
\r
1249 set(Curr_pt_line{I}, 'Visible', 'off');
\r
1250 Curr_pt_line{I} = 0;
\r
1251 Segment_demo('Down', 0, I);
\r
1252 Segment_demo('Refresh', 0, I);
\r
1255 % ----------------------------------------- "left" region
\r
1256 elseif strcmp(Action, 'Left')
\r
1257 I = varargin{length(varargin)}; % last argument always
\r
1258 Index_left = get(Arguments, 'Value');
\r
1259 Labels = get(Arguments, 'String');
\r
1260 Temp_region_left = base2dec(Labels{Index_left}, 10);
\r
1261 % restoring the old region first
\r
1262 if Region_left{I} % was assigned some value
\r
1263 if not(Region_left{I} == Region_right{I})
\r
1264 Segment_demo('Draw boundary', Region_left{I}, I);
\r
1267 Region_left{I} = Temp_region_left;
\r
1268 Segment_demo('Draw boundary', Region_left{I}, 'white', I);
\r
1271 %-----------------------------------------
\r
1273 elseif strcmp(Action, 'Right')
\r
1274 I = varargin{length(varargin)}; % last argument always
\r
1275 Index_right = get(Arguments, 'Value');
\r
1276 Labels = get(Arguments, 'String');
\r
1277 Temp_region_right = base2dec(Labels{Index_right}, 10);
\r
1278 % restoring the old region first
\r
1279 if Region_right{I} % was defined
\r
1280 if not(Region_left{I} == Region_right{I})
\r
1281 Segment_demo('Draw boundary', Region_right{I}, I);
\r
1284 Region_right{I} = Temp_region_right;
\r
1285 Segment_demo('Draw boundary', Region_right{I}, 'white', I);
\r
1288 %-----------------------------------------
\r
1289 elseif strcmp(Action, 'Cancel operation') % don't perform operation
\r
1290 I = varargin{length(varargin)}; % last argument always
\r
1293 Segment_demo('Draw boundary', Region_left{I}, I);
\r
1294 Region_left{I} = 0;
\r
1296 if Region_right{I}
\r
1297 Segment_demo('Draw boundary', Region_right{I}, I);
\r
1298 Region_right{I} = 0;
\r
1300 Store_mouse_pts{I} = 0;
\r
1303 %-----------------------------------------
\r
1304 elseif strcmp(Action, 'Merge prompt')
\r
1305 I = varargin{length(varargin)}; % last argument always
\r
1306 Operation_prompt; % the common piece
\r
1307 Right_button; % one more button here
\r
1309 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Merge', 0, I);
\r
1311 set(Ok_button, 'Callback', Callback);
\r
1314 Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Merge', 1, I);
\r
1318 %-----------------------------------------
\r
1319 elseif strcmp(Action, 'Merge') % the two regions have been defined
\r
1320 I = varargin{length(varargin)}; % last argument always
\r
1321 if Arguments == 0 % interactive call
\r
1324 Bndred_regions{I} = Bndred_regions{I}+1;
\r
1325 [Regions{I}{Bndred_regions{I}}.Public.Inner_pts, ...
\r
1326 Regions{I}{Bndred_regions{I}}.Public.Boundary, ...
\r
1327 Regions{I}{Bndred_regions{I}}.Public.Shifts] = Merge_regions(...
\r
1328 Regions{I}{Region_left{I}}.Public, Regions{I}{Region_right{I}}.Public);
\r
1329 Regions{I}{Bndred_regions{I}}.Public.Region_matr = ...
\r
1330 Region_matrix(Regions{I}{Bndred_regions{I}}.Public);
\r
1331 Regions{I}{Bndred_regions{I}}.Private.Color = ...
\r
1332 Region_colors{1, 1+rem(Bndred_regions{I}-1, size(Region_colors, 2))};
\r
1333 Regions{I}{Bndred_regions{I}}.Private.Number = Bndred_regions{I};
\r
1334 Regions{I}{Bndred_regions{I}}.Private.From=[Region_left{I}, Region_right{I}];
\r
1335 Regions{I}{Bndred_regions{I}}.Public.Area = -99;
\r
1336 Regions{I}{Bndred_regions{I}}.Public.Volume = -99;
\r
1337 Regions{I}{Bndred_regions{I}}.Public.Avg_intensity = -99;
\r
1338 Segment_demo('Draw boundary first time', Bndred_regions{I}, I);
\r
1340 Segment_demo('Enable region', Bndred_regions{I}, I);
\r
1342 Segment_demo('Disable region', Region_left{I}, I);
\r
1343 Segment_demo('Disable region', Region_right{I}, I);
\r
1344 Region_left{I} = 0; Region_right{I} = 0;
\r
1347 % --------------------
\r
1348 elseif strcmp(Action, 'Draw boundary first time')
\r
1349 I = varargin{length(varargin)}; % last argument always
\r
1350 Regions{I}{Arguments}.Private.Line = ...
\r
1351 line('XData', Regions{I}{Arguments}.Public.Boundary(2, :), ...
\r
1352 'YData', Regions{I}{Arguments}.Public.Boundary(1, :), ...
\r
1353 'Parent', Main_axis{I}, 'Color', Regions{I}{Arguments}.Private.Color, ...
\r
1354 'Marker', '.', 'LineStyle', '.', 'EraseMode', 'none', ...
\r
1355 'Visible', 'off', 'LineWidth', Marker_size, 'MarkerSize', Marker_size);
\r
1357 % -------------------- since repeates so often
\r
1358 elseif strcmp(Action, 'Draw boundary')
\r
1359 I = varargin{length(varargin)}; % last argument always
\r
1360 if length(varargin) == 1 % draw the region in the usual color
\r
1361 if Regions{I}{Arguments}.Private.Active
\r
1362 set(Regions{I}{Arguments}.Private.Line, 'Visible', 'off');
\r
1363 set(Regions{I}{Arguments}.Private.Line, ...
\r
1364 'Color', Regions{I}{Arguments}.Private.Color, 'Visible', 'on');
\r
1366 set(Regions{I}{Arguments}.Private.Line, 'Visible', 'off');
\r
1369 if strcmp(varargin{1}, 'white') % draw in white
\r
1370 set(Regions{I}{Arguments}.Private.Line, ...
\r
1371 'Color', 'white', 'Visible', 'on');
\r
1372 elseif strcmp(varargin{1}, 'none') % don't draw
\r
1373 set(Regions{I}{Arguments}.Private.Line, 'Visible', 'off');
\r
1378 % let's represent regions (whose boundaries we were asked to find) by
\r
1379 % those boundaries and make the merging part as filling those
\r
1380 % boundaries and taking union as it was done before. In this fashion
\r
1381 % we can handle new regions, not to abuse the original image. Merging
\r
1382 % will be done on pairs of regions resulting in a new region
\r