简体   繁体   English

在GUI中使用可编辑框在MATLAB中加载部分图像

[英]Using editable boxes in a GUI to load part of an image in MATLAB

I have a displayable image which I load via uigetfile. 我有一个可显示的图像,该图像通过uigetfile加载。 I want to allow the user to choose which portion of the image he wants to load by keying in the pixel coordinates of the top-left pixel and the bottom-right pixel into editable boxes. 我想允许用户通过将左上角像素和右下角像素的像素坐标键入到可编辑框中来选择要加载的图像部分。 The problem is that I'm having some serious issues with the handles structure used to store data and don't quite understand how to use it. 问题是我在用于存储数据的句柄结构上遇到了一些严重的问题,并且不太了解如何使用它。

Here is my code. 这是我的代码。 I can easily load the 4 pixels in the topleft corner of the image (that's the default setting), but I fail to load anything else when the editable box values are changed. 我可以轻松加载图像左上角的4个像素(这是默认设置),但是当更改可编辑框的值时,我无法加载其他任何内容。 Is there something I'm missing here? 我在这里想念什么吗?

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

And as usual, here's the picture which I'm trying to trim: 和往常一样,这是我要修剪的图片: 马里奥和朋友的样本图片
(source: gawkerassets.com ) (来源: gawkerassets.com

I can suggest a solution with some changes that might be not as efficient, bu they'll work. 我可以提出一些可能不那么有效的更改的解决方案,以使它们起作用。 I would do this kind of passing data between callbacks simply using the fact that Your whole GUI is a nested function, so all the callbacks can acces handles without even running the guidata function: 我将使用您的整个GUI是一个嵌套函数的事实来完成在回调之间传递数据的方式,因此所有回调都可以访问handles ,甚至无需运行guidata函数:

Do achieve this just change the way boxes are calling their callbacks from 要做到这一点,只需更改框从中调用其回调的方式

(... 'Callback', {@topleft1_Callback, handles})

to: 至:

(... 'Callback', @topleft1_Callback)

Now adjust arguments taken by Your callbacks, so the don't take three but two: 现在调整您的回调所采用的参数,因此不要采用三个,而是两个:

function myCallback(source,eventdata)

although none of those will be used, so You could simply write: 尽管这些都不会被使用,所以您可以简单地写:

function myCallback(~,~) as You MATlab will probably suggest. MATlab可能会建议您使用myCallback(〜,〜)函数。 And You don't need the 而且您不需要

guidata(source, handles);

line in any of Your callbacks anymore, since handles can be accesed anyway just by its name. 不再在您的任何回调中添加行,因为无论如何都可以仅通过其名称来访问handles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM