How to Use Sliders to Change RGB Values of an Image in GUI
9 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
More Answers (1)
Cris LaPierre
on 7 May 2020
Edited: Cris LaPierre
on 7 May 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 Comments
Rik
on 7 May 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
on 7 May 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.
See Also
Categories
Find more on Display Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!