从matlab中单击图像中的任何对象获取信息

时间:2010-12-01 17:21:26

标签: matlab onclick

我从事基于颜色对象的图像分割。现在我需要在对象上获取用户点击值,以便在另一个进程中使用此信息(单击值)。如何在matlab中获取此值。任何人都可以帮助我。

此致

2 个答案:

答案 0 :(得分:2)

如果您希望用户点击绘图或图像并获取他们点击的坐标,您可以使用ginput。例如,

[x,y] = ginput(1);

将为您提供一次点击的坐标。然后,您可以使用自己的逻辑来确定与哪个对象相对应。

如果这不是你想要做的,你必须更清楚地解释。

答案 1 :(得分:2)

我不确定这是否能回答你的问题,但是情节对象(例如linespatchesimages等)有一个ButtonDownFcn回调当指针在对象上时按下鼠标按钮时执行。

以下是一个简单示例(使用nested functionsfunction handles),了解如何使用ButtonDownFcn回调来获取有关所选对象的信息。首先,将此函数保存在m文件中:

function colorFcn = colored_patches

  selectedColor = [1 0 0];  %# The default selected color

  figure;                                  %# Create a new figure
  axes;                                    %# Create a new axes
  patch([0 0 1 1],[0 1 1 0],'r',...        %# Plot a red box
        'ButtonDownFcn',@patch_callback);
  hold on;                                 %# Add to the existing plot
  patch([2 2 4 4],[1 2 2 1],'g',...        %# Plot a green box
        'ButtonDownFcn',@patch_callback);
  patch([1 1 2 2],[3 4 4 3],'b',...        %# Plot a blue box
        'ButtonDownFcn',@patch_callback);
  axis equal;                              %# Set axis scaling

  colorFcn = @get_color;  %# Return a function handle for get_color

%#---Nested functions below---

  function patch_callback(src,event)
    selectedColor = get(src,'FaceColor');  %# Set the selected color to the
                                           %#   color of the patch clicked on
  end

  function currentColor = get_color
    currentColor = selectedColor;          %# Return the last color selected
  end

end

接下来,运行上面的代码并将返回的函数句柄保存在变量中:

colorFcn = colored_patches;

这将创建一个带有3个彩色框的图形,如下所示:

alt text

现在,当您在其中一个彩色框上单击鼠标时,colorFcn的输出将会改变:

%# Click the red box, then call colorFcn
>> colorFcn()
ans =
     1     0     0  %# Returns red

%# Click the blue box, then call colorFcn
>> colorFcn()
ans =
     0     0     1  %# Returns blue

%# Click the green box, then call colorFcn
>> colorFcn()
ans =
     0     1     0  %# Returns green