How to add image overlay in GUIDE ?
3 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!