MATLAB: Changing the brightness of an image using slider in Matlab GUI

brightnessbrightness levelbrightness sliderbwgrayscalegrayscale imageguiimageimage brightnessimage processingimportmatlab guiread imagergbslider

I need to create a programme where I can import image (RGB or Grayscale) and with the slider I could change the brightness level of it. It works all well, but I have a problem: grayscale images are detected also as RGB images. Any ideas how to solve it?
Thank you in advance!
My code is below:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
fullname=fullfile(pathname,filename);
ImageFile=imread(fullname);
set(handles.text2, 'string', fullname);
handles.im_ax1=imagesc(ImageFile,'Parent',handles.axes1);
handles.im_ax2=imagesc(ImageFile,'Parent',handles.axes2);
axis(handles.axes1,'off')
axis(handles.axes2,'off')
guidata(hObject,handles)
function slider_Callback(hObject, eventdata, handles)
valor=get(hObject, 'value');
new_image=get(handles.im_ax1,'CData')+valor*2.5;
set(handles.im_ax2,'CData',new_image)
set(handles.texto,'string',num2str(valor));
guidata(hObject,handles);
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)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
axes(handles.axes2);
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 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

Best Answer

It probably really is RGB even though it looks like RGB to you. You can cast all images to gray scale like this:
ImageFile=imread(fullname);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(ImageFile);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(ImageFile);
else
grayImage = ImageFile; % It's already gray scale.
end
Related Question