How to find the mean value of R G B component in an image without using the comment 'mean'

19 views (last 30 days)
I'm looping through an input image 1 pixel at a time and determining its RGB value. Afterwards i'm trying to find the average RGB value for the image overall. For some reason the averaging portion of my code isnt working.
I = imread(filename);
[len,wid,d] = size(I)
count = 0;
meanR = 0;
meanG = 0;
meanB = 0;
for j=1:len
for k=1:wid
meanR = meanR + I(j,k,1);
meanG = meanG + I(j,k,2);
meanB = meanB + I(j,k,3);
count= count+1;
end
end
meanR1 = meanR/count;
meanG1 = meanG/count;
meanB1 = meanB/count;

Answers (2)

Guillaume
Guillaume on 17 Apr 2015
Edited: Guillaume on 17 Apr 2015
'isnt working though' does not tell us much. At a guess, your image is of type int8 or int16. To know for sure:
whos I
If you add two uint8 whose combined values is greater than 255 then you'll get to 255 since that's the maximum value that can be encoded in 8 bits. Same with uint16 at 65535.
To fix this:
I = double(imread(filename));
If that's not the issue then you need to give more details about isnt working

Ataur Rahman
Ataur Rahman on 8 Aug 2020
function avgrgb=avgpict(img)
sz=imread(img); %read the image
pict=double(sz);% convert it to double for calculations
dim=size(pict); %determine the dimension of the pict
count=0;meanR=0;meanG=0;meanB=0;%preallocations
for i=1:dim(1)
for j=1:dim(2)
meanR=meanR+pict(i,j,1);
meanG=meanG+pict(i,j,2);
meanB=meanB+pict(i,j,3);
count=count+1;
end
meanR1=meanR/count;
meanG1=meanG/count;
meanB1=meanB/count;
end
avgrgb=[meanR1,meanG1,meanB1] %prints the rgb average value
end
check my code. It is working

Categories

Find more on Denoising and Compression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!