head_detector.cpp
Go to the documentation of this file.
00001 
00061 
00062 
00063 #ifdef __LINUX__
00064 #include "cob_people_detection/head_detector.h"
00065 #include "cob_vision_utils/GlobalDefines.h"
00066 #include "cob_vision_utils/VisionUtils.h"
00067 #else
00068 #endif
00069 
00070 #include <opencv/cv.h>
00071 #include <opencv/cvaux.h>
00072 
00073 using namespace ipa_PeopleDetector;
00074 
00075 HeadDetector::HeadDetector(void)
00076 {
00077         m_range_cascade = 0;
00078         m_initialized = false;
00079 }
00080 
00081 unsigned long HeadDetector::init(std::string model_directory, double depth_increase_search_scale, int depth_drop_groups, int depth_min_search_scale_x, int depth_min_search_scale_y)
00082 {
00083         // parameters
00084         m_depth_increase_search_scale = depth_increase_search_scale;
00085         m_depth_drop_groups = depth_drop_groups;
00086         m_depth_min_search_scale_x = depth_min_search_scale_x;
00087         m_depth_min_search_scale_y = depth_min_search_scale_y;
00088 
00089         // Load Haar-Classifier for head detection in depth images
00090         std::string rangeCascadePath = model_directory + "haarcascades/haarcascade_range_multiview_5p_bg.xml";
00091         //std::string rangeCascadePath = model_directory + "haarcascades/haarcascade_range_multiview_5p_bg+.xml";       // + "haarcascades/haarcascade_range.xml";
00092         m_range_cascade = (CvHaarClassifierCascade*)cvLoad(rangeCascadePath.c_str(), 0, 0, 0);
00093 
00094         // Create Memory
00095         m_storage = cvCreateMemStorage(0);
00096 
00097         m_initialized = true;
00098 
00099         return ipa_Utils::RET_OK;
00100 }
00101 
00102 HeadDetector::~HeadDetector(void)
00103 {
00104         // Release Classifiers and memory
00105         cvReleaseHaarClassifierCascade(&m_range_cascade);
00106         cvReleaseMemStorage(&m_storage);
00107 }
00108 
00109 unsigned long HeadDetector::interpolateUnassignedPixels(cv::Mat& img)
00110 {
00111         CV_Assert( img.type() == CV_8UC3 )
00112                 ;
00113 
00114         cv::Mat temp = img.clone();
00115 
00116         uchar* data = img.data;
00117         uchar* data2 = temp.data;
00118         int stride = img.step;
00119         for (int repetitions = 0; repetitions < 10; repetitions++)
00120         {
00121                 // each pixel with a value can propagate its value to black pixels in the 4 pixel neighborhood
00122                 for (int v = 1; v < img.rows - 1; v++)
00123                 {
00124                         for (int u = 1; u < img.cols - 1; u++)
00125                         {
00126                                 // only pixels with a value can propagate their value
00127                                 int index = v * stride + 3 * u;
00128                                 if (data[index] != 0)
00129                                 {
00130                                         uchar val = data[index];
00131                                         if (data2[index - 3] == 0)
00132                                                 for (int i = -3; i < 0; i++)
00133                                                         data2[index + i] = val; // left
00134                                         if (data2[index + 3] == 0)
00135                                                 for (int i = 3; i < 6; i++)
00136                                                         data2[index + i] = val; // right
00137                                         if (data2[index - stride] == 0)
00138                                                 for (int i = -stride; i < -stride + 3; i++)
00139                                                         data2[index + i] = val; // up
00140                                         if (data2[index + stride] == 0)
00141                                                 for (int i = stride; i < stride + 3; i++)
00142                                                         data2[index + i] = val; // down
00143                                 }
00144                         }
00145                 }
00146                 // copy back new data
00147                 for (int i = 0; i < img.rows * stride; i++)
00148                         data[i] = data2[i];
00149         }
00150         return ipa_Utils::RET_OK;
00151 }
00152 
00153 unsigned long HeadDetector::detectRangeFace(cv::Mat& depth_image, std::vector<cv::Rect>& rangeFaceCoordinates, bool fillUnassignedDepthValues)
00154 {
00155         if (m_initialized == false)
00156         {
00157                 std::cout << "Error: HeadDetector::detectRangeFace: init() must be called first." << std::endl;
00158                 return ipa_Utils::RET_FAILED;
00159         }
00160 
00161         rangeFaceCoordinates.clear();
00162 
00163         cv::Mat depth_image_8U3;
00164         ipa_Utils::ConvertToShowImage(depth_image, depth_image_8U3, 3);
00165         if (fillUnassignedDepthValues)
00166                 interpolateUnassignedPixels(depth_image_8U3);
00167         //cv::namedWindow("depth image");
00168         //cv::imshow("depth image", depth_image);
00169         //cv::waitKey(10);
00170         IplImage imgPtr = (IplImage)depth_image_8U3;
00171         CvSeq* rangeFaces = cvHaarDetectObjects(&imgPtr, m_range_cascade, m_storage, m_depth_increase_search_scale, m_depth_drop_groups, CV_HAAR_DO_CANNY_PRUNING,
00172                         cvSize(m_depth_min_search_scale_x, m_depth_min_search_scale_y));
00173 
00174         for (int i = 0; i < rangeFaces->total; i++)
00175         {
00176                 cv::Rect *rangeFace = (cv::Rect*)cvGetSeqElem(rangeFaces, i);
00177                 rangeFaceCoordinates.push_back(*rangeFace);
00178         }
00179 
00180         return ipa_Utils::RET_OK;
00181 }


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:13