📄 cascade.cpp
字号:
void CClassifierCascade::ParseFrom(const string& filename){ FILE* fp = fopen(filename.c_str(), "r"); if (!fp) { throw ITEFileNotFound(filename); } char* buf = new char[10000]; setvbuf(fp, buf, _IOFBF, sizeof(buf)); CClassifierCascade* pCascade = parse_cascade(fp); delete buf; if (pCascade==NULL) { throw ITException("parsing exception"); } *this = *pCascade; delete pCascade; return;}ostream& operator<<(ostream& os, const CClassifierCascade& casc){ return casc.output(os);}ostream& CClassifierCascade::output(ostream& os) const { os << "ClassifierCascade \"" << m_name << "\", "; if (m_structure_type==CClassifierCascade::CASCADE_TYPE_SEQUENTIAL) { os << "sequential, "; } else if (m_structure_type==CClassifierCascade::CASCADE_TYPE_FAN) { os << "fan, "; } else if (m_structure_type==CClassifierCascade::CASCADE_TYPE_TREE) { os << "tree, "; ASSERT(0); throw ITException("not implemented yet"); } else { ASSERT(0); } os << m_template_width << "x" << m_template_height << ", ratio " << m_image_area_ratio << " (fpr:" << m_total_false_positive_rate << ", dr:" << m_last_detection_rate << ", " << (m_trainset_exhausted?"exhausted":"successful") << ")" << endl; if (m_structure_type==CClassifierCascade::CASCADE_TYPE_FAN) { os << (int) m_branch_classifiers.size() << " branches." << endl; os << "COMMON BRANCH" << endl; } os << (int)m_classifiers.size() << " strong classifiers." << endl; for (int hcnt=0; hcnt<(int)m_classifiers.size(); hcnt++) { os << "STRONG classifier " << hcnt <<" (fpr:" << m_lyr_false_positive_rates[hcnt] << ", dr:" << m_lyr_detection_rates[hcnt] << "):\n" << m_classifiers[hcnt]; } for (int brc=0; brc<(int)m_branch_classifiers.size(); brc++) { os << "BRANCH " << brc << " \"" << m_branch_names[brc] << "\" (fpr:" << m_branch_false_positive_rates[brc] << ", dr:" << m_branch_detection_rates[brc] << ")" << endl; os << (int) m_branch_classifiers[brc].size() << " strong classifiers." << endl; for (int hcnt=0; hcnt<(int)m_branch_classifiers[brc].size(); hcnt++) { os << "STRONG classifier " << hcnt+(int)m_classifiers.size() <<" (fpr:" << m_branch_lyr_false_positive_rates[brc][hcnt] << ", dr:" << m_branch_lyr_detection_rates[brc][hcnt] << "):\n" << m_branch_classifiers[brc][hcnt]; } } return os;}bool CClassifierCascade::Evaluate(const CIntegralImage& image, CStringVector& matches) const{ for (CSClsfVector::const_iterator it=m_classifiers.begin(); it!=m_classifiers.end(); it++) { bool is_pos = it->Evaluate(image); if (!is_pos) return false; } // what structure? if (m_structure_type==CASCADE_TYPE_SEQUENTIAL) { matches.push_back(m_name); return true; } else if (m_structure_type==CASCADE_TYPE_FAN) { ASSERT(matches.size()==0); int num_branches = (int) m_branch_classifiers.size(); for (int brcnt=0; brcnt<num_branches; brcnt++) { bool is_pos = true; for (CSClsfVector::const_iterator it=m_branch_classifiers[brcnt].begin(); it!=m_branch_classifiers[brcnt].end(); it++) { is_pos = it->Evaluate(image); if (!is_pos) break; } if (is_pos) { matches.push_back(m_branch_names[brcnt]); } } return matches.size()>0; } else if (m_structure_type==CASCADE_TYPE_TREE) { throw ITException("not implemented yet"); } else { ASSERT(0); return 0; }}#ifdef DEBUGbool CClassifierCascade::EvaluateDebug(const CIntegralImage& image, CStringVector& matches, ostream& os) const{ os << "Cascade evaluating image, " << (int)m_classifiers.size() << " strong classifiers." << endl; int hcnt=0; for (CSClsfVector::const_iterator it=m_classifiers.begin(); it!=m_classifiers.end(); it++, hcnt++) { bool is_pos = it->Evaluate(image); os << "STRONG " << hcnt << ": " << is_pos << endl; if (!is_pos) return false; } // what structure? if (m_structure_type==CASCADE_TYPE_SEQUENTIAL) { matches.push_back(m_name); return true; } else if (m_structure_type==CASCADE_TYPE_FAN) { ASSERT(matches.size()==0); int num_branches = (int) m_branch_classifiers.size(); for (int brcnt=0; brcnt<num_branches; brcnt++) { bool is_pos = true; for (CSClsfVector::const_iterator it=m_branch_classifiers[brcnt].begin(); it!=m_branch_classifiers[brcnt].end(); it++) { is_pos = it->Evaluate(image); if (!is_pos) break; } if (is_pos) { matches.push_back(m_branch_names[brcnt]); } } return matches.size()>0; } else if (m_structure_type==CASCADE_TYPE_TREE) { throw ITException("not implemented yet"); } else { ASSERT(0); return 0; }}#endif // DEBUGbool CClassifierCascade::Evaluate( const CIntegralImage& image, double mean, double stddev, int left, int top, CStringVector& matches) const{ for (CSClsfVector::const_iterator it=m_classifiers.begin(); it!=m_classifiers.end(); it++) { bool is_pos = it->Evaluate(image, mean, stddev, left, top); if (!is_pos) return false; } // what structure? if (m_structure_type==CASCADE_TYPE_SEQUENTIAL) { matches.push_back(m_name); return true; } else if (m_structure_type==CASCADE_TYPE_FAN) { ASSERT(matches.size()==0); int num_branches = (int) m_branch_classifiers.size(); for (int brcnt=0; brcnt<num_branches; brcnt++) { bool is_pos = true; for (CSClsfVector::const_iterator it=m_branch_classifiers[brcnt].begin(); it!=m_branch_classifiers[brcnt].end(); it++) { is_pos = it->Evaluate(image, mean, stddev, left, top); if (!is_pos) break; } if (is_pos) { matches.push_back(m_branch_names[brcnt]); } } return matches.size()>0; } else if (m_structure_type==CASCADE_TYPE_TREE) { throw ITException("not implemented yet"); } else { ASSERT(0); return 0; }}void CClassifierCascade::ScaleFeaturesEvenly(double scale_x, double scale_y, int scaled_template_width, int scaled_template_height) const{ CSClsfVector& mutable_classifers = (CSClsfVector&) m_classifiers; for (CSClsfVector::iterator it=mutable_classifers.begin(); it!=mutable_classifers.end(); it++) { it->ScaleFeaturesEvenly(scale_x, scale_y, scaled_template_width, scaled_template_height); } if (m_structure_type==CASCADE_TYPE_FAN) { for (int brcnt=0; brcnt<(int)m_branch_classifiers.size(); brcnt++) { CSClsfVector& mutable_classifers = (CSClsfVector&) m_branch_classifiers[brcnt]; for (CSClsfVector::iterator it=mutable_classifers.begin(); it!=mutable_classifers.end(); it++) { it->ScaleFeaturesEvenly(scale_x, scale_y, scaled_template_width, scaled_template_height); } } }}// return a copy to name(s)CStringVector CClassifierCascade::GetNames() const{ if (m_structure_type==CASCADE_TYPE_SEQUENTIAL) { CStringVector sv; sv.push_back(m_name); return sv; } else if (m_structure_type==CASCADE_TYPE_FAN) { return m_branch_names; } else { throw ITException("not implemented yet"); }}int CClassifierCascade::GetNumStrongClassifiers(int branch/*=-1*/) const{ if (branch==-1) { return (int)m_classifiers.size(); } else { if (m_structure_type==CASCADE_TYPE_SEQUENTIAL) { throw ITException("no such branch for sequential type"); } else if (m_structure_type==CASCADE_TYPE_FAN) { if (branch<0 || branch>=(int)m_branch_classifiers.size()) { throw ITException("branch number out of range"); } return (int)m_branch_classifiers[branch].size(); } else { ASSERT(0); throw ITException("structure type not implemented"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -