"OpenCV" Histogram application: Histogram equalization, histogram matching, contrast histogram

Source: Internet
Author: User
Tags comparison float range ranges

The gray histogram of digital image is described earlier, and the application of histogram is now tried. 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 of each point is to find the original distribution of the Y value in the position of the uniform distribution, the following diagram is the ideal simple Gaussian distribution map diagram:


(Image source: "Learnning OpenCV" p189) OpenCV cvequalizehist

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

void cvequalizehist (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

int main ()
{
	iplimage * image= cvloadimage ("baboon.jpg");
	Display the original and histogram
	myshowhist ("Source", image);
	
	iplimage* Eqlimage=cvcreateimage (cvgetsize (image), image->depth,3);
	Equalize each channel
	iplimage* redimage=cvcreateimage (cvgetsize (image), image->depth,1) respectively;
	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
	Cvmerge (blueimage,greenimage,redimage,null,eqlimage) after equalization;
	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.

The image is distributed with a specific function histv[] match void Myhistmatch (Iplimage *img,double histv[]) {int bins = 256;
	int sizes[] = {bins};
	Cvhistogram *hist = cvcreatehist (1,sizes,cv_hist_array);
	Cvcalchist (&img,hist);
	Cvnormalizehist (hist,1);
	Double val_1 = 0.0;
	Double val_2 = 0.0;
	Uchar t[256] = {0};
	Double s[256] = {0};
	Double g[256] = {0};
		for (int index = 0; index<256; ++index) {val_1 + = cvqueryhistvalue_1d (Hist,index);
		Val_2 + = Histv[index];
		G[index] = val_2;
	S[index] = Val_1;
	} double min_val = 0.0;
	int PG = 0;
		for (int i = 0; i<256; ++i) {min_val = 1.0; for (int j = 0;j<256; ++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 (int x = 0; xheight;++x) {p = (uchar*) (Img->imagedata + img->widthstep*x);
		for (int y = 0; ywidth;++y) {P[y] = t[p[y]]; }}}//Generate Gaussian distribution void Generategaussmodel (double mOdel[]) {double m1,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;
	Double C1 = a1* (1.0/(sqrt (2*CV_PI)) *sigma1);
	Double K1 = 2*sigma1*sigma1;
	Double C2 = a2* (1.0/(sqrt (2*CV_PI)) *sigma2);
	Double K2 = 2*sigma2*sigma2;
	Double p = 0.0,val= 0.0,z = 0.0;
		for (int zt = 0;zt < 256;++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/256;
	} for (int i = 0;i<256; ++i) {model[i] = model[i]/p;  }
}
Practice: Histogram matching

Match each channel of the sample image separately


Contrast histogram The Cvcomparehist function is provided in OPENCV to compare the similarity of two histograms:

Double cvcomparehist ( 
             const cvhistogram* HIST1,//histogram 1
             const cvhistogram* HIST2,//histogram 2
             int method//Contrast method
);
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: Two image histograms compared to different illumination conditionsThe histogram comparison is mainly used to determine the matching degree of two images, we test the following two image histogram comparison results:
int main ()
{
	iplimage * image= cvloadimage ("myhand1.jpg");
	Iplimage * image2= cvloadimage ("myhand2.jpg");
	int hist_size=256;
	Float range[] = {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-square: CV_COMP_CHISQR
	//Histogram intersection: Cv_comp_intersect
	//bhattacharyya Distance: cv_comp_ Bhattacharyya
	Double  com=cvcomparehist (Gray_hist,gray_hist2,cv_comp_bhattacharyya);

	cout<<com<<endl;
}

The output is: 0.396814 cvcomparehist The result 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 matching change:
Then use Cvcomparehist () to compare two images of the histogram, the output result is 0.267421
Indicates that the matching degree of the two graphs is higher. Note that different methods are used to compare the results.
ApplicationBy 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 specify Source: http://blog.csdn.net/xiaowei_cqu/article/details/7606607 Experiment code Download: http://download.csdn.net/detail/xiaowei_cqu/4332914

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.