我正在创建一个GUI,在其中我显示一个3D图像并使用滑块浏览它。我想使用鼠标在某个切片上绘制一些点(使用“WindowButtonDownFcn”回调,即,我没有任何关于滑块位置的信息,一旦我调用绘图函数),并且该点应该出现在所有其他点上一旦用户使用滑块在切片之间切换,切片就可以编辑。
我想到的一个可能的解决方案是使用“监听器”,因此每当用户移动滑块时,我的绘图功能都会被通知,并且滑块位置会传递给它。 。 在MATLAB教程之后,我定义了我的类,因此我可以将监听器包含在我的代码中,但它不起作用。 。 我该如何实现此功能?
GUI代码:
function varargout = teste_window(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @teste_window_OpeningFcn, ...
'gui_OutputFcn', @teste_window_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function teste_window_OpeningFcn(hObject, eventdata, handles, varargin)
load IMG.mat
nimgs = 10;
imgs = zeros(size(img, 1), size(img, 2), nimgs);
for ii = 1: nimgs
imgs(:, :, ii) = img;
end
imshow(imgs(:, :, 1), [], 'Parent', handles.axs_plot);
handles.imgs = imgs;
handles.nimgs = nimgs;
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes teste_window wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = teste_window_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in btn_ok.
function btn_ok_Callback(hObject, eventdata, handles)
btns = findobj(hObject, 'Style', 'pushbutton');
set(btns, 'Enable', 'Off');
[x, y] = getRoi4(handles.axs_plot)
handles.xcoord = x;
handles.ycoord = y;
set(btns, 'Enable', 'On');
guidata(hObject, handles);
% dragDrop(handles.axs_plot);
% --- Executes on slider movement.
function sld_img_pos_Callback(hObject, eventdata, handles)
nimgs = handles.nimgs;
set(handles.sld_img_pos, 'Max', nimgs);
set(handles.sld_img_pos, 'SliderStep', [1/nimgs , 1/nimgs]);
sld_val = round(get(handles.sld_img_pos, 'Value'));
if (sld_val < 1)
sld_val = 1;
elseif (sld_val > nimgs)
sld_val = nimgs;
end
imshow(handles.imgs(:, :, sld_val), [], 'Parent', handles.axs_plot);
% --- Executes during object creation, after setting all properties.
function sld_img_pos_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
getRoi4的代码:
function [x, y, xc, yc] = getRoi4(axeshandle)
zoom off;
x = [];
y = [];
xc = [];
yc = [];
th = linspace(0., 2.*pi, 40);
fig = ancestor(axeshandle, 'figure');
xlim = get(axeshandle, 'XLim');
ylim = get(axeshandle, 'YLim');
set(axeshandle, 'XLim', xlim, 'YLim' , ylim, 'NextPlot', 'add');
set(fig, 'KeyPressFcn', @keyRead, 'WindowButtonDownFcn', @mouseRead);
uiwait(fig);
function keyRead(src, callback)
key = callback.Key;
returnPressed = strcmp(key, 'return');
% deletePressed = strcmp(key, 'backspace');
if returnPressed
set(fig, 'KeyPressFcn', '');
set(fig, 'WindowButtonDownFcn', '');
uiresume(fig);
end
end
function mouseRead(src, callback)
mouseKey = get(fig, 'SelectionType');
leftMouse = strcmp(mouseKey, 'normal');
rigthMouse = strcmp(mouseKey, 'alt');
middleMouse = strcmp(mouseKey, 'extend');
% LEFT CLICK:
if leftMouse
cp = get(axeshandle, 'CurrentPoint');
x = [x, cp(1, 1)];
y = [y, cp(1, 2)];
plot(axeshandle, x, y, '*b');
hold on;
[xc, yc] = getCirc(x, y, xc, yc);
npts = size(xc, 1);
for ii = 1: npts
plot(axeshandle, xc(ii, :), yc(ii, :), 'c')
end
% RIGHT CLICK
elseif rigthMouse
lc = get(axeshandle, 'CurrentPoint');
dist = sqrt((lc(1, 1) - xc).^2. + (lc(1, 2) - yc).^2);
[ix, ~] = find(min(min(dist)) == abs(dist));
ix = ix(1);
set(fig, 'WindowButtonMotionFcn', @(xc, yc, index)dragPoint, 'WindowButtonUpFcn', @(s,e)stopDrag);
% MIDDLE CLICK
elseif middleMouse
cp = get(axeshandle, 'CurrentPoint');
if ~isempty(x)
[~, ind] = min((x - cp(1,1)).^2 + (y - cp(1,2)).^2);
ind = ind(1);
x(ind) = [];
y(ind) = [];
xc(ind, :) = [];
yc(ind, :) = [];
dots = findobj(get(axeshandle, 'Children'), 'Type', 'Line');
set(dots, 'XData', x, 'YData', y, 'color', 'blue', 'Marker', '*', 'LineStyle', 'None');
npts = size(xc, 1);
for ii = 1: npts
plot(axeshandle, xc(ii, :), yc(ii, :), 'c')
end
end
end
% DRAG AND DROP:
function dragPoint(src, callback)
xcenter = x(ix);
ycenter = y(ix);
cp = get(axeshandle, 'CurrentPoint');
radius = sqrt((cp(1, 1) - xcenter)^2. + (cp(1, 2) - ycenter)^2.);
newxc = radius*cos(th) + xcenter;
newyc = radius*sin(th) + ycenter;
dots = findobj(get(axeshandle, 'Children'), 'Type', 'Line');
set(dots, 'XData', x, 'YData', y, 'color', 'blue', 'Marker', '*', 'LineStyle', 'None');
xc(ix, :) = newxc;
yc(ix, :) = newyc;
npts = size(xc, 1);
for jj = 1: npts
plot(axeshandle, xc(jj, :), yc(jj, :), 'c')
end
end
function stopDrag(src, callback)
set(fig, 'WindowButtonMotionFcn', '');
set(fig, 'WindowButtonUpFcn', '');
end
end
function [xc, yc] = getCirc(x, y, xc, yc)
r = 10;
npts = length(x);
xc(npts, :) = r*cos(th) + x(npts);
yc(npts, :) = r*sin(th) + y(npts);
end
end
提前谢谢大家,