How to Zoom in multi-axes in the same time in matlab GUI? - matlab

I have a GUI with two axes. The first one for original image while the second one is for interpolated image.
First in my code, I use imrect to select part of the original image and then I crop that part using imcrop . After that I display the cropped image in both axes.
What I want is to know how to zoom in the first axes (the original image) and it shows the same zooming the second axes ( interpolated image ) automatically.
Thanks a lot in advance.

It is not clear the relationship between the two actions you mention in your question:
using imrect and imcrop to work on the image
zooming on one axes and have the same zoom on the second
A possible solution to automatically apply the zoom made on the first axes to the second one could be using the linkaxes built-in function.
If in your GUI you have two axex with, respectively, tag axes1 and axes2, you can add the following statements in the GUI OpeningFcn
linkaxes([handles.axes1 handles.axes2])
This allows automatically applying the zoom you make on axes1 also to axes2.
Hope this helps.

Related

How can I prevent objects from blocking ButtonDownFcn callback in a figure?

I have a plot of an image, made with imagesc. I'm developing a workflow where a user can click on the image and interactively overlay a curve. My first issue was that the ButtonDownFcn for the figure is blocked by the image. I worked around this by setting the ButtonDownFcn on the image object instead (and walking back up the parents to get to the figure). Now I'm finding that the curve I'm plotting can also block callbacks intended for the image.
My searches have indicated that I should be able to disable the "hittest" property to prevent the problem, but this has been ineffective for both the image and plotted lines. Suggestions?
Example:
file 1
function testCallback(src,data)
disp('Hello')
file 2
fig = imagesc(rand(100,100));
set(fig,'ButtonDownFcn',#testCallback);
hold on
plot([0 100],[0 100],'m-','linewidth',5,'hittest','off')
Unable to trigger callback when clicking on the line.
You could also set the button down function for the line that you add. Change your plot command to this:
plot([0 100],[0 100],'m-','linewidth',5,'ButtonDownFcn',#testCallback)

Matlab: Put an image ON TOP of a plot, after the data has been plotted

I have searched the internet for a solution to the question above but have had no luck up to now. I have been producing a number of 2D plots where the origin of (0,0 point) is represented by an image. I have made these by plotting the data on an image, where the image is all white apart from the desired symbol at the center point (so I can reshape it as needed). I then move the axis so they cross at the center point. This all works fine, but I now have to create a number of plots using ‘fill’ to place two shaded areas on the plot that overlap in the center. This causes the symbol to be difficult to see even using ‘alpha’.
I therefore have two options to get the desired effect, both requiring me to put an image on top of the figure after the data is plotted. These options are:
1) I place the image on top of the plot and apply alpha to it (May not look very good as it will mute the plot).
2) The better option would be to crop the image around the symbol and then position it on top of the plot so the image center is at the origin (I have no idea how to position the image this way).
Both methods need the image to be placed on top of the plot after the data is plotted. I have tried 'hold on' and calling 'figure(imagesc(Image))' neither work. I have Matlab 2012b but no toolboxes (so cannot use subimage etc.)
Thanks for any help you can give
You can set the transparency of individual pixels of an image using the 'AlphaData' option. So, you can plot the overlay as follows:
% plot your data normally
...
hold on
% assuming the overlay is a 11x11 image
image(-5:5,-5:5,image_matrix,'AlphaData',alpha_matrix);
image_matrix would obviously be the matrix with your image data stored in it, while alpha_matrix would be a matrix of the same size as image_matrix. Every pixel you want to show would have a value of 1, every pixel you want to hide (the white pixels) would be 0.

Convert just the content of a figure to an image in MATLAB

I am currently using getframe() and frame2im in MATLAB to convert a figure of a plot to an image.
I just realized that this is working almost like a screenshot of the figure, with all the axes and labels taken into account as well.
How can I convert JUST the contents of the figure (aka the "plot") into an image?
I don't really want to save all of them to file first.
You can use the getframe / cdata idiom. What this will do is that if you call getframe on the current frame in focus without any parameters, it will return a structure to you that contains a structure element called cdata. This stores the RGB pixel array of just the figure contents themselves. The axes and labels are not captured - only what is painted onto the figure is captured.
Here's an example to get you started:
im = imread('cameraman.tif');
imshow(im);
h = getframe;
out = h.cdata;
figure;
imshow(out); %// Should give you the contents within the imshow frame.
FWIW, I also answered this same question here, though it was for a different situation: keep new image when drawing lines by dragging the mouse in matlab
As far as i know, cdata DOES NOT WORK. I had a major problem recently with the same thing - the only work around i could find is to crop each image after using getframe and cdata - this will work fine for images that are all the same size (ugly as it is - you just need to find the grey edges in the image), but if the images are all different, this wont work (well, it wont work well. there might be some way to automatically adjust the crop size)

How can I display different images on different axes in the same GUI in MATLAB?

I'm using MATLAB R2012a to develop a GUI for manual medical image segmentation. In particular, I want this regions to train a classifier for automatic brain tissue classification.
The GUI I design contains 2 axes, with tags 'figureImage' and 'figureVOI', respectively. In the first one I want to display a single slice of a 3D MRI scan, and in the other one I want to show the mask associated to that slice. I allow the user to move between slices using a scroll bar.
I'm using a 3D matrix to represent the image ('image'), and a 3D matrix to represent the mask ('voi'), both of them in the handles structure. I initialize the 'voi' matrix with zeros when the GUI is loaded.
The code I applied when the user clics on the scroll bar is the next:
% update the number of the actual slice
handles.actualSlice = round(get(handles.sliceSelector, 'Value'));
% update the image and the mask
axes(handles.figureImage)
imshow(handles.image(:, :, handles.actualSlice));
axes(handles.figureVOI)
imshow(handles.voi(:, :, handles.actualSlice));
However, when I clic on the scroll bar, the GUI just scroll to cut nº 70 aprox., and then all then the GUI stops to update the axes. If I close the window and try to run the GUI again, and MATLAB shows me a system error.
I want to know what I'm doing bad, and if there is another way to do what I need to do. Thanks a lot! :)
This is fairly easy question. You may have have different axis names and axes positioned at different locations.Then you route your images to the respective axes depending on which ever you want to choose to work with as the axes and you can choose both at the same time. Hope this helps. Good luck.

MATLAB ButtonDownFcn

I have a project of "Optical Character Recognition" in MATLAB and i need your help:
how do i recognize when the user press the mouse on an image?
i trying to do this with ButtonDownFcn but even when i just printing message
the message is not printed.
i want to allow the user to select the license plate from the image.
how can i do this and save the Pixels of the selected area?
thanks in advance.
Addressing your two questions:
I'm guessing that you are trying to set the 'ButtonDownFcn' of the figure window, which won't work how you expect it to. If you want to do something when the user clicks on an image, you should make sure that you're setting the 'ButtonDownFcn' of the image, and not the figure window or the axes object. Note this line in the figure property documentation (emphasis added by me):
Executes whenever you press a mouse button while the pointer is in the figure window, but not over a child object (i.e., uicontrol, uipanel, axes, or axes child).
This is why you have to set a 'ButtonDownFcn' for each object you want it to work for. Setting it for the figure window won't make it work automatically for each object in the figure. Here's an example that sets the 'ButtonDownFcn' for the figure and an image object:
img = imread('peppers.png'); %# Load a sample image
hFigure = figure; %# Create a figure window
hImage = image(img); %# Plot an image
set(hFigure,'ButtonDownFcn',... %# Set the ButtonDownFcn for the figure
#(s,e) disp('hello'));
set(hImage,'ButtonDownFcn',... %# Set the ButtonDownFcn for the image
#(s,e) disp('world'));
Notice how clicking inside and outside the image displays a different message, since each calls the 'ButtonDownFcn' for a different object. Notice also that if you click on the tick mark label of one of the axes, nothing is displayed. This is because the axes object has its own 'ButtonDownFcn', which wasn't set to anything.
If you have access to the Image Processing Toolbox you can use the function IMFREEHAND to have the user draw an ROI (region of interest) in the image. Then you can use the createMask method to create a binary mask of the image with ones for pixels inside the ROI and zeroes for pixels outside the ROI.