How to increase the pixel intensity of all pixels of an image
9 views (last 30 days)
Show older comments
I have an image that I need to increase the intensity of all of the pixels and then output the new image with the increased pixel intensity and save it to a folder. Is there any easy way to accomplish this? If you would like more information I have provided it below.
I have tried adding a constant to it using two for loops to add to each pixel RGB is the imread of the original image.
for x = 1:480
for y = 1:540
C(x,y) = RGB(x,y) + constant
end
end
However, when I attempt to output the new image using imwrite, the image shows up completely black instead of being similar to the original image.
Thanks in advance.
1 Comment
Walter Roberson
on 16 Jul 2017
RGB images are 3 dimensional; your output from your code here will reflect only the red color plane. Also you probably accidentally switched to double
Accepted Answer
Rik
on 14 Jul 2017
I advise you to rethink your approach. But if you really want to do this, you have to make sure you don't make your constant so large that you will cause an overflow.
Two things may be the cause of your black image: a changed datatype or something going wrong with indexing. The first can be easily fixed by explicitly casting your output to the correct datatype, the second by not using loops, but a direct addition. (usually an RGB image will be a (m,n,3) uint8)
As a last remark: you don't need the nested loops, because you can simply do the addition in one go.
C=uint8(RGB+constant);
2 Comments
Rik
on 16 Jul 2017
At the very least you can set that constant to a value that won't make you lose information (i.e. make sure that no pixel is increased to a value above what uint8 can hold).
constant=min([preferred_constant uint8(inf)-max(RGB(:))]);
The inf trick doesn't work with single or double.
More Answers (1)
Sardar Fahadullah
on 6 Oct 2018
I need your help my for loops are not working. Its an restriction that I should have to increase the pixel intensity by using two nested for loops but my loop are not working any suggestion about this.
0 Comments
See Also
Categories
Find more on Convert Image Type 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!