How to Use Sliders to Change RGB Values of an Image in GUI
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Cris LaPierre
2020-5-7
编辑:Cris LaPierre
2020-5-7
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 个评论
Rik
2020-5-7
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
2020-5-7
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!