00001
00002 #include "gkll_kl.h"
00003
00004 #include <vxl_config.h>
00005 #include <vil1/vil1_pixel.h>
00006 #include <vil1/vil1_memory_image_of.h>
00007 #include <vil1/vil1_image_as.h>
00008 #include <gkll/gkll_multi_view_data.h>
00009 #include <vtol/vtol_vertex_2d.h>
00010 #include <vidl_vil1/vidl_vil1_frame.h>
00011 #include <vidl_vil1/vidl_vil1_movie.h>
00012
00013 #include <vcl_iostream.h>
00014
00015
00016 gkll_kl::gkll_kl(const gkll_kl_params & params) : params_(params)
00017 {
00018 }
00019
00020 gkll_kl::~gkll_kl()
00021 {
00022 }
00023
00024 void gkll_kl::match_sequence(vcl_vector<vil1_image> &image_list,gkll_multi_view_data_vertex_sptr matches)
00025 {
00026
00027 int nFeatures = params_.numpoints;
00028 int nFrames = image_list.size();
00029
00030 if (nFrames < 1) return;
00031
00032
00033 KLT_TrackingContext tc = KLTCreateTrackingContext();
00034 KLT_FeatureList fl = KLTCreateFeatureList(nFeatures);
00035 KLT_FeatureTable ft = KLTCreateFeatureTable(nFrames, nFeatures);
00036
00037
00038 set_tracking_context (tc);
00039 tc->sequentialMode = TRUE;
00040
00041 int width=image_list[0].width();
00042 int height=image_list[0].height();
00043
00044
00045 KLT_PixelType* img1=convert_to_gs_image(image_list[0]);
00046
00047
00048 KLTSelectGoodFeatures(tc, img1, width, height, fl);
00049 KLTStoreFeatureList(fl, ft, 0);
00050
00051 for (int i=1; i<nFrames; i++)
00052 {
00053 KLT_PixelType* img2=convert_to_gs_image(image_list[i]);
00054
00055
00056 KLTTrackFeatures(tc, img1, img2, width, height, fl);
00057
00058
00059 if (params_.replaceLostPoints)
00060 KLTReplaceLostFeatures(tc, img2, width, height, fl);
00061
00062
00063 KLTStoreFeatureList(fl, ft, i);
00064 }
00065
00066 int matchnum = -1;
00067 int pointnum, viewnum;
00068
00069
00070 for (pointnum=0; pointnum<ft->nFeatures; pointnum++)
00071 for (viewnum=0; viewnum<ft->nFrames; viewnum++)
00072 {
00073
00074 KLT_Feature feat = ft->feature[pointnum][viewnum];
00075
00076
00077 float x = feat->x;
00078 float y = feat->y;
00079
00080
00081
00082
00083 if (feat->val == 0)
00084 {
00085 vtol_vertex_2d_sptr vertex=new vtol_vertex_2d(x,y);
00086 matches->set(viewnum, matchnum, vertex);
00087 }
00088
00089
00090 if (feat->val > 0)
00091 {
00092
00093
00094 if (viewnum < ft->nFrames-1 &&
00095 ft->feature[pointnum][viewnum+1]->val == 0)
00096 {
00097
00098 matchnum++;
00099
00100
00101 vtol_vertex_2d_sptr vertex=new vtol_vertex_2d(x,y);
00102 matches->set (viewnum, matchnum, vertex);
00103 }
00104 }
00105 }
00106
00107
00108 }
00109
00110
00111 void gkll_kl::match_sequence(vidl_vil1_movie_sptr movie,gkll_multi_view_data_vertex_sptr matches)
00112 {
00113 vcl_vector<vil1_image> image_list;
00114 for (vidl_vil1_movie::frame_iterator pframe = movie->first();
00115 pframe <= movie->last();
00116 ++pframe)
00117 {
00118 vil1_image im = vil1_image(pframe->get_image());
00119 image_list.push_back(im);
00120 }
00121 match_sequence(image_list,matches);
00122 }
00123
00124 vcl_vector<vtol_vertex_2d_sptr>* gkll_kl::extract_points(vil1_image & image)
00125 {
00126 int width=image.width();
00127 int height=image.height();
00128 vcl_cerr << "Beginning points extraction\n";
00129
00130 KLT_PixelType* img1=convert_to_gs_image(image);
00131
00132
00133 int nFeatures = params_.numpoints;
00134
00135 vcl_cerr << "Setting up the context...\n";
00136
00137 KLT_TrackingContext tc = KLTCreateTrackingContext();
00138
00139
00140 set_tracking_context (tc);
00141
00142
00143
00144
00145 vcl_cerr << "Setting up structure to hold the features...\n";
00146 KLT_FeatureList fl = KLTCreateFeatureList(nFeatures);
00147
00148
00149 vcl_cerr << "Extracting the features...\n";
00150 KLTSelectGoodFeatures(tc, img1, width, height, fl);
00151
00152
00153 vcl_vector<vtol_vertex_2d_sptr> *grp = new vcl_vector<vtol_vertex_2d_sptr>();
00154
00155 for (int i=0 ; i< fl->nFeatures ; i++)
00156 {
00157
00158 float x = fl->feature[i]->x;
00159 float y = fl->feature[i]->y;
00160
00161 vtol_vertex_2d_sptr point=new vtol_vertex_2d(x,y);
00162
00163 grp->push_back(point);
00164 }
00165
00166
00167
00168
00169
00170 return grp;
00171 }
00172
00173
00174 KLT_PixelType* gkll_kl::convert_to_gs_image(vil1_image &image)
00175 {
00176 vcl_cerr << "Converting image to grey scale...\n";
00177 if (vil1_pixel_format(image)==VIL1_RGB_BYTE)
00178 {
00179 int w=image.width();
00180 int h=image.height();
00181 KLT_PixelType* tab_mono=new KLT_PixelType[w*h];
00182 vcl_cerr << "width: " <<w<< " height"<<h<< vcl_endl;
00183
00184 vil1_memory_image_of<vxl_byte> ima_mono;
00185 ima_mono.resize(w,h);
00186
00187 vil1_image_as_byte(image).get_section(ima_mono.get_buffer(), 0, 0, w, h);
00188 vxl_byte* p=ima_mono.get_buffer();
00189
00190 for (int i=0;i<w;i++)
00191 for (int j=0;j<h;j++)
00192 {
00193 tab_mono[i*h+j]=(KLT_PixelType)p[i*h+j];
00194 }
00195 return tab_mono;
00196 } else return NULL;
00197 }
00198
00199 void gkll_kl::set_tracking_context( KLT_TrackingContext tc)
00200 {
00201
00202 tc->mindist = params_.mindist;
00203 tc->window_width = params_.window_width;
00204 tc->window_height = params_.window_height;
00205 tc->sequentialMode = params_.sequentialMode;
00206 tc->smoothBeforeSelecting = params_.smoothBeforeSelecting;
00207 tc->writeInternalImages = params_.writeInternalImages;
00208 tc->min_eigenvalue = params_.min_eigenvalue;
00209 tc->min_determinant = params_.min_determinant;
00210 tc->max_iterations = params_.max_iterations;
00211 tc->min_displacement = params_.min_displacement;
00212 tc->max_residue = params_.max_residue;
00213 tc->grad_sigma = params_.grad_sigma;
00214 tc->smooth_sigma_fact = params_.smooth_sigma_fact;
00215 tc->pyramid_sigma_fact = params_.pyramid_sigma_fact;
00216 tc->nSkippedPixels = params_.nSkippedPixels;
00217
00218
00219 KLTChangeTCPyramid (tc, params_.search_range);
00220 KLTUpdateTCBorder (tc);
00221 }