get information from click on any object in the image in matlab
I work on image segmentation based on color object.. now I need to get the user click value on the object in order to use this information (click value)in another process. How can开发者_开发知识库 I get this value in matlab. any one can help me please.
Regards
I'm not sure if this answers your question, but plot objects (i.e. lines, patches, images, etc.) have a ButtonDownFcn
callback that will execute when you press a mouse button while the pointer is over the object.
Here's a simple example (using nested functions and function handles) of how you could use ButtonDownFcn
callbacks to get information about the objects selected. First, save this function in an m-file:
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
Next, run the above code and save the returned function handle in a variable:
colorFcn = colored_patches;
This will create a figure with 3 colored boxes, like so:
Now, when you click the mouse over one of the colored boxes, the output from colorFcn
will change:
%# 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
If you want the user to click on a plot or image and get the coordinates where they clicked, you can use ginput. For example,
[x,y] = ginput(1);
will give you the coordinates of one click. You can then use your own logic to figure out which object that corresponds to.
If this isn't what you're trying to do, you'll have to explain more clearly.
精彩评论