Finding the mean of a histogram
61 次查看(过去 30 天)
显示 更早的评论
How can i find the mean of the histogram(R)?
clear;
im = imread('folders01.jpg');
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
mask = im(:,:,j);
if(yC > xC * m + b)
mask(y<x*m+b) = 0;
else
mask(y>x*m+b) = 0;
end
im(:,:,j) = mask;
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])
0 个评论
回答(2 个)
Thiago Henrique Gomes Lobato
2020-7-12
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 个评论
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:
grayImage = imread('cameraman.tif');
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/330863/image.png)
You'll see
meanImageGL =
118.724487304688
meanBinnedGrayLevel =
110.93896484375
meanBinnedGrayLevel2 =
118.93896484375
So, which do you want?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!