📄 segment_demo.m
字号:
Callback = sprintf('Segment_demo(''Smooth'', %18.16f, %d)', Choice, I);
set(Choice, 'Style', 'Popup', ...
'String', {'Circle-5', 'Symlet', 'Avg-3'}, ...
'Units', 'Normalized', 'Position', [0 0.5 1 0.5]);
Cancel_button = uicontrol('Parent', New_wind, ...
'String', 'Cancel', 'Style', 'Pushbutton', 'Callback', 'close', ...
'Units', 'Normalized', 'Position', [0.6 0.3 0.3 0.4]);
Ok_button = uicontrol('Parent', New_wind, ...
'String', 'OK', 'Style', 'Pushbutton', 'Callback', Callback, ...
'Units', 'Normalized', 'Position', [0.1 0.3 0.3 0.4]);
%----------------------------------------
elseif strcmp(Action, 'Smooth')
I = varargin{length(varargin)}; % last argument always
Choice = get(Arguments, 'Value');
close; % closing the prompt window
switch Choice
case 2
Filter = 'Symlets';
case 3
Filter = ones(3);
Filter = Filter/sum(sum(Filter));
case 1
Filter = ones(5);
Filter(1:2, 1) = zeros(2, 1); Filter(4:5, 1) = zeros(2, 1);
Filter(1:2, 5) = zeros(2, 1); Filter(4:5, 5) = zeros(2, 1);
Filter(1, 2) = 0; Filter(5, 2) = 0;
Filter(2, 5) = 0; Filter(5, 4) = 0;
Filter = Filter/sum(sum(Filter));
otherwise
Filter = 1;
end
if size(Filter, 2) > 1
if strcmp(Filter, 'Symlets')
if Image_is_gray{I}
Image_filtered = Smooth_wv(Gray_image{I});
else
for i=1:3
Image_filtered(:, :, i) = Smooth_wv(Image{I}(:, :, i));
end
end
else
if Image_is_gray{I}
Image_filtered = filter2(Filter, Gray_image{I});
else
for i=1:3
Image_filtered(:, :, i) = filter2(Filter, Image{I}(:, :, i));
end
end
end
Segment_demo ('Initialize', Image_filtered, 0, I); % no new window
end
%-----------------------------------------
% now saves both in low-quality and high quality formats in jpeg/tif/bmp and
% in eps format with a standard size
elseif strcmp(Action, 'Save image')
I = varargin{length(varargin)}; % last argument always
% Filter = '*.jpeg; *.jpg; *.tif; *.tiff; *.bmp; *.eps; *.ps';
Filter = '*.jpeg; *.jpg; *.tif; *.bmp; *.eps';
[File_name, Path_name] = uiputfile(Filter, 'Save to file ...');
[Screen, Screen_map] = capture(Main_figure{I});
% splitting file name into extension and file itself
for i=length(File_name):-1:1
if File_name(i) == '.'
File_type = File_name(i+1:length(File_name));
File_name = File_name(1:i-1);
break;
end
end
if File_name(1) > 0
if (strcmp(File_type, 'jpeg') | strcmp(File_type, 'jpg'))
% low-level file first
imwrite(Screen, Screen_map, [Path_name, File_name, '_low.', File_type], ...
File_type, 'Quality', 80);
print_string = sprintf('%s %s', 'print -djpeg ', ...
[Path_name, File_name, '_high.', File_type]);
eval(print_string);
msgbox(['Image was saved in ', File_name, '_low.', File_type, ...
' and ', File_name, '_high.', File_type], 'Files saved');
end
if (strcmp(File_type, 'tiff') | strcmp(File_type, 'tif'))
% low-level file first
imwrite(Screen, Screen_map, [Path_name, File_name, '_low.', File_type], ...
File_type);
print_string = sprintf('%s %s', 'print -dtiff ', ...
[Path_name, File_name, '_high.', File_type]);
eval(print_string);
msgbox(['Image was saved in ', File_name, '_low.', File_type, ...
' and ', File_name, '_high.', File_type], 'Files saved');
end
if strcmp(File_type, 'bmp')
% low-level file first
imwrite(Screen, Screen_map, [Path_name, File_name, '.', File_type], ...
File_type);
print_string = sprintf('%s %s', 'print -djpeg ', ...
[Path_name, File_name, '.jpg']);
eval(print_string);
msgbox(['Image was saved in ', File_name, '.', File_type, ...
' and ', File_name, '.jpg'], 'Files saved');
end
if (strcmp(File_type, 'eps') | strcmp(File_type, 'ps'))
print_string = sprintf('%s %s', 'print -depsc -loose ', ...
[Path_name, File_name, '.', File_type]);
eval(print_string);
msgbox(['Image was saved in ', File_name, '.', File_type], ...
'Files saved');
end
end
%-----------------------------------------
elseif strcmp(Action, 'Read image')
Filter = '*.jpeg; *.jpg; *.tif; *.tiff; *.bmp';
[File_name, Path_name] = uigetfile(Filter, 'Open file');
Image_name = File_name;
File_name = [Path_name, File_name];
if File_name(1) > 0 % not zeros there
[Im_read, Map] = imread(File_name);
if length(Map) > 0 % not gray or RGB, but could be made such
Im_read = ind2rgb(Im_read, Map);
Im_read = 256*Im_read;
end
Im_read = double(Im_read);
if length(size(Im_read)) == 3 % color image
for i=1:3
Img(:,:,i) = min(Im_read(:,:,i)+ones(size(Im_read(:,:,i))), ...
255*ones(size(Im_read(:,:,i))));
end
else
Img = min(Im_read+ones(size(Im_read)),255*ones(size(Im_read)));
end
figure; % creating one more window
set (gcf, 'Name', Image_name);
if length(Instance) > 0 % was defined
Instance = Instance+1;
Active_instances = Active_instances+1;
else
Instance = 1;
Active_instances = 1;
end
Segment_demo('Initialize', Img, 1, Instance); % needs new window
end
%-----------------------------------------
elseif strcmp(Action, 'Refresh image')
I = varargin{length(varargin)}; % last argument always
if length(Curr_pt_line) >= I
if Curr_pt_line{I} > 0
set(Curr_pt_line{I}, 'EraseMode', 'normal');
set(Curr_pt_line{I}, 'Visible', 'off');
set(Curr_pt_line{I}, 'Visible', 'on');
set(Curr_pt_line{I}, 'EraseMode', 'none');
end
end
% deleting rectangles if present - they have few points
Kids = get(Main_axis{I}, 'Children');
for i=1:length(Kids)
if strcmp(get(Kids(i), 'type'), 'line') & ...
length(get(Kids(i), 'XData')) == 5 % rectangle
set(Kids(i), 'XData', 0, 'YData', 0, 'Visible', 'off');
end
end
%-----------------------------------------
% labeling the image at a chosen place
elseif strcmp(Action, 'Label prompt')
I = varargin{length(varargin)}; % last argument always
% determining whether near an existing label
Curr_pt_local = get(gca, 'CurrentPoint');
Curr_pt_local = round(Curr_pt_local(1, 1:2));
kids = get(gca, 'children');
for i=1:length(kids)
if strcmp(get(kids(i), 'type'), 'text') & ...
strcmp(get(kids(i), 'visible'), 'on') & ...
length(get(kids(i), 'string')) > 0
text_pos = get(kids(i), 'position');
text_pos = text_pos(1:2);
if norm(Curr_pt_local-text_pos)<8
set(kids(i), 'editing', 'on');
return;
end
end
end
Prompt = {'Label', 'Font size', 'Font type', 'Font color', 'Rotated'};
Default = {'Region ', 10, 'Courier', 'black', 0};
Title = 'Labeling image';
Line_number = 1;
Label_input = My_inputdlg(Prompt, Title, Line_number, Default);
if size(Label_input, 2) > 0
new_text = text(Curr_pt_local(1), Curr_pt_local(2), Label_input{1});
set(new_text, 'fontsize', base2dec(Label_input{2}, 10), ...
'fontname', Label_input{3}, 'color', Label_input{4}, ...
'rotation', base2dec(Label_input{5}, 10));
Button = questdlg('Is it suitable?', 'Labeling', 'Yes', 'No', 'Yes');
if strcmp(Button, 'No')
set(new_text, 'visible', 'off');
end
end
%-----------------------------------------
elseif strcmp(Action, 'Show image') % for internal purposes only
I = varargin{length(varargin)}; % last argument always
Rows = size(Image{I}, 1);
Cols = size(Image{I}, 2);
if Arguments % needs size setting
Max_rc = max(Rows, Cols);
Scr_sz = get(0, 'ScreenSize');
% "tiling" the images on the screen into 3 by 4 grid
Grid_v = mod(floor((I-1)/4), 3); % finding row
Grid_h = mod(I+3, 4); % finding column
New_position(1:2) = [Scr_sz(3)*(0.04+0.24*Grid_h) ...
Scr_sz(4)*(0.08+0.31*Grid_v)];
New_position(3) = Scr_sz(3)*0.22*Cols/Max_rc;
New_position(4) = Scr_sz(4)*0.22*Rows/Max_rc;
set(Main_figure{I}, 'Position', New_position);
end
Axis_children = get(Main_axis{I}, 'Children');
Image_handle = 0;
for i=length(Axis_children):-1:1
if strcmp(get(Axis_children(i), 'Type'), 'image')
Image_handle = Axis_children(i);
break;
end
end
if Image_is_gray{I}
if not(Image_handle)
Image_handle = image(Gray_image{I});
set(Image_handle, 'Parent', Main_axis{I});
axis([1, Cols, 1, Rows]);
else
set(Image_handle, 'CData', Gray_image{I});
axis([1, Cols, 1, Rows]);
end
colormap(gray(255));
else
if not(Image_handle)
Image_handle = image(uint8(floor(double(Image{I})))); % no maps
set(Image_handle, 'Parent', Main_axis{I});
axis([1, Cols, 1, Rows]);
else
set(Image_handle, 'CData', uint8(floor(double(Image{I}))));
axis([1, Cols, 1, Rows]);
end
end
set(Main_axis{I}, 'Position', [0 0 1 1], 'XTick', [], 'YTick', []);
%----------------------------------------
elseif strcmp(Action, 'Enable region')
I = varargin{length(varargin)}; % last argument always
if Arguments == 0 % call from interactive input
close; % prompt
Arguments = Region_left{I};
Region_left{I} = 0;
end
Regions{I}{Arguments}.Private.Active = 1;
Segment_demo('Draw boundary', Arguments, I);
%-----------------------------------------
elseif strcmp(Action, 'Background region prompt')
I = varargin{length(varargin)}; % last argument always
Operation_prompt; % the common piece
if Interactive
Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Background region', 0, I);
if Ok_button
set(Ok_button, 'Callback', Callback);
end
else
Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Background region', 1, I);
eval(Callback);
end
%----------------------------------------
elseif strcmp(Action, 'Background region')
I = varargin{length(varargin)}; % last argument always
if Arguments == 0 % from interactive
close; % prompt
end
[Rows, Cols] = size(Regions{I}{Region_left{I}}.Public.Region_matr);
Shift = Regions{I}{Region_left{I}}.Public.Shifts(1:2, 1);
if Image_is_gray{I}
for i=1:Rows
for j=1:Cols
if Regions{I}{Region_left{I}}.Public.Region_matr(i, j)
Image{I}(i+Shift(1)-1, j+Shift(2)-1) = 0;
Gray_image{I}(i+Shift(1)-1, j+Shift(2)-1) = 0;
end
end
end
else
for i=1:Rows
for j=1:Cols
if Regions{I}{Region_left{I}}.Public.Region_matr(i, j)
Image{I}(i+Shift(1)-1, j+Shift(2)-1, :) = [0, 0, 0];
end
end
end
end
Segment_demo('Draw boundary', Region_left{I}, 'none', I);
Region_left{I} = 0;
Segment_demo('Show image', 0, I);
%-----------------------------------------
elseif strcmp(Action, 'Color prompt')
I = varargin{length(varargin)}; % last argument always
Operation_prompt; % the common piece
if Interactive
Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region color prompt', 0, I);
if Ok_button
set(Ok_button, 'Callback', Callback);
end
else
Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region color prompt', 1, I);
eval(Callback);
end
%-----------------------------------------
elseif strcmp(Action, 'Region color prompt')
I = varargin{length(varargin)}; % last argument always
if Arguments == 0 % from interactive
close; % prompt
end
Scr_sz = get(0, 'ScreenSize');
New_wind = dialog('Name', 'Choose region color', 'Position', ...
[Scr_sz(3)*0.3 Scr_sz(4)*0.4 Scr_sz(3)*0.3 Scr_sz(4)*0.1]);
set(New_wind, 'WindowStyle', 'normal');
Choice_color = uicontrol('Parent', New_wind);
set(Choice_color, 'Style', 'Popup', 'String', Region_colors, ...
'Units', 'Normalized', 'Position', [0 0.5 0.45 0.5]);
Cancel_button = uicontrol('Parent', New_wind, ...
'String', 'Cancel', 'Style', 'Pushbutton', 'Callback', 'close', ...
'Units', 'Normalized', 'Position', [0.6 0.3 0.3 0.4]);
Callback=sprintf('Segment_demo(''%s'',%18.16f,%d)', 'Region color', Choice_color, I);
Ok_button = uicontrol('Parent', New_wind, ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -