"OpenCV: Histogram application: Histogram equalization, histogram matching, Comparison of straight square graph"

Source: Internet
Author: User
Tags ranges

Histogram equalization

Histogram equalization (histogram equalization) is the most typical application of histogram, which is a kind of image point operation . For an input image, an output image is generated by the operation, which means that the grayscale value of each pixel of the output image is determined by the input pixel point, i.e.:

Histogram equalization is the conversion of one image to another with a balanced histogram, that is, each gray level has the same pixel count process. The understanding from the distribution map is that you want the values of the y-axis in the original image to unfold as much as possible in the new distribution. The transformation process uses the cumulative distribution function to map the original distribution and generate a new uniform stretching distribution. Therefore, the operation for each point is to find the position of the Y value in the uniform distribution of the original distribution, as the ideal simple Gaussian distribution map:

(Image source: "Learnning OpenCV" p189)

The cvequalizehist in OpenCV

In OpenCV, there is a function cvequalizehist with grayscale histogram equalization, the interface is very clear:

void Const cvarr* src, cvarr* DST);

Note that this function can only handle single-channel gray images, and for color images, we can equalize each channel separately and merge it into a color image.

Practice: Histogram equalization of images
intMain () {Iplimage* Image= Cvloadimage ("baboon.jpg"); //display original and histogramMyshowhist ("Source", image); Iplimage* Eqlimage=cvcreateimage (cvgetsize (image), image->depth,3); //equalization of each channel separatelyiplimage* Redimage=cvcreateimage (cvgetsize (image), image->depth,1); Iplimage* Greenimage=cvcreateimage (cvgetsize (image), image->depth,1); Iplimage* Blueimage=cvcreateimage (cvgetsize (image), image->depth,1);        Cvsplit (Image,blueimage,greenimage,redimage,null);      Cvequalizehist (Redimage,redimage);       Cvequalizehist (Greenimage,greenimage);       Cvequalizehist (Blueimage,blueimage); //the image after equalizationCvmerge (blueimage,greenimage,redimage,null,eqlimage); Myshowhist ("equalized", Eqlimage); }  

The original image and grayscale histogram are as follows:

The histogram after equalization:

Histogram matching histogram matching or histogram regulation (histogram normalization/matching) refers to the transformation of a pair of images, so that its histogram and another image of the histogram or a specific function of the form of a histogram matching. Application scenarios such as two images under different lighting conditions, we can make a matching change before comparing two images.

Refer to the histogram matching code uploaded by shlkl99 to specify the image as a Gaussian distribution function.

//match an image to a specific function distribution histv[]voidMyhistmatch (Iplimage *img,Doublehistv[]) {      intBins = the; intSizes[] ={bins}; Cvhistogram*hist = Cvcreatehist (1, Sizes,cv_hist_array); Cvcalchist (&img,hist); Cvnormalizehist (hist,1); DoubleVal_1 =0.0; DoubleVal_2 =0.0; Uchar t[ the] = {0}; Doubles[ the] = {0}; Doubleg[ the] = {0};  for(intindex =0; index< the; ++index) {Val_1+=cvqueryhistvalue_1d (Hist,index); Val_2+=Histv[index]; G[index]=val_2; S[index]=Val_1; }        DoubleMin_val =0.0; intPG =0;  for(inti =0; i< the; ++i) {min_val=1.0;  for(intj =0;j< the; ++j) {if((G[j]-s[i]) < Min_val && (G[j]-s[i]) >=0) {Min_val= (G[j]-S[i]); PG=J; }} T[i]=(Uchar) PG; } Uchar*p =NULL;  for(intx =0; xheight;++x) {p= (uchar*) (Img->imagedata + img->widthstep*x);  for(inty =0; ywidth;++y) {p[y]=T[p[y]]; }      }  }    //Generate Gaussian distributionvoidGenerategaussmodel (Doublemodel[]) {      Doublem1,m2,sigma1,sigma2,a1,a2,k; M1=0.15; M2=0.75; SIGMA1=0.05; Sigma2=0.05; A1=1; A2=0.07; K=0.002; DoubleC1 = a1* (1.0/(sqrt (2*CV_PI)) *sigma1); DoubleK1 =2*sigma1*sigma1; DoubleC2 = a2* (1.0/(sqrt (2*CV_PI)) *sigma2); DoubleK2 =2*sigma2*Sigma2; Doublep =0.0, val=0.0, z =0.0;  for(intZT =0; ZT < the;++ZT) {Val= K + c1*exp (-(Z-M1) * (Z-M1)/k1) + c2*exp (-(Z-M2) * (Z-M2)/K2); MODEL[ZT]=Val; P= p +Val; Z= z +1.0/ the; }       for(inti =0;i< the; ++i) {Model[i]= model[i]/p; }  } 
Practice: Histogram Matching

Match each channel of the sample image separately

The Cvcomparehist function is provided in contrast histogram OpenCV to compare the similarity of two histograms:
Double cvcomparehist (                const// histogram 1               const// straight-side Figure 2               int Method// contrast methods   );  
Method has Cv_comp_correl, Cv_comp_chisqr,cv_comp_intersect,cv_comp_bhattacharyya four kinds of methods, The corresponding formula is as follows: Practice: Comparing the histogram histogram of two images with different illumination conditions is mainly used to judge the matching degree of two images, we test the following two image histogram comparison results:
intMain () {Iplimage* Image= Cvloadimage ("myhand1.jpg"); Iplimage* Image2= Cvloadimage ("myhand2.jpg"); intHist_size= the; floatRange[] = {0,255}; float* ranges[]={range}; Iplimage* Gray_plane = Cvcreateimage (cvgetsize (image),8,1);      Cvcvtcolor (Image,gray_plane,cv_bgr2gray); Cvhistogram* Gray_hist = Cvcreatehist (1, &hist_size,cv_hist_array,ranges,1); Cvcalchist (&gray_plane,gray_hist,0,0); Iplimage* Gray_plane2 = Cvcreateimage (Cvgetsize (Image2),8,1);      Cvcvtcolor (Image2,gray_plane2,cv_bgr2gray); Cvhistogram* Gray_hist2 = Cvcreatehist (1, &hist_size,cv_hist_array,ranges,1); Cvcalchist (&gray_plane2,gray_hist2,0,0); //Related: Cv_comp_correl//Chi Fang: Cv_comp_chisqr//histogram intersection: Cv_comp_intersect//Bhattacharyya Distance: Cv_comp_bhattacharyya    DoubleCom=cvcomparehist (Gray_hist,gray_hist2,cv_comp_bhattacharyya); cout<<com<<Endl; }  
The output is as follows: The result of 0.396814cvCompareHist is "0,1" floating-point number, the smaller the two picture matching degree is higher, 0.0 when the two images exactly match. (You can experiment with two identical graphs that are 0.0). For the above two images, we first make a histogram match change: Then use Cvcomparehist () to compare two images of the histogram, the output is 0.267421
Indicates that the matching degree of the two graphs is higher. Note that different methods are used to compare the results. By contrast we can set the EMD threshold to determine the ROI of the skin or hand. "Learnning OpenCV" after the corresponding exercises: the collection of hand skin color histogram, compared to indoor, outdoor hand histogram of the EMD distance, using these measurements to set a distance threshold value. A. Use this threshold to detect a third image (such as an outdoor shadow) to see if the color histogram can be detected well. B. Randomly select a histogram of background blocks that are not skin tones, observing the EMD changes, and whether the test is a good rejection of the background when compared to a real complexion. As above is also the histogram comparison can be applied to the scene. Reprint please indicate source: http://blog.csdn.net/xiaowei_cqu/article/details/7606607 experiment code Download: http://download.csdn.net/detail/xiaowei_cqu/4332914

"OpenCV: Histogram application: Histogram equalization, histogram matching, Comparison of straight square graph"

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.