Finding the mean of a histogram

74 vues (au cours des 30 derniers jours)
Denny Mannakulathil
Denny Mannakulathil le 12 Juil 2020
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])

Réponses (2)

Thiago Henrique Gomes Lobato
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.

Image Analyst
Image Analyst le 12 Juil 2020
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)
You'll see
meanImageGL =
118.724487304688
meanBinnedGrayLevel =
110.93896484375
meanBinnedGrayLevel2 =
118.93896484375
So, which do you want?

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by