【问题标题】:MATLAB how to get mouse click coordinatesMATLAB如何获取鼠标点击坐标
【发布时间】:2013-02-04 10:15:43
【问题描述】:

我使用 GUIDE 制作了我的 GUI。我有一个轴(tag=axes1)。图像显示在轴中。 当我单击图像(内轴)时,我需要获取坐标。 当图像没有添加到轴时,我得到了坐标值。但是图像显示在轴上没有获得轴。如何获取坐标?

【问题讨论】:

    标签: matlab user-interface axes


    【解决方案1】:

    假设您使用句柄 imageHandle 绘制了图像:

    imageHandle = imshow(imageObj);
    

    您应该将ButtonDownFcn 分配给图像句柄而不是轴句柄:

    set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
    

    并从此函数中获取鼠标坐标如下:

    function ImageClickCallback ( objectHandle , eventData )
       axesHandle  = get(objectHandle,'Parent');
       coordinates = get(axesHandle,'CurrentPoint'); 
       coordinates = coordinates(1,1:2);
       %// then here you can use coordinates as you want ...
    end
    

    你可以试试这个小demo来检查我的答案:

    function demoOnImageClick
    clc;clear;
    imObj = rand(500,500);
    figure;
    hAxes = axes();
    imageHandle = imshow(imObj);
    set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
    
    function ImageClickCallback ( objectHandle , eventData )
    axesHandle  = get(objectHandle,'Parent');
    coordinates = get(axesHandle,'CurrentPoint'); 
    coordinates = coordinates(1,1:2);
    message     = sprintf('x: %.1f , y: %.1f',coordinates (1) ,coordinates (2));
    helpdlg(message);
    end
    
    end
    

    【讨论】:

    • @HelloASP ,如果满足您的问题要求,您可以接受答案,结束本问题的讨论。
    • @HelloASP 请确保检查 Sameh 的答案是否正确。 thnx :]
    • 感谢您的示例。也许用imagesc 替换imshow,因为并非所有Matlab 用户都有Image Processing Toolbox
    【解决方案2】:

    你可以通过图片句柄获取坐标轴句柄

    ah = get( imageHandle, 'Parent' );
    

    然后就可以通过

    得到鼠标点击的位置了
    p = get( ah, 'CurrentPoint' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2014-09-07
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多