[ECE 453] Re: Final Presentation Version 1

  • From: David Bonnie <dbonnie@xxxxxxxxxxxxx>
  • To: 453_all@xxxxxxxxxxxxx
  • Date: Sun, 30 Nov 2008 22:37:36 -0500

Everyone -

I've completed the automated startup code for the tracking program as well. I have it on one of the nodes now, I couldn't get a consistent enough connection to throw it on the other two due to the current nasty weather. David Busch - I've attached the modified code, could you build a new TigerEye.exe and send it back to me with your code inserted?

We're getting close to getting this up and running, let's all cross our fingers that nothing crazy happens between now and Thurs/Fri.

- David Bonnie


At 08:42 PM 11/30/2008, you wrote:
Ladies and Gents,

I created (stole and modified) a template for the final presentation that
we're going to deliver on Friday (oh no!). It has a section for each group
to deliver a short discussion on all the work conducted throughout the
semester. Feel free to add additional slides and to modify any errors in
other slides.

If you can increment the version number and e-mail the updated project to
453_all it will help people work on the most up-to-date presentation.

As for this week:

The new compact flash cards I ordered came in. I ordered two in case one
fails. I'm going to give the embedded PC and a fresh flash card to David
Bonnie tomorrow (Monday). It should be ready to go by Tuesday. If we can
go ahead and schedule with facilities we need to get to every node to
spray Pledge on the acryclic and make sure everything is situated with the
polarized lenses.

We also need to find a day/time with good weather to record video of the
system up and running for the presentation. Hopefully we can start this on
Wednesday.

Good luck this week!

Kenny
Content-Type:
     application/vnd.openxmlformats-officedocument.presentationml.presentat
     ion; name="Presentation Version 1.pptx"
Content-Disposition: attachment; filename="Presentation Version 1.pptx"
// TigerEyeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TigerEye.h"
#include "TigerEyeDlg.h"

#include "TrackerInterface.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define WRITE_RESULT_FRAMES 0
#define EXPIRATION_YEAR_FOR_WRITING_RESULTS  2009

/////////////////////////////////////////////////////////////////////////////
// CTigerEyeDlg dialog


CTigerEyeDlg::CTigerEyeDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CTigerEyeDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CTigerEyeDlg)
        m_avi_start = 0;
        m_bg_duration = 0;
        m_known_length = 0.0f;
        m_lane_width = 0.0f;
        m_nlanes = 0;
        m_mask_size_close = 0;
        m_mask_size_open = 0;
        m_stable_threshold_z = 0.0f;
        m_min_stable_features = 0;
        m_min_new_width = 0.0f;
        m_max_stable_group_length = 0.0f;
        m_stable_merge_width = 0.0f;
        m_max_missing_multiple = 0.0f;
        m_classification_line_y = 0.0f;
        m_min_truck_height = 0.0f;
        m_min_truck_length = 0.0f;
        m_min_unstable_features_for_truck = 0;
        m_sigma_x = 0.0f;
        m_sigma_y = 0.0f;
        m_sigma_z = 0.0f;
        m_sigma_motion = 0.0f;
        m_min_unstable_prob = 0.01f;
        m_min_unstable_prob_ratio = 0.0f;
        m_min_tracked_frames = 0;
        m_min_feature_age = 0;
        m_mask_bin_thr = 0;
        m_avi_step = 0;
        m_x_perturb = 0;
        m_max_y_perturb = 0.0f;
        m_max_matching_dist_x = 0.0f;
        m_max_matching_dist_y = 0.0f;
        m_min_occupancy = 0.0f;
        m_downsample = FALSE;
        m_min_trailer_height = 0.0f;
        m_min_trailer_length = 0.0f;
        m_min_multi_trailer_height = 0.0f;
        m_min_multi_trailer_length = 0.0f;
        m_checkbox_use_saved = TRUE;
        m_checkbox_save_info = TRUE;
        m_show_debug_images = FALSE;
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTigerEyeDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CTigerEyeDlg)
        DDX_Control(pDX, IDC_BUTTON_SLIDE, m_cmd_slide);
        DDX_Text(pDX, IDC_EDIT_AVI_START, m_avi_start);
        DDV_MinMaxUInt(pDX, m_avi_start, 1, 999999);
        DDX_Text(pDX, IDC_EDIT_BG_DURATION, m_bg_duration);
        DDV_MinMaxUInt(pDX, m_bg_duration, 1, 120);
        DDX_Text(pDX, IDC_EDIT_KNOWN_LENGTH, m_known_length);
        DDX_Text(pDX, IDC_EDIT_LANE_WIDTH, m_lane_width);
        DDX_Text(pDX, IDC_EDIT_NLANES, m_nlanes);
        DDV_MinMaxUInt(pDX, m_nlanes, 1, 10);
        DDX_Text(pDX, IDC_EDIT_MASK_SIZE_CLOSE, m_mask_size_close);
        DDV_MinMaxUInt(pDX, m_mask_size_close, 0, 15);
        DDX_Text(pDX, IDC_EDIT_MASK_SIZE_OPEN, m_mask_size_open);
        DDV_MinMaxUInt(pDX, m_mask_size_open, 0, 15);
        DDX_Text(pDX, IDC_EDIT_STABLE_THR_Z, m_stable_threshold_z);
        DDX_Text(pDX, IDC_EDIT_MIN_STABLE_FEATURES, m_min_stable_features);
        DDX_Text(pDX, IDC_EDIT_MIN_NEW_WIDTH, m_min_new_width);
        DDX_Text(pDX, IDC_EDIT_MAX_STABLE_GROUP_LENGTH, 
m_max_stable_group_length);
        DDX_Text(pDX, IDC_EDIT_STABLE_MERGE_WIDTH, m_stable_merge_width);
        DDX_Text(pDX, IDC_EDIT_MAX_MISSING_MULTIPLE, m_max_missing_multiple);
        DDX_Text(pDX, IDC_EDIT_CLASSIFICATION_LINE_Y, m_classification_line_y);
        DDX_Text(pDX, IDC_EDIT_MIN_TRUCK_HEIGHT, m_min_truck_height);
        DDX_Text(pDX, IDC_EDIT_MIN_TRUCK_LENGTH, m_min_truck_length);
        DDX_Text(pDX, IDC_EDIT_MIN_FEATURES_FOR_TRUCK, 
m_min_unstable_features_for_truck);
        DDX_Text(pDX, IDC_EDIT_SIGMA_X, m_sigma_x);
        DDX_Text(pDX, IDC_EDIT_SIGMA_Y, m_sigma_y);
        DDX_Text(pDX, IDC_EDIT_SIGMA_Z, m_sigma_z);
        DDX_Text(pDX, IDC_EDIT_SIGMA_MOTION, m_sigma_motion);
        DDX_Text(pDX, IDC_EDIT_MIN_UNSTABLE_PROB, m_min_unstable_prob);
        DDX_Text(pDX, IDC_EDIT_MIN_UNSTABLE_PROB_RATIO, 
m_min_unstable_prob_ratio);
        DDX_Text(pDX, IDC_EDIT_MIN_TRACKED_FRAMES, m_min_tracked_frames);
        DDX_Text(pDX, IDC_EDIT_MIN_FEATURE_AGE, m_min_feature_age);
        DDX_Text(pDX, IDC_EDIT_MASK_BIN_THR, m_mask_bin_thr);
        DDV_MinMaxUInt(pDX, m_mask_bin_thr, 0, 255);
        DDX_Text(pDX, IDC_EDIT_AVI_STEP, m_avi_step);
        DDV_MinMaxUInt(pDX, m_avi_step, 1, 30);
        DDX_Text(pDX, IDC_EDIT_X_PERTURB_SIZE, m_x_perturb);
        DDV_MinMaxInt(pDX, m_x_perturb, 0, 50);
        DDX_Text(pDX, IDC_EDIT_MAX_Y_PERTURB, m_max_y_perturb);
        DDX_Text(pDX, IDC_EDIT_MAX_MATCHING_DIST_X, m_max_matching_dist_x);
        DDX_Text(pDX, IDC_EDIT_MAX_MATCHING_DIST_Y, m_max_matching_dist_y);
        DDX_Text(pDX, IDC_EDIT_MIN_OCCUPANCY, m_min_occupancy);
        DDV_MinMaxFloat(pDX, m_min_occupancy, 0.f, 1.f);
        DDX_Check(pDX, IDC_CHECK_DOWNSAMPLE, m_downsample);
        DDX_Text(pDX, IDC_EDIT_MIN_TRAILER_HEIGHT, m_min_trailer_height);
        DDX_Text(pDX, IDC_EDIT_MIN_TRAILER_LENGTH, m_min_trailer_length);
        DDX_Text(pDX, IDC_EDIT_MIN_MULTI_TRAILER_HEIGHT, 
m_min_multi_trailer_height);
        DDX_Text(pDX, IDC_EDIT_MIN_MULTI_TRAILER_LENGTH, 
m_min_multi_trailer_length);
        DDX_Check(pDX, IDC_CHECK_USE_SAVED, m_checkbox_use_saved);
        DDX_Check(pDX, IDC_CHECK_SAVE_INFO, m_checkbox_save_info);
        DDX_Check(pDX, IDC_CHECK_DEBUG_IMAGES, m_show_debug_images);
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTigerEyeDlg, CDialog)
        //{{AFX_MSG_MAP(CTigerEyeDlg)
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_BN_CLICKED(IDC_BUTTON_AVI, OnButtonAvi)
        ON_BN_CLICKED(IDC_BUTTON_GENERATE_BG, OnButtonGenerateBg)
        ON_BN_CLICKED(IDC_BUTTON_ROLL, OnButtonRoll)
        ON_BN_CLICKED(IDC_BUTTON_CALIBRATE, OnButtonCalibrate)
        ON_BN_CLICKED(IDC_BUTTON_ABORT, OnButtonAbort)
        ON_BN_CLICKED(IDC_BUTTON_START_TRACKER, OnButtonStartTracker)
        ON_BN_CLICKED(IDC_BUTTON_UPDATE_PARAMS, OnButtonUpdateParams)
        ON_BN_CLICKED(IDC_BUTTON_FRAMEGRABBER, OnButtonFramegrabber)
        ON_BN_CLICKED(IDC_BUTTON_LOG_OUT, OnButtonLogOut)
        ON_BN_CLICKED(IDC_BUTTON_BG_RESET, OnButtonBgReset)
        ON_BN_CLICKED(IDC_BUTTON_CROP, OnButtonCrop)
        ON_BN_CLICKED(IDC_BUTTON_RESET_CROP, OnButtonResetCrop)
        ON_BN_CLICKED(IDC_BUTTON_RESET_ROLL, OnButtonResetRoll)
        ON_BN_CLICKED(IDC_BUTTON_SLIDE, OnButtonSlide)
        ON_BN_CLICKED(IDC_CHECK_DEBUG_IMAGES, OnCheckDebugImages)
        ON_BN_CLICKED(IDC_CHECK_SAVE_INFO, OnCheckSaveInfo)
        ON_BN_CLICKED(IDC_CHECK_USE_SAVED, OnCheckUseSaved)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTigerEyeDlg message handlers

BOOL CTigerEyeDlg::OnInitDialog()
{
        CDialog::OnInitDialog();

        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);                 // Set big icon
        SetIcon(m_hIcon, FALSE);                // Set small icon
        
        // TODO: Add extra initialization here

  m_abort = false;
  m_show_settings = false;
  OnButtonSlide();

  SetDefaultParameters();
  Sleep(60000);                         // prevent the tracker from starting 
before the camera driver is loaded, could need tweaking
  OnButtonFramegrabber();       // use the framegrabber, must be ready before 
starting
  OnButtonGenerateBg();         // generate background if not saved
  OnButtonCalibrate();          // use saved calibration data
  OnButtonStartTracker();       // start tracking
  return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTigerEyeDlg::OnPaint() 
{
        if (IsIconic())
        {
                CPaintDC dc(this); // device context for painting

                SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

                // Center icon in client rectangle
                int cxIcon = GetSystemMetrics(SM_CXICON);
                int cyIcon = GetSystemMetrics(SM_CYICON);
                CRect rect;
                GetClientRect(&rect);
                int x = (rect.Width() - cxIcon + 1) / 2;
                int y = (rect.Height() - cyIcon + 1) / 2;
    
                // Draw the icon
                dc.DrawIcon(x, y, m_hIcon);
        }
        else
        {
                CDialog::OnPaint();
        }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTigerEyeDlg::OnQueryDragIcon()
{
        return (HCURSOR) m_hIcon;
}

void CTigerEyeDlg::EnableDisable()
{
//  GetDlgItem(IDC_EDIT_AVI_START)->EnableWindow(FALSE);
}


void CTigerEyeDlg::SetDefaultParameters()
{
  m_avi_start = 1;
        m_avi_step = 2;

        m_min_feature_age = 1;
  m_min_tracked_frames = 2; //5;

  m_downsample = true;
  m_mask_bin_thr = 20;
        m_mask_size_close = 3;
        m_mask_size_open = 3;
        
  m_stable_threshold_z = 5.0f;

  if(USE_VIRTUAL_FEATURES)
  {
    m_stable_threshold_z = 2.0f;
  }

        m_x_perturb = 5;//5;
        m_max_y_perturb = 0;//0.0f;
  m_min_stable_features = 3;
  m_min_new_width = 1.0f; //0.5f;
        
  m_max_stable_group_length = 10.0f;//12.0f
        m_stable_merge_width = 8.0f;
    
        m_max_missing_multiple = 100.0f;
        m_max_matching_dist_x = 2.0f;
        m_max_matching_dist_y = 10.0f;

  m_classification_line_y = 0.5f;

  
  m_min_occupancy = 0.2f;

  m_min_truck_height = 10.0f;
  m_min_trailer_height = 10.0f;
  m_min_multi_trailer_height = 10.0f;

  m_min_truck_length = 30.0f;
        m_min_trailer_length = 90.0f;  
        m_min_multi_trailer_length = 120.0f;
  
  m_bg_duration = 5;
        
        m_nlanes = 4;
        m_lane_width = 12.0f;
  m_known_length = 50.0f;




  //------- 
  m_min_unstable_features_for_truck = 25;
        m_sigma_x = 0.1f;
        m_sigma_y = 0.1f;
        m_sigma_z = 0.1f;
        m_sigma_motion = 1.0f;
        m_min_unstable_prob = 0.01f;
        m_min_unstable_prob_ratio = 2.0f;
        //--------

  UpdateData(false);
}

void CTigerEyeDlg::ReadTrackingParamsFromDialog(TrackingParams* params)
{
  UpdateData();
  params->downsample = m_downsample;
        params->mask_bin_thr = m_mask_bin_thr;
        params->mask_size_close = m_mask_size_close ;
        params->mask_size_open = m_mask_size_open ;
        params->stable_threshold_z = m_stable_threshold_z ;
        params->x_perturb = m_x_perturb;
        params->max_y_perturb= m_max_y_perturb;
        params->min_stable_features = m_min_stable_features;
        params->min_new_width = m_min_new_width ;
        params->max_stable_group_length = m_max_stable_group_length ;
        params->stable_merge_width = m_stable_merge_width ;
  params->min_occupancy = m_min_occupancy;

        params->max_missing_multiple = m_max_missing_multiple;
        params->max_matching_dist_x = m_max_matching_dist_x ;
        params->max_matching_dist_y = m_max_matching_dist_y ;
        params->classification_line_y = m_classification_line_y ;
        params->min_truck_height = m_min_truck_height ;
        params->min_truck_length = m_min_truck_length ;
        params->min_trailer_height = m_min_trailer_height ;
        params->min_trailer_length = m_min_trailer_length ;
        params->min_multi_trailer_height = m_min_multi_trailer_height ;
        params->min_multi_trailer_length = m_min_multi_trailer_length ;
        
  
  params->min_unstable_features_for_truck = m_min_unstable_features_for_truck ;
        params->sigma_x = m_sigma_x ;
        params->sigma_y = m_sigma_y ;
        params->sigma_z = m_sigma_z ;
        params->sigma_motion = m_sigma_motion ;
        params->min_unstable_prob = m_min_unstable_prob ;
        params->min_unstable_prob_ratio = m_min_unstable_prob_ratio ;
        params->min_tracked_frames = m_min_tracked_frames;
        params->min_feature_age = m_min_feature_age ;
  
}

//================================================================================================================
void CTigerEyeDlg::OnButtonAvi() 
{

  UpdateData();

  CFileDialog dlg(TRUE);
  
  if (dlg.DoModal() == IDOK)
  {     
    CString file_name = dlg.GetPathName(); 
    CString ext = file_name.Right(3);
    if (ext.Compare("avi") != 0)
    {
      AfxMessageBox("Select an AVI file");
      return;
    }
    
    if (m_tracker_interface.m_video_input.InitializeAvi(file_name, m_avi_start, 
m_avi_step) == 0)
    {
      m_tracker_interface.Reset();
    }
    else
    {
      AfxMessageBox("Error initializing AVI file");
    }
    
  }
  
  EnableDisable(); 

//  if(m_checkbox_use_saved == TRUE)
//  {
//    OnButtonCrop();
//    OnButtonRoll();
//    OnButtonGenerateBg();    
//    OnButtonCalibrate();
//  }

  ShowSampleInputFrame();

}


//================================================================================================================

void CTigerEyeDlg::OnButtonGenerateBg()
{
  UpdateData();
  //if(1-GET_AND_SAVE_BG_AND_CLICKS)
/*  if(m_checkbox_use_saved == TRUE)
  {
    FILE* fp = fopen("bg.pgm", "r");
    if(fp != NULL)
    {
      fclose(fp);
      ImgGray bg;
      Load("bg.pgm", &bg);
      m_tracker_interface.m_bg.SetBg(bg);
      return;
    }
    else
    {
      //AfxMessageBox("bg.pgm not found\n Uncheck the 'Use saved info' checkbox 
and try again.");
      //return;
    }
    
  } */
  
  m_abort = false;
  UpdateData();
  ImgGray img;
  ImgBgr bgr;
  
  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -1)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }
  
  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -2)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }
  
  int bg_duration = 1000 * m_bg_duration;
  
  if (m_tracker_interface.m_video_input.Initialized())
  {
    Figure fig(NULL, -1, -1, false);
    bool keep_going = true;
    while(keep_going)
    {
      keep_going = false;
      SYSTEMTIME systime;
      TickCount old_tick, new_tick;
      int ret_val = -1;
      while(ret_val < 0)
      {
        ret_val = m_tracker_interface.m_video_input.GetLatestImage(&img, 
&systime, &old_tick);
        if(ret_val == -1)
        {
          return;
        }
        
        if(ret_val == -2)
        {
          Sleep(20);
          continue;  
        }
      }
      
      new_tick = old_tick;
      
      //m_tracker_interface.m_bg.Reset();
      
      int tick_diff = new_tick-old_tick;
            
      while ( (tick_diff < bg_duration)   &  (!m_abort) )
      {      
        
        ret_val = m_tracker_interface.m_video_input.GetLatestImage(&img, 
&systime, &new_tick);
        
        if(ret_val == -1)
        {
          break;
        }
        
        if(ret_val == -2)
        {
          Sleep(20);
          continue;
        }
        
        m_tracker_interface.m_bg.Update(img);      
        tick_diff = new_tick-old_tick;
        
        ImgGray local_bg = m_tracker_interface.m_bg.GetBg();
        fig.Draw(local_bg);
        CString tmp;
        tmp.Format("%d of %d", tick_diff/1000, m_bg_duration);
        fig.SetTitle(tmp);
      }

      //int ret = AfxMessageBox("Is background ok?", MB_YESNO);
      if (1) // || ret == IDYES)  
      {
        keep_going = false;
      }
      else 
      {
        keep_going = true;
      }
      
      //AfxMessageBox("Done generating background image.");
      
    }
  }
  else
  {
    AfxMessageBox("Video input not initialized");
  }

  //if(GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_save_info == TRUE)
  {
    Save(m_tracker_interface.m_bg.GetBg(), "bg.pgm", "PGM");
  }
}



//================================================================================================================

void CTigerEyeDlg::OnButtonRoll() 
{
  UpdateData();

  ImgBgr bgr, orig_bgr;  

  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -1)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }

  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -2)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }

  
  orig_bgr = bgr;
  

  Point pt1(999, 999);
  Point pt2;
  
  double roll_angle = 0.0;

  //if(1-GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_use_saved == TRUE)
  {
    FILE* fp = fopen("roll.txt", "r");
    if(fp != NULL)
    {
      fclose(fp);
      MatDbl mat_roll;
      Load("roll.txt", &mat_roll);
      roll_angle = mat_roll(0);
      m_tracker_interface.m_camroll.SetRollAngle(bgr.Width(), bgr.Height(), 
roll_angle);    
      
m_tracker_interface.m_video_input.SetAffine(m_tracker_interface.m_camroll.GetRollAffine());
      return;
    }
    else
    {
      //AfxMessageBox("roll.txt not found\n Uncheck the 'Use saved info' 
checkbox and try again.");
      //return;
    }
  }

  
  Figure fig;
  fig.SetVisible(TRUE);

  while (1)
  {
    fig.Draw(bgr);
    pt1 = fig.GrabMouseClick();
    DrawDot(pt1, &bgr, COLOR_RED, 2);
    fig.Draw(bgr);
    
    pt2 = fig.GrabMouseClick();
    
    roll_angle = m_tracker_interface.m_camroll.ComputeRollAngle(pt1, pt2);
    
    // Set the roll angle which computes the associated affine matrix
    m_tracker_interface.m_camroll.SetRollAngle(bgr.Width(), bgr.Height(), 
roll_angle);    
    
    bgr = orig_bgr;
    m_tracker_interface.m_camroll.CompensateForRoll(&bgr);
    
    fig.Draw(bgr);
    
    int ret = AfxMessageBox("Roll angle okay?", MB_YESNO);
    if (ret == IDYES)  break;
    else 
    {
      // Revert back to previous roll and try again
      m_tracker_interface.m_camroll.SetRollAngle(bgr.Width(), bgr.Height(), 0);
      bgr = orig_bgr;
    }
  }
  
  
  // Get the affine matrix for the roll angle and copy it to the affine matrix
  // in the videoinput object so that each frame that is read from now on will 
be compensated for roll.
  
m_tracker_interface.m_video_input.SetAffine(m_tracker_interface.m_camroll.GetRollAffine());

  fig.SetVisible(FALSE);
  ShowSampleInputFrame();

  //if(GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_save_info == TRUE)
  {
    MatDbl mat_roll(1,1);
    mat_roll(0) = roll_angle;
    Save(mat_roll, "roll.txt");
  }
}

//================================================================================================================
void CTigerEyeDlg::OnButtonCrop() 
{
        // TODO: Add your control notification handler code here
  UpdateData();

  ImgBgr bgr, orig_bgr;  
  m_tracker_interface.m_video_input.ResetCropRect();

  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -1)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }

  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -2)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }

  
  orig_bgr = bgr;
  
  Point pt1;
  Point pt2;
  
  Rect rect(0,0,0,0);

  //if(1-GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_use_saved == TRUE)
  {
    FILE* fp = fopen("crop.txt", "r");
    if(fp != NULL)
    {
      fclose(fp);
      MatDbl mat_crop;
      Load("crop.txt", &mat_crop);
      rect.top = mat_crop(0);
      rect.left = mat_crop(1);
      rect.bottom = mat_crop(2);
      rect.right = mat_crop(3);
      m_tracker_interface.m_video_input.SetCropRect(rect);
      ShowSampleInputFrame();      
      return;
    }
    else
    {
      //AfxMessageBox("crop.txt not found\n Uncheck the 'Use saved info' 
checkbox and try again.");
      //return;
    }
  }

  
  Figure fig;
  fig.SetVisible(TRUE);

  while (1)
  {
    fig.Draw(bgr);
    pt1 = fig.GrabMouseClick();
    DrawDot(pt1, &bgr, COLOR_RED, 2);
    fig.Draw(bgr);
    
    pt2 = fig.GrabMouseClick();
    
    rect.top = pt1.y;
    rect.bottom = pt2.y;
    if(pt2.y < pt1.y)
    {
      rect.top = pt2.y;
      rect.bottom = pt1.y;
    }

    rect.left = pt1.x;
    rect.right = pt2.x;

    if(pt2.x < pt1.x)
    {
      rect.left = pt2.x;
      rect.right = pt1.x;
    }
    
    // without this check, OpenCV feature tracker did not find many features
    rect.right -= rect.Width()%16;
    rect.bottom -= rect.Height()%16;
    

    // Set the roll angle which computes the associated affine matrix
    m_tracker_interface.m_video_input.SetCropRect(rect);
    
m_tracker_interface.m_video_input.CropImage(m_tracker_interface.m_video_input.GetCropRect(),
 &bgr);
    fig.Draw(bgr);

    int ret = AfxMessageBox("Crop rect okay?", MB_YESNO);
    if (ret == IDYES)  
    {
      break;
    }
    else 
    {
      m_tracker_interface.m_video_input.ResetCropRect();
      bgr = orig_bgr;
    }
  }  

  fig.SetVisible(FALSE);
  
  ShowSampleInputFrame();

  //if(GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_save_info == TRUE)
  {
    MatDbl mat_crop(1,4);
    mat_crop(0) = rect.top;
    mat_crop(1) = rect.left;
    mat_crop(2) = rect.bottom;
    mat_crop(3) = rect.right;

    Save(mat_crop,"crop.txt");
  }

}

//================================================================================================================
void CTigerEyeDlg::OnButtonCalibrate() 
{
  UpdateData();
  ShowSampleInputFrame();

  ImgBgr bgr, tmp_bgr;  

  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -1)
  {
    return;
  }

  tmp_bgr = bgr;
  
  //if(1-GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_use_saved == TRUE)
  {
    FILE* fp = fopen("calib.txt", "r");
    if(fp != NULL)
    {
      fclose(fp);
      MatDbl mat_calib(3, 6);
      Load("calib.txt", &mat_calib);
      Array<Point> pts;
      pts.Reset(6);
      for(int i=0; i < 6; i++)
      {
        pts[i].x = mat_calib(0, i);
        pts[i].y = mat_calib(1, i);
      }
    
      m_nlanes = (int) mat_calib(2, 0);
      m_lane_width = (float) mat_calib(2, 1);
      m_known_length = (float) mat_calib(2, 2);
      UpdateData(false);

      m_tracker_interface.m_calib.Calibrate(bgr.Width(), bgr.Height(), pts, 
m_nlanes, m_lane_width, m_known_length);  
      ShowSampleInputFrame();
      return;
    }
    else
    {
      //AfxMessageBox("calib.txt not found\n Uncheck the 'Use saved info' 
checkbox and try again.");
      //return;
    }
  }
  


  Figure fig(NULL, -1, -1, false);
  fig.SetVisible(TRUE);
  
  CString instructions;
  instructions.Format("%s\n%s\n%s\n%s", "1) Draw a line in the direction of 
traffic flow (along the outer edge of slowest lane).",
    "This line determines the length of the detection zone.",    
    "2) Draw a second line in the direction of traffic flow (along the outer 
edge of the fastest lane).",
    "3) Draw a line along the direction of traffic flow corresponding to a 
known length in that direction.") ;

  //texter.DrawText(&bgr, "Draw lines in the direction of traffic flow", 
Point(2,2), COLOR_WHITE, COLOR_BLACK);
  //texter.DrawText(&bgr, "Select first road edge", Point(2,16), COLOR_WHITE, 
COLOR_BLACK);
  fig.Draw(bgr);

  AfxMessageBox(instructions);

  Point pt11 = fig.GrabMouseClick();
  DrawDot(pt11, &bgr, COLOR_RED, 2);
  fig.Draw(bgr);
  Point pt12 = fig.GrabMouseClick();
  DrawLine(pt11,pt12, &bgr, COLOR_RED, 2);
  DrawDot(pt11, &bgr, COLOR_RED, 2);
  DrawDot(pt12, &bgr, COLOR_RED, 2);
  //texter.DrawText(&bgr, "Select second road edge", Point(2,28), COLOR_WHITE, 
COLOR_BLACK);
  fig.Draw(bgr);

  
  Point pt21 = fig.GrabMouseClick();
  DrawDot(pt21, &bgr, COLOR_RED, 2);
  fig.Draw(bgr);
  Point pt22 = fig.GrabMouseClick();
  DrawLine(pt21,pt22, &bgr, COLOR_RED, 2);
  DrawDot(pt21, &bgr, COLOR_RED, 2);
  DrawDot(pt22, &bgr, COLOR_RED, 2);
  //texter.DrawText(&bgr, "Select known length", Point(2,40), COLOR_WHITE, 
COLOR_BLACK);
  fig.Draw(bgr);

  
  Point pt31 = fig.GrabMouseClick();
  DrawDot(pt31, &bgr, COLOR_GREEN, 2);
  fig.Draw(bgr);
  Point pt32 = fig.GrabMouseClick();
  DrawLine(pt31,pt32, &bgr, COLOR_GREEN, 2);
  DrawDot(pt31, &bgr, COLOR_GREEN, 2);
  DrawDot(pt32, &bgr, COLOR_GREEN, 2);
  fig.Draw(bgr);

  Array<Point> pts;
 
  pts.Push(pt11); // pt11, pt12: Edge of the road
  pts.Push(pt12);
  
  if (((pt11.y-pt12.y)*(pt21.y-pt22.y)) > 0)
  {
    // both lines drawn with same order of points (lines drawn in same 
direction)
    pts.Push(pt21); // pt21, pt22: Another edge of the road
    pts.Push(pt22);
  }
  else // lines drawn with oppsite order of points (lines drawn in opposite 
directions)
  {
    pts.Push(pt22);
    pts.Push(pt21);
  }

  
  pts.Push(pt31); // pt31, pt32: Points corresponding to known length along the 
direction of travel
  pts.Push(pt32);
  
  m_tracker_interface.m_calib.Calibrate(bgr.Width(), bgr.Height(), pts, 
m_nlanes, m_lane_width, m_known_length);  

  //bgr = tmp_bgr;
  //m_tracker_interface.m_calib.DrawDetectionZone(&bgr);    
    

  
  ShowSampleInputFrame();

  AfxMessageBox("Done calibrating.");

  //if(GET_AND_SAVE_BG_AND_CLICKS)
  if(m_checkbox_save_info == TRUE)
  {
    MatDbl mat_calib(3, 6);
    for(int i=0; i < 6; i++)
    {
      mat_calib(0, i) = pts[i].x;
      mat_calib(1, i) = pts[i].y;
    }
    mat_calib(2, 0) = m_nlanes ;
    mat_calib(2, 1) = m_lane_width ;
    mat_calib(2, 2) = m_known_length ;

    Save(mat_calib, "calib.txt");
  }

}




//================================================================================================================

void CTigerEyeDlg::OnButtonAbort() 
{
  m_abort = true;
}

//================================================================================================================
void CTigerEyeDlg::OnButtonStartTracker() 
{
  
  TrackingParams params;
  ReadTrackingParamsFromDialog(&params);  
  
  if ((!m_tracker_interface.m_bg.Initialized()) | 
(!m_tracker_interface.m_video_input.Initialized())  | 
(!m_tracker_interface.m_calib.Calibrated()))
  {
    AfxMessageBox("Initialization or calibration not complete");
    return;
  }
  
  if(!m_tracker_interface.m_tracker.Initialized())
  {
    m_tracker_interface.m_tracker.Initialize(params, 
&m_tracker_interface.m_video_input, &m_tracker_interface.m_calib, 
&m_tracker_interface.m_bg);
  }

  Figure fig1;
  fig1.Draw(m_tracker_interface.m_bg.GetBg());
  Rect rect = fig1.GetWindowRect();
  Figure fig2;
  fig2.Draw(m_tracker_interface.m_bg.GetBg());
  fig2.SetWindowPosition(rect.left, rect.bottom+10);
  
  
  int fnum = 0;
   CString str;

  while (!m_abort)
  {
    int processed_frame = m_tracker_interface.m_tracker.ProcessNextFrame();
    if(processed_frame == -1)
    {
      continue;
    }

    if(processed_frame == -2)
    {
      break;
    }

        if (processed_frame % 1800 == 0) {
          CTigerEyeDlg::OnButtonGenerateBg();
        }
    
    CString title;
    title.Format("%d", processed_frame);
    fig1.Draw(m_tracker_interface.m_tracker.GetOutputImg());
    fig2.Draw(m_tracker_interface.m_tracker.GetTableImg());
    fig1.SetTitle(title);
    //fig1.GrabMouseClick();
    
    if(WRITE_RESULT_FRAMES)
    {
      ImgBgr bgr = m_tracker_interface.m_tracker.GetOutputImg();    
      ImgBgr table = m_tracker_interface.m_tracker.GetTableImg();    
      ImgBgr out_img(bgr.Width(), bgr.Height()+table.Height());
      Set(&out_img, Point(0, 0), bgr);
      Set(&out_img, Point(0, bgr.Height()), table);

      str.Format("c:/tmp1/img/%04d.jpg", fnum);
      Save(out_img, str, "jpg");

//      Save(m_tracker_interface.m_tracker.GetOutputImg(), str, "jpg");
//      str.Format("c:/tmp/table/%04d.jpg", fnum);
//      Save(m_tracker_interface.m_tracker.GetTableImg(), str, "jpg");
      fnum++;
    }

  }

  m_abort = false;
}


void CTigerEyeDlg::OnButtonUpdateParams() 
{
        // TODO: Add your control notification handler code here
  TrackingParams params;
  ReadTrackingParamsFromDialog(&params);  
  m_tracker_interface.m_tracker.SetParameters(params);
}

//================================================================================================================
void CTigerEyeDlg::OnOK() 
{
        //exit(0);

  m_tracker_interface.m_video_input.~VideoInput();

  exit(0);
        CDialog::OnOK();
}



void CTigerEyeDlg::OnButtonFramegrabber() 
{
  if(m_tracker_interface.m_video_input.InitializeFrameGrabber() != 0)
  {
    AfxMessageBox("Error initializing frame grabber");
  }

  ShowSampleInputFrame();

//  CString str;
//  Rect rect;
//  Figure fig;
//  for(int i=0; i < 4491; i++)
//  {
//    str.Format("d:/out1/img/%04d.jpg",i);
//    ImgBgr in, out;
//    Load(str, &in);
//    out.Reset(in.Width(), 320);
//    rect.top = 0;
//    rect.left = 0;
//    rect.right = in.Width()-1;
//    rect.bottom = 215;
//    Set(&out, Point(0,0), in, rect);
//    
//    rect.top = 315;
//    rect.bottom = 415;
//    Set(&out, Point(0,215), in, rect);
//    fig.Draw(out);
//    //Point pt = fig.GrabMouseClick();
//    str.Format("d:/out1/%04d.jpg",i);
//    Save(out, str, "jpg");
//  }
  
}














void CTigerEyeDlg::OnButtonLogOut() 
{
        // TODO: Add your control notification handler code here
  CTime t = CTime::GetCurrentTime();
  if (t.GetYear() <= EXPIRATION_YEAR_FOR_WRITING_RESULTS)
  {
    m_tracker_interface.m_tracker.WriteLogFile();    
  }
  else
  {
    AfxMessageBox("Expired. Please contact the author");
  }
}

void CTigerEyeDlg::OnButtonBgReset() 
{
        // TODO: Add your control notification handler code here
  m_tracker_interface.m_bg.Reset();     
}



void CTigerEyeDlg::OnButtonResetCrop() 
{
        // TODO: Add your control notification handler code here
  m_tracker_interface.m_video_input.ResetCropRect();    
}

void CTigerEyeDlg::OnButtonResetRoll() 
{
        // TODO: Add your control notification handler code here
  
m_tracker_interface.m_camroll.SetRollAngle(m_tracker_interface.m_video_input.GetWidth(),m_tracker_interface.m_video_input.GetHeight(),
 0.0f);
  
m_tracker_interface.m_video_input.SetAffine(m_tracker_interface.m_camroll.GetRollAffine());
  ShowSampleInputFrame();
}


void CTigerEyeDlg::OnButtonSlide() 
{
        // TODO: Add your control notification handler code here
  if(m_show_settings == false)
  {
    Rect rect;
    this->GetWindowRect(&rect);
    this->SetWindowPos(NULL, rect.left, rect.top, 270, rect.Height(), 
SWP_NOMOVE|SWP_NOZORDER);
    m_cmd_slide.SetWindowText(">>");
    m_show_settings = true;
  }
  else
  {
    Rect rect;
    this->GetWindowRect(&rect);
    this->SetWindowPos(NULL, rect.left, rect.top, 800, rect.Height(), 
SWP_NOMOVE|SWP_NOZORDER);
    m_cmd_slide.SetWindowText("<<");
    m_show_settings = false;
  }
}

void CTigerEyeDlg::OnCheckDebugImages() 
{
        // TODO: Add your control notification handler code here
  UpdateData();
  m_tracker_interface.m_tracker.ShowDebugImages(m_show_debug_images);
}

void CTigerEyeDlg::ShowSampleInputFrame()
{
  static Figure fig;
  ImgBgr bgr;
  int temp = -1;

  while(temp == -1 || temp == -2)
  {
          temp = m_tracker_interface.m_video_input.GetLatestImage(&bgr);
          fig.Draw(bgr);
  }

  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -1)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }
  
  if (m_tracker_interface.m_video_input.GetLatestImage(&bgr)  == -2)
  {
    AfxMessageBox("Error reading from frabegrabber");
    return;
  }

  if(m_tracker_interface.m_calib.Calibrated())
  {
    m_tracker_interface.m_calib.DrawDetectionZone(&bgr);
  }

  fig.Draw(bgr);
}

void CTigerEyeDlg::OnCheckSaveInfo() 
{
        // TODO: Add your control notification handler code here
  UpdateData(); 
}

void CTigerEyeDlg::OnCheckUseSaved() 
{
        // TODO: Add your control notification handler code here
  UpdateData();         
}

Other related posts: