Histogram of a monochrome image with LL possible gray levels, l=0,1,...,L−1l = 0, 1, ..., L-1.

  • P(l)=nl/nP(l) = n_l / n,
  • nln_l is the number of pixels with gray level ll.
  • nn is the total number of pixels in the image.

Histogram Calculation (Sample Matlab Script)

function h = histogram(imgname)
img = imread(imgname);
figure; imshow(img);

% method 1
h = zeros(256,1);
for l = 0:255
	for i = 1:N,
		for j = 1:M,
			if img(i, j) == l,
				h(l + 1) = h(l + 1) + 1;
			end
		end
	end
end

figure; bar(h);

% method 2
img = double(img); h = zeros(256,1);
for i=1:M,
	for j=1:N,
		f = img(i,j);
		h(f+1) = h(f+1) + 1;
	end
end

% method 3
h = zeros(256,1);
for l = 0 : 255,
	h(l + 1)=sum(sum(img == l));
end
  • Photoshop has extensive histogram display tools
  • Matlab: imhist( ): can compute and display histograms
  • Python: matplotlib.pyplot.hist( ), numpy.histogram( )

See: Histogram Equalization