MATLAB File Help: cv.calcHist Index
cv.calcHist

Calculates a histogram of a set of arrays

H = cv.calcHist(images, ranges)
H = cv.calcHist(..., 'OptionName',optionValue, ...)

Input

Output

Options

The function cv.calcHist calculates the histogram of one or more arrays. The elements of a tuple used to increment a histogram bin are taken from the corresponding input arrays at the same location.

Example

The sample below shows how to compute a 2D Hue-Saturation histogram for a color image:

hsv = cv.cvtColor(img, 'RGB2HSV');
edges = {linspace(0,180,30+1), linspace(0,256,32+1)};
H = cv.calcHist(hsv(:,:,1:2), edges);

Here is another example showing the different options:

% read some image, and convert to HSV colorspace
imgRGB = imread(fullfile(mexopencv.root(),'test','img001.jpg'));
imgHSV = cv.cvtColor(imgRGB, 'RGB2HSV');

% quantize the hue to 30 levels, and the saturation to 32 levels
histSize = [30, 32];
hranges = linspace(0, 180, histSize(1)+1);  % hue varies from 0 to 179
sranges = linspace(0, 256, histSize(2)+1);  % sat varies from 0 to 255
ranges = {hranges, sranges};

% one way
H = cv.calcHist(imgHSV(:,:,[1 2]), ranges);

% another way
H = cv.calcHist(imgHSV, ranges, 'Channels',[1 2]-1, 'HistSize',histSize);

% or similarly
H = cv.calcHist({imgHSV(:,:,1), imgHSV(:,:,2)}, {[0,180], [0,256]}, ...
    'HistSize',histSize, 'Uniform',true);

% show H-S histogram
imagesc(H, 'YData',[0 180], 'XData',[0 256])
axis image; colormap gray; colorbar
ylabel('Hue'); xlabel('Saturation'); title('Histogram')
See also