36 input_img = cv::imread(file_name);
48 std::vector<cv::Rect> pedestrian, upperbody, final_humans;
49 cv::Mat grayscale_img;
50 if( input_img.empty() )
54 cv::cvtColor(input_img, grayscale_img, CV_BGR2GRAY);
55 cv::equalizeHist(grayscale_img, grayscale_img);
59 cv::HOGDescriptor hog;
60 hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
61 hog.detectMultiScale(grayscale_img, pedestrian, 0, cv::Size(8, 8), cv::Size(32, 32), 1.05, 2);
64 std::string haar_file_path =
"/usr/share/opencv/haarcascades/haarcascade_upperbody.xml";
81 const std::string& haar_path)
83 std::vector<cv::Rect> found, final_humans;
86 cv::CascadeClassifier human_cascade;
88 human_cascade.load(haar_path);
91 int groundThreshold = 2;
92 double scaleStep = 1.1;
93 cv::Size minimalObjectSize = cv::Size(100, 100);
94 cv::Size maximalObjectSize = cv::Size(800, 800);
96 human_cascade.detectMultiScale(input_img, found, scaleStep, groundThreshold, 0 | cv::CASCADE_SCALE_IMAGE, minimalObjectSize);
107 for(
unsigned int i = 0 ; i < found.size() ; i++)
109 cv::Rect tmp_rect = found[i];
112 tmp_rect.width += 20;
113 tmp_rect.height += 20;
114 if(tmp_rect.x < 0 || tmp_rect.y < 0 ||
115 (tmp_rect.x + tmp_rect.width) > input_img.size().width ||
116 (tmp_rect.y + tmp_rect.height) > input_img.size().height)
120 cv::Mat temp_map = input_img(found[i]);
121 std::vector<cv::Rect> tmp_found;
122 human_cascade.detectMultiScale(temp_map, tmp_found, 1.1, 3);
123 if(tmp_found.size() == 0)
127 final_humans.push_back(found[i]);
140 const std::vector<cv::Rect> pedestrianVector,
141 const std::vector<cv::Rect> upperbodyVector)
143 std::vector<cv::Rect> final_vector;
145 final_vector = pedestrianVector;
147 final_vector.insert( final_vector.end(), upperbodyVector.begin(),
148 upperbodyVector.end() );
150 int size = final_vector.size();
151 for(
unsigned int i = 0; i < size; i++ )
153 final_vector.push_back( final_vector[i] );
155 cv::groupRectangles( final_vector, 1, 0.2 );
std::vector< cv::Rect > findHuman2D(std::string file_name)
Finds humans in an image retrieved from a file URL.
std::vector< cv::Rect > identifyUniqueHumans(const std::vector< cv::Rect > pedestrianVector, const std::vector< cv::Rect > upperbodyVector)
Identify unique humans from two sets of humans.
std::vector< cv::Rect > detectHuman2D(const cv::Mat &input_img)
Detects humans from a cv::Mat.
cv::Mat loadImage(std::string file_name)
Loads an image from a file URL.
HumanDetector(void)
Default constructor.