📄 image_binary.m
字号:
function image_binary(command)%IMAGE_BINARY Trasform the image in binary mode.%% IMAGE_BINARY(COMMAND)%% Valid Commands:% START - Inizialize the button frame% DOIT - Do the actual image tranformation% RADIO - Set the radio buttons% SETLEVEL - Set the definable level in between the min and max limits% SETSTAT - Set the statistical parameter min, max, mean, median.% UNDO - Undo the image tranformation.%% Claudio Oct. 30 1995%% Copyright (c) 1995 by Claudio Rivetti and Mark Young% claudio@alice.uoregon.edu, mark@alice.uoregon.edu%global I B_frame Handlefig Uibgcolor undo_bt prev_radio prev_level level_edglobal radio_unit mean_txt median_txt level_txt min_txt max_txt prev_level2global Oldzrange Oldcontrast Oldoffset Oldmap Undofun level2_txt level2_edif nargin==0 command='START';endif isempty(I) return;endif strcmp(upper(command), 'START') % Position variables--------------------------------------------------- pos=get(B_frame, 'Position'); uiwidth=0.12; uithick=0.035; middle=pos(1)+(pos(3)-uiwidth)/2; %--------------------------------------------------------------------- % Positions of buttons ----------------------------------------------------- rbframe_pos = [pos(1)+0.02, pos(2)+.38, pos(3)-0.04 0.35]; sp=rbframe_pos(4)/6; f1pos=[middle-0.03 pos(2)+0.24 uiwidth+0.06 0.485]; txt_pos=[middle-0.03 pos(2)+0.72 uiwidth uithick]; rb1_pos=[rbframe_pos(1)+0.005, rbframe_pos(2)+5*sp, rbframe_pos(3)-0.01, uithick]; rb2_pos=[rbframe_pos(1)+0.005, rbframe_pos(2)+4*sp, rbframe_pos(3)-0.01, uithick]; rb3_pos=[rbframe_pos(1)+0.005, rbframe_pos(2)+3*sp, rbframe_pos(3)-0.01, uithick]; level_txt_pos=[rbframe_pos(1), 0.58+uithick, rbframe_pos(3), uithick]; level_ed_pos=[middle, 0.585, uiwidth, 0.03]; level2_txt_pos=[rbframe_pos(1), 0.5+uithick, rbframe_pos(3), uithick]; level2_ed_pos=[middle, 0.505, uiwidth, 0.03]; min_txt_pos=[rbframe_pos(1)+0.005, 0.45, rbframe_pos(3)-0.01, uithick]; max_txt_pos=[rbframe_pos(1)+0.005, 0.42, rbframe_pos(3)-0.01, uithick]; mean_txt_pos=[rbframe_pos(1)+0.005, 0.39, rbframe_pos(3)-0.01, uithick]; median_txt_pos=[rbframe_pos(1)+0.005, 0.36, rbframe_pos(3)-0.01, uithick]; undo_pos = [middle pos(2)+0.12 uiwidth, uithick]; exec_pos = [middle pos(2)+0.18 uiwidth, uithick]; areatxt_pos = [middle-.01 pos(2)+0.55 uiwidth+.02, uithick]; areaed_pos = [middle pos(2)+0.52 uiwidth, uithick]; %--------------------------------------------------------------------------- if isempty(prev_radio) prev_radio=1; end; if isempty(prev_level) prev_level=-inf; end; if isempty(prev_level2) prev_level2=inf; end; % Callbacks for the buttons ------------------ whendone = ''; exec_cbk = 'image_binary(''DOIT'');'; undo_cbk = 'image_binary(''UNDO'');'; %---------------------------------------------- if ~isimage setviewmode('TOPVIEW'); showimage; end initbuttons('Binary Trasformation', 'Done', whendone); uicontrol(Handlefig, 'Style', 'text',... 'String', 'CutOff Level',... 'Units', 'normalized',... 'Position', txt_pos,... 'BackgroundColor', Uibgcolor,... 'HorizontalAlignment', 'left'); uicontrol(Handlefig, 'Style', 'frame',... 'Units', 'normalized',... 'Position',f1pos,... 'BackgroundColor', Uibgcolor); radio_unit=zeros(3,1); radio_unit_callback = 'image_binary(''RADIO'');'; radio_unit(1) = uicontrol(Handlefig, 'Style', 'radio',... 'String', 'Image mean',... 'Units', 'Normalized',... 'Position', rb1_pos,... 'HorizontalAlignment', 'left',... 'BackgroundColor', Uibgcolor,... 'CallBack', radio_unit_callback,... 'Value', 0); radio_unit(2) = uicontrol(Handlefig, 'Style', 'radio',... 'String', 'Image median',... 'Units', 'Normalized',... 'Position', rb2_pos,... 'HorizontalAlignment', 'left',... 'BackgroundColor', Uibgcolor,... 'CallBack', radio_unit_callback,... 'Value', 0); radio_unit(3) = uicontrol(Handlefig, 'Style', 'radio',... 'String', 'Define',... 'Units', 'Normalized',... 'Position', rb3_pos,... 'HorizontalAlignment', 'left',... 'BackgroundColor', Uibgcolor,... 'CallBack', radio_unit_callback,... 'Value', 0); set(radio_unit(prev_radio), 'value', 1); level_txt=uicontrol(Handlefig, 'Style', 'text',... 'String', 'Level 1',... 'Units', 'normalized',... 'Position', level_txt_pos,... 'BackgroundColor', Uibgcolor,... 'Visible', onoff(prev_radio==3),... 'HorizontalAlignment', 'center'); level_ed = uicontrol(Handlefig, 'Style', 'edit',... 'String', num2str(prev_level),... 'Units', 'normalized',... 'Position', level_ed_pos,... 'Visible', onoff(prev_radio==3),... 'Value', prev_level,... 'HorizontalAlignment', 'center',... 'CallBack', 'image_binary(''SETLEVEL'');'); level2_txt=uicontrol(Handlefig, 'Style', 'text',... 'String', 'Level 2',... 'Units', 'normalized',... 'Position', level2_txt_pos,... 'BackgroundColor', Uibgcolor,... 'Visible', onoff(prev_radio==3),... 'HorizontalAlignment', 'center'); level2_ed = uicontrol(Handlefig, 'Style', 'edit',... 'String', num2str(prev_level2),... 'Units', 'normalized',... 'Position', level2_ed_pos,... 'Visible', onoff(prev_radio==3),... 'Value', prev_level2,... 'HorizontalAlignment', 'center',... 'CallBack', 'image_binary(''SETLEVEL'');'); min_txt=uicontrol(Handlefig, 'Style', 'text',... 'String', '',... 'Units', 'normalized',... 'Position', min_txt_pos,... 'BackgroundColor', Uibgcolor,... 'ForegroundColor', 'y',... 'HorizontalAlignment', 'left'); max_txt=uicontrol(Handlefig, 'Style', 'text',... 'String', '',... 'Units', 'normalized',... 'Position', max_txt_pos,... 'BackgroundColor', Uibgcolor,... 'ForegroundColor', 'y',... 'HorizontalAlignment', 'left'); mean_txt=uicontrol(Handlefig, 'Style', 'text',... 'String', '',... 'Units', 'normalized',... 'Position', mean_txt_pos,... 'BackgroundColor', Uibgcolor,... 'ForegroundColor', 'y',... 'HorizontalAlignment', 'left'); median_txt=uicontrol(Handlefig, 'Style', 'text',... 'String', '',... 'Units', 'normalized',... 'Position', median_txt_pos,... 'BackgroundColor', Uibgcolor,... 'ForegroundColor', 'y',... 'HorizontalAlignment', 'left'); uicontrol(Handlefig,'Style','push',... 'String', 'Execute',... 'Units', 'normalized', ... 'Position', exec_pos,... 'CallBack',exec_cbk); undo_bt=uicontrol(Handlefig,'Style','push',... 'String','Undo',... 'Enable', 'off',... 'Units', 'normalized', ... 'Position', undo_pos,... 'CallBack',undo_cbk); image_binary('SETSTAT');end %STARTif strcmp(upper(command), 'RADIO') set(radio_unit(find(radio_unit ~= gco)), 'value', 0); set(gco, 'value', 1); prev_radio=find(radio_unit==gco); set(level_ed, 'Visible', onoff(gco==radio_unit(3))); set(level_txt, 'Visible', onoff(gco==radio_unit(3))); set(level2_ed, 'Visible', onoff(gco==radio_unit(3))); set(level2_txt, 'Visible', onoff(gco==radio_unit(3)));end %RADIOif strcmp(upper(command), 'SETLEVEL') num=str2num(get(level_ed, 'string')); if isempty(num), num=-inf;end set(level_ed, 'value', num, 'string', num2str(num)); prev_level=num; num=str2num(get(level2_ed, 'string')); if isempty(num), num=inf;end set(level2_ed, 'value', num, 'string', num2str(num)); prev_level2=num;end %LEVELif strcmp(upper(command), 'DOIT') statusbar('Converting image to binary...'); if get(radio_unit(1), 'value') level1=mean2(I); level2=inf; elseif get(radio_unit(2), 'value') level1=median(I(:)); level2=inf; else level1=get(level_ed, 'value'); level2=get(level2_ed, 'value'); end Oldzrange=zrange; Oldcontrast=getcontrast; Oldoffset=getoffset; Oldmap=getmap; zrange([0 1]); setcontrast(0); setoffset(0); setmap(30); i=find(I>=level1 & I<=level2); A=zeros(size(I)); A(i)=A(i)+1; setimage(A); clear A; Undofun='image_binary(''UNDO'')'; set(undo_bt, 'Enable', 'on'); image_binary('SETSTAT'); clearstatusbar; if isempty(i) | length(i)==prod(size(I)) message('Warning:|| Both levels were outside| the image Z range'); endendif strcmp(upper(command), 'SETSTAT') statusbar('Computing image statistic'); set(min_txt, 'string', ['min: ' num2str(min(min(I)))]); set(max_txt, 'string', ['max: ' num2str(max(max(I)))]); set(mean_txt, 'string', ['mean: ' num2str(mean2(I))]); set(median_txt, 'string', ['median: ' num2str(median(I(:)))]); clearstatusbar;end % SETSTATif strcmp(upper(command), 'UNDO') zrange(Oldzrange); setimage(getclipboard,inf,0); setcontrast(Oldcontrast); setoffset(Oldoffset); setmap(Oldmap); setclipboard([]); if isobj(undo_bt) set(undo_bt, 'Enable', 'off'); image_binary('SETSTAT'); endend %UNDO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -