27 #include <fvclassifiers/sift.h> 29 #include <utils/time/clock.h> 30 #include <utils/time/tracker.h> 34 #include <sift/sift.h> 35 #include <sift/imgfeatures.h> 36 #include <sift/kdtree.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> 46 #include <opencv/cv.h> 47 #include <opencv/cxcore.h> 48 #include <opencv/highgui.h> 78 SiftClassifier::SiftClassifier(
const char * object_file,
79 unsigned int pixel_width,
unsigned int pixel_height,
80 int kdtree_bbf_max_nn_chks,
float nn_sq_dist_ratio_thr,
int flags)
83 __kdtree_bbf_max_nn_chks = kdtree_bbf_max_nn_chks;
84 __nn_sq_dist_ratio_thr = nn_sq_dist_ratio_thr;
91 __ttc_objconv = __tt->
add_class(
"ObjectConvert");
92 __ttc_objfeat = __tt->
add_class(
"ObjectFeatures");
93 __ttc_imgconv = __tt->
add_class(
"ImageConvert");
94 __ttc_imgfeat = __tt->
add_class(
"ImageFeatures");
95 __ttc_matchin = __tt->
add_class(
"Matching");
96 __ttc_roimerg = __tt->
add_class(
"MergeROIs");
102 __obj_img = cvLoadImage( object_file, 1 );
104 throw Exception(
"Could not load object file");
113 __obj_num_features = 0;
114 __obj_num_features = sift_features( __obj_img, &__obj_features );
115 if ( ! __obj_num_features > 0 ) {
116 throw Exception(
"Could not compute object features");
118 std::cout <<
"SiftClassifier(classify): computed '" << __obj_num_features <<
"' features from object" << std::endl;
125 __image = cvCreateImage(cvSize(pixel_width, pixel_height), IPL_DEPTH_8U, 3);
134 cvReleaseImage(&__obj_img);
135 cvReleaseImage(&__image);
147 std::list< ROI > *rv =
new std::list< ROI >();
149 struct feature * feat;
150 struct feature** nbrs;
151 struct kd_node* kd_root;
156 std::vector< CvPoint > ftlist;
170 convert(YUV422_PLANAR, BGR,
_src, (
unsigned char *)__image->imageData,
_width,
_height);
179 int num_img_ft = sift_features( __image, &__img_features );
180 kd_root = kdtree_build( __img_features, num_img_ft );
186 std::cerr <<
"SiftClassifier(classify): KD-Root NULL!" << std::endl;
192 std::cout <<
"SiftClassifier(classify): matching ..." << std::endl;
193 for(
int i = 0; i < __obj_num_features; ++i ) {
195 feat = __obj_features + i;
196 k = kdtree_bbf_knn( kd_root, feat, 2, &nbrs, __kdtree_bbf_max_nn_chks );
199 d0 = descr_dist_sq( feat, nbrs[0] );
200 d1 = descr_dist_sq( feat, nbrs[1] );
201 if( d0 < d1 * __nn_sq_dist_ratio_thr )
203 pt1 = cvPoint( cvRound( feat->x ), cvRound( feat->y ) );
204 pt2 = cvPoint( cvRound( nbrs[0]->x ), cvRound( nbrs[0]->y ) );
206 __obj_features[i].fwd_match = nbrs[0];
208 ftpt = cvPoint( cvRound( nbrs[0]->x), cvRound( nbrs[0]->y ) );
209 ftlist.push_back(ftpt);
217 std::cout <<
"SiftClassifier(classify): found '" << m <<
"' matches" << std::endl;
218 kdtree_release( kd_root );
226 std::cout <<
"SiftClassifier(classify): computing ROI" << std::endl;
228 for ( std::vector< CvPoint >::size_type i = 0; i < ftlist.size(); ++i) {
229 if( ftlist[i].x < x_min )
231 if( ftlist[i].y < y_min )
233 if( ftlist[i].x > x_max )
235 if( ftlist[i].y > y_max )
254 std::cout <<
"SiftClassifier(classify): done ... returning '" << rv->size() <<
"' ROIs." << std::endl;
void ping_start(unsigned int cls)
Start of given class task.
Fawkes library namespace.
unsigned int _width
Width in pixels of _src buffer.
unsigned int _height
Height in pixels of _src buffer.
Base class for exceptions in Fawkes.
unsigned int add_class(std::string name)
Add a new class.
virtual ~SiftClassifier()
Destructor.
void ping_end(unsigned int cls)
End of given class task.
void print_to_stdout()
Print results to stdout.
virtual std::list< ROI > * classify()
Classify image.
Classifier to extract regions of interest.
unsigned char * _src
Source buffer, encoded as YUV422_PLANAR.