contrib/brl/bseg/vpro/vpro_lucas_kanade_process.cxx
Go to the documentation of this file.
00001 #include "vpro_lucas_kanade_process.h"
00002 //:
00003 // \file
00004 #include <vcl_iostream.h>
00005 #include <vcl_cmath.h>
00006 #include <vil1/vil1_rgb.h>
00007 #include <brip/brip_vil1_float_ops.h>
00008 
00009 vpro_lucas_kanade_process::vpro_lucas_kanade_process(bool down_sample,
00010                                                      int window_size,
00011                                                      double thresh)
00012 {
00013   downsample_ = down_sample;
00014   window_size_ = window_size;
00015   thresh_ = thresh;
00016   state_ = NO_IMAGE;
00017 }
00018 
00019 vpro_lucas_kanade_process::~vpro_lucas_kanade_process()
00020 {
00021 }
00022 
00023 void vpro_lucas_kanade_process::
00024 compute_lucas_kanade(vil1_memory_image_of<float>& image)
00025 {
00026   int w = image.width(), h = image.height();
00027   vil1_memory_image_of<float> vx, vy;
00028   vx.resize(w,h);
00029   vy.resize(w,h);
00030 
00031   vil1_memory_image_of<float> prev(queue_[0]);
00032   brip_vil1_float_ops::Lucas_KanadeMotion(image, prev, window_size_, thresh_,
00033                                      vx, vy);
00034   vil1_memory_image_of< vil1_rgb<unsigned char> > output;
00035   output.resize(w,h);
00036   vil1_rgb<unsigned char> z(0,0,0);
00037   for (int y = 0; y<h; y++)
00038     for (int x = 0; x<w; x++)
00039     {
00040       double  fx = vx(x,y), fy = vy(x,y);
00041       if (!fx&&!fy)
00042       {
00043         output(x,y)= z;
00044         continue;
00045       }
00046       double ang = vcl_atan2(fy, fx);
00047       double red = 63*vcl_cos(ang), grn = 63*vcl_sin(ang);
00048       unsigned char  r = (unsigned char)(red+127);
00049       unsigned char  g = (unsigned char)(grn+127);
00050       vil1_rgb<unsigned char> v(r,g, 127);
00051       output(x,y) = v;
00052     }
00053   output_image_ = output;
00054 }
00055 
00056 void vpro_lucas_kanade_process::update_queue(vil1_image image)
00057 {
00058   queue_[0]=queue_[1];
00059   queue_[1]=image;
00060 }
00061 
00062 bool vpro_lucas_kanade_process::execute()
00063 {
00064   if (!this->get_N_input_images()==1)
00065   {
00066     vcl_cout << "In vpro_lucas_kanade_process::execute() -"
00067              << " not at exactly one input image\n";
00068     return false;
00069   }
00070   vil1_image img = vpro_video_process::get_input_image(0);
00071   vil1_memory_image_of<float> fimg = brip_vil1_float_ops::convert_to_float(img);
00072   vil1_memory_image_of<float> temp2;
00073   if (downsample_)
00074     temp2 = brip_vil1_float_ops::half_resolution(fimg);
00075   else
00076     temp2 = fimg;
00077   vil1_memory_image_of<float> fsmooth = brip_vil1_float_ops::gaussian(temp2, 0.8f);
00078   this->clear_input();
00079   switch (state_)
00080   {
00081     case NO_IMAGE:
00082       queue_.push_back(fsmooth);
00083       state_ = FIRST_IMAGE;
00084       break;
00085     case FIRST_IMAGE:
00086       queue_.push_back(fsmooth);
00087       state_ = IN_PROCESS;
00088       break;
00089     case IN_PROCESS:
00090       this->compute_lucas_kanade(fsmooth);
00091       this->update_queue(fsmooth);
00092       state_ = IN_PROCESS;
00093       break;
00094     default:
00095       vcl_cout << "In vpro_lucas_kanade_process::execute() - shouldn't happen\n";
00096       return false;
00097   }
00098   return true;
00099 }
00100 
00101 bool vpro_lucas_kanade_process::finish()
00102 {
00103   queue_.clear();
00104   state_=NO_IMAGE;
00105   return true;
00106 }