Filter löschen
Filter löschen

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

47 Ansichten (letzte 30 Tage)
Avi Patani
Avi Patani am 27 Feb. 2020
Kommentiert: DGM am 25 Okt. 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);

Akzeptierte Antwort

Guillaume
Guillaume am 27 Feb. 2020
Bearbeitet: Guillaume am 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 Kommentare
Avi Patani
Avi Patani am 4 Mär. 2020
That works for me, Thank you so much for your help and time
DGM
DGM am 25 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by