How to Change the brightness of an image by using loops and no loops

5 views (last 30 days)
Hello,
I'm trying to create a function that will brighten an image by using loops and by not using loops. Can someone pleases help me and check if I'm on the right pathway please?
by not using loop:
function [ outimg ] = makebright_L(inimg, brightness)
outimg = brightness+inimg;
if inimg>255
inimg=255;
end
disp(outimg);
end
However, it appears to be wrong cause I get this error message
Using loops:
I'm kind of stuck. I don't know what to do
Can someone please help

Accepted Answer

Mohammad Haghighat
Mohammad Haghighat on 6 Oct 2016
You don't need the if statement for reducing the values to be in the range [1, 255]. The pixel type uint8 cannot go higher than 255. So, you are safe there.
As another advice, you can use imshow for displaying the images:
function outimg = makebright_L(inimg, brightness)
outimg = uint8(brightness + inimg);
subplot(1,2,1), imshow(inimg);
subplot(1,2,2), imshow(outimg);
end

More Answers (1)

Marc Jakobi
Marc Jakobi on 6 Oct 2016
What error message are you getting?
You should change your code to the following:
function [ outimg ] = makebright_L(inimg, brightness)
outimg = brightness+inimg;
outimg(outimg > 255) = 255;
disp(outimg);
end
To test it, use:
imshow(outimg)
Regarding the version with loops: Do you have any attempted code you can post? People here are less likely to answer a question if you don't have an attempt. P.S. please use the {}Code button to wrap code snippets so that it is more readable.
One way to do it using loops would be:
function [ outimg ] = makebright_L(inimg, brightness)
outimg = zeros(size(inimg));
for i = 1:size(inimg,1)
for j = 1:size(inimg,2)
outimg(i,j) = inimg(i,j) + max(outimg(i,j) + brightness, 255);
end
end
end
  2 Comments
Image Analyst
Image Analyst on 6 Oct 2016
Edited: Image Analyst on 6 Oct 2016
If inimg is of type uint8, then you'll need to cast it to double before you can do the sum. Then you might want to cast outimg back to uint8 from double.
If inimg is not uint8, then I'm not sure why you're clipping to 255 since floating point numbers can be in any range, not just 0-255.
A third way is to change the display by using imshow(), colormap(), or imadjust(). There are other ways also.

Sign in to comment.

Categories

Find more on Images 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!