Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Steve on Image Processing

November 21st, 2006

Old habits

Last time I showed off custom data cursors with a little utility function that displays object labels as data cursors:

My code used label2rgb to display a "colorized" label matrix. The custom data cursor callback, though, needed the original label matrix, so I used setappdata to save the label matrix in a place where the callback routine could retrieve it.

Within a few hours of posting that code, I had a "Doh!" moment. I haven't used that kind of setappdata trick since nested function handles appeared in MATLAB 7.0. I have no idea why I suddenly reverted to my previous coding patterns. Old habits die hard, as the cliche says.

Anyway, nested function handles in MATLAB 7 can be used to create callback functions that carry around their own state. I thought I would show you how my display_label_matrix function looks using this technique.

type display_label_matrix
function display_label_matrix(L)
% display_label_matrix Display label matrix with custom data cursors
% display_label_matrix(L) displays the label matrix L as colored objects on
% a gray background.  It also installs a custom data cursor handler in the
% figure.  In data cursor mode, clicking on an object displays a data
% cursor containing the object's number.

% Steve Eddins
% Copyright 2006 The MathWorks, Inc.
% $Revision: 1.2 $  $Date: 2006/11/21 02:41:00 $

rgb = label2rgb(L, 'jet', [.7 .7 .7], 'shuffle');
h = imshow(rgb);

% Enable the figure data cursor mode, and use our own custom data cursor
% strings.
dcm = datacursormode(ancestor(h, 'figure'));
set(dcm,'Enable','on', ...
    'Updatefcn', @dataCursorText);

    %======================================================================
    function output_text = dataCursorText(obj, event_obj)
    % Respond to a user click in data cursor mode by displaying the label
    % number of the pixel that was clicked on.

        h_image = get(event_obj, 'Target');
        pos = get(event_obj, 'Position');
        clicked_label = L(round(pos(2)), round(pos(1)));
        output_text = sprintf('%d', clicked_label);
    end
    %----------------------------------------------------------------------
    
end




In the new version, the callback function, dataCursorText, is nested within the outer function, display_label_matrix. Notice how dataCursorText uses a variable, L, that is created in the scope of the outer function, display_label_matrix. When the outer function creates a function handle to dataCursorText, the value of L at that moment is captured into a kind of private workspace that goes along with the function handle. No auxiliary information or state needs to be stored anywhere else; the function handle has inside of it all the information it needs to perform its computation.

If you are interested in learning more about nested functions in MATLAB, you might want to look at Loren Shure's News & Notes article on the topic.


Get the MATLAB code

Published with MATLAB® 7.3

4 Responses to “Old habits”

  1. ramzy replied on :

    the equivalence of bwlabel in Matlab7 ?

  2. Steve replied on :

    Ramzy - bwlabel is a function in the Image Processing Toolbox. There’s no equivalent function in MATLAB. You might find something if you searched on the MATLAB Central File Exchange.

  3. aditya replied on :

    I tried the above code with a figure plot.But to my dismay it didnt work there…can u please help me out for the same.
    This is what I tried to do:
    function displayLabelData(fusedData)

    dataMat = [fusedData(:,1) fusedData(:,2) fusedData(:,3)];
    intMat = [fusedData(:,4)];

    h = figure,plot3(fusedData(:,1),fusedData(:,2),fusedData(:,3),’.');

    setappdata(h, ‘rangeData’, dataMat);
    setappdata(h, ‘intData’, intMat);

    dcm = datacursormode(ancestor(h, ‘figure’));
    set(dcm,’Enable’,'on’, …
    ‘Updatefcn’, @dataCursorText);

    function output_text = dataCursorText(obj, event_obj)

    h_image = get(event_obj, ‘Target’);
    pos = get(event_obj, ‘Position’);

    dataMat = getappdata(h_image, ‘rangeData’);
    intMat = getappdata(h_image, ‘intData’);

    clicked_label = dataMat;
    clicked_label_Z = intMat;

    output_text = {[’X: ‘,num2str(pos(1))],[’Y: ‘,num2str(pos(2))],[’Z: ‘,num2str(pos(3))],clicked_label_Z};

    %%%
    I want display a fourth variable alongwith three other variables of the graph..

    please hepl its kind of uregent

  4. aditya replied on :

    Hi,
    Please ignore my earlier message for I have solved the problem.
    Thanks and Regards
    Aditya

Leave a Reply


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Assaf: Hi, I have 3 questions regarding the following sentence: “Rather than using the power spectrum from a...
  • Steve: Cara—I do not understand your question. Can you clarify it?
  • Cara Schiek: Hi. How could I do this same thing with image vales within the patches? cara
  • Steve: Vincent—OK, thanks for the information.
  • Vincent: My only data point on multithreading is that the Performance tab of the Task Manager shows increased CPU...
  • Steve: Vincent—Thanks for giving it a try and reporting back. I’m a bit skeptical that multithreading...
  • Vincent: Oops numbers were wrong. Data set was 450MB large so the numbers are: Results: ImageJ alone =< 5 s (or 90...
  • Vincent: Steve- I just had a quick run at the new imread.m patch. It’s much faster than the previous version...
  • Steve: Erik—Also, separability of the kernel provides no speed benefit in FFT-based implementations....
  • Steve: Erik—Good questions. Remember that, practically speaking, when we filter a 2-D signal with a 1-D filter...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics