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

interactive mouse pointer location in matlab

朋友

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

并假设在 1000*1000 零图像上,我们有一些节点(路由器)由大小为 15*15 的正方形和白色显示,因此我们在零图像中有一些白色部分,

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

这可以通过充分巧妙地使用图形的 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

可以用图windowcallbacks。 查看 the Figure object 和 Control+F 找到单词 'Callback',这将向您显示可用的回调。 使用这些方法,您可以编写一个应用程序来按照您的要求进行操作。

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,一个零图像,带有一些白色部分)

  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