Change image contrast and brightness - GUI and sliders
4 ビュー (過去 30 日間)
古いコメントを表示
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);
0 件のコメント
回答 (1 件)
Image Analyst
2017 年 5 月 14 日
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 件のコメント
Image Analyst
2017 年 5 月 26 日
Yes, use the slider values and pass them into imadjust. Unlike colormapping, it creates a brand new image that you'll have to display with imshow(). You also will not use a colormap if you use imadjust() since essentially the new image already was created by applying the colormap and so you don't want to do it twice.
Ely Raz
2017 年 5 月 26 日
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.
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!