Я использую 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
Интересно, что
- Люди работают на пределетаких функций рисования, как
insertShape
, найденных в наборе инструментов, и это тот факт, что круг сгорел на изображении, и нет простого способа «переместить» его. - Недокументированная функция
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
, измененная функция не возвращает никакого значения x
или y
,Фактически, выходные данные функции в цикле 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