来自「选择其中图像中的矩形及正方形等感兴趣区域ROI」· 代码 · 共 748 行 · 第 1/3 页
TXT
748 行
delete(gcf);
ROI.Xmin = GP.SP1(1);
ROI.Xmax = GP.SP2(1);
ROI.Ymin = GP.SP1(2);
ROI.Ymax = GP.SP2(2);
ROI.DX = GP.SW;
ROI.DY = GP.SH;
% fprintf(' nargin = %g\n',nargin);
% fprintf('nargout = %g\n',nargout);
switch nargout
case 0, % No output parameters
disp(ROI);
if ischar(img)
% FileName was provided
im = imread(img);
figure;
imshow( im( ROI.Xmin:ROI.Xmax, ROI.Ymin:ROI.Ymax, : ) );
else
% Image matrix was provided
figure;
imshow( img( ROI.Xmin:ROI.Xmax, ROI.Ymin:ROI.Ymax, : ) );
end
return
case 1, % 1 output parameter
ROI.Xrange = ROI.Xmin:ROI.Xmax;
ROI.Yrange = ROI.Ymin:ROI.Ymax;
varargout{1} = ROI;
case 2, % 2 output parameters
ROI.Xrange = ROI.Xmin:ROI.Xmax;
ROI.Yrange = ROI.Ymin:ROI.Ymax;
varargout{1} = ROI;
if ischar(img)
% FileName was provided
im = imread(img);
varargout{2} = im( ROI.Xmin:ROI.Xmax, ROI.Ymin:ROI.Ymax, : );
else
% Image matrix was provided
varargout{2} = img( ROI.Xmin:ROI.Xmax, ROI.Ymin:ROI.Ymax, : );
end
otherwise
msgId = 'VarArgOutTest:invalidNumOutputArguments';
msg = 'Tne number of output arguments is invalid.';
error(msgId,'%s',msg);
end
else
fprintf('%s: Warning! Figure was closed before selection was retrieved! Empty matrix returned.\n',mfilename);
switch nargout
case 0,
return;
case 1,
varargout{1} = [];
return;
case 2,
varargout{1} = [];
varargout{2} = [];
return;
end
end
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Done button callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function SelectionDone(src,evt)
PDelay = 0.1;
GP = guidata(gcf);
if ~ValidateSelection
tf = isempty( get(GP.hStatus, 'String') );
if tf,set(GP.hStatus, 'String','ERROR: Wrong selection!'); end
for i=1:7
set(GP.hStatus, 'ForegroundColor',[1 0 0]); pause(PDelay);
set(GP.hStatus, 'ForegroundColor',[0 0 0]); pause(PDelay);
end
if tf, set(GP.hStatus, 'String',''); end
else
uiresume;
end
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% StartOver button callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function StartOver(src,evt)
GP = guidata(gcf);
GP.P1 = [];
GP.P2 = [];
GP.P1 = [];
GP.P2 = [];
GP.SP1 = [];
GP.SP2 = [];
GP.SW = [];
GP.SH = [];
set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2), GP.SW, GP.SH) );
set(GP.hStatus, 'String','');
DeleteHandleSafely(GP.hCrs); GP.hCrs = [];
DeleteHandleSafely(GP.hRct); GP.hRct = [];
guidata(gcf,GP);
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Mouse click callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function FigureMouseBtnUp(src,evt)
GP = guidata(gcf);
if isnan(GP.cx)||isnan(GP.cy)
set(gcf, 'Name','MouseUp outside axes' );
else
%set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2), GP.SW, GP.SH) );
if isempty(GP.P1)
% P1 is empty
GP.P1 = [ GP.cx GP.cy ];
set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2), GP.SW, GP.SH) );
DeleteHandleSafely(GP.hCrs); GP.hCrs = [];
if strcmpi( GP.SelectionMode, 'Centered' )
GP.hCrs = caDrawCross(...
GP.P1(1),GP.P1(2),13,...
'xor', 1, 'k',':',...
'k',8,'normal',...
'','','','');
end
guidata(gcf,GP);
return;
else
% P1 is NOT empty
if isempty(GP.P2)
% P2 is empty
GP.P2 = [ GP.cx GP.cy ];
%set(gcf, 'Name',sprintf('[ %06g : %06g ] P1 = [ %s] P2 = [ %s]',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2) ) );
%set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2), GP.SW, GP.SH) );
%set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.SP1), sprintf('%06g ',GP.SP2), GP.SW, GP.SH) );
set(gcf, 'Name',sprintf('P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',sprintf('%06g ',GP.SP1), sprintf('%06g ',GP.SP2), GP.SW, GP.SH) );
DeleteHandleSafely(GP.hCrs); GP.hCrs = [];
DeleteHandleSafely(GP.hRct); GP.hRct = [];
GP.hRct = caDrawRectangle(...
GP.SP1(1),GP.SP1(2),...
GP.SP2(1),GP.SP2(2),...
'xor', 1, 'k', '-', 'none',...
'k',8,'normal',...
'selection','','','');
guidata(gcf,GP);
% FastReturn trick
if strcmpi(GP.FastReturn_i,'on')
% GP.FastReturn_i
SelectionDone(src,evt);
end
else
% P2 is NOT empty
% set(gcf, 'Name',sprintf('P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',sprintf('%06g ',GP.SP1), sprintf('%06g ',GP.SP2), GP.SW, GP.SH) );
end
end
end
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Mouse move callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function FigureMouseMove(src,evt)
GP = guidata(gcf);
% set(gcf, 'Name',get(GP.hPixVal,'String'));
ts = get(GP.hPixVal,'String');
p1 = strfind(ts, '(');
p2 = strfind(ts, ',');
p3 = strfind(ts, ')');
GP.cx = str2double( ts( p1(1)+1 : p2(1)-1 ) );
GP.cy = str2double( ts( p2(1)+1 : p3(1)-1 ) );
GP.CP = [ GP.cx GP.cy ];
if ~isempty( GP.P1 )
if ~isempty( GP.P2 )
% P1 - P2
DeleteHandleSafely(GP.hCrs); GP.hCrs = [];
%set(gcf, 'Name',sprintf('P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;', sprintf('%06g ',GP.SP1), sprintf('%06g ',GP.SP2), GP.SW, GP.SH) );
else
% P1 - CP
if strcmpi( GP.SelectionMode, 'Centered' )
if isempty(GP.hCrs)
GP.hCrs = caDrawCross(...
GP.P1(1),GP.P1(2),13,...
'xor', 1, 'k',':',...
'k',8,'normal',...
'','','','');
end
else
DeleteHandleSafely(GP.hCrs); GP.hCrs = [];
end
set(GP.hStatus, 'String','');
if isnan(GP.cx)||isnan(GP.cy)
set(GP.hStatus, 'String','WARNING: Selection is partially outside the image');
else
[GP.SP1,GP.SP2] = RecalculateSelection(GP.P1,GP.CP, GP.AllowedShape, GP.SelectionMode);
GP.SW = GP.SP2(1) - GP.SP1(1) + 1;
GP.SH = GP.SP2(2) - GP.SP1(2) + 1;
guidata(gcf,GP);
if ~ValidateSelection
set(GP.hStatus, 'String','WARNING: Selection is partially outside the image');
end
DeleteHandleSafely(GP.hRct); GP.hRct = [];
%caDrawRectangle(X1,Y1,X2,Y2, EraseMode, LWidth,LColor,LStyle, FaceColor, TColor,TSize,FontWeight, TopLabel,BottomLabel,RightLabel,LeftLabel)
GP.hRct = caDrawRectangle(...
GP.SP1(1),GP.SP1(2),...
GP.SP2(1),GP.SP2(2),...
'xor', 1, 'k', ':', 'none',...
'k',8,'normal',...
'selection','','','');
guidata(gcf,GP);
end
set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2), GP.SW, GP.SH) );
end
%set(gcf, 'Name',sprintf('[ %06g : %06g ]; P1 = [ %s]; P2 = [ %s]; Width = %06g; Height = %06g;',GP.cx,GP.cy, sprintf('%06g ',GP.P1), sprintf('%06g ',GP.P2), GP.SW, GP.SH) );
end
guidata(gcf,GP);
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Zoom callback
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function AdjustZoom(src,evt)
GP = guidata(gcf);
c = get(gcbo,'String');
GP.hSPAPI.setMagnification( str2double( c{get(gcbo,'Value')}(1:end-1) ) / 100 );
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Deleting handle safely
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function DeleteHandleSafely(h)
for i=1:length(h(:))
if ishandle( h(i) )
delete ( h(i) );
end
end
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Updating AllowedShape and SelectionMode
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function UpdateShapeAndMode(src,evt)
GP = guidata(gcf);
GP.AllowedShape = GP.AllowedShapes { get(GP.hShapeLBox,'Value') };
GP.SelectionMode = GP.SelectionModes { get(GP.hModeLBox, 'Value') };
guidata(gcf,GP);
return;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Retrieving parameters from varargin
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function res = GetParameterValue(PName,PNameValArray,DefaultValue)
res = DefaultValue; % in case PName is not present in PNameValArray
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?