# Finding the mean of a histogram

89 次查看（过去 30 天）
How can i find the mean of the histogram(R)?
clear;
figure(1); imshow(im); axis on;
[xP, yP] = ginput(4);
xP(5) = xP(1);
yP(5) = yP(1);
[x,y] = meshgrid(1:size(im,2),1:size(im,1));
xC = mean(xP); yC = mean(yP);
for i = 1:4
m = (yP(i+1) - yP(i)) / (xP(i+1) - xP(i));
b = yP(i) - xP(i) * m;
for j = 1:3
if(yC > xC * m + b)
else
end
end
end
figure(2);
imshow(im);
R = im(:,:,1)
R = R(R ~= 0); R = R.';
figure(3);
subplot(3,1,1)
histogram(R)
xlim([0 260])
set(get(gca,'children'),'facecolor',[1 0 0])
set(get(gca,'children'),'edgecolor',[1 0 0])

### 回答（2 个）

What do you mean by mean? The mean number of bins or the mean of the variables that the histogram represents? For the second you can just take the mean of R. For the first look at histogram properties and average the "values" property.
##### 0 个评论显示 -2更早的评论隐藏 -2更早的评论

Image Analyst 2020-7-12
The mean of the histogram will not be as accurate as the mean of the image since it's quantized into bins. Here's an illustration:
h = histogram(grayImage, 16) % 16 bins
grid on;
meanImageGL = mean2(grayImage)
grayLevels = h.BinEdges(1:end-1);
counts = h.Values;
meanBinnedGrayLevel = sum(grayLevels .* counts) / sum(counts)
xline(meanImageGL, 'Color', 'g', 'LineWidth', 2);
xline(meanBinnedGrayLevel, 'Color', 'r', 'LineWidth', 2);
% Or you could use the gray levels at the centers of the bins
centerBinGrayLevels = (h.BinEdges(1:end-1) + h.BinEdges(2:end)) / 2;
meanBinnedGrayLevel2 = sum(centerBinGrayLevels .* counts) / sum(counts)
You'll see
meanImageGL =
118.724487304688
meanBinnedGrayLevel =
110.93896484375
meanBinnedGrayLevel2 =
118.93896484375
So, which do you want?

### 类别

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

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by