⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classifiers.cpp

📁 tracciatore di mani con webcam
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    is.putback(c);    throw ITException(string("wrong character on input stream: '")+c+string("'"));  }  is >> c >> d;  if (c=='<') {    sign_lt=true;    is.putback(d);  } else if (c=='>' && d=='=') {    sign_lt=false;  } else {    char buf[256];    sprintf(buf, "wrong characters on input stream (%c|%c)\n", c, d);    throw ITException(buf);  }  double thresh;  is >> thresh;  SetThreshold(thresh);  is >> c >> train_error >> d;  if (c!='(' || d!=')') {    is.putback(d);    char s[32];    sprintf(s, "%f", train_error);    for (int i=0; i<(int)strlen(s); i++) is.putback(s[i]);    is.putback(c);    char buf[256];    sprintf(buf, "wrong character on input stream: '%c|%f|%c'\n",             c, (float)train_error, d);    throw ITException(buf);  }}void CWeakClassifier::ScaleFeatureEvenly(double scale_x, double scale_y,        int scaled_template_width, int scaled_template_height){  feature->ScaleEvenly(scale_x, scale_y, 		       scaled_template_width, scaled_template_height);}int CWeakClassifier::GetComputeCost() const{  ASSERT(feature);  return feature->GetComputeCost();}///////////////////////////////////////////////////////////////////////////////// 	CStrongClassifier implementation///////////////////////////////////////////////////////////////////////////////CStrongClassifier::CStrongClassifier(const CStrongClassifier& from):	m_num_hyps(from.m_num_hyps),	m_alphas_thresh(from.m_alphas_thresh),	m_sum_alphas(from.m_sum_alphas),  m_pClassifiers(NULL),  m_alphas(NULL){  if (m_num_hyps) {  	m_pClassifiers = new CWeakClassifier*[m_num_hyps];	  m_alphas = new double[m_num_hyps];  }	double sum_alphas=0.0;	for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {		m_pClassifiers[hcnt] = new CWeakClassifier(*from.m_pClassifiers[hcnt]);		m_alphas[hcnt] = from.m_alphas[hcnt];		sum_alphas += m_alphas[hcnt];	}	ASSERT(sum_alphas==m_sum_alphas);}CStrongClassifier::CStrongClassifier():	m_num_hyps(0),	m_alphas_thresh(0.5),  m_pClassifiers(NULL),  m_alphas(NULL),	m_sum_alphas(0.0){}CStrongClassifier::~CStrongClassifier(){	for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {		delete m_pClassifiers[hcnt];		m_pClassifiers[hcnt] = NULL;	} 	delete[] m_pClassifiers;	m_pClassifiers = NULL;  delete[] m_alphas;  m_alphas = NULL;}CStrongClassifier& CStrongClassifier::operator=(const CStrongClassifier& from){	this->~CStrongClassifier();	m_num_hyps = from.m_num_hyps;	m_alphas_thresh = from.m_alphas_thresh;	m_sum_alphas = from.m_sum_alphas;  if (m_num_hyps) {  	m_pClassifiers = new CWeakClassifier*[m_num_hyps];	  m_alphas = new double[m_num_hyps];  } else {    m_pClassifiers = NULL;    m_alphas = NULL;  }	double sum_alphas=0.0;	for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {		m_pClassifiers[hcnt] =                   new CWeakClassifier(*from.m_pClassifiers[hcnt]);		m_alphas[hcnt] = from.m_alphas[hcnt];		sum_alphas += m_alphas[hcnt];	}	ASSERT(sum_alphas==m_sum_alphas);	return *this;}void CStrongClassifier::AddWeakClassifier(CWeakClassifier* pClassifier, double alpha){	// save old array pointers	CWeakClassifier** tmp_pClassifiers=m_pClassifiers;	double* tmp_alphas=m_alphas;	int old_num_hyps=m_num_hyps;	m_num_hyps++;	// create new arrays	m_pClassifiers=new CWeakClassifier*[m_num_hyps];	m_alphas=new double[m_num_hyps];	// copy old to new	int hcnt=0;	for (; hcnt<old_num_hyps; hcnt++) {		m_pClassifiers[hcnt]=tmp_pClassifiers[hcnt];		m_alphas[hcnt]=tmp_alphas[hcnt];	}	// delete old	delete[] tmp_pClassifiers;	delete[] tmp_alphas;	// insert new	m_pClassifiers[hcnt]=new CWeakClassifier(*pClassifier);	m_alphas[hcnt]=alpha;	m_sum_alphas+=alpha;}intCStrongClassifier::RemoveLastWeakClassifier(){  if (m_num_hyps) {    m_num_hyps--;    delete m_pClassifiers[m_num_hyps];    m_pClassifiers[m_num_hyps] = NULL;    m_sum_alphas -= m_alphas[m_num_hyps];    return m_num_hyps;  } else {    return m_num_hyps;  }}const CWeakClassifier&CStrongClassifier::GetWeakClassifier(int num) const{	ASSERT(num<m_num_hyps);	return *m_pClassifiers[num];}void CStrongClassifier::ParseFrom(istream& is, int template_width, int template_height){  // delete old, make consistent state  this->~CStrongClassifier();  m_num_hyps = 0;  string str, str2, str3;  char c;  int num_hyps;  is >> num_hyps >> str >> str2 >> str3 >> m_alphas_thresh >> c;  if (str!="weak" || str2!="classifiers," || str3!="threshold" || c!=':') {    throw ITException("expected 'n weak classsfiers, threshold f:'");  }  // we have to post-parse assign m_num_hyps, and we have to   // do CWeakClassifier() allocation in a separate, early loop,  // because we might bail out with "return false" at any moment  // which would otherwise leave the StrongClassifier datastructure  // in an inconsistent state, which is hard to delete  m_num_hyps = num_hyps;  if (m_num_hyps) {    m_pClassifiers = new CWeakClassifier*[m_num_hyps];    m_alphas = new double[m_num_hyps];  } else {    m_pClassifiers = NULL;    m_alphas = NULL;  }  for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {    double alpha=-1;    is >> alpha >> str;    if (str!="*") {      // check if all weak classifiers have numbers as alpha values      throw ITException("expected '*'");    }    m_pClassifiers[hcnt] = new CWeakClassifier();    m_pClassifiers[hcnt]->ParseFrom(is, template_width, template_height);    /*    ofstream out("c:\\tmp\\dump.txt", ios::out | ios::app);    out << *m_pClassifiers[hcnt] << endl;    out.flush();    out.close();*/    m_alphas[hcnt]=alpha;    m_sum_alphas+=alpha;  }}ostream& operator<<(ostream& os, const CStrongClassifier& clsf){  os << clsf.m_num_hyps << " weak classifiers, threshold "      << clsf.m_alphas_thresh << ":" << endl;  for (int hcnt=0; hcnt<clsf.m_num_hyps; hcnt++) {    os << clsf.m_alphas[hcnt] << " * ";    os << *clsf.m_pClassifiers[hcnt];    os << "\n";  }  return os;}bool CStrongClassifier::Evaluate(const CIntegralImage& image) const{	double sum=0.0;	for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {		sum += m_alphas[hcnt] * m_pClassifiers[hcnt]->Evaluate(image);	}	ASSERT(m_alphas_thresh); // this should be greater than zero, otherwise	// the strong classifier doesn't do any classification.	return (sum >= m_alphas_thresh*m_sum_alphas);}bool CStrongClassifier::Evaluate(const CIntegralImage& image, double mean, double stddev, int left, int top) const{	double sum=0.0;	for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {	  sum += 	    m_alphas[hcnt] 	    * m_pClassifiers[hcnt]->Evaluate(image, mean, stddev, left, top);	}	ASSERT(m_alphas_thresh); // this should be greater than zero, otherwise	// the strong classifier doesn't do any classification.	return (sum >= m_alphas_thresh*m_sum_alphas);}void CStrongClassifier::EvaluateThreshs(const CIntegralImage& image,					double mean, double stddev,					int left, int top,					CIntVector& numMatches,					const CDoubleVector& threshs) const{  double sum=0.0;  for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {    sum +=       m_alphas[hcnt]       * m_pClassifiers[hcnt]->Evaluate(image, mean, stddev, left, top);  }  // do the classification for each threshold  int num_threshs = (int) threshs.size();  for (int tcnt=0; tcnt<num_threshs; tcnt++) {    double thresh = threshs[tcnt];    if (sum >= thresh*m_sum_alphas) {      numMatches[tcnt]++;    }  }}void CStrongClassifier::ScaleFeaturesEvenly(double scale_x, double scale_y,					    int scaled_template_width, 					    int scaled_template_height){  for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {    m_pClassifiers[hcnt]->ScaleFeatureEvenly(scale_x, scale_y,                                             scaled_template_width, 					     scaled_template_height);  }}int CStrongClassifier::GetComputeCost() const{  int sum = 0;  for (int hcnt=0; hcnt<m_num_hyps; hcnt++) {    sum += m_pClassifiers[hcnt]->GetComputeCost();  }  return sum;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -