MATLAB Answers

0

Matlab GUI changing image brightness using slider

Asked by Diana Stazinskaja on 23 May 2019
Latest activity Commented on by Diana Stazinskaja on 26 May 2019
i created programme than loads image, and i need to change its brightness and then save it. All buttons work, but im struggling with changing the brightness, sadly it doesnt work. Could someone help me state where is my mistake in the code bellow?
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, ~, handles)
% 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)
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
fullname=[pathname, filename];
ImageFile=imread(fullname);
axes(handles.axes1)
imagesc(ImageFile);
axis off;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, ~, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
toBeSaved=getframe(gca);
[fileName, filePath] = uiputfile('*.bmp', 'Save As');
fileName = fullfile(filePath,fileName);
imwrite(toBeSaved.cdata, fileName, 'bmp');
guidata(hObject,handles);
% --- Executes on slider movement.
function slider1_Callback(hObject, ~, handles)
% hObject handle to slider1 (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
axes(handles.axes1);
val=0.5*get(hObject, 'value')-0.5;
imbright=(handles.axes1)+val;
axes(handles.axes1);
imshow(imbright);
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, ~, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 24 May 2019
 Accepted Answer

Diana - the problem may be with this line of code
imbright=(handles.axes1)+val;
Note that you are adding a value val (calculated based on your slider position to the axes handle and not to the image. I think that what you want to do is save the image (that you have read from file) and then adjust it whenever the slider changes. Your pushbutton1 callback would become somethign like
function pushbutton1_Callback(hObject, ~, handles)
% 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)
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
fullname=[pathname, filename];
ImageFile=imread(fullname);
axes(handles.axes1)
imagesc(ImageFile);
axis off;
handles.image = ImageFile; % <--- new line to save the image to the handles object
guidata(hObject, handles); % <--- new line to save the updated handles objet
Your slider callback would then look something like
function slider1_Callback(hObject, ~, handles)
% hObject handle to slider1 (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
if isfield(handles, 'image')
val=0.5*get(hObject, 'value')-0.5;
imbright=double(handles.image)+val; % <--- new line, note the cast to double
imshow(uint8(imbright), 'Parent', handles.axes1); % <--- new line, note the cast to uint8
end
Note the casting of the image to double before we add the val, and then the cast back to uint8 before we show it on the axes. One or both may not be necessary depending upon the original data type of the image.

  1 Comment

works perfectly, thank you a lot!

Sign in to comment.