How to increase the pixel intensity of all pixels of an image

9 views (last 30 days)
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
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

Sign in to comment.

Accepted Answer

Rik
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
Seth Rohs
Seth Rohs on 14 Jul 2017
Edited: Seth Rohs on 14 Jul 2017
I appreciate the quick response and I will definitely try what you provided me with! That being said, do you have any suggestions on the different approach to take that you talked about at the beginning?
Rik
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.

Sign in to comment.

More Answers (1)

Sardar Fahadullah
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.

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!