increasing brightness/contrast of image in different color channel using slider in GUI
4 views (last 30 days)
Show older comments
I am trying to make sliders where I can change the brightness and contrast of an image using slider. However, I now want to make sliders that can change the brightness and contrast of the red channel only and then display the new merged image. How would I do that? This is my code for the whole image change:
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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
global im2
val=0.1*get(hObject,'Value')-0.1;
imbright=im2+val;
axes(handles.axes1);
imshow(imbright);
impixelinfo
and for contrast my code is:
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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
global im2
val=0.1*get(hObject,'Value')-0.1;
imcontrast=im2+val;
axes(handles.axes1);
imshow(imcontrast);
impixelinfo
Is my code to change the contrast even right, I just changed imbrightness to imcontrast?
But for how would I change the brightness and contrast for red channel using slider?
Do I need to?
redChannel = im2(:, :, 1);
im2(:, :, 2) = 0
im2(:, :, 3); = 0
Also how do I make sure every slider is changing the current image and not the original one? Is that solved by calling global im2 at the beginning of every slider to make sure it is changing the current image and not the original one?
thanks
2 Comments
Geoff Hayes
on 28 Jun 2017
Aa - don't use global variables. If you want all callbacks to have access to a particular image (the current one), then save it to the handles structure. For example, suppose you have loaded an image (however you have chosen to do this, in a callback, etc.) via something like
img = imread(...);
You then decide to display this image (maybe using imshow or image) on one of your axes. Since img is the image being shown, you can now save it (in the same callback that read and displayed the image) to the handles structure as
handles.img = img;
guidata(hObject, handles);
Now, in any other callback where you want to update the currently displayed image you would do
if isfield(handles, 'img')
img = handles.img;
% manipulate the image
updatedImg = someFunction(img);
% display the updated image
imshow(updatedImg);
% save it to handles
handles.img = updatedImg;
guidata(hObject, handles);
end
This should allow you to avoid the use of globals.
Accepted Answer
Image Analyst
on 30 Jun 2017
You need to split the color image up into the separate channels, then multiply the red channel by some factor - an equation of a line - then recombine the 3 color channels (the new red one plus the other two original ones) with cat(3, r, g, b) and call imshow(). Yous sliders could control the slope of the line (for contrast control) and offset of the line (for brightness control).
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Change red
slope = handles.sldSlope.Value;
intercept = handles.sldIntercept.Value;
redChannel = uint8(double(redChannel) * slope + intercept);
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
5 Comments
Trinh Nguyen
on 11 Jul 2018
If offset and slope are set to 0 at the beginning then according to this formula:
redChannel = uint8(double(redChannel) * slope + intercept);
the redChannel will be 0, i.e. there is no redChannel at start?
Image Analyst
on 11 Jul 2018
If it's an RGB image to begin with, there will be a red channel at the start, unless you cropped it off.
Correct, if offset(intercept) and slope are zero then that formula would set the redChannel variable to zero. If you then stuck that back into rgbImage, you'd have no red in the color image.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!