在GUI中使用可编辑框在MATLAB中加载部分图像
我有一个通过uigetfile加载的可显示图像。我想让用户通过在可编辑框中输入左上像素和右下像素的像素坐标来选择要加载的图像部分。问题是,我在用于存储数据的句柄结构方面遇到了一些严重问题,并且不太了解如何使用它 这是我的密码。我可以轻松地加载图像左上角的4个像素(这是默认设置),但当可编辑框值更改时,我无法加载任何其他内容。这里有我遗漏的东西吗在GUI中使用可编辑框在MATLAB中加载部分图像,matlab,user-interface,image-processing,Matlab,User Interface,Image Processing,我有一个通过uigetfile加载的可显示图像。我想让用户通过在可编辑框中输入左上像素和右下像素的像素坐标来选择要加载的图像部分。问题是,我在用于存储数据的句柄结构方面遇到了一些严重问题,并且不太了解如何使用它 这是我的密码。我可以轻松地加载图像左上角的4个像素(这是默认设置),但当可编辑框值更改时,我无法加载任何其他内容。这里有我遗漏的东西吗 function mygui %% %Initialise GUI and set up editable boxes and push button
function mygui
%%
%Initialise GUI and set up editable boxes and push buttons
f = figure('Visible', 'off', 'Position', [360 500 450 285]);
handles.data.topleft1 = 1; %x-axis position of topleft pixel
handles.data.topleft2 = 1; %y-axis position of topleft pixel
handles.data.botright1 = 2; %x-axis position of bottom right pixel
handles.data.botright2 = 2; %y-axis position of bottom right pixel
hloader = uicontrol('Style', 'pushbutton', 'String', 'Load File', 'Position', [8 5 50 20], 'Callback', {@loadbutton_Callback, handles});
htopleft1 = uicontrol('Style', 'edit', 'String', handles.data.topleft1, 'Position', [25 40 15 10], 'Callback', {@topleft1_Callback, handles});
htopleft2 = uicontrol('Style', 'edit', 'String', handles.data.topleft2, 'Position', [40 40 15 10], 'Callback', {@topleft2_Callback, handles});
hbotright1 = uicontrol('Style', 'edit', 'String', handles.data.botright1, 'Position', [25 30 15 10], 'Callback', {@botright1_Callback, handles});
hbotright2 = uicontrol('Style', 'edit', 'String', handles.data.botright2, 'Position', [40 30 15 10], 'Callback', {@botright2_Callback, handles});
set([f, hloader, htopleft1, htopleft2, hbotright1, hbotright2], 'Units', 'normalized');
movegui(f, 'center')
set(f, 'Visible', 'on', 'toolbar', 'figure');
%%
%Loader pushbutton
function loadbutton_Callback(source, eventdata, handles)
[filename, pathname, filterindex] = uigetfile('*.jpg'); %Choose mario picture here from the directory you downloaded it from
picture = imread(strcat(pathname,filename));
topleft1 = handles.data.topleft1;
topleft2 = handles.data.topleft2;
botright1 = handles.data.botright1;
botright2 = handles.data.botright2;
picture = picture([topleft1:botright1], [topleft2:botright2], :); %Trim picture dimensions according to editable box inputs
imagesc(picture)
end
%%
%Editable boxes
function topleft1_Callback(source, eventdata, handles)
%Get new input from editable box; Save it into guidata handles structure thingy
topleft1 = str2double(get(source, 'String'));
handles.data.topleft1 = topleft1;
guidata(source, handles)
end
%(Repeat 3 more times for topleft2, botright1 and botright2)
end
和往常一样,下面是我要整理的图片:
(来源:)我可以建议一个解决方案,其中有些改动可能没有那么有效,但它们会起作用。我只需要使用整个GUI是一个嵌套函数的事实,就可以在回调之间传递这种数据,这样所有回调都可以访问
句柄
,而无需运行guidata
函数:
要做到这一点,只需改变方框从中调用回调的方式即可
(... 'Callback', {@topleft1_Callback, handles})
致:
现在调整回调函数所采用的参数,使回调函数不采用三个参数,而采用两个参数:
function myCallback(source,eventdata)
虽然这些都不会被使用,所以你可以简单地写:
函数myCallback(~,~)
正如你可能会建议的那样。而且你不需要
guidata(source, handles);
再也不能在任何回调中使用行,因为
句柄
可以通过其名称进行访问。这似乎只检索我更改的最新可编辑框。它不记得所有框中的输入。没错,句柄
也需要初始化为guidata,我会在一秒钟内更新我的代码。我已经改变了方法,现在它可以正常工作,但可能不是最优雅的
guidata(source, handles);