How to Change the brightness of an image by using loops and no loops
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Mohammad Haghighat
2016-10-6
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
更多回答(1 个)
Marc Jakobi
2016-10-6
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 个评论
Image Analyst
2016-10-6
编辑:Image Analyst
2016-10-6
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!