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

47 次查看(过去 30 天)
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);

采纳的回答

Guillaume
Guillaume 2020-2-27
编辑:Guillaume 2020-2-27
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 个评论
DGM
DGM 2022-10-25
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Octave 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by