MATLAB: How to combine multiple noise type using checkbox. for example double noise and triple noise and apply it to the image when click the checkbox

checkboximage processingImage Processing Toolbox

I have 5 checkbox that represent 5 types of noise which is salt&pepper noise,localvar noise,gaussian noise, speckle noise and poisson noise. i want the user to be able to select multiple noise which is double noise or triple noise by clicking the checkbox and the noises will apply to the image.and after user click the checkboxes at the top of the image, it will show title of the combine noises.for example, title('poisson noise and speckle noise'). and also when the user uncheck the checkbox the noise is dissapear from the image. So here my current coding:
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile({'*.jpg';'*.tif';'*.png'},'Select File');
if isequal(filename,0)|| isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','please select an image') )
hold on;
else
uiwait(msgbox('User selected image sucessfully','sucess'));
hold off;
img = imread([pathname filename]);
img = img(:,:,1);
axes(handles.axes1);
imshow(img);
title('original image');
%update the handles structure with the image
handles.imgData = img;
%save the handles data
guidata(hObject,handles)
end
%check to make sure that the image data exists in handles
if isfield(handles,'imgData')
%grab the image data from handles





I = handles.imgData;
axes(handles.axes2);
imshow(img);
end
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB





% handles structure with handles and user data (see GUIDATA)





% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
isChecked = get(hObject,'Value');
if isChecked==1
if isfield(handles,'imgData')
%grab the image data from handles
I = handles.imgData;
J = imnoise(I,'salt & pepper',0.3);
axes(handles.axes2);
imshow(J);
title('Noise type: Salt & pepper');
end
guidata(hObject, handles)
end
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
isChecked = get(hObject,'Value');
if isChecked==1
if isfield(handles,'imgData')
%grab the image data from handles
I = handles.imgData;
M = imnoise(I,'gaussian',0.1,0.1);
axes(handles.axes2);
imshow(M);
title('Noise type: Gaussian noise');
end
guidata(hObject, handles)
end
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
isChecked = get(hObject,'Value');
if isChecked==1
if isfield(handles,'imgData')
%grab the image data from handles
I = handles.imgData;
N=imnoise(I,'localvar',0.05*rand(size(I)));
axes(handles.axes2);
imshow(N);
title('Noise type: Localvar noise');
end
guidata(hObject, handles)
end
% hObject handle to checkbox3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox3
% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
isChecked = get(hObject,'Value');
if isChecked==1
if isfield(handles,'imgData')
%grab the image data from handles
I = handles.imgData;
P=imnoise(I,'poisson');
axes(handles.axes2);
imshow(P);
title('Noise type: Poisson noise');
end
guidata(hObject, handles)
end
% hObject handle to checkbox4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox4
% --- Executes on button press in checkbox5.
function checkbox5_Callback(hObject, eventdata, handles)
isChecked = get(hObject,'Value');
if isChecked==1
if isfield(handles,'imgData')
%grab the image data from handles
I = handles.imgData;
Q = imnoise(I,'speckle', 0.3);
axes(handles.axes2);
imshow(Q);
title('Noise type: Speckle noise');
end
guidata(hObject, handles)
end
% hObject handle to checkbox5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox5

Best Answer

amira - I would create one function that all of your checkbox callbacks would call whenever the checkbox is checked or unchecked. This single function would then apply (or remove) the noise depending upon which checkbox is checked. For example, your first and second checkboxes would be replaced with just
function checkbox1_Callback(hObject, eventdata, handles)
applyNoise(handles);
function checkbox2_Callback(hObject, eventdata, handles)
applyNoise(handles);
The applyNoise function would then
function applyNoise(handles)
if isfield(handles,'imgData')
imgData = handles.imgData;
noiseStr = '';
if get(handles.checkbox1,'Value')
noiseStr = 'Salt & pepper';
imgData = imnoise(imgData,'salt & pepper',0.3);
end
if get(handles.checkbox2,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'Gaussian'];
imgData = imnoise(imgData,'gaussian',0.1,0.1);
end
axes(handles.axes2);
imshow(imgData);
title(['Noise type: ' noiseStr]);
end
The above function gets the initial image and first applies the Salt and Pepper noise followed by the Gaussian noise (if either has been selected) before being displayed in axes2. If neither has been selected, then there is no noise and so the original image is displayed in axes2.
You can do the same for the remaining three noise types.