41 #include <pcl/people/person_classifier.h> 43 #ifndef PCL_PEOPLE_PERSON_CLASSIFIER_HPP_ 44 #define PCL_PEOPLE_PERSON_CLASSIFIER_HPP_ 46 template <
typename Po
intT>
49 template <
typename Po
intT>
52 template <
typename Po
intT>
bool 56 std::ifstream SVM_file;
57 SVM_file.open(svm_filename.c_str());
59 getline (SVM_file,line);
60 size_t tok_pos = line.find_first_of(
":", 0);
61 window_height_ = std::atoi(line.substr(tok_pos+1, line.npos - tok_pos-1).c_str());
63 getline (SVM_file,line);
64 tok_pos = line.find_first_of(
":", 0);
65 window_width_ = std::atoi(line.substr(tok_pos+1, line.npos - tok_pos-1).c_str());
67 getline (SVM_file,line);
68 tok_pos = line.find_first_of(
":", 0);
69 SVM_offset_ = std::atof(line.substr(tok_pos+1, line.npos - tok_pos-1).c_str());
71 getline (SVM_file,line);
72 tok_pos = line.find_first_of(
"[", 0);
73 size_t tok_end_pos = line.find_first_of(
"]", 0);
75 while (tok_pos < tok_end_pos)
77 prev_tok_pos = tok_pos;
78 tok_pos = line.find_first_of(
",", prev_tok_pos+1);
79 SVM_weights_.push_back(std::atof(line.substr(prev_tok_pos+1, tok_pos-prev_tok_pos-1).c_str()));
83 if (SVM_weights_.size() == 0)
85 PCL_ERROR (
"[pcl::people::PersonClassifier::loadSVMFromFile] Invalid SVM file!\n");
94 template <
typename Po
intT>
void 97 window_height_ = window_height;
98 window_width_ = window_width;
99 SVM_weights_ = SVM_weights;
100 SVM_offset_ = SVM_offset;
103 template <
typename Po
intT>
void 106 window_height = window_height_;
107 window_width = window_width_;
108 SVM_weights = SVM_weights_;
109 SVM_offset = SVM_offset_;
112 template <
typename Po
intT>
void 124 output_image->points.resize(width*height, new_point);
125 output_image->height = height;
126 output_image->width = width;
129 float scale1 = float(height) / float(input_image->height);
130 float scale2 = float(width) / float(input_image->width);
132 Eigen::Matrix3f T_inv;
133 T_inv << 1/scale1, 0, 0,
141 for (
int i = 0; i < height; i++)
143 for (
int j = 0; j < width; j++)
145 A = T_inv * Eigen::Vector3f(i, j, 1);
153 (f1 >= static_cast<int> (input_image->height)) ||
154 (c1 >= static_cast<int> (input_image->height)) ||
157 (f2 >= static_cast<int> (input_image->width)) ||
158 (c2 >= static_cast<int> (input_image->width)))
163 g1 = (*input_image)(f2, c1);
164 g3 = (*input_image)(f2, f1);
165 g4 = (*input_image)(c2, f1);
166 g2 = (*input_image)(c2, c1);
170 new_point.r = int((1 - w1) * ((1 - w2) * g1.r + w2 * g4.r) + w1 * ((1 - w2) * g3.r + w2 * g4.r));
171 new_point.g = int((1 - w1) * ((1 - w2) * g1.g + w2 * g4.g) + w1 * ((1 - w2) * g3.g + w2 * g4.g));
172 new_point.b = int((1 - w1) * ((1 - w2) * g1.b + w2 * g4.b) + w1 * ((1 - w2) * g3.b + w2 * g4.b));
175 (*output_image)(j,i) = new_point;
180 template <
typename Po
intT>
void 192 output_image->points.resize(height*width, black_point);
193 output_image->width = width;
194 output_image->height = height;
196 int x_start_in = std::max(0, xmin);
197 int x_end_in = std::min(
int(input_image->width-1), xmin+width-1);
198 int y_start_in = std::max(0, ymin);
199 int y_end_in = std::min(
int(input_image->height-1), ymin+height-1);
201 int x_start_out = std::max(0, -xmin);
203 int y_start_out = std::max(0, -ymin);
206 for (
int i = 0; i < (y_end_in - y_start_in + 1); i++)
208 for (
int j = 0; j < (x_end_in - x_start_in + 1); j++)
210 (*output_image)(x_start_out + j, y_start_out + i) = (*input_image)(x_start_in + j, y_start_in + i);
215 template <
typename Po
intT>
double 221 if (SVM_weights_.size() == 0)
223 PCL_ERROR (
"[pcl::people::PersonClassifier::evaluate] SVM has not been set!\n");
227 int height = floor((height_person * window_height_) / (0.75 * window_height_) + 0.5);
228 int width = floor((height_person * window_width_) / (0.75 * window_height_) + 0.5);
229 int xmin = floor(xc - width / 2 + 0.5);
230 int ymin = floor(yc - height / 2 + 0.5);
237 copyMakeBorder(image, box, xmin, ymin, width, height);
241 resize(box, sample, window_width_, window_height_);
244 float* sample_float =
new float[sample->width * sample->height * 3];
245 int delta = sample->height * sample->width;
246 for (uint32_t row = 0; row < sample->height; row++)
248 for (uint32_t col = 0; col < sample->width; col++)
250 sample_float[row + sample->height * col] = ((float) ((*sample)(col, row).r))/255;
251 sample_float[row + sample->height * col + delta] = ((float) ((*sample)(col, row).g))/255;
252 sample_float[row + sample->height * col + delta * 2] = (float) (((*sample)(col, row).b))/255;
258 float *descriptor = (
float*) calloc(SVM_weights_.size(),
sizeof(float));
259 hog.
compute(sample_float, descriptor);
263 for(
unsigned int i = 0; i < SVM_weights_.size(); i++)
265 confidence += SVM_weights_[i] * descriptor[i];
268 confidence -= SVM_offset_;
271 delete[] sample_float;
275 confidence = std::numeric_limits<double>::quiet_NaN();
281 template <
typename Po
intT>
double 283 Eigen::Vector3f& bottom,
284 Eigen::Vector3f& top,
285 Eigen::Vector3f& centroid,
293 pixel_height = bottom(1) - top(1);
294 pixel_width = pixel_height / 2.0f;
298 pixel_width = top(0) - bottom(0);
299 pixel_height = pixel_width / 2.0f;
301 float pixel_xc = centroid(0);
302 float pixel_yc = centroid(1);
306 return (evaluate(pixel_height, pixel_xc, pixel_yc, image));
310 return (evaluate(pixel_width, pixel_yc, image->height-pixel_xc+1, image));
boost::shared_ptr< PointCloud > PointCloudPtr
virtual ~PersonClassifier()
Destructor.
double evaluate(float height, float xc, float yc, PointCloudPtr &image)
Classify the given portion of image.
HOG represents a class for computing the HOG descriptor described in Dalal, N.
void setSVM(int window_height, int window_width, std::vector< float > SVM_weights, float SVM_offset)
Set trained SVM for person confidence estimation.
void getSVM(int &window_height, int &window_width, std::vector< float > &SVM_weights, float &SVM_offset)
Get trained SVM for person confidence estimation.
void copyMakeBorder(PointCloudPtr &input_image, PointCloudPtr &output_image, int xmin, int ymin, int width, int height)
Copies an image and makes a black border around it, where the source image is not present...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void compute(float *I, int h, int w, int n_channels, int bin_size, int n_orients, bool soft_bin, float *descriptor)
Compute HOG descriptor.
void resize(PointCloudPtr &input_image, PointCloudPtr &output_image, int width, int height)
Resize an image represented by a pointcloud containing RGB information.
PersonClassifier()
Constructor.
A point structure representing Euclidean xyz coordinates, and the RGB color.
bool loadSVMFromFile(std::string svm_filename)
Load SVM parameters from a text file.