Go to the documentation of this file.00001
00002 #include "vmal_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 <vmal/vmal_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 vmal_kl::vmal_kl(const vmal_kl_params & params) : params_(params)
00017 {
00018 }
00019
00020 vmal_kl::~vmal_kl()
00021 {
00022 }
00023
00024 void vmal_kl::match_sequence(vcl_vector<vil1_image> &image_list,vmal_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 if (feat->val == 0)
00083 {
00084 vtol_vertex_2d_sptr vertex=new vtol_vertex_2d(x,y);
00085 matches->set(viewnum, matchnum, vertex);
00086 }
00087
00088
00089 if (feat->val > 0)
00090 {
00091
00092
00093 if (viewnum < ft->nFrames-1 &&
00094 ft->feature[pointnum][viewnum+1]->val == 0)
00095 {
00096
00097 matchnum++;
00098
00099
00100 vtol_vertex_2d_sptr vertex=new vtol_vertex_2d(x,y);
00101 matches->set (viewnum, matchnum, vertex);
00102 }
00103 }
00104 }
00105
00106
00107 }
00108
00109
00110 void vmal_kl::match_sequence(vidl_vil1_movie_sptr movie,vmal_multi_view_data_vertex_sptr matches)
00111 {
00112 vcl_vector<vil1_image> image_list;
00113 for (vidl_vil1_movie::frame_iterator pframe = movie->first();
00114 pframe <= movie->last();
00115 ++pframe)
00116 {
00117 vil1_image im = vil1_image(pframe->get_image());
00118 image_list.push_back(im);
00119 }
00120 match_sequence(image_list,matches);
00121 }
00122
00123 vcl_vector<vtol_vertex_2d_sptr>* vmal_kl::extract_points(vil1_image & image)
00124 {
00125 int width=image.width();
00126 int height=image.height();
00127 vcl_cerr << "Beginning points extraction\n";
00128
00129 KLT_PixelType* img1=convert_to_gs_image(image);
00130
00131
00132 int nFeatures = params_.numpoints;
00133
00134 vcl_cerr << "Setting up the context...\n";
00135
00136 KLT_TrackingContext tc = KLTCreateTrackingContext();
00137
00138
00139 set_tracking_context (tc);
00140
00141
00142
00143
00144 vcl_cerr << "Setting up structure to hold the features...\n";
00145 KLT_FeatureList fl = KLTCreateFeatureList(nFeatures);
00146
00147
00148 vcl_cerr << "Extracting the features...\n";
00149 KLTSelectGoodFeatures(tc, img1, width, height, fl);
00150
00151
00152 vcl_vector<vtol_vertex_2d_sptr> *grp = new vcl_vector<vtol_vertex_2d_sptr>();
00153
00154 for (int i=0 ; i< fl->nFeatures ; i++)
00155 {
00156
00157 float x = fl->feature[i]->x;
00158 float y = fl->feature[i]->y;
00159
00160 vtol_vertex_2d_sptr point=new vtol_vertex_2d(x,y);
00161
00162 grp->push_back(point);
00163 }
00164
00165
00166
00167
00168
00169 return grp;
00170 }
00171
00172
00173 KLT_PixelType* vmal_kl::convert_to_gs_image(vil1_image &image)
00174 {
00175 vcl_cerr << "Converting image to grey scale...\n";
00176 if (vil1_pixel_format(image)==VIL1_RGB_BYTE)
00177 {
00178 int w=image.width();
00179 int h=image.height();
00180 KLT_PixelType* tab_mono=new KLT_PixelType[w*h];
00181 vcl_cerr << "width: " <<w<< " height"<<h<< vcl_endl;
00182
00183 vil1_memory_image_of<vxl_byte> ima_mono;
00184 ima_mono.resize(w,h);
00185
00186 vil1_image_as_byte(image).get_section(ima_mono.get_buffer(), 0, 0, w, h);
00187 vxl_byte* p=ima_mono.get_buffer();
00188
00189 for (int i=0;i<w;i++)
00190 for (int j=0;j<h;j++)
00191 tab_mono[i*h+j]=(KLT_PixelType)p[i*h+j];
00192
00193 return tab_mono;
00194 } else return NULL;
00195 }
00196
00197 void vmal_kl::set_tracking_context( KLT_TrackingContext tc)
00198 {
00199
00200 tc->mindist = params_.mindist;
00201 tc->window_width = params_.window_width;
00202 tc->window_height = params_.window_height;
00203 tc->sequentialMode = params_.sequentialMode;
00204 tc->smoothBeforeSelecting = params_.smoothBeforeSelecting;
00205 tc->writeInternalImages = params_.writeInternalImages;
00206 tc->min_eigenvalue = params_.min_eigenvalue;
00207 tc->min_determinant = params_.min_determinant;
00208 tc->max_iterations = params_.max_iterations;
00209 tc->min_displacement = params_.min_displacement;
00210 tc->max_residue = params_.max_residue;
00211 tc->grad_sigma = params_.grad_sigma;
00212 tc->smooth_sigma_fact = params_.smooth_sigma_fact;
00213 tc->pyramid_sigma_fact = params_.pyramid_sigma_fact;
00214 tc->nSkippedPixels = params_.nSkippedPixels;
00215
00216
00217 KLTChangeTCPyramid (tc, params_.search_range);
00218 KLTUpdateTCBorder (tc);
00219 }
00220