Getting started with opencv-Image Histogram

Source: Internet
Author: User
Tags scale image
I. Concepts of image histograms

The image histogram is a statistical table that reflects the Pixel Distribution of an image. In fact, the abscissa represents the type of the image pixel, which can be gray or color. The ordinate represents the total number of pixels in the image or the percentage of all pixels in each color value.

The image is composed of pixels, because the histogram that reflects the pixel distribution can often be a very important feature of the image. In practical engineering, image histograms are well applied in Feature Extraction and image matching.

Ii. Use opencv to calculate the Image Histogram

The calchist function used to calculate the image histogram in opencv has many parameters. The following describes its interface and usage.

void calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int*histSize, const float** ranges, bool uniform=true, bool accumulate=false );

Const mat * images: pointer to the input image.

Int nimages: number of images to calculate the histogram. This function can calculate histograms for multiple images. Generally, we only apply to a single image, so nimages = 1.

Const int * channels: the channel of the image. It is an array. for Grayscale Images, channels [1] = {0 }; for a color image, channels [3] = {2nd, 2}; for a color image, channels [1] = {1 };

Iuputarray mask: it is a mask image used to determine which points are involved in computing. It is a good parameter in actual application. By default, we set it to an empty image, that is, MAT ().

Outarray hist: calculated Histogram

Int dims: the dimension of the histogram. The gray-scale image is one-dimensional, and the color image is three-dimensional.

Const int * histsize: Number of intervals in the horizontal coordinate of the histogram. If the value is 10, the x-axis is divided into 10 parts, and the sum of pixels in each interval is calculated.

Const float ** ranges: a two-dimensional array that specifies the range of each interval.

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

Next we will calculate the gray histogram, color histogram, and custom gray distribution map of an image.

Grayscale histogram:

 1 int main() 2 { 3 Mat Image=imread("../cat.png"); 4 cvtColor(Image,Image,CV_BGR2GRAY); 5 const int channels[1]={0}; 6 const int histSize[1]={256}; 7 float hranges[2]={0,255}; 8 const float* ranges[1]={hranges}; 9 MatND hist;10 calcHist(&Image,1,channels,Mat(),hist,1,histSize,ranges);11 return 0;12 }

Color histogram:

 1 int main() 2 { 3 Mat Image=imread("../cat.png"); 4 const int channels[3]={0,1,2}; 5 const int histSize[3]={256,256,256}; 6 float hranges[2]={0,255}; 7 const float* ranges[3]={hranges,hranges,hranges}; 8 MatND hist; 9 calcHist(&Image,1,channels,Mat(),hist,3,histSize,ranges);10 return 0;11 }

Uneven histogram. We calculate the intervals of 0-50, 50-80, 80-150,150-230,230-255, respectively ??? Gray distribution:

 1 int main() 2 { 3 Mat Image=imread("../cat.png"); 4 cvtColor(Image,Image,CV_BGR2GRAY); 5 const int channels[1]={0}; 6 int histSize[1]={5}; 7 float hranges[6]={0,50,80,150,230,255}; 8 const float* ranges[1]={hranges}; 9 MatND hist;10 calcHist(&Image,1,channels,Mat(),hist,1,histSize,ranges,false);11 return 0;12 }
3. histogram display

From the above example, we can see that the histogram is actually a multi-dimensional array, which is not intuitive enough. We hope to express the relevant data in the form of a table in Excel.

The following uses the dash function to display a grayscale histogram:

1 mat gethistimg (const matnd & hist) 2 {3 double maxval = 0; 4 double minval = 0; 5 // locate the maximum and minimum values of 6 minmaxloc (Hist, & minval, & amp; maxval, 0, 0); 7 int histsize = Hist. rows; 8 mat histimg (histsize, histsize, cv_8u, scalar (255 )); 9 // set the maximum peak value to 90% 10 int hpt = static_cast (0.9 * histsize); 11 for (INT h = 0; H 

Iv. histogram Transformation

Histogram transformation is a very important concept in image processing. Image histograms can reflect image contrast, brightness, brightness, and other features. Therefore, we can adjust image images using histogram transformation.

Histogram transformation is widely used in practical engineering, and many tools for Beautifying photos are made on the histogram of images. If readers are interested in this aspect, we recommend: http://www.cnblogs.com/Imageshop/

The following describes two simple histogram transformation functions: histogram stretching and histogram equalization.

If the gray scale of the image is concentrated in a certain range on the histogram, it indicates that the image color is single. We can extend it to a wider gray scale to make the image more layered.

Transform function: convert one gray value of an image to another gray value.

The core of the histogram transformation is the transformation function, S = T (r), r is the gray value before the transformation, and s is the gray value after the transformation. If you want to convert [, b] When the gray scale of the interval is changed to the range of [0,255], the transformation function is T (r) = 255 * (R-A)/(B-).

We create such a transform function in opencv:

1 // create a 1*256 Vector 2 mat LUT (1,256, cv_8u); 3 for (INT I = 0; I <256; I ++) 4 {5 If (LUT. at <uchar> (I) <Imin) 6 LUT. at <uchar> (I) = 0; 7 else if (LUT. at <uchar> (I)> IMAX) 8 LUT. at <uchar> (I) = 255; 9 else10 LUT. at <uchar> (I) = static_cast <uchar> (11 255.0 * (I-Imin)/(IMAX-Imin) + 0.5); 12}

Here, IMAX and Imin are the minimum and maximum gray levels in the image. We can find from the histogram:

 1 int imax,imin; 2 for(imin=0;imin<256;imin++) 3 { 4 if(hist.at<uchar>(imin)>minValue) 5 break; 6 } 7 for(imax=255;imax>-1;imax--) 8 { 9 if(hist.at<uchar>(imax)>minValue)10 break;11 }

Finally, we can apply the LUT function in opencv to apply the transformation to the histogram.

LUT(image,lut,result);

The second parameter is like a search table. It searches for the gray scale in the original image according to the table, and then replaces the gray scale value with the corresponding value in the table.

With the above example of grayscale stretching, it is not difficult to understand the histogram balancing of the image. The histogram balancing can make the gray distribution of the image more even, so that the image contrast is enhanced.

In opencv, histogram balancing does not need to construct a transformation function like grayscale stretching. It has a direct function, of course, if you are interested, you can try to write the transformation principle of the transformation function equalization, which will be a little more complicated. In the opencv series, there will not be much introduction to the algorithms in digital images, I will have the opportunity to discuss it later.

1 int main()2 {3 Mat Image=imread("../cat.png");4 cvtColor(Image,Image,CV_BGR2GRAY);5  6 Mat result;7 equalizeHist(Image,result);8 return 0;9 }

Getting started with opencv-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.