OpenCV growth path: Image histogram

Source: Internet
Author: User
Tags ranges scalar

http://ronny.blog.51cto.com/8801997/1394115

2014-04-11 13:47:27Tags: OpenCV histogram table original works, allow reprint, when reproduced please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://ronny.blog.51cto.com/8801997/1394115, the concept of image histogram

Image histogram is a statistical table that reflects an image pixel distribution, in fact, the horizontal axis represents the type of image pixels, can be grayscale, can also be color. The ordinate represents the total number of pixels in the image for each color value or the percentage of the number of pixels.

Images are made of pixels, because histograms that reflect the distribution of pixels can often be used as an important feature of images. In practical engineering, image histogram has good application in feature extraction, image matching and so on.

Second, using OPENCV to calculate the histogram of the image

OpenCV in the calculation of the image histogram function is calchist, its parameters are more, the following analysis of its interface and usage.

1 voidcalcHist(constMat* images, int nimages, constint* channels, InputArray mask, OutputArray hist, intdims, constint*histSize, constfloat** ranges, booluniform=trueboolaccumulate=false);

Const mat* Images: A pointer to the input image.

int Nimages: The number of images to be computed for the histogram. This function can be used for multi-image histogram, we usually only for a single image, so usually nimages=1.

Const INT* Channels: The channel of the image, it is an array, if it is a grayscale image channels[1]={0}, if it is a color image channels[3]={0,1,2}, if it is only a color image of the 2nd channel histogram, Then channels[1]={1};

Iuputarray Mask: is a mask image used to determine which points to participate in the calculation, the actual application is a good parameter, by default we are set to an empty image, namely: Mat ().

Outarray hist: Calculated histogram

int dims: The dimension of the obtained histogram, the grayscale image is 1 dimensions, the color image is 3 dimensions.

Const int* Histsize: The interval number of the histogram horizontal axis. If it is 10, it divides the horizontal axis into 10 parts and then counts the total pixel points of each interval.

Const float** Ranges: This is a two-dimensional array that is used to indicate the range of each interval.

The following two parameters all have default values, the uniform parameter indicates whether the histogram is equidistant, and the last parameter is related to the display and storage of the histogram under multiple images.

Let's calculate a grayscale histogram of an image, a color histogram, and a custom grayscale distribution map.

Grayscale histogram:

123456789101112 intmain(){    Mat Image=imread("../cat.png");    cvtColor(Image,Image,CV_BGR2GRAY);    constintchannels[1]={0};    constinthistSize[1]={256};    floathranges[2]={0,255};    constfloat* ranges[1]={hranges};    MatND hist;    calcHist(&Image,1,channels,Mat(),hist,1,histSize,ranges);    return0;}

Color histogram:

1234567891011 intmain(){    Mat Image=imread("../cat.png");    const intchannels[3]={0,1,2};    constinthistSize[3]={256,256,256};    floathranges[2]={0,255};    constfloat* ranges[3]={hranges,hranges,hranges};    MatND hist;    calcHist(&Image,1,channels,Mat(),hist,3,histSize,ranges);    return0;}

In the non-uniform histogram, we separately statistic the gray distribution of 0-50,50-80,80-150,150-230,230-255 interval:

123456789101112 intmain(){    Mat Image=imread("../cat.png");    cvtColor(Image,Image,CV_BGR2GRAY);    constintchannels[1]={0};    inthistSize[1]={5};    floathranges[6]={0,50,80,150,230,255};    const float* ranges[1]={hranges};    MatND hist;    calcHist(&Image,1,channels,Mat(),hist,1,histSize,ranges,false);    return0;}
Three, the display of the histogram

As we can see from the above example, the histogram calculation is actually a multidimensional array, which is not intuitive enough, and we want to be able to represent the relevant data in Excel in the form of a table.

A grayscale histogram is shown by the underscore function below:

123456789101112131415161718 Mat getHistImg(constMatND& hist){    doublemaxVal=0;    doubleminVal=0;    //找到直方图中的最大值和最小值    minMaxLoc(hist,&minVal,&maxVal,0,0);    inthistSize=hist.rows;    Mat histImg(histSize,histSize,CV_8U,Scalar(255));    // 设置最大峰值为图像高度的90%    inthpt=static_cast<int>(0.9*histSize);    for(inth=0;h    {        floatbinVal=hist.at<float>(h);        intintensity=static_cast<int>(binVal*hpt/maxVal);        line(histImg,Point(h,histSize),Point(h,histSize-intensity),Scalar::all(0));    }    returnhistImg;}

Four, histogram transformation

Histogram transformation is an important concept in image processing, the image histogram can reflect the image contrast, light and dark degree and so on, so we can use the histogram transform to adjust the image picture.

Histogram transformation in the practical application of the project is very wide, some landscaping photos of the software many tools are on the histogram of the image as an article, if there are readers interested in this aspect of the recommendation: http://www.cnblogs.com/Imageshop/

Here are two simple histogram transformation functions: histogram stretching and histogram equalization.

If the grayscale of the image is displayed on a histogram in a certain interval, then the color of the image is single, and we can extend it to a wider gray scale to give the image a more layered feel.

Transform function: Transforms a grayscale value of an image into another grayscale.

The core of the histogram transformation is the transformation function, S=t (R), R is the gray value before the transformation, S is the transformed gray value, if we want to transform [a, b] interval of gray to the [0,255] range, then the transformation function is: T (r) =255* (r-a)/(B-A).

We create such a transformation function in OPENCV:

123456789101112 // 创建一个1*256的矢量Mat lut(1,256,CV_8U);for(inti=0;i<256;i++){    if(lut.at<uchar>(i)<imin)        lut.at<uchar>(i)=0;    elseif(lut.at<uchar>(i)>imax)        lut.at<uchar>(i)=255;    else        lut.at<uchar>(i)=static_cast<uchar>(        255.0*(i-imin)/(imax-imin)+0.5);}

Where Imax,imin is the minimum grayscale and maximum grayscale in the image. We can find out from a histogram:

1234567891011 intimax,imin;for(imin=0;imin<256;imin++){    if(hist.at<uchar>(imin)>minValue)        break;}for(imax=255;imax>-1;imax--){    if(hist.at<uchar>(imax)>minValue)        break;}

Finally, we apply the LUT function in OpenCV and apply the transform to the histogram.

1 LUT(image,lut,result);

The second parameter, like a lookup table, finds the grayscale in the original image as a table, and then replaces the grayscale value with the corresponding value in the table.

With the above gray-scale stretching example is not difficult to understand the image of the histogram equalization, histogram equalization can make the image gray distribution more evenly, so that the contrast of the image enhancement.

In OpenCV, histogram equalization does not have to construct a transform function like grayscale stretching, it has a direct corresponding function, of course, if you are interested can also try to write a transformation Function equalization transformation principle will be slightly more complex, in OpenCV this series, not too much introduction of digital Image algorithm, I'll have a chance to discuss it later.

123456789 int  < Code class= "CPP Plain" >main () {       mat image=imread ( &NBSP;&NBSP;&NBSP;&NBSP; cvtcolor (image,image,cv_bgr2gray); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   &NBSP;&NBSP;&NBSP;&NBSP; mat result; &NBSP;&NBSP;&NBSP;&NBSP; equalizehist (image,result); &NBSP;&NBSP;&NBSP;&NBSP; return  0;

OpenCV growth path: Image histogram

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.