Superimpose several images in one axes by enabling checkboxes in GUI
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have several grayscale images with the color map. I have written a GUI to plot a single image on the defined axes. Now I want to write several checkboxes so that if I check each one, it shows the related image. I want it in a way that it can show both the individual (when just one checkbox is enabled) and superimposed (when several checkboxes are enabled). The code that I written is like this:
function Gui
%%create the figure
Fig_handle = figure;
set(Fig_handle,'Tag','screen','HandleVisibility','on','Visible','on');
%%Image is read from a structure named Image (actually it is more than 3)
Image.1;
Image.2;
Image.3;
%%Colormaps are stored in a structure named ColorMap: (actually it is more than 3)
ColorMap.1 = [255 0 0];
ColorMap.1 = [255 123 0];
ColorMap.1 = [0 255 123];
%%creating the Axes:
axes_handle=axes('Units','normalized','Position', [.01 0.1 .75 .85]);
imshow(Image.1,'Parent', axes_handle);
% create checkbox:
for m = 1: 3
Sfields = filedsname (Image); % getting the name of each image for checkbox string
name = char(Image.(Sfields));
uicontrol(Fig_handle,'Style', 'checkbox',...
'String', sprintf('%s', name),...
'Value', 0, 'Tag', sprintf('%s', name),...
'Units','normalized','Position',[.01+(m-1)/15 .05 .1 .02],...
'Callback', {@Callback_Function});
end
end
I prefer to have just one callback function for all the Images because I have almost 20 images and it is just one part of my GUI. So, I know that the callback function should be written in a way that it can find the checkbox values from figure handle and run a specific code, knowing which images are enabled to be shown.
function Callback_Function(hObject, ~, ~)
handles = guidata(hObject);
for i = 1: size(SFields,1)
checkboxStatus = handles.Fig.(SFields).Value;
if checkboxStatus ==1
imh = imhandles(handles.Fig.screen);
newImage = ??????
set(imh,'CData',newImage);
else
imh = imhandles(handles.Fig.screen);
newImage = ??????
set(imh,'CData',newImage);
end
end
end
Can you please help me writing the callback function? Thanks,
0 comentarios
Respuestas (1)
Voss
el 3 de En. de 2024
It sounds like you could create an image object in the axes for each of the fields in the Image structure, and set the checkboxes to toggle the visibility of their corresponding image. Here's one way to do that.
function Gui
% create the figure
Fig_handle = figure('Tag','screen','HandleVisibility','on','Visible','on');
% create the axes:
axes_handle = axes('Parent',Fig_handle,'Units','normalized','Position', [.01 0.1 .75 .85]);
% some image data in a structure:
Image = struct( ...
'image_1',cat(3,randi([0 255],100,100,'uint8'),zeros(100,100,2,'uint8')), ...
'image_2',cat(3,zeros(100,100,'uint8'),randi([0 255],100,100,'uint8'),zeros(100,100,'uint8')), ...
'image_3',cat(3,zeros(100,100,2,'uint8'),randi([0 255],100,100,'uint8')), ...
'image_4',repmat(randi([0 255],100,100,'uint8'),[1 1 3]));
% create checkboxes and image objects:
Sfields = fieldnames(Image);
NI = numel(Sfields);
cb = gobjects(1,NI);
im = gobjects(1,NI);
for m = 1:NI
name = Sfields{m};
im(m) = image( ...
'Parent',axes_handle, ...
'XData',[1 size(Image.(name),2)], ...
'YData',[1 size(Image.(name),1)], ...
'CData',Image.(name), ...
'AlphaData',0.5, ...
'Visible','off');
cb(m) = uicontrol(Fig_handle,'Style', 'checkbox',...
'String', name,...
'Value', 0, 'Tag', name,...
'Units','normalized','Position',[.01+(m-1)/15 .05 .1 .02],...
'Callback', {@Callback_Function,im(m)});
end
set(axes_handle,'XLim',[1 100],'YLim',[1 100]);
function Callback_Function(src,~,my_im)
if get(src,'Value')
set(my_im,'Visible','on');
else
set(my_im,'Visible','off');
end
end
end
The checkbox callback works by passing the corresponding image object as an input; then the callback merely sets the visibility of the image based on the checkbox's value.
In this simple example, each image is the same size (100x100) and they are all in the same location in the axes, so I set the image objects' AlphaData (opacity = 1-transparency) to 0.5 to allow seeing multiple images when they overlap and are visible. In general, I imagine you'd want to set the XData and YData (i.e., the location within the axes) of each image appropriately, and if they don't overlap (much) and it makes sense to do so, you can remove setting the AlphaData so that all images are fully opaque.
0 comentarios
Ver también
Categorías
Más información sobre Red en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!