gpt4 book ai didi

matlab - 更改 ginput 十字准线外观 - Matlab

转载 作者:行者123 更新时间:2023-12-01 14:38:45 26 4
gpt4 key购买 nike

如何将十字准线更改为具有相同 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

最佳答案

我使用的是 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在工具箱中找到,这是因为圆圈被刻在图像中并且没有简单的方法来“移动”它。
  2. 未记录的函数 hgconvertunits 用于确保单位为所需类型。

我发现可以移动的是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

关于matlab - 更改 ginput 十字准线外观 - Matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com