Image processing opencv2-grayscale histogram and grayscale histogram equalization

Source: Internet
Author: User
Tags arrays int size ranges scalar

1 Grayscale Histogram

1.1 Concepts

Gray scale histogram is a function of gray level distribution, and it is the statistic of gray level distribution in image.

Grayscale histogram is the number of pixels in a digital image, according to the size of the gray value, the frequency of its occurrence.    Gray-scale histogram is a function of gray level, which indicates the number of pixels in the image that have some sort of gray level, which reflects the frequency of some grayscale in the image. If the total pixel brightness (grayscale level) is regarded as a random variable, the distribution of the image reflects the statistical characteristics of the image, which can be characterized and described by probability density function (PDF), which is represented as a grayscale histogram.
1.2 OpenCV says OpenCV provides a calchist function to calculate the histogram of the image.

The function prototypes of C + + are as follows:

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);

Parameter explanation:

Arrays: The input image pointer, can be multiple images, all images must have the same depth (cv_8u or cv_32f). At the same time, a picture can have multiple channes.

Narrays: The number of images entered.

Channels: An array of channes used to calculate the histogram. For example, the input is 2 pairs of images, the first image has 0,1,2 a total of three channel, the second image has only 1 channel, then the input is a total of 4 channes, if int channels[3] = {3, 2, 0}, Then the histogram is calculated using the first channel of the second pair of images and the 2nd and No. 0 channels of the first pair of images.

Mask: Mask. If mask is not empty, then it must be a 8-bit (cv_8u) array, and its size is the same size as arrays[i], and a point with a value of 1 will be used to calculate the histogram.

hist: Calculated Histogram

Dims: The dimension of the computed histogram.

Histsize: The number of histograms on each dimension. Simply think of the histogram as a vertical bar, that is, the number of vertical bars on each dimension.

Ranges: The range used for statistics. For example, float rang1[] = {0, 20};  Float rang2[] = {30, 40}; const FLOAT *rangs[] = {rang1, rang2}; Then the values for the 0,20 and 30,40 ranges are counted.

Uniform: Whether the width of each vertical bar is equal.

Accumulate: Whether or not to accumulate. If true, Hist will not be emptied first at the next calculation.

2 Histogram equalization

1.1 Concepts
If the pixels of a pair of images occupy a lot of gray scale and evenly distributed, then such images tend to have high contrast and changeable shades of gray.

Histogram equalization is a kind of transformation function that can achieve this effect automatically by the histogram information of input image.

Its basic idea is to broaden the number of pixels in the image, and to compress the gray scale of the number of pixels in the image, so as to expand the dynamic range of the original value, improve the contrast and gray tone change, make the image clearer.

The "central idea" of histogram equalization is to change the gray histogram of the original image from a certain gray area in the comparative concentration to the uniform distribution in the whole gray range. Histogram equalization is the non-linear stretching of the image, re-allocating the image pixel value, so that the number of pixels within a certain gray scale is roughly the same. Histogram equalization is to change the histogram distribution of a given image into a "uniform" distribution histogram distribution.

The basic idea of histogram equalization is to transform the histogram of the original graph into the form of uniform distribution, thus increasing the dynamic range of the gray value of the pixel so as to achieve the effect of enhancing the overall contrast of the image.

2. 2 OpenCV said

cv::equalizehist (img_gray,equalize_hist)


3. Routines

int img_hist (mat& image) {if (!image.data) {cout << "fail to load image" << Endl;
    return 0;
    } Mat Img_gray;
    GRAY if (image.channels () ==3) {Cvtcolor (image, Img_gray, Cv_bgr2gray);
    } else {Image.copyto (img_gray);

    } cv::imwrite ("Img_gray.jpg", Img_gray);       Matnd hist;
    In CV with cvhistogram *hist = cvcreatehist int dims = 1;
    Float hranges[] = {0, 255};   const FLOAT *ranges[] = {Hranges};
    Here is required for the const type int size = 256;
    int channels = 0;    Calculate the histogram of the image calchist (&img_gray, 1, &channels, Mat (), Hist, dims, &size, ranges);
    CV is cvcalchist int scale = 1;
    Mat imageshow (Size * scale, size, cv_8u, Scalar (0));
    Gets the maximum and minimum values of double minval = 0;
    Double maxval = 0;  Minmaxloc (Hist,&minval, &maxval, 0, 0);

    CV is the Cvgetminmaxhistvalue//display histogram of the image int HPT = saturate_cast<int> (0.9 * size); for (int i = 0; I < 256;           i++) {Float value = hist.at<float> (i);
        Note that hist is in float type CV with cvqueryhistvalue_1d int realvalue = saturate_cast<int> (value * hpt/maxval);
    Rectangle (Imageshow,point (I*scale, size-1), point ((i+1) *scale-1, Size-realvalue), Scalar (255));
    } namedwindow ("Hist");
    Imshow ("Hist", imageshow);
    Cv::imwrite ("Hist.jpg", imageshow);
    Mat equalize_hist;

    Cv::equalizehist (img_gray,equalize_hist);
    Namedwindow ("Equalize_hist");
    Imshow ("Equalize_hist", equalize_hist);
    Cv::imwrite ("Equalize_hist.jpg", equalize_hist);    Calculate the histogram of the image calchist (&equalize_hist, 1, &channels, Mat (), Hist, dims, &size, ranges);
       CV is cvcalchist Mat imageshow_equal (Size * scale, size, cv_8u, Scalar (0));  Gets the maximum and minimum values Minmaxloc (hist,&minval, &maxval, 0, 0);
       CV is the Cvgetminmaxhistvalue//display histogram of the image HPT = saturate_cast<int> (0.9 * size); for (int i = 0; I < 256;           i++) {Float value = hist.at<float> (i);
           Note that hist is in float type CV with cvqueryhistvalue_1d int realvalue = saturate_cast<int> (value * hpt/maxval);
       Rectangle (Imageshow_equal,point (I*scale, size-1), point ((i+1) *scale-1, Size-realvalue), Scalar (255));
       } namedwindow ("Hist_equalize");
       Imshow ("Hist_equalize", imageshow_equal);
    Cv::imwrite ("Hist_equalize.jpg", imageshow_equal);
    Waitkey (0);
return 0;    } int main (int args, char** argv) {Mat image = Imread ("/home/odroid/test/111.jpg", 1);
 	This can also be BGR but think of the extract contour effect is the same as the Imshow ("original", image);
	Img_hist (image);
    Waitkey ();
return 0; }




             
Original diagram Grayscale ImageGrayscale histogram Histogram of gray scale after equalization
Grayscale image after histogram equalization

          

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.