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

79 vues (au cours des 30 derniers jours)
Avi Patani
Avi Patani le 27 Fév 2020
Commenté : DGM le 25 Oct 2022
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);

Réponse acceptée

Guillaume
Guillaume le 27 Fév 2020
Modifié(e) : Guillaume le 27 Fév 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 commentaires
Avi Patani
Avi Patani le 4 Mar 2020
That works for me, Thank you so much for your help and time
DGM
DGM le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Modify Image Colors dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by