00001 #include <face_contour_detector/filters/DeleteShortLines.h> 00002 00003 #include <face_contour_detector/ImageForest.h> 00004 #include <opencv/cv.h> 00005 #include <string> 00006 00007 namespace face_contour_detector { 00008 namespace filters { 00009 00010 DeleteShortLines::DeleteShortLines() { 00011 ResetParameters(); 00012 } 00013 00014 std::vector<Parameter> DeleteShortLines::GetParameters() { 00015 std::vector<Parameter> re; 00016 std::string name("minNumPixels"); 00017 re.push_back(Parameter(name, &m_minNumPixels, 1, 50)); 00018 return re; 00019 } 00020 00021 void DeleteShortLines::Apply(const cv::Mat& input, cv::Mat& result) { 00022 assert(input.channels() == 1); 00023 result = input; 00024 ImageForest forest(input, 200); 00025 std::map<int, ImageForest::Graph> graphs = forest.GetGraphs(); 00026 bool* toDelete = new bool[forest.NumGraphs()+1]; 00027 toDelete[0] = false; 00028 for (int i = 1; i <= forest.NumGraphs(); i++) { 00029 if (graphs[i].numPixels >= m_minNumPixels) { 00030 toDelete[i] = false; 00031 } else { 00032 toDelete[i] = true; 00033 } 00034 } 00035 for (int y = 0; y < input.rows; y++) { 00036 for (int x = 0; x < input.cols; x++) { 00037 if (forest.GetGraphId(x,y) != 0 && toDelete[forest.GetGraphId(x,y)]) { 00038 result.at<unsigned char>(y,x) = 0; 00039 } 00040 } 00041 } 00042 delete toDelete; 00043 } 00044 00045 void DeleteShortLines::ResetParameters() { 00046 m_minNumPixels = 3; 00047 } 00048 00049 const std::string& DeleteShortLines::GetFilterName() { 00050 return m_filterName; 00051 } 00052 00053 //static 00054 std::string DeleteShortLines::m_filterName = std::string("DeleteShortLines"); 00055 } //namepspace filters 00056 } //namespace face_contour_detector