|On this page…|
When you create a modular tool and associate it with a target image, the tool automatically makes the necessary connections to the target image to do its job. For example, the Pixel Information tool sets up a connection to the target image so that it can display the location and value of the pixel currently under the pointer.
As another example, the Overview tool sets up a two-way connection to the target image:
Target image to the Overview tool — If the visible portion of the image changes, by scrolling, panning, or by changing the magnification, the Overview tool changes the size and location of the detail rectangle to the indicate the portion of the image that is now visible.
Overview tool to the target image — If a user moves the detail rectangle in the Overview tool, the portion of the target image visible in the scroll panel is updated.
The modular tools accomplish this interactivity by using callback properties of the graphics objects. For example, the figure object supports a WindowButtonMotionFcn callback that executes whenever the mouse button is depressed. You can customize the connectivity of a modular tool by using the application programmer interface (API) associated with the tool to set up callbacks to get notification of events.
For example, the Magnification box supports a single API function: setMagnification. You can use this API function to set the magnification value displayed in the Magnification box. The Magnification box automatically notifies the scroll panel to change the magnification of the image based on the value. The scroll panel also supports an extensive set of API functions. To get information about these APIs, see the reference page for the modular tool.
To illustrate how to use callbacks to make the connections required for interactions between tools, this example uses the Scroll Panel API to build a simple image comparison GUI. This custom tool displays two images side by side in scroll panels that are synchronized in location and magnification. The custom tool also includes an Overview tool and a Magnification box.
function my_image_compare_tool(left_image, right_image) % Create the figure hFig = figure('Toolbar','none',... 'Menubar','none',... 'Name','My Image Compare Tool',... 'NumberTitle','off',... 'IntegerHandle','off'); % Display left image subplot(121) hImL = imshow(left_image); % Display right image subplot(122) hImR = imshow(right_image); % Create a scroll panel for left image hSpL = imscrollpanel(hFig,hImL); set(hSpL,'Units','normalized',... 'Position',[0 0.1 .5 0.9]) % Create scroll panel for right image hSpR = imscrollpanel(hFig,hImR); set(hSpR,'Units','normalized',... 'Position',[0.5 0.1 .5 0.9]) % Add a Magnification box hMagBox = immagbox(hFig,hImL); pos = get(hMagBox,'Position'); set(hMagBox,'Position',[0 0 pos(3) pos(4)]) %% Add an Overview tool imoverview(hImL) %% Get APIs from the scroll panels apiL = iptgetapi(hSpL); apiR = iptgetapi(hSpR); %% Synchronize left and right scroll panels apiL.setMagnification(apiR.getMagnification()) apiL.setVisibleLocation(apiR.getVisibleLocation()) % When magnification changes on left scroll panel, % tell right scroll panel apiL.addNewMagnificationCallback(apiR.setMagnification); % When magnification changes on right scroll panel, % tell left scroll panel apiR.addNewMagnificationCallback(apiL.setMagnification); % When location changes on left scroll panel, % tell right scroll panel apiL.addNewLocationCallback(apiR.setVisibleLocation); % When location changes on right scroll panel, % tell left scroll panel apiR.addNewLocationCallback(apiL.setVisibleLocation);
The tool sets up a complex interaction between the scroll panels with just a few calls to Scroll Panel API functions. In the code, the tool specifies a callback function to execute every time the magnification changes. The function specified is the setMagnification API function of the other scroll panel. Thus, whenever the magnification changes in one of the scroll panels, the other scroll panel changes its magnification to match. The tool sets up a similar connection for position changes.
The following figure is a sequence diagram that shows the interaction between the two scroll panels set up by the comparison tool for both changes in magnification and location.
Scroll Panel Connections in Custom Image Comparison Tool
To use the image comparison tool, pass it two images as arguments.
left_image = imread('peppers.png'); right_image = edge(left_image(:,:,1),'canny'); my_image_compare_tool(left_image,right_image);
The tool opens a figure window, displaying the two images side by side, in separate scroll panels. The custom compare tool also includes an Overview tool and a Magnification box. When you move the detail rectangle in the Overview tool or change the magnification in one image, both images respond.