How to add image overlay in GUIDE ?

조회 수: 3 (최근 30일)
Emilie Mussard
Emilie Mussard 2019년 3월 1일
댓글: Emilie Mussard 2019년 3월 1일
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
Adam
Adam 2019년 3월 1일
편집: Adam 2019년 3월 1일
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 2019년 3월 1일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Adam
Adam 2019년 3월 1일
편집: Adam 2019년 3월 1일
(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
Emilie Mussard
Emilie Mussard 2019년 3월 1일
Yes, indeed, I was probably not holding the right image! Thanks a lot for your help, I am new to these handles in Matlab!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by