How to uncheck checkbox
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
i have checkbox and axes to display image. when i click checkbox1 it will apply noise to the image.how can i uncheck the checkbox1 to remove only the noise and click another checkbox to apply other noise.here my code for every checkbox.
fucntion checkbox1_callback(hObject,eventdata,handles)
if isChecked==1
if isfield(handles,'imgData)
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
end
guidata(hObject,handles)
end
1 commentaire
Adam
le 19 Sep 2016
I'm not sure I understand the question, but a simple if else statement in the checkbox callback should do the job - keep hold of your original image and just return to that when you untick the checkbox.
What is
isChecked
though? This doesn't seem to be valid (or rather useful) code for this check.
Réponse acceptée
test run
le 19 Sep 2016
Modifié(e) : test run
le 19 Sep 2016
Hello amira, you could add another condition to your callback, like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 % This is like your isChecked==1
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
elseif hObject.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
Note that I have changed isChecked to hObject.Value because isChecked is a variable that does not exist when you call the callback, and hObject.Value or handles.checkbox1.Value (which are equivalent if you are inside the callback function of checkbox1) they give you value 1 if it's checked and 0 if it's not checked.
Now if you want to apply different types of noise then you need to create for example a second checkbox, and let's suppose it's tag is checkbox2, then you could do like this:
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise',0.3);
imshow(J);
title('Noise type: mynoise');
elseif hObject.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
Now the problem is that if you want to apply only one noise at a time and not allow 2 noises to be together you could do like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox2.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
elseif hObject.Value==0 && handles.checkbox2==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise',0.3);
imshow(J);
title('Noise type: mynoise');
elseif hObject.Value==0 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
Like this you can only apply one noise at a time and also the checkboxes won't apply noise to an image that has allready a noise applied to it. If you want to add warning dialogs to warn the user that he is applying 2 noises then you could simply write them like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox2.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
elseif hObject.Value==0 && handles.checkbox2==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
elseif hObject.Value==1 && handles.checkbox2==1
warndlg('Only one noise may be applied', 'Invalid choice warning');
end
guidata(hObject,handles)
end
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise',0.3);
imshow(J);
title('Noise type: mynoise');
elseif hObject.Value==0 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
elseif hObject.Value==1 && handles.checkbox1==1
warndlg('Only one noise may be applied', 'Invalid choice warning');
end
guidata(hObject,handles)
end
Hope this answer helped you get what you were looking for.
1 commentaire
test run
le 19 Sep 2016
Hello again amira, if you want to apply more than just one noise then you should add another condition like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox2.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise1',0.3);
imshow(J);
title('Noise type: my_noise1');
end
elseif hObject.Value==0 && handles.checkbox2==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
elseif hObject.Value==1 && handles.checkbox2==1
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise1',0.3);
J=imnoise(I,'my_noise2',0.3);
imshow(J);
title('Noise type: my_noise1 and my_noise2');
end
end
guidata(hObject,handles)
end
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise2',0.3);
imshow(J);
title('Noise type: my_noise2');
end
elseif hObject.Value==0 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
elseif hObject.Value==1 && handles.checkbox1==1
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise1',0.3);
J=imnoise(I,'my_noise2',0.3);
imshow(J);
title('Noise type: my_noise1 and my_noise2');
end
end
guidata(hObject,handles)
end
About the error you got I need to see the entire code to look for the error, I can't tell like this.
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur DCAM Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!