<< Histograms | Histograms | Image analysis >>

scicv >> Histograms > calcHist

# calcHist

Calculates a histogram of image(s)

### Syntax

    hist = calcHist(imgs, channels, mask, dims, bins_sizes, ranges)

### Parameters

img_in

  Input images (a single image or a list of images of Mat type).

channels

  List of dims channels of the image used to compute the histogram (1 x dims double)

mask

  Optional mask, must be same size as the image or empty (Mat type).

dims

  Dimension of the output histogram (1 double).

bins_sizes

  Sizes of the bins in each dimension (1 x dims double).

ranges

  Array of the dims arrays of the bin boundaries in each dimension (2 x dims double).

hist

  Output histogram (dims dimensions Mat).

### Description

`calcHist` computes one histogram of one or several channels, on one or several images.

The output histogram is multi dimensional with dims dimensions, and contains the cross distribution of the channels channels.

To have separate histograms of each channel independently, `calcHist` must be called several times, with the dims equal to 1 and the channels argument set to the corresponding channel.

### Examples

    scicv_Init();

    img_gray = imread(getSampleImage("lena.jpg"), CV_LOAD_IMAGE_GRAYSCALE);

    // Histogram of the gray level
    hist = calcHist(img_gray, 0, [], 1, 32, [0 256]);
    bar(hist(:), 'black');

    delete_Mat(img_gray);
    delete_Mat(hist);

    scicv_Init();

    img = imread(getSampleImage("lena.jpg"));

    // Histogram of the three RBG channels taken separately
    // Note: OpenCV color channel order is reversed (BGR)
    histB = calcHist(img, 0, [], 1, 32, [0 256]);
    scf();
    bar(histB(:), 'blue');

    histG = calcHist(img, 1, [], 1, 32, [0 256]);
    scf();
    bar(histG(:), 'green');

    histR = calcHist(img, 2, [], 1, 32, [0 256]);
    scf();
    bar(histR(:), 'red');

    delete_Mat(img);
    delete_Mat(histB);
    delete_Mat(histG);
    delete_Mat(histR);

Report an issue
<< Histograms | Histograms | Image analysis >>