用户选择没有GUI的两个图像之一

时间:2017-03-22 16:03:37

标签: matlab user-input matlab-figure

我有一个二进制图像Bimg,我让用户在它和补充之间进行选择。我使用了子图来向用户显示两个图像:

subplot(1,2,1);
imshow(Bimg);
subplot(1,2,2);
imshow(~Bimg);

我可以在不构建GUI的情况下从用户那里获取点击输入吗? 我可以以某种方式使用ginput()吗?

1 个答案:

答案 0 :(得分:2)

您可以简单地将回调函数绑定到图像对象的ButtonDownFcn。我们可以将此回调函数与waitfor结合使用,使其类似于一个对话框,供用户选择两个图像中的一个。

function clicked = imgdlg(Bimg)
    hfig = dialog();
    hax1 = subplot(1,2,1, 'Parent', hfig);
    him1 = imshow(Bimg, 'Parent', hax1);
    title('Normal')

    hax2 = subplot(1,2,2, 'Parent', hfig);
    him2 = imshow(~Bimg, 'Parent', hax2);
    title('Complement')

    % Assign a tag to each of the images corresponding to what it is.
    % Also have "callback" execute when either image is clicked
    set([him1, him2], ...
        {'Tag'}, {'normal', 'complement'}, ...
        'ButtonDownFcn', @callback)

    drawnow

    % Wait for the UserData of the figure to change
    waitfor(hfig, 'Userdata');

    % Get the value assigned to the UserData of the figure
    clicked = get(hfig, 'Userdata');

    % Delete the figure
    delete(hfig);

    function callback(src, evnt)
        % Store the tag of the clicked object in the UserData of the figure
        set(gcbf, 'UserData', get(src, 'tag'))
    end
end