MATLAB: How to add brightness slider in matlab gui that would change the second image’s brightness

adjust brightnessbrightnessimage processingimage readingMATLABmatlab guiposition;real timeslidebar stepslider

Hello,
the task I've got is to make a programme where the user can choose the image file from any directory and in one block it shows original image, while in the second it shows the same image with adjusted brightness, which is controlled with slidebar. As I'm totally new in working with this programme, it sounds and looks extremely hard for me.
I've been "googling" for weeks, but still there left some questions.
The questions are:
  1. how can i make the slidebar for regulating brightness for the second image?
  2. how to make to read both- colorful and bw- images correctly?
Here's my code that I wrote so far (without the beginning, which never changes as I understand) :
function pushbutton1_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
fullname=[pathname, filename];
ImageFile=imread(fullname);
axes(handles.axes1)
imagesc(ImageFile);
axis off;
set(handles.text2, 'string', fullname);
axes(handles.axes2)
imagesc(ImageFile);
axis off;
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
axes(handles.axes2);
[filename, foldername] = uiputfile('Kur norite issaugoti faila?');
complete_name = fullfile(foldername, filename);
imwrite(TheArray, complete_name);
% --- Executes on slider movement.
function slider_Callback(hObject, eventdata, handles)
valor=get(hObject, 'value');
set(handles.texto,'string',num2str(valor));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.

function slider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function texto_Callback(hObject, eventdata, handles)
edit=get(hObject,'string');
set(handles.slider,'value',str2num(edit));
guidata(hObject,handles);
axes(handles.axes2)
val=0.5*get(handles.axes2, 'value')-0.5;
imbright=(handles.axes2)+val;
imshow(imbright);
axes(handles.axes2);
% --- Executes during object creation, after setting all properties.
function texto_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Thank you in advance!
~you might save the day and the life for the human~

Best Answer

You should probably be using something like this:
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
fullname=fullfile(pathname,filename);
ImageFile=imread(fullname);
handles.im_ax1=imagesc(ImageFile,'Parent',handles.axes1);
handles.im_ax2=imagesc(ImageFile,'Parent',handles.axes2);
guidata(hObject,handles)
function slider_Callback(hObject, eventdata, handles)
valor=get(hObject, 'value');
new_image=get(handles.im_ax1,'CData')+valor;
set(handles.im_ax2,'CData',new_image)
function slider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
set(hObject,'Max',100,'Min',-100,'Value',0)
I would also suggest not using GUIDE, since it makes it more difficult to understand how a GUI works in Matlab and is harder to maintain and edit.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.