How to find the mean value of R G B component in an image without using the comment 'mean'
17 ビュー (過去 30 日間)
古いコメントを表示
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;
0 件のコメント
回答 (2 件)
Guillaume
2015 年 4 月 17 日
編集済み: Guillaume
2015 年 4 月 17 日
'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
0 件のコメント
Ataur Rahman
2020 年 8 月 8 日
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Denoising and Compression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!