Can't Use getappdata on a Callback
Show older comments
I have a piece of code in which I want to add two different noise on images sequentially (i want to see each result everytime the noise has been added). I use setappdata and then later on I call the data using getappdata which returns an empty matrix even though it is not empty.
Here is the summary of what i want to do in my codes:
(1) Open images (2) Using setappdata to store all images on axes1, axes2, axes3, axes4, and axes5 (3) Using getapp data to add first noise to all of images (the function is in another .m file) (4) Using setappdata to store all the noisy images on axes1, axes2, axes3, axes4, and axes5 (5) Using getapp data to add second noise to all of the noisy images (the function is also in another .m file)
Please see the codes below.
Step 1 (I did it for all 5 images stored in variable H2, H3, H4, H5):
function open_Callback(hObject, eventdata, handles)
project=guidata(gcbo);
[filename,directory]=uigetfile({'*.bmp';'*.*'},'Open Image');
H1 = imread(fullfile(directory,filename));
imshow(H1,'Parent',handles.axes1);
Step 2:
setappdata(handles.axes1, 'img1', H1)
setappdata(handles.axes2, 'img2', H2)
setappdata(handles.axes3, 'img3', H3)
setappdata(handles.axes4, 'img4', H4)
setappdata(handles.axes5, 'img5', H5)
Step 3 on another .m file:
function AddNoiseF1(handles)
% This is a function to add the 1st noise
% It's executed when the user press the "Add Noise 1" button
% _________________________________________________________________
% get noise type
noiseString = get(handles.inoise1,'String');
noiseValue = get(handles.inoise1,'Value');
% _________________________________________________________________
% Passing image to function AddNoiseF1
% get the previously-set data
I1 = getappdata(handles.axes1,'img1')
I2 = getappdata(handles.axes2,'img2')
I3 = getappdata(handles.axes3,'img3')
I4 = getappdata(handles.axes3,'img3')
I5 = getappdata(handles.axes3,'img3')
% _________________________________________________________________
% switch for the selected noise type
% if speckle is selected, the variable needed is only the variance
% if gaussian is selected, the variable needed are mean and variance
% if salt & pepper is selected, the variable needed is only the density
% if motion blur is selected, the variable needed is len and theta
switch noiseString{noiseValue};
case 'Speckle' % User selects speckle.
varVal = get(handles.ivariance1,'Value');
image1 = imnoise(I1,'speckle',varVal);
image2 = imnoise(I2,'speckle',varVal);
image3 = imnoise(I3,'speckle',varVal);
image4 = imnoise(I4,'speckle',varVal);
image5 = imnoise(I5,'speckle',varVal);
case 'Gaussian' % User selects gaussian.
meanVal = get(handles.imean1,'Value');
varVal = get(handles.ivariance1,'Value');
image1 = imnoise(I1,'gaussian',meanVal,varVal);
image2 = imnoise(I2,'gaussian',meanVal,varVal);
image3 = imnoise(I3,'gaussian',meanVal,varVal);
image4 = imnoise(I4,'gaussian',meanVal,varVal);
image5 = imnoise(I5,'gaussian',meanVal,varVal);
case 'Salt & Pepper' % User selects salt & pepper.
denVal = get(handles.idensity1,'Value');
image1 = imnoise(I1,'salt & pepper',denVal);
image2 = imnoise(I2,'salt & pepper',denVal);
image3 = imnoise(I3,'salt & pepper',denVal);
image4 = imnoise(I4,'salt & pepper',denVal);
image5 = imnoise(I5,'salt & pepper',denVal);
case 'Motion Blur' % User selects Motion Blur.
lenVal = (get(handles.ilen1,'Value'));
thetaVal = (get(handles.itheta1,'Value'));
motionFilter = fspecial('motion',lenVal,thetaVal);
image1 = imfilter(I1,motionFilter,'replicate');
image2 = imfilter(I2,motionFilter,'replicate');
image3 = imfilter(I3,motionFilter,'replicate');
image4 = imfilter(I4,motionFilter,'replicate');
image5 = imfilter(I5,motionFilter,'replicate');
end
imshow(image1,'Parent',handles.axes1);
imshow(image2,'Parent',handles.axes2);
imshow(image3,'Parent',handles.axes3);
imshow(image4,'Parent',handles.axes4);
imshow(image5,'Parent',handles.axes5);
step 4:
function addnoise2_Callback(hObject, eventdata, handles)
% hObject handle to addnoise2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
K1 = getappdata(handles.axes1,'image1') % get the noisy image from axes1
K2 = getappdata(handles.axes2,'image2') % get the noisy image from axes2
K3 = getappdata(handles.axes3,'image3') % get the noisy image from axes3
K4 = getappdata(handles.axes4,'image4') % get the noisy image from axes4
K5 = getappdata(handles.axes5,'image5') % get the noisy image from axes5
% set the data for further command
setappdata(handles.axes1, 'noisy1', K1)
setappdata(handles.axes2, 'noisy2', K2)
setappdata(handles.axes3, 'noisy3', K3)
setappdata(handles.axes4, 'noisy4', K4)
setappdata(handles.axes5, 'noisy5', K5)
AddNoiseF2(handles);
Step 5 also on another .m file
function AddNoiseF2(handles)
% This is a function to add the 2nd noise
% It's executed when the user press the "Add Noise 2" button
% _________________________________________________________________
% get noise type
noiseString = get(handles.inoise2,'String');
noiseValue = get(handles.inoise2,'Value');
% _________________________________________________________________
% Passing image to function AddNoiseF2
% get the previously-set data
I1 = getappdata(handles.axes1,'img1')
I2 = getappdata(handles.axes2,'img2')
I3 = getappdata(handles.axes3,'img3')
I4 = getappdata(handles.axes4,'img3')
I5 = getappdata(handles.axes5,'img3')
% _________________________________________________________________
% switch for the selected noise type
% if speckle is selected, the variable needed is only the variance
% if gaussian is selected, the variable needed are mean and variance
% if salt & pepper is selected, the variable needed is only the density
% if motion blur is selected, the variable needed is len and theta
switch noiseString{noiseValue};
case 'Speckle' % User selects speckle.
varVal = get(handles.ivariance2,'Value');
image1 = imnoise(I1,'speckle',varVal);
image2 = imnoise(I2,'speckle',varVal);
image3 = imnoise(I3,'speckle',varVal);
image4 = imnoise(I4,'speckle',varVal);
image5 = imnoise(I5,'speckle',varVal);
case 'Gaussian' % User selects gaussian.
meanVal = get(handles.imean2,'Value');
varVal = get(handles.ivariance2,'Value');
image1 = imnoise(I1,'gaussian',meanVal,varVal);
image2 = imnoise(I2,'gaussian',meanVal,varVal);
image3 = imnoise(I3,'gaussian',meanVal,varVal);
image4 = imnoise(I4,'gaussian',meanVal,varVal);
image5 = imnoise(I5,'gaussian',meanVal,varVal);
case 'Salt & Pepper' % User selects salt & pepper.
denVal = get(handles.idensity2,'Value');
image1 = imnoise(I1,'salt & pepper',denVal);
image2 = imnoise(I2,'salt & pepper',denVal);
image3 = imnoise(I3,'salt & pepper',denVal);
image4 = imnoise(I4,'salt & pepper',denVal);
image5 = imnoise(I5,'salt & pepper',denVal);
case 'Motion Blur' % User selects Motion Blur.
lenVal = (get(handles.ilen2,'Value'));
thetaVal = (get(handles.itheta2,'Value'));
motionFilter = fspecial('motion',lenVal,thetaVal);
image1 = imfilter(I1,motionFilter,'replicate');
image2 = imfilter(I2,motionFilter,'replicate');
image3 = imfilter(I3,motionFilter,'replicate');
image4 = imfilter(I4,motionFilter,'replicate');
image5 = imfilter(I5,motionFilter,'replicate');
end
imshow(image1,'Parent',handles.axes1);
imshow(image2,'Parent',handles.axes2);
imshow(image3,'Parent',handles.axes3);
imshow(image4,'Parent',handles.axes4);
imshow(image5,'Parent',handles.axes5);
When i run the codes, the variable K1, K2, K3, K4, and K5 didn't get the data. So the function AddNoiseF2(handles) add noise on the data set on step 2, which is not what i want to do.
K1 =
[]
K2 =
[]
K3 =
[]
K4 =
[]
K5 =
[]
Please help me on this. Thanks in advance!
2 Comments
You haven't shown any code where you actually set the data that you get as K1 etc so it is a bit difficult to make any intelligent suggestions without that. Also in addNoise2 you get the app data called 'img1' rather than the (supposedly) previously set image1 or noisy1
Pearly Adinegoro
on 6 Apr 2017
Accepted Answer
More Answers (0)
Categories
Find more on Update figure-Based Apps in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!