www.pudn.com > several_kinds_Histogram.rar > Histogram.CPP


#include"stdafx.h" 
 
#include"Histogram.h" 
#include"COLSPACON.h" 
#include 
 
 
Histogram::~Histogram() 
{ 
	//release the image 
	if(pimagedata!=NULL) 
	{ 
		cvReleaseImage(&pimagedata); 
/*		pimagedata=NULL;*/ 
	} 
	//release the space for the histogram 
	if(pHist!=NULL) 
	{ 
		delete []pHist; 
		pHist=NULL; 
	} 
} 
 
 
bool Histogram::Init(BYTE c1,BYTE c2,BYTE c3,COLORSPACE colorspace1) 
{ 
	m_c1=c1;m_c2=c2;m_c3=c3; 
// 	m_bc1=0;m_bc2=0;m_bc3=0; 
	if(c1<=0 || c2<=0 ||c3<=0) 
		return false; 
	 
	for(;(c1&1)==0;c1>>= 1) 
		m_bc1++; 
	if(c1!=1) 
		return false; 
	for(;(c2&1)==0;c2>>= 1) 
		++m_bc2; 
	if(c2!=1) 
		return false; 
	for(;(c3&1)==0;c3>>= 1) 
		++m_bc3; 
	if(c3!=1) 
		return false; 
	 
	colorspace=colorspace1;	 
	//设置头文件头 
	 histfileheader.htMaker=HIST_HEADER_MARKER; 
	 histfileheader.htOffBits=sizeof(HISTFILEHEADER); 
	 histfileheader.htColorSpace=colorspace; 
	 histfileheader.htNSegCH1=m_c1; 
	 histfileheader.htNSegCH2=m_c2; 
	 histfileheader.htNSegCH3=m_c3; 
	// cout<height; 
histfileheader.imagewidth=pimagedata->width; 
//color space convertion 
	if(!COLORQUAN()) 
		return false;//color space convertion 
//extract the histogram 
//when you extract the other type histogram,you should overwrite the virtual function 
    if(!ExtractHistogram()) 
		return false; 
	 
	return true; 
} 
 
bool Histogram::COLORQUAN() 
{ 
    double *QuanRes=new double[pimagedata->height*pimagedata->width*3](); 
	if(QuanRes==NULL) 
		return false; 
    COLSPACON colorspacon(pimagedata,QuanRes); 
 
	switch(colorspace) 
	{ 
	case RGBSPACE: 
			     colorspacon.RGB2RGB();  
		         break;                     //[255,255,255] to [double,double,double] 
	case HSVSPACE: 
		colorspacon.RGB2HSV(); 
		break; 
	case YCbCrSPACE: 
		colorspacon.RGB2YCbCr(); 
		break; 
	case HSISPACE: 
		colorspacon.RGB2HSI(); 
		break; 
	default: 
		colorspacon.RGB2RGB(); 
 
	} 
	for(int i=0;i!=pimagedata->height*pimagedata->width;++i) 
	{ 
		   double a=QuanRes[i*3]*(m_c1); 
		   double b=QuanRes[i*3+1]*(m_c2); 
		   double c=QuanRes[i*3+2]*(m_c3); 
	       int channel1=static_cast(floor(a)); 
		   int  channel2=static_cast(floor(b)); 
		   int  channel3=static_cast(floor(c)); 
		   int temp=channel1<<(m_bc3+m_bc2)|channel2<::iterator iter=feature.begin(); 
	for(;iter!=feature.end();++iter) 
	{ 
		pHist[*iter]+=1.0/(pimagedata->height*pimagedata->width); 
	} 
	return true; 
} 
 
bool Histogram::GetHist(vector &query_hist) 
{ 
	if(HistSize==0) 
		return false; 
	for(int i=0;i