How to add image overlay in GUIDE ?

13 views (last 30 days)
Hello!
I am trying to overlay markers on an image displayed in a GUI. I tried to have my markers in a matrix which I would like to overlay on my image but couldn't find the way to do that. Coulc you please help me?
I am displaying the main image on axes, from the slider callback which is looking like that:
function slider3_Callback(hObject, eventdata, handles)
% ...
dims=size(handles.nii.img);
handles.cursor(1)=int16(get(hObject, 'Value'));
image1=reshape(handles.nii.img(handles.cursor(1),:,:), dims(2), dims(3));
imghandle=findobj(handles.axes1, 'Type','image');
set(imghandle, 'CData',image1');
% like that my image is displayed, how to overlay a second one?
% ...
guidata(hObject,handles)
doCommonUpdate(handles,'x')
end
I tried to 'hold on' and add the second image in 'CData' and then modify the transparency of this second image with 'AlphaData' but the overlay image is not an overlay and it takes the place of my primary image.
  2 Comments
Adam
Adam on 1 Mar 2019
Edited: Adam on 1 Mar 2019
The CData is of your original image. If you add a second image in addition to your original and set the AlphaData on that instead then you will have both images and the second one will have tramsparency defined by the AlphaData while the original will be full opaque underneath it - i.e. visible anywhere the AlphaData of the second image is < 1 (to varying degrees, and clearly where AlphaData is 0)
You should get used to using graphics handles returned by calls to things like imagesc or image or imshow ideally and keep hold of these for further use rather than having to do a findobj to relocate them. In many cases it doesn't matter, but if performance is an isse then having to search for objects that you could just access directly is not ideal.
Emilie Mussard
Emilie Mussard on 1 Mar 2019
Thanks a lot for your answer!
I hence tried to keep the handlers to my images, for example:
handles.hximage = image([1 dims(2)], [1 dims(3)], image1', 'CDataMapping', 'scaled', 'Parent', handles.axes1);
hold on
and then I do create a second image:
red = cat(3, ones(size(xslice_met)), zeros(size(xslice_met)), zeros(size(xslice_met)));
htest = image([1 dims(2)], [1 dims(3)], red, 'Parent', handles.axes1, 'AlphaData', image2_met');
But after having done that, only the second image is visible, it does not overlay the first one. When I try to access handles.hximage I got an error message:
"Error using matlab.graphics.primitive.Image/set
Invalid or deleted object."
It seems that I deleted my first image! What do I do wrong?

Sign in to comment.

Accepted Answer

Adam
Adam on 1 Mar 2019
Edited: Adam on 1 Mar 2019
(Original comment prepended to the answer for the last comment):
The CData is of your original image. If you add a second image in addition to your original and set the AlphaData on that instead then you will have both images and the second one will have tramsparency defined by the AlphaData while the original will be full opaque underneath it - i.e. visible anywhere the AlphaData of the second image is < 1 (to varying degrees, and clearly where AlphaData is 0)
Make sure you use explicit axes handles when using all plotting-based instructions also. Most plotting functions (and things like hold, xlim, axis, etc) have a syntax that allows you to pass in an axes handle as the first argument and then the remaining arguments are exactly as you would use them otherwise.
This ensure that your instructions are being applied to the axes you expect.
e.g.
hold on
applies hold on status to the current axes. Is the current axes actually the one you think it is at that moment? Don't take that risk. Give it an explicit instruction:
hold( hAxes, 'on' )
where hAxes is your axes handle, e.g. handles.axes1 is the default handle for the first axes in GUIDE, but this will be whatever you set its tag to.
I didn't notice until I scrolled just now actually that you are already doing this for plotting the original image, which is good, so just do it with the hold instruction too and you should be fine.
  1 Comment
Emilie Mussard
Emilie Mussard on 1 Mar 2019
Yes, indeed, I was probably not holding the right image! Thanks a lot for your help, I am new to these handles in Matlab!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!