改变ginput十字准线外观 - Matlab

时间:2018-04-24 14:30:33

标签: matlab

如何将十字准线更改为具有相同r(20)的圆圈?

代码:

clc;
clear;
I = imread('peppers.png');
imshow(I); 
colorCode = [0.6 0.8 0.5] *255;

r=20;
button = 1;
 while sum(button) <=1    
    [x,y,button] = ginput(1)
    I= insertShape(I,'FilledCircle',[x y r],'LineWidth',1, 'Color', colorCode,  'Opacity', 1);
    imshow(I); 
 end

1 个答案:

答案 0 :(得分:1)

我正在使用Matlab R2015b,我不知道从那时起是否已修改ginput。 在我所拥有的特定版本中,ginput的十字代码主要有两个函数:

function updateCrossHair(fig, crossHair)
% update cross hair for figure.
gap = 3; % 3 pixel view port between the crosshairs
cp = hgconvertunits(fig, [fig.CurrentPoint 0 0], fig.Units, 'pixels', fig);
cp = cp(1:2);
figPos = hgconvertunits(fig, fig.Position, fig.Units, 'pixels', fig.Parent);
figWidth = figPos(3);
figHeight = figPos(4);

% Early return if point is outside the figure
if cp(1) < gap || cp(2) < gap || cp(1)>figWidth-gap || cp(2)>figHeight-gap
    return
end

set(crossHair, 'Visible', 'on');
thickness = 1; % 1 Pixel thin lines. 
set(crossHair(1), 'Position', [0 cp(2) cp(1)-gap thickness]);
set(crossHair(2), 'Position', [cp(1)+gap cp(2) figWidth-cp(1)-gap thickness]);
set(crossHair(3), 'Position', [cp(1) 0 thickness cp(2)-gap]);
set(crossHair(4), 'Position', [cp(1) cp(2)+gap thickness figHeight-cp(2)-gap]);
end


function crossHair = createCrossHair(fig)
% Create thin uicontrols with black backgrounds to simulate fullcrosshair pointer.
% 1: horizontal left, 2: horizontal right, 3: vertical bottom, 4: vertical top
for k = 1:4
    crossHair(k) = uicontrol(fig, 'Style', 'text',...
                             'Visible', 'off',...
                             'Units', 'pixels',...
                             'BackgroundColor', [1 0 0],...
                             'HandleVisibility', 'off',...
                             'HitTest', 'off'); %#ok<AGROW>
end

end

有趣的是看到了

  1. 人们正在努力解决绘图功能的限制,例如工具箱中的insertShape,这就是圆圈被烧成图像的事实,没有简单的方法来移动&#34; ;它。
  2. 未记录的函数hgconvertunits用于确保单位是所需类型。
  3. 我发现可以移动的是annotation。 下面是一些示例代码,用于按照鼠标指针移动椭圆。 由于ginput不可编辑,因此我将所有内容复制到一个函数中,并将调用更改为ginput到新函数。 以下是对ginput的修改。

    function updateCrossHair(fig, crossHair)
    % update cross hair for figure.
    % get current point and positions; take care of units
    cp = hgconvertunits(fig, [fig.CurrentPoint 0 0], fig.Units, 'pixels', fig);
    figPos = hgconvertunits(fig, fig.Position, fig.Units, 'pixels', fig.Parent);
    cp = cp(1:2)./figPos(3:4);
    axesPos = fig.Children.Position;
    % Early return if point is outside the figure
    if cp(1) < axesPos(1) || cp(2) < axesPos(2) || cp(1) > (axesPos(1)+axesPos(3)) || cp(2) > axesPos(2)+axesPos(4)
        return
    end
    
    diameter = 10; % pixels
    crossHair.Position = [cp-diameter./figPos(3:4)/2, diameter./figPos(3:4)];
    
    end
    
    
    function crossHair = createCrossHair(fig)
    crossHair = annotation(fig, 'ellipse', [0,0,0,0]);
    crossHair.Color = 'w';
    end
    

    此方法存在潜在错误,即在用户输入结束时,当enter键被命中时,修改后的函数不会返回任何xy值。 实际上,while循环中的函数输出是空矩阵。

    为避免崩溃,只需在用户输入后添加一个检查,如下所示:

     while sum(button) <=1    
        [x,y,button] = testf(1) % use modified ginput
        if isempty(x) || isempty(y)
            break
        end
        I= insertShape(I,'FilledCircle',[x y r],'LineWidth',1, 'Color', colorCode,  'Opacity', 1);
        imshow(I); 
     end