用户在没有 GUI 的情况下选择两个图像之一

user chooses one of two images without GUI

我有一个二进制图像 Bimg,我让用户在它和互补图像之间 select。我使用子图向用户展示了两张图片:

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

我可以在不构建 GUI 的情况下接受用户的点击输入吗? 我能以某种方式使用 ginput() 吗?

您可以简单地将回调函数绑定到图像对象的ButtonDownFcn。我们可以将此回调函数与 waitfor 结合起来,使它类似于一个对话框,供用户 select 两张图片之一。

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