I have a figure with a slider on it and then I want to have an external figure that displays in image. I want to be able to use the slider to change the transparency patches I am drawing on the image.
Currently this works but it generates a new image every time I move the slider. I would like the patch to be updated on the current image instead of opening a new window.
I have been having a difficult time figuring this out. I t开发者_StackOverflow中文版hink I need to define the image figure and handles outside of the slider callback, but I cant seem to figure it out. I have some of my code below.
I have updated my code to include your suggestions. However I am getting this error
??? Error using ==> set
Invalid property found.
Object Name : root
Property Name : 'FaceAlpha'.
Error in ==> readraw4>slider_callback at 539
set(hpatch, 'FaceAlpha',alpha)
I am not sure how to fix this or what is going wrong. Everything else seems to be working though.
hFig = figure('menubar','none');
hAx = axes('Parent',hFig);
hSlider = uicontrol('Parent',hFig, 'Style','slider', ...
'Value',0.5, 'Min',0, 'Max',1, 'SliderStep',[1 10]./100, ...
'Units','pixels', 'Position',[10 10 200 20], ...
'Callback',@slider_callback);
hTxt = uicontrol('Parent',hFig, 'Style','text', 'String','0.5', ...
'Units','pixels', 'Position',[290 28 30 15]);
imshow(im,'DisplayRange', [0.05 0.4], 'Parent',hAx)
hpatch = [];
for i =1:200
if spectImage(i) == 1
%disp('hi');
hpatch(i) = patch([x2(i+18) x2(i+19) x2(i+2) x2(i+1)],[y2(i+18) y2(i+19) y2(i+2) y2(i+1)],[0 0 0 0],'Parent',hAx, 'FaceColor','r', 'EdgeColor','none', 'FaceAlpha',0.5)
else
hpatch(i) = patch([x2(i+17) x2(i+18) x2(i+1) x2(i)],[y2(i+17) y2(i+18) y2(i+1) y2(i)],[0 0 0 0],'Parent',hAx, 'FaceColor','r', 'EdgeColor','none', 'FaceAlpha',0.5);
end
end
function slider_callback(hObj,eventdata)
%# get new alpha value
alpha = get(hObj,'Value');
%# update patches transparency and label
set(hTxt, 'String',num2str(alpha,'%.02f'))
set(hpatch, 'FaceAlpha',alpha)
end
In the callback function, you are creating a figure and plotting the image and patches. This is done over and over again each time the slider emits an event. You should instead take this part out, and only update the transparency of the patches inside the callback function.
Here is a working example. I am using nested function to share the data, but you can pass the necessary handles explicitly if you prefer it:
function patchAlphaSliderGUI()
%# read image, and create patches locations
img = imread('coins.png');
BW = imfill(im2bw(img),'holes');
B = bwboundaries(BW,8);
%# setup GUI
hFig = figure('menubar','none');
hAx = axes('Parent',hFig);
hSlider = uicontrol('Parent',hFig, 'Style','slider', ...
'Value',0.5, 'Min',0, 'Max',1, 'SliderStep',[1 10]./100, ...
'Units','pixels', 'Position',[150 5 300 20], ...
'Callback',@slider_callback);
hTxt = uicontrol('Parent',hFig, 'Style','text', 'String','0.5', ...
'Units','pixels', 'Position',[290 28 30 15]);
%# draw image and patches
imshow(img, 'Parent',hAx)
hPatch = zeros(numel(B),1);
for i=1:numel(B)
hPatch(i) = patch('XData',B{i}(:,2), 'YData',B{i}(:,1), ...
'Parent',hAx, 'FaceColor','r', 'EdgeColor','none', 'FaceAlpha',0.5);
end
function slider_callback(hObj,eventdata)
%# get new alpha value
alpha = get(hObj,'Value');
%# update patches transparency and label
set(hTxt, 'String',num2str(alpha,'%.02f'))
set(hPatch, 'FaceAlpha',alpha)
end
end
精彩评论