00001 00002 #include <face_contour_detector/filters/List.h> 00003 #include <face_contour_detector/filters/Parameter.h> 00004 #include <face_contour_detector/Properties.h> 00005 #include <boost/function.hpp> 00006 #include <cassert> 00007 #include <vector> 00008 00009 namespace face_contour_detector { 00010 namespace filters { 00011 List::List() { 00012 } 00013 List::~List() { 00014 for (std::vector<Filter*>::iterator it = m_filters.begin(); it != m_filters.end(); it++) { 00015 delete *it; 00016 } 00017 } 00018 void List::Add(Filter* filter) { 00019 m_filters.push_back(filter); 00020 m_filtersActive.push_back(new bool(true)); 00021 } 00022 00023 void List::SaveParameters(Properties& properties) { 00024 int filterId = 0; 00025 std::vector<Filter*>::iterator it; 00026 for (it = m_filters.begin(); it != m_filters.end(); it++) { 00027 std::vector<Parameter>::iterator paramsIt; 00028 std::vector<Parameter> params = (*it)->GetParameters(); 00029 for (paramsIt = params.begin(); paramsIt != params.end(); paramsIt++) { 00030 std::stringstream s; 00031 s<<"filters["<<filterId<<"]."<<paramsIt->GetName(); 00032 std::string key = s.str(); 00033 switch (paramsIt->GetType()) { 00034 case face_contour_detector::filters::TBOOL: 00035 properties.Set(key, *(paramsIt->GetBoolValuePtr())); 00036 break; 00037 case face_contour_detector::filters::TDOUBLE: 00038 properties.Set(key, *(paramsIt->GetDoubleValuePtr())); 00039 break; 00040 case face_contour_detector::filters::TINT: 00041 properties.Set(key, *(paramsIt->GetIntValuePtr())); 00042 break; 00043 case face_contour_detector::filters::TSTRING: 00044 properties.Set(key, *(paramsIt->GetStringValuePtr())); 00045 break; 00046 } 00047 } 00048 filterId++; 00049 } 00050 } 00051 void List::LoadParameters(const Properties& properties) { 00052 int filterId = 0; 00053 std::vector<Filter*>::iterator it; 00054 for (it = m_filters.begin(); it != m_filters.end(); it++) { 00055 (*it)->ResetParameters(); 00056 std::vector<Parameter>::iterator paramsIt; 00057 std::vector<Parameter> params = (*it)->GetParameters(); 00058 for (paramsIt = params.begin(); paramsIt != params.end(); paramsIt++) { 00059 std::stringstream s; 00060 s<<"filters["<<filterId<<"]."<<paramsIt->GetName(); 00061 std::string key = s.str(); 00062 if (properties.Exists(key)) { 00063 switch (paramsIt->GetType()) { 00064 case face_contour_detector::filters::TBOOL : 00065 { 00066 (*(paramsIt->GetBoolValuePtr())) = properties.ReadBool(key); 00067 } 00068 break; 00069 case face_contour_detector::filters::TDOUBLE : 00070 { 00071 (*(paramsIt->GetDoubleValuePtr())) = properties.ReadDouble(key); 00072 } 00073 break; 00074 case face_contour_detector::filters::TINT : 00075 { 00076 (*(paramsIt->GetIntValuePtr())) = properties.ReadInt(key); 00077 } 00078 break; 00079 case face_contour_detector::filters::TSTRING : 00080 { 00081 (*(paramsIt->GetStringValuePtr())) = properties.ReadString(key); 00082 } 00083 break; 00084 } 00085 } 00086 } 00087 filterId++; 00088 } 00089 } 00090 00091 void List::Apply(const cv::Mat& input, cv::Mat& result) { 00092 std::vector<Filter*>::iterator it; 00093 std::vector<bool*>::iterator activeIt = m_filtersActive.begin(); 00094 cv::Mat resultBuffer; 00095 cv::Mat* currResultTarget = 0; 00096 for (it = m_filters.begin(); it != m_filters.end(); it++) { 00097 ROS_ASSERT(activeIt != m_filtersActive.end()); 00098 if (*(*activeIt)) { 00099 if (currResultTarget == 0) { 00100 (*it)->Apply(input, result); 00101 currResultTarget = &resultBuffer; 00102 } else if (currResultTarget == &resultBuffer) { 00103 (*it)->Apply(result, resultBuffer); 00104 currResultTarget = &result; 00105 } else if (currResultTarget == &result) { 00106 (*it)->Apply(resultBuffer, result); 00107 currResultTarget = &resultBuffer; 00108 } else { 00109 ROS_ASSERT(false); 00110 } 00111 } 00112 activeIt++; 00113 } 00114 if (currResultTarget == 0) { 00115 result = cv::Mat(input); 00116 } else if (currResultTarget == &result) { 00117 result = cv::Mat(resultBuffer); 00118 } 00119 } 00120 00121 void List::DeactivateAll() { 00122 std::vector<bool*>::iterator activeIt; 00123 for (activeIt = m_filtersActive.begin(); activeIt != m_filtersActive.end(); activeIt++) { 00124 (*(*activeIt)) = false; 00125 } 00126 } 00127 00128 List::Iterator List::Begin() { 00129 return m_filters.begin(); 00130 } 00131 00132 List::Iterator List::End() { 00133 return m_filters.end(); 00134 } 00135 00136 } 00137 }