You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to save image using one pushbutton
1 view (last 30 days)
Show older comments
i add noise in image at axes2, and then i want to save the image.can anyone help me?here is my code that i try
function checkbox1_Callback(hObject, eventdata, handles)
applyNoise(handles);
function applyNoise(handles)
if isfield(handles,'imgData')
imgData = handles.imgData;
noiseStr = '';
if get(handles.checkbox1,'Value')
noiseStr = 'Salt & pepper';
JData = imnoise(imgData,'salt & pepper',0.3);
end
if get(handles.checkbox2,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'Gaussian'];
JData = imnoise(imgData,'gaussian',0.1,0.1);
end
if get(handles.checkbox3,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'localvar'];
JData = imnoise(imgData,'localvar',0.05*rand(size(imgData)));
end
if get(handles.checkbox4,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'poisson'];
JData = imnoise(imgData,'poisson');
end
if get(handles.checkbox5,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'speckle'];
JData = imnoise(imgData,'speckle', 0.3);
end
axes(handles.axes2);
%imshow(imgData);
imshow(JData)
title(['Noise type: ' noiseStr,]);
handles.JData = imgData;
%save the handles data
%guidata(hObject,handles);
end
if isfield(handles,'JData')
imgData = handles.JData;
end
and here my save button
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'JData')
imgData = handles.JData;
%if isfield(handles,'imgData')
%imgData = handles.imgData;
[Save,savename] = uiputfile('*.jpg','save this file')
fname=fullfile(savename,Save);
imwrite(imgData,fname); % where F is your image
end
Accepted Answer
Image Analyst
on 29 Sep 2016
Change the declaration to return handles, like this
function handles = applyNoise(handles)
otherwise the changes you make to handles (like adding a field handles.JData) will never be seen by your other functions.
22 Comments
amira fauzey
on 5 Oct 2016
i try to make changes to my code,certain noise that i apply to image and save it,i check on my folder it successfully save the image with noise ,and certain noise when i save it and check at my folder,it show ori image without noise.i dont know how to solve my problem.here is my code
Image Analyst
on 6 Oct 2016
I'm traveling in Washington D.C. this week so I may not get to it, but attach your .fig file also so people can run your code.
amira fauzey
on 10 Oct 2016
hello image analyst, i can save the image now. but right now i have problem to add filter
Image Analyst
on 10 Oct 2016
I've read your comment/announcement.
amira fauzey
on 11 Oct 2016
can you help me please.... i really dont know how to solve my problem.
Walter Roberson
on 11 Oct 2016
You need to explain what your current problem is. What error message are you observing when you add a filter? What is your code for that section?
amira fauzey
on 11 Oct 2016
i want to add filter after i save my image noise using pushbutton.this is the code for filter.if user uncheck the checkbox i want the image with noise remain in the axes.
function checkbox6_Callback(hObject, eventdata, handles)
global noiseImage
applyFilter(handles);
function applyFilter(handles)
if isfield(handles,'imgData')
imgData = handles.imgData;
filterStr = '';
if get(handles.checkbox6,'Value')
filterStr = 'Average';
imgData = filter2(fspecial('average',3),imgData)/255;
end
if get(handles.checkbox7,'Value')
if ~isempty(filterStr)
filterStr = [filterStr ', '];
end
filterStr = [filterStr 'Median'];
imgData = medfilt2(imgData,[3 3]);
end
axes(handles.axes3);
noiseImage = imgData;
imshow(imgData);
title(['Filter: ' filterStr]);
end
i think maybe the problem is the line if isfield(handles,'imgData') seems like filter didnt pass through this line. I am not pretty sure , i dont know how to solve this problem. i attach my full coding too. i really hope that you can help me.
Image Analyst
on 11 Oct 2016
If you want the noise image, you need to show it, not the median filtered image. You're filtering it and then assigning the smoothed, filtered image to the same variable, thus overwriting your original, noisy data. You need to assign it to a new variable. Like:
filteredImage = filter2(fspecial('average',3),imgData)/255;
amira fauzey
on 12 Oct 2016
is it like this? i dont know if i'm doing it right
function checkbox6_Callback(hObject, eventdata, handles)
global noiseImage
%applyFilter(handles);
%function applyFilter(handles)
if isfield(handles,'imgData')
imgData = handles.imgData;
noiseImage = handles.imgData;
filterStr = '';
if get(handles.checkbox6,'Value')
filterStr = 'Average';
filteredImage = filter2(fspecial('average',3),imgData)/255; end
if get(handles.checkbox7,'Value')
if ~isempty(filterStr)
filterStr = [filterStr ', '];
end
filterStr = [filterStr 'Median'];
filteredImage = medfilt2(imgData,[3 3]);
end
axes(handles.axes3);
% noiseImage = imgData;
imshow(noiseImage );
title(['Filter type: ' filterStr]);
end
when i unchecked the checkbox, i want the noise before i add filter remain,how to do it?
Image Analyst
on 12 Oct 2016
Replace that with these 3 functions:
function checkbox6_Callback(hObject, eventdata, handles)
CheckBoxClicked(handles);
function checkbox7_Callback(hObject, eventdata, handles)
CheckBoxClicked(handles);
function CheckBoxClicked(handles)
global noiseImage
fontSize = 13;
% If noiseImage is not there, see if you can get it from handles.
if isempty(noiseImage)
if isfield(handles,'imgData')
noiseImage = handles.imgData;
else
warningMessage = sprintf('WARNING: noiseImage not defined yet!');
uiwait(warndlg(warningMessage));
return;
end
end
filterName = 'None';
% Initialize to original, noisy image.
% If no checkboxes are checked, this is what will display.
filteredImage = noiseImage;
% Now examine checkboxes to see if they want one of the filters.
if get(handles.checkbox6,'Value')
% Checkbox 6 is checked so do an average.
filterName = 'Average';
kernel = ones(3)/9;
filteredImage = conv2(double(noiseImage), kernel, 'same');
elseif get(handles.checkbox7,'Value')
% Checkbox 7 is checked so do a median filter.
filterName = 'Median';
filteredImage = medfilt2(noiseImage, [3, 3]);
end
axes(handles.axes3);
imshow(filteredImage, []);
caption = sprintf('Filter type = %s', filterName);
title(caption, 'FontSize', fontSize);
If you're using "end" to finish off your function definitions, then add ends. It's optional but if you do it for one function you need to do it for all of them. They either all have ends or they all do not have ends. You can't mix styles.
amira fauzey
on 13 Oct 2016
thank you so much image analyst, result come correctly
amira fauzey
on 14 Oct 2016
Edited: amira fauzey
on 14 Oct 2016
how to make user can add more then one filter at a time?
Image Analyst
on 14 Oct 2016
Replace the if/elseif construct with a bunch of separate if's. That way it can do more than 1. But I hope you know that filters are not commutative. Filtering an image with filter1 followed by filter2 will give a different image than filtering first with filter2, then filter1.
amira fauzey
on 14 Oct 2016
if get(handles.checkbox6,'Value')
% Checkbox 6 is checked so do an average.
filterName = 'Average';
kernel = ones(3)/9;
filteredImage = conv2(double(noiseImage), kernel, 'same');
end
if get(handles.checkbox7,'Value')
% Checkbox 7 is checked so do a median filter.
filterName = 'Median';
filteredImage = medfilt2(noiseImage, [3, 3]);
end
is it like this?
Image Analyst
on 14 Oct 2016
No. And it would depend it you want to keep separate output images, or if you want them to be cascaded, like the output of one filter goes into the input of the other filter.
amira fauzey
on 14 Oct 2016
Edited: amira fauzey
on 14 Oct 2016
i want it to be cascaded,like you said output of one filter goes into the input of the other filter.Like the combination of filters applied in the same noise image.after user click the checkboxes at the top of the image, it will show title of the combine filters.for example, Filter type = Average,Median
Walter Roberson
on 15 Oct 2016
filteredImage = double(noiseImage);
applied_filters = {'Noise'};
if get(handles.checkbox6,'Value')
% Checkbox 6 is checked so do an average.
filterName = 'Average';
kernel = ones(3)/9;
filteredImage = conv2(filteredImage, kernel, 'same');
applied_filters{end+1} = 'Average';
end
if get(handles.checkbox7,'Value')
% Checkbox 7 is checked so do a median filter.
filterName = 'Median';
filteredImage = medfilt2(filteredImage, [3, 3]);
applied_filters{end+1} = 'Median';
end
imshow(filteredImage, 'Parent', handles.axes3);
filt_list = strjoin(applied_filters, ', ');
title(filt_list, 'Parent', handles.axes3);
amira fauzey
on 15 Oct 2016
Edited: amira fauzey
on 15 Oct 2016
when i click the checkboxes, axes3 become white blank, and i get an error ??? Undefined function or method 'strjoin' for input arguments of type 'cell'.
amira fauzey
on 15 Oct 2016
i have edited the code to this,i change strjoin to horzcat
filteredImage = double(noiseImage);
applied_filters = {'Filter'};
if get(handles.checkbox6,'Value')
% Checkbox 6 is checked so do an average.
filterName = 'Average';
kernel = ones(3)/9;
filteredImage = conv2(double(noiseImage), kernel, 'same');
applied_filters{end+1} = 'Average';
end
if get(handles.checkbox7,'Value')
% Checkbox 7 is checked so do a median filter.
filterName = 'Median';
filteredImage = medfilt2(noiseImage, [3, 3]);
applied_filters{end+1} = 'Median';
end
axes(handles.axes3);
imshow(filteredImage, []);
filt_list = horzcat(applied_filters, ', ');
title(filt_list, 'Parent', handles.axes3);
not sure if correct, when i add two filter it look like it does reduce the noise.
Walter Roberson
on 15 Oct 2016
strjoin was introduced in R2013a . You can use
filt_list = sprintf('%s, ', applied_filters{:});
filt_list = filt_list(1:end-2); %trim off final ', '
Image Analyst
on 15 Oct 2016
OK, so are we done now? Just recall what I said about the order of the filters being applied makes a big difference!
amira fauzey
on 17 Oct 2016
It's okay now, thanks sir :)
More Answers (0)
See Also
Tags
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)