How to calculate the intensity of every pixel in a RGB image

78 views (last 30 days)
Hi,
% I am trying to calulate the intensity of every pixel in an image.
% The project I am working on, we are trying to see the blomming effect on every pixel and how it affects us.
% I am using the following code and not sure if its correct.
J = imread('Image7.png');
R = J(:,:,1);
G = J(:,:,2);
B = J(:,:,3);
% Intensity Calcultions:
I = (R(:,:)+G(:,:)+B(:,:))/3;
I3 = (R(:,:)+G(:,:)+B(:,:));
%I2= I/2;
histogram(I3,25)
xlabel('Intensity')
ylabel('# of pixels')
Me = mean(I(:));
histogram(Me,25)
csvwrite('Intensity7.csv',I3);

Accepted Answer

Guillaume
Guillaume on 27 Feb 2020
Edited: Guillaume on 27 Feb 2020
For a start, you'll have to define what the intensity of a colour pixel is. According to the code you wrote, it looks like you define intensity as the sum of red, green and blue which is not what most people would call intensity (no idea what you'd call that sum it's fairly meaningless). Most people would probably define intensity as the perceived luminance but there are other definition you could use.
Secondly, most likely your image is of class uint8. The maximum value of uint8 is 255. You can't add uint8 values and expect the result to make sense. If the sum is greater than 255, you'll get 255:
>> uint8(200) + uint8(150) %does not sum to 300
ans =
uint8
255
Thirdly note that R(:, :) is just a complicated way of writing just R. Same for the other channels. I'm not sure what you think R(:, :) would do differently.
Anyway, if you want to calculate the mean of the red, green and blue channel, which is also not something people would call intensity, it's simply:
%J a RGB image of any class
I = mean(J, 3); %mean across the colour planes. Will work properly unlike your (R+G+B)/3
  2 Comments
Avi Patani
Avi Patani on 4 Mar 2020
That works for me, Thank you so much for your help and time
DGM
DGM on 25 Oct 2022
Not that it really matters now, but the mean of the channels is "intensity" (I) within the context of HSI. For some reason it seems some courses focus on using HSI for image processing.

Sign in to comment.

More Answers (0)

Categories

Find more on Modify Image Colors 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!