24 #include <fvclassifiers/sift.h>
29 #include <utils/time/clock.h>
30 #include <utils/time/tracker.h>
34 #include <sift/imgfeatures.h>
35 #include <sift/kdtree.h>
36 #include <sift/sift.h>
37 #include <sift/utils.h>
38 #include <sift/xform.h>
41 #include <core/exception.h>
42 #include <core/exceptions/software.h>
43 #include <fvutils/color/colorspaces.h>
44 #include <fvutils/color/conversions.h>
45 #include <opencv/cv.h>
46 #include <opencv/cxcore.h>
47 #include <opencv/highgui.h>
51 namespace firevision {
74 SiftClassifier::SiftClassifier(
const char * object_file,
75 unsigned int pixel_width,
76 unsigned int pixel_height,
77 int kdtree_bbf_max_nn_chks,
78 float nn_sq_dist_ratio_thr,
82 kdtree_bbf_max_nn_chks_ = kdtree_bbf_max_nn_chks;
83 nn_sq_dist_ratio_thr_ = nn_sq_dist_ratio_thr;
89 ttc_objconv_ = tt_->
add_class(
"ObjectConvert");
90 ttc_objfeat_ = tt_->
add_class(
"ObjectFeatures");
91 ttc_imgconv_ = tt_->
add_class(
"ImageConvert");
92 ttc_imgfeat_ = tt_->
add_class(
"ImageFeatures");
93 ttc_matchin_ = tt_->
add_class(
"Matching");
94 ttc_roimerg_ = tt_->
add_class(
"MergeROIs");
100 obj_img_ = cvLoadImage(object_file, 1);
102 throw Exception(
"Could not load object file");
111 obj_num_features_ = 0;
112 obj_num_features_ = sift_features(obj_img_, &obj_features_);
113 if (!obj_num_features_ > 0) {
114 throw Exception(
"Could not compute object features");
116 std::cout <<
"SiftClassifier(classify): computed '" << obj_num_features_
117 <<
"' features from object" << std::endl;
124 image_ = cvCreateImage(cvSize(pixel_width, pixel_height), IPL_DEPTH_8U, 3);
131 cvReleaseImage(&obj_img_);
132 cvReleaseImage(&image_);
143 std::list<ROI> *rv =
new std::list<ROI>();
145 struct feature * feat;
146 struct feature **nbrs;
147 struct kd_node * kd_root;
152 std::vector<CvPoint> ftlist;
166 convert(YUV422_PLANAR, BGR,
_src, (
unsigned char *)image_->imageData,
_width,
_height);
175 int num_img_ft = sift_features(image_, &img_features_);
176 kd_root = kdtree_build(img_features_, num_img_ft);
182 std::cerr <<
"SiftClassifier(classify): KD-Root NULL!" << std::endl;
188 std::cout <<
"SiftClassifier(classify): matching ..." << std::endl;
189 for (
int i = 0; i < obj_num_features_; ++i) {
191 feat = obj_features_ + i;
192 k = kdtree_bbf_knn(kd_root, feat, 2, &nbrs, kdtree_bbf_max_nn_chks_);
194 d0 = descr_dist_sq(feat, nbrs[0]);
195 d1 = descr_dist_sq(feat, nbrs[1]);
196 if (d0 < d1 * nn_sq_dist_ratio_thr_) {
197 pt1 = cvPoint(cvRound(feat->x), cvRound(feat->y));
198 pt2 = cvPoint(cvRound(nbrs[0]->x), cvRound(nbrs[0]->y));
200 obj_features_[i].fwd_match = nbrs[0];
202 ftpt = cvPoint(cvRound(nbrs[0]->x), cvRound(nbrs[0]->y));
203 ftlist.push_back(ftpt);
211 std::cout <<
"SiftClassifier(classify): found '" << m <<
"' matches" << std::endl;
212 kdtree_release(kd_root);
220 std::cout <<
"SiftClassifier(classify): computing ROI" << std::endl;
222 for (std::vector<CvPoint>::size_type i = 0; i < ftlist.size(); ++i) {
223 if (ftlist[i].x < x_min)
225 if (ftlist[i].y < y_min)
227 if (ftlist[i].x > x_max)
229 if (ftlist[i].y > y_max)
248 std::cout <<
"SiftClassifier(classify): done ... returning '" << rv->size() <<
"' ROIs."