Change image contrast and brightness - GUI and sliders

39 views (last 30 days)
I am building a simple GUI in Matlab that I aim to use in order to change image brightness and contrast using sliders (similar to Brightness / Contrast in paint.net).
I was calculating the the brightness via mean2 and set it as the slider value. Unfortunately, when I move the slider (set 0...255) the I = I + BrightSliderValue does not seems to be the right operation. Any idea how to solve it? Furthermore, how do I proceed with the contrast issue? How do I calculate it and how do I change it?
Code:
% --- Executes on button press in convertGSbutton.
function convertGSbutton_Callback(hObject, eventdata, handles)
% hObject handle to convertGSbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = getappdata(handles.imageAxes , 'yourVariable');
disp(size(I, 3));
if size(I, 3)>1
I = rgb2gray(I);
else
h = msgbox('A none color image');
end
axes(handles.imageAxes);
imshow(I);
brightness = mean2(I)
set(handles.BrightSlider, 'value', brightness);
%contrast = (I) % to be calculated
%set(handles.ContrastSlider, 'value', contrast);
setappdata(handles.imageAxes, 'yourVariable', I);
% --- Executes on slider movement.
function BrightSlider_Callback(hObject, eventdata, handles)
% hObject handle to BrightSlider (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
I = getappdata(handles.imageAxes , 'yourVariable');
BrightSliderValue = get(handles.BrightSlider, 'Value');
I = I + BrightSliderValue ; % to be change
axes(handles.imageAxes);
imshow(I);
setappdata(handles.imageAxes, 'yourVariable', I);
% --- Executes on slider movement.
function ContrastSlider_Callback(hObject, eventdata, handles)
% hObject handle to ContrastSlider (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
I = getappdata(handles.imageAxes , 'yourVariable');
ContrastSliderValue = get(handles.ContrastSlider, 'Value');
I = I + ContrastSliderValue; % to be change
I = getappdata(handles.imageAxes , 'yourVariable');
% I = %change contrast
axes(handles.imageAxes);
imshow(I);
setappdata(handles.imageAxes, 'yourVariable', I);

Answers (1)

Image Analyst
Image Analyst on 14 May 2017
I recommend you don't change the image and don't use getappdata and setappdata. Simply read the sliders in the callback functions, create a lookup table, and apply the lookup table to the axes.
  5 Comments
Ely Raz
Ely Raz on 26 May 2017
In the script above I already implemented the imshow() but I do not know how to add to the script the imadjust for contrast and brightness. I will appreciate a lot if you can provide a more explanations and it will be great if you can add some code so I can learn from it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!