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



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

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

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

只需将回调函数绑定到图像对象的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

相关内容

  • 没有找到相关文章

最新更新