matlab中的交互式鼠标指针位置

时间:2015-01-19 12:17:55

标签: matlab mouse geometry interactive

朋友

我有一个问题,我正在为网络中的路由编写代码,特别是我想在matlab中实现Aodv协议,现在我想在我的代码中添加一些图形部分。

并且假设在一个1000 * 1000的零图像上,我们有一些节点(路由器)由一个尺寸为15 * 15和白色的正方形显示,所以我们在零图像中有一些白色部分,

现在我写了一个函数,用特定的半径在每个部分周围画一个圆圈,但是因为我们有很多节点,我们的图像会很脏,所以我希望你能帮我改变这个功能鼠标指针指向每个白色部分,显示一个圆圈,当我们将鼠标指针移动到另一个部分时,该圆圈将被隐藏。

3 个答案:

答案 0 :(得分:0)

这可以通过充分巧妙地使用图形的WindowButtonMotionFcn属性和轴的CurrentPoint属性来实现。

如果图像中有黑色背景和一些白色区域,这里通过随机播种5个中心点然后将它们扩展到所需大小来生成,您可以使用WindowButtonMotionFcn回调来测试光标是否为1 )在图像尺寸内和2)在白点上。如果是这样,在白色方块上的图像轴上画一个圆圈。可以对任何输入图像进行类似的工作,但是如果提供图像而不是在函数内生成,则可能需要进行一些预处理以确定区域中心的位置。

代码:

function circlePointer

% Initialize figure and axes within figure window
fig = figure('Position', [200 200 600 600], 'WindowButtonMotionFcn', @MouseOverFcn);
ax = axes('Parent', fig, 'Position', [0.05 0.05 .90 .90], ...
    'XLim', [0.5 1000.5], 'YLim', [0.5 1000.5]);

% Make black image with random white squares
img = zeros(1000, 1000);
whiteSquares = randperm(1000, 10);
whiteSquares = reshape(whiteSquares, [], 2);
for k = 1:size(whiteSquares, 1);
    img(whiteSquares(k, 1), whiteSquares(k,2)) = 1;
end
halfSquareSize = 7; % Size of one side of the square to be drawn, divided by 2
img = bwmorph(img, 'dilate', halfSquareSize);

% Display image in axes generated above
imagesc(1:1000, 1:1000, img, 'Parent', ax);
set(ax, 'XTick', [], 'YTick', []);
colormap('gray');


% Use MouseOverFcn to determine where the cursor is currently pointing when
% positioned in the figure window. 
% If the cursor is within the image axes and is hovered over a white
% square, then draw a red circle around that square.  If one or both of
% these conditions is not met, clear out all of the previously-drawn
% circles. 
    function MouseOverFcn(varargin)

        PostNow = round(get(ax, 'CurrentPoint'));

        if all(PostNow(1, 1:2) < 1000) && all(PostNow(1, 1:2) > 1)
            if (img(PostNow(1,2), PostNow(1,1)) == 1)
                % Overlay circle

                % Find which square is the one currently hovered over by
                % the cursor
                nearestSquare = knnsearch(whiteSquares, [PostNow(1,2) PostNow(1,1)]);

                % Draw a circle with circRad centered on the current square
                set(ax, 'NextPlot', 'add');
                circRad = 30; % circle radius in image axis units
                circCent = [whiteSquares(nearestSquare, 2), whiteSquares(nearestSquare, 1)];
                circAngle = [0:.2:(2*pi) 0];
                [circPtsX, circPtsY] = pol2cart(circAngle, circRad*ones(1, length(circAngle)));
                plot(circPtsX+circCent(1), circPtsY+circCent(2), 'r', 'Tag', 'drawnCircles');
                set(ax, 'NextPlot', 'replace');

            else
                % If cursor is not currently over a white square in the
                % image, clear all previously drawn circles
                delete(findobj('Tag', 'drawnCircles'));

            end

        else
            % If cursor is not currently over the image,
            % clear all previously drawn circles
            delete(findobj('Tag', 'drawnCircles'));
        end

    end

end

答案 1 :(得分:0)

您可以使用图窗口callbacks。 请参阅the Figure object和Control + F查找单词&#39; Callback&#39;,这将显示可供使用的回调。 然后,您可以使用这些方法编写要执行的应用程序。

set(gcf, 'UserData', {some, data, my, callbacks, need});
set(gcf,'ButtonDownFcn',@myMouseBtnDownCallback);
set(gcf,'windowbuttonmotionfcn',@myMouseMoveCallback);
set(gcf,'windowbuttonupfcn',@myMouseBtnUpCallback);

function myMouseBtnDownCallback(src, eventdata)
function myMouseMoveCallback(src,eventdata,r)
function myMouseBtnUpCallback(src,evendata)

答案 2 :(得分:0)

具体我改变了这样的代码但当我指向白色部分时,在该部分周围没有出现任何圆圈。(我的图像是测试2,带有一些白色部分的零图像)

  function circlePointer
           fig = figure( 'WindowButtonMotionFcn', @MouseOverFcn);
           ax = axes('Parent', fig, 'Position', [0.05 0.05 .90 .90], ...
          'XLim', [0.5 1000.5], 'YLim', [0.5 1000.5]);
           img=imread('test2.jpg');
           img=img(:,:,1);
           img=im2double(img);
           [r,c]=size(img);
           imagesc(1:r, 1:c, img, 'Parent', ax);
           set(ax, 'XTick', [], 'YTick', []);
           colormap('gray');


function MouseOverFcn(varargin)

    PostNow = round(get(ax, 'CurrentPoint'));

    if all(PostNow(1, 1) < c) && all(PostNow(1, 1:2) > 1) && all(PostNow(1, 2) < r)
        if (img(PostNow(1,2), PostNow(1,1)) == 1)

        % this function (insert shape) put a circle arround  a pointed pixel        

        img=insertShape(img, 'circle', [PostNow(1, 1) PostNow(1, 1) 300],'Color',{'red'},'Opacity', .4);
        imshow(img)

        else
            % If cursor is not currently over a white square in the
            % image, clear all previously drawn circles
            delete(findobj('Tag', 'drawnCircles'));

        end

    else
        % If cursor is not currently over the image,
        % clear all previously drawn circles
        delete(findobj('Tag', 'drawnCircles'));
    end

end
end