[Computer Vision] histogram processing function in opencv

Source: Internet
Author: User
Tags ranges
Calculate the histogram calchist

A histogram is a collection of statistics, and the statistical results are distributed in a series of predefined bins. Here the data not only refers to the gray value, but statistics may be any feature that can effectively describe the image.
Assume that a matrix contains the information of an image (the gray scale value is 0-255 ):


Gray

Since the range of known numbers contains 256 values, we can divide this range into subareas (called bins), for example:


Bins

Then count the number of pixels dropped into each bin _ {I. Using this method to calculate the preceding numeric matrix, we can obtain (the X axis represents bin, and the Y axis represents the number of pixels in each bin ).


Hist1

A Histogram can calculate not only the gray color, but also any image features (such as gradients and directions ).

Histogram details

Dims: number of features to be counted. In the preceding example, dims = 1 because we only collect the gray value (grayscale image)
Bins: each feature spaceSub-sectionIn the preceding example, bins = 16
Range: The value range of each feature space. In the preceding example, range = [0,255].

Histogram Calculation of opencv

Opencv provides a simple histogram function calchist for calculating array sets (usually images or separated channels. Supports a histogram of up to 32 dimensions.

Void calchist (const mat * arrays, // image source array, same depth (cv_8u or cv_32f), same size int narrays, // number of images const int * channels, // channel inputarray mask, // mask image outputarray Hist, // returned histogram int dims, // the histogram dimension const int * histsize, // const float ** ranges for the number of histograms on each dimension, // bool uniform = true, bool accumulate = false );

Note:

Channels-array of channels used to calculate the Histogram
Mask-mask. If the mask is not empty, it must be an 8-bit (cv_8u) array, and its size is the same as that of arrays [I]. The point with a value of 1 will be used for calculation.
Dim-the dimension of the histogram. Must be positive and not greater than cv_max_dims (the current opencv version is 32, that is, the histogram with a maximum of 32 dimensions can be counted)
Histsize-Number of histograms on each dimension. Simply regard a histogram as a vertical bar, that is, the number of vertical bars in each dimension.
Ranges-range used for statistics

Inverse Projection Histogram

Reverse projection is a way to record how pixels in a given image adapt to the Pixel Distribution in the histogram model.
In short, the so-called reverse projection is to calculate the histogram model of a feature, and then use the model to find the feature that exists in the image.

void calcBackProject(const Mat* arrays, int narrays, const int* channels, InputArray hist, OutputArray backProject, const float** ranges, double scale=1, bool uniform=true );

Hist-input Histogram
Backproject-Inverse Projection vector, which is a single-channel vector with the same size and depth as arrays [0]

The skin color histogram is used as an example to explain the working principle of reverse projection:
Use a model histogram (representing the skin tone of the palm) to detect the skin area in the test image,

  • For each pixel (P (I, j) in the test image, obtain the color data and find the colors (H (I, j), S (I, j )) bin position in the Histogram
  • Query the corresponding Bin (H (I, j), S (I, j) in the model histogram and read the value of this bin.
  • Store this value in a new image (Backprojection ). You can also normalize the model histogram so that the output of the test image can be displayed on the screen.
  • The Backprojection result is displayed by taking the preceding steps for each pixel in the test image.
    Backprojection
  • In a statistical language, the value stored in the Backprojection indicates that the pixel in the test image belongs to the skin area.Probability. For example, a brighter area is more likely to be a skin area (this is true), while a darker area indicates a lower probability.
Threshold

The threshold is the simplest method for image segmentation.
Application Example: Use the threshold value in a pair of images to split the desired part of the object (of course, the object here can be a part or whole ). This image segmentation method is based on the gray difference between the object and the background in the image, and is a pixel-level segmentation.
To extract the required parts from an image, compare the gray value of each pixel in the image with the selected threshold and make corresponding judgments. (Note: The selection of thresholds depends on specific issues. That is, the object may have different gray-scale values in different images .)
Once the pixels of the objects to be split are found, we can set some specific values for these pixels. (For example, you can set the gray value of the object's pixel to '0' (black), and the gray value of other pixel points to '200' (white ); of course, the gray value of the pixel can be any, but it is best to set a strong contrast between the two colors to facilitate observation of the results ).


Threshold_example threshold type
Threshold type 1
Threshold type 2
Threshold Type 3
Threshold type 4
Threshold type 5 threshold API
double threshold(InputArray src, OutputArray dst, double thresh, double maxVal, int thresholdType);
Mean Shift algorithm Function

This function uses the iterative object search algorithm. It uses the Back Projection and initial position of an object as the input.
The center of gravity of the search window moves to the Mass Center of the reflected histogram. This process repeats until the number of iterations is reached (criteria. maxcount), or the window center is smaller than a threshold (criteria. epsilon ).

int meanShift(InputArray probImage,         // Back projection of the object histogramRect& window,                 // Initial search windowTermCriteria criteria        // Stop criteria for the iterative search algorithm.);
Camshift Algorithm functions

This function first uses the meanshift () function to find the center of the object, then adjusts the window size and finds the optimal rotation angle. This function returns a rotated rectangle data structure (including the position, size, and Rotation Angle of an object ). The position of the next search window can be obtained through rotatedrect: boundingrect.

RotatedRect CamShift(InputArray probImage,         // Back projection of the object histogramRect& window,                 // Initial search windowTermCriteria criteria        // Stop criteria for the underlying meanShift());
Termcriteria template class

This class is used as the termination condition of the iteration algorithm. Its constructor requires three parameters:One is type, the second parameter is the maximum number of iterations, And the last parameter is a specific threshold.

TermCriteria(int type, int maxCount, double epsilon);

The types include cv_termcrit_iter, cv_termcrit_eps, cv_termcrit_iter + cv_termcrit_eps, which indicate that the iteration termination condition is the maximum number of iterations, the iteration ends at the threshold, or both are used as iteration termination conditions.

References

Opencv histograms API documentation
Use of histogram calchist
Use of the histogram calchist (Supplement)
Opencv tutorial imgproc Module
Reverse projection
Threshold operation

Reprinted, please indicate the author Jason Ding and its source
GitHub home (http://jasonding1354.github.io /)
Csdn blog (http://blog.csdn.net/jasonding1354)
Home (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

[Computer Vision] histogram processing function in opencv

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.