Effacer les filtres
Effacer les filtres

How to Use Sliders to Change RGB Values of an Image in GUI

9 vues (au cours des 30 derniers jours)
Judy Yang
Judy Yang le 6 Mai 2020
Commenté : Rik le 7 Mai 2020
I'm creating a GUI where one can click a button to select an image to be loaded onto an axes and then using 4 sliders, I want to be able to adjust the red, green, and blue values individually and also have 1 slider adjust all of them at once. Also the sliders need to have a text displaying it's numerical value (-1 to 1). Finally, each individual slider should take into account tthe values for the other 3 sliders as well e.g., if I move the red slider up and then move the blue slider up, the final image shold have increased red and blue, not just blue.
Currently, I have the image selection button working and I am able to display the value of each slider as it moves, but I cannot figure out how to alter the image (and then save a copy of it). Here's what I have for the red slider and my button:
% --- Executes on slider movement.
function RedBar_Callback(hObject, eventdata, handles)
% hObject handle to RedBar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
rvalue = hObject.Value;
rvaluestr = num2str(rvalue);
set(handles.RedBarPosition,'String',rvaluestr);
Image(:,:,1) = Image(:,:,1) + 128*rvaluestr;
imshow(Image,'Parent',handles.Image);
handles.Image = imread(Image);
guidata(hObject,handles);
% --- Executes on button press in LoadButton.
function LoadButton_Callback(hObject, eventdata, handles)
% hObject handle to LoadButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, path]=uigetfile('*.jpg','Select an Image')
fullfilename=fullfile(path,filename)
Image= imread(fullfilename);
set(handles.Image);
imshow(Image,'Parent',handles.Image);
handles.Image = Image;
guidata(hObject,handles);
  3 commentaires
Judy Yang
Judy Yang le 7 Mai 2020
Sorry I am incredibly new to MATLAB (I'm doing this for an intro class) so I don't know how I would execute that. I keep hearing that I need to store the original and the modified version as a copy but what would the code for that be?
Judy Yang
Judy Yang le 7 Mai 2020
Modifié(e) : Judy Yang le 7 Mai 2020
OK so i managed to solve all my problems except one. I still cannot get the changes to stay (I'm assuming I'm supposed to store the modified image somewhere as said) but I cannot figure how to do that. As a result each of my sliders alters the image, but once another slider is used, the effects of the first one disappears. Here's the relevant code; it is identical for each slider except the tags change for each Image (:, :, 1) line changes for each color (i.e., green has GreenBar & (:, :, 2) and blue has BlueBar & (:, :, 3) and the brightness has all 3 of them changing simultaneously:
% --- Executes on slider movement.
function RedBar_Callback(hObject, eventdata, handles)
% hObject handle to RedBar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% Display Slider Value
value = hObject.Value;
valuestr = num2str(value);
set(handles.RedBarPosition,'String',valuestr);
% Adjust Red in Image from Slider
value = hObject.Value;
Image = handles.PicData;
Image (:,:,1) = get(handles.RedBar,'Value') * 128 + Image(:,:,1);
image(Image,'Parent',handles.Image)
set(handles.Image,'visible','off');
guidata(hObject, handles)

Connectez-vous pour commenter.

Réponse acceptée

Judy Yang
Judy Yang le 7 Mai 2020
Okay so I finally managed to fix my issues. Here's the working relevant code for posterity's sake:
% --- Executes on slider movement.
function RedBar_Callback(hObject, eventdata, handles)
% hObject handle to RedBar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% Display Slider Value
value = hObject.Value;
valuestr = num2str(value);
set(handles.RedBarPosition,'String',valuestr);
% Adjust Red in Picture from Slider
Picture = handles.PicData;
Picture (:,:,1) = (get(handles.RedBar,'Value') * 128 + Picture(:,:,1))+(get(handles.BrightnessBar,'Value') * 128 + Picture(:,:,1));
Picture (:,:,2) = (get(handles.GreenBar,'Value') * 128 + Picture(:,:,2))+(get(handles.BrightnessBar,'Value') * 128 + Picture(:,:,2));
Picture (:,:,3) = (get(handles.BlueBar,'Value') * 128 + Picture(:,:,3))+(get(handles.BrightnessBar,'Value') * 128 + Picture(:,:,3));
set(handles.Picture,'visible','off');
imshow(Picture,'Parent', handles.Picture);
  1 commentaire
Rik
Rik le 7 Mai 2020
imshow is a relatively high level function. I would suggest replacing the CData property instead.

Connectez-vous pour commenter.

Plus de réponses (1)

Cris LaPierre
Cris LaPierre le 7 Mai 2020
Modifié(e) : Cris LaPierre le 7 Mai 2020
Yes, it has to do with storing the image. You callbacks are functions. The variables do not exist once the function exits. You need to take advantage of handles structure to store variables you want to keep and use in other callback functions. See this doc page for more on how to use them.
  7 commentaires
Rik
Rik le 7 Mai 2020
The code Cris posted only adjusts the variable, not the visual image.
set(handles.Image,'CData',handles.PicData)%that should work, haven't tested it
Cris LaPierre
Cris LaPierre le 7 Mai 2020
From your description, I understood there was a separate function used to display the image after the changes were made. The code I shared updates the data. You now just need to update the display. Try Rik's code out. We don't know your component tag names, so you might have to adapt it to get it to work.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Display Image dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by