How to use OpenCV to realize histogram calculation in C++
Today, I will talk to you about how to use OpenCV to achieve histogram calculation in C++, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Code demonstration
Create a new project opencv-0020, configure properties (VS2017 configure OpenCV common properties), and then write # include and main methods in the source file
Code for histogram calculation
Step-by-step instructions:
1. Split-channel display
This is to display the source image in different channels through the function of split. In imshow (img2,bgr_planes [0]), we change it to 0,1,2, respectively, and the display effect is as follows:
Cv::imshow (img2, bgr_planes [0])
Cv::imshow (img2, bgr_planes [1])
Cv::imshow (img2, bgr_planes [2])
two。 Set the number and value range of bin
3. Computational histogram
The parameters are described as follows:
& rgb_planes [0]: enter an array (or array set)
1: enter the number of arrays (here we use a single-channel image, we can also enter an array set)
0: the dim index is required. Here we only count the grayscale (and each array is a single channel), so just write 0.
Mat (): mask (0 means ignore the pixel), if not defined, do not use the mask
R_hist: the matrix that stores the histogram
1: histogram dimension
HistSize: number of bin per dimension
HistRange: the range of values for each dimension
Uniform and accumulate: bin of the same size, clear histogram traces
4. Create a histogram canvas
5. Normalization of histogram to range
Before drawing a histogram, use normalize to normalize the histogram so that the values in the histogram bin are scaled to the specified range.
This function accepts the following parameters:
R_hist: input array
R_hist: normalized output array (support in-place calculation)
0 and histImage.rows: here, they are the limits of values after normalized r_hist
NORM_MINMAX: normalization method (the method specified in the example scales the value to the specified range above)
-1: indicates that the normalized output array is of the same type as the input array
Mat (): optional mask
6. Draw a histogram on a histogram.
Here is an one-dimensional histogram, using the following expression:
R_hist.at (I)
: math: `i` indicates the dimension. If we want to access the 2D histogram, we need to use an expression like this:
R_hist.at (I, j)
7. Show histogram
After reading the above, do you have any further understanding of how to use OpenCV to achieve histogram calculation in C++? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.