opencv2+ Introduction Series (iv): Calculate the histogram of the image, average grayscale, gray variance

Source: Internet
Author: User
Tags ranges

This article lazy typesetting, directly in the HTML editor editor

In the image processing, we often need to find out the histogram of the image, gray average, gray variance, here is a opencv2+ to bring a program to achieve these functions.

Histogram

For histograms, use the Cv::calchist function to find out.

Prototype

void Calchist (const mat* arrays, int narrays, const int* channels, Inputarray mask, Outputarray
hist, int dims, const int* histsize, const float** ranges, BOOL uniform=true, BOOL accumulate=
false); void Calchist (const mat* arrays, int narrays, const int* channels, Inputarray mask, sparsemat&
hist, int dims, const int* histsize, const float** ranges, BOOL uniform=true, BOOL accumulate=
FALSE);
    • Arrays: Input image (or array, array set)
    • Narrays: Number of input arrays
    • Channels: The channel to be counted
    • Mask: Mask (0 means ignore this pixel), if undefined, no mask is used
    • hist: Matrix for storing histograms
    • Dims: Dimension of histogram
    • histsize: Number of bins per dimension
    • Ranges: the range of values for each dimension
    • uniform and accumulate: Bin size Same, clear histogram traces

Here's a way to calculate the histogram I've encapsulated.

1 voidGetgrayhist (cv::mat& src, cv::mat& grayhist,intHistsize=255)2 {3     floatRange[] = {0,255 };4     Const float* Histrange ={range};5     BOOLUniform =true;BOOLaccumulate =false;6(&SRC,1,0, Cv::mat (), Grayhist,1, &histsize, &histrange, uniform, accumulate);7}

Mean and variance

Calculation of mean and variance using Cv::meanstddev function

The function has four input parameters

1Mat Images

2Mat average, calculated to become Float64 type (double) 1*1.

3Mat Variance, the calculation will also become Float64 type 1*1.

4 Masking film

Here is a program that calculates the average grayscale and gray variance of an image:

voidGetgrayavgstddev (cv::mat& src,Double& AVG,Double&StdDev)    {Cv::mat img; if(Src.channels () = =3) cv::cvtcolor (SRC, img, cv_bgr2gray); Elseimg=src;    Cv::mean (SRC);    Cv::mat mean;    Cv::mat StdDev;    Cv::meanstddev (IMG, mean, StdDev); Avg= mean.ptr<Double> (0)[0]; StdDev= stddev.ptr<Double> (0)[0];}

opencv2+ Introduction Series (iv): Calculate the histogram of the image, average grayscale, gray variance

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.