Changing the brightness of an image using slider in Matlab GUI

6 views (last 30 days)
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
  2 Comments
Rik
Rik on 12 May 2019
What is the exact problem? The way you describe the problem it is very similar to your previous question.
Emilija Valujaviciute
Emilija Valujaviciute on 12 May 2019
The problem is that I can upload and make changes just with RGB images; if I upload black and white picture, it is not grayscale, it is colorful.
To better understand, I add a screenshot of how the black and white image looks when I import it to the programme and what happens if I try to change its brightness level. (on the left side in the folder you can see how the image originally looks)
I'm sorry for not describing my question clearly- I really try to be clear, although it's hard when it's not my native language and when it is about programming haha.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 May 2019
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

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!