<commit_before>#include "sfm.h"

using namespace cv;


void StructureFromMotion::newFrame(Mat frame_in)
{
  // this method is to be overwritten by the derived class
  // it is supposed to perform logic based on the frame id to determine if
  // the current frame is a keyframe
  std::cout << "base class method needs to be overwritten!" << std::endl;
}

void StructureFromMotion::featureTracker(Mat frame_in)
{
  // convert to intensity image
  Mat frame_gray;
  cvtColor(frame_in, frame_gray, CV_BGR2GRAY);


  if (features_cur.size()==0)
  {
    // no features have been initialized
    // populate the feature vector

    int max_points = 1000;
    double quality = 0.01;
    double min_dist = 10;
    Mat mask;
    int blockSize = 3;
    bool useHarris = false;
    double k = 0.04;
    goodFeaturesToTrack(frame_gray, features_cur, max_points, quality, min_dist, mask, blockSize, useHarris, k);

    drawFeatures(frame_in, features_cur);

    // remember the last iteration's frame
    frame_gray_old = frame_gray.clone();

    // REMEMBER TO PUSH BACK ON features_all !!!!!!!!!!!!!!!!!!!!!!!!
  }
  else
  {
    // features have been initialized, perform tracking, remove outliers (from all feature sets of all idx)
    // look into the "keep" command with masking

    // template matching
    int dim_win = 81;
    int dim_tem = 31;

    // rename features_cur to old
    std::vector<Point2f> features_new;
    for (int i=0; i<features_cur.size(); i++)
    {

      // create a window around from current image for the current feature
      int x1 = features_cur[i].x - (dim_win-1)/2;
      if (x1 < 0)
      {
        x1 = 0;
      }
      if ((x1+dim_win)>frame_in.cols)
      {
        x1 = frame_in.cols - dim_win;
      }
      int y1 = features_cur[i].y - (dim_win-1)/2;
      if (y1 < 0)
      {
        y1 = 0;
      }
      if ((y1+dim_win)>frame_in.rows)
      {
        y1 = frame_in.rows - dim_win;
      }
      // window established in next image
      Mat win = frame_gray(Rect(x1, y1, dim_win, dim_win));

      // create a template around features_cur[i], which is Point2f
      int x2 = features_cur[i].x - (dim_tem-1)/2;
      if (x2 < 0)
      {
        x2 = 0;
      }
      if ((x2+dim_tem)>frame_in.cols)
      {
        x2 = frame_in.cols - dim_tem;
      }
      int y2 = features_cur[i].y - (dim_tem-1)/2;
      if (y2 < 0)
      {
        y2 = 0;
      }
      if ((y2+dim_tem)>frame_in.rows)
      {
        y2 = frame_in.rows - dim_tem;
      }

      // window established in next image
      Mat tem = frame_gray_old(Rect(x2, y2, dim_tem, dim_tem));

      // match the current template of image 1 to window of image 2
      Mat output;
      matchTemplate(win, tem, output, TM_CCORR_NORMED);

      // normalize the intensity output
      normalize(output, output, 0, 1, NORM_MINMAX,  -1, Mat());

      // locate the position of highest correlation
      Point max_point;
      minMaxLoc(output, 0, 0, 0, &max_point, Mat());

      // represent the max point in original image coordinates
      max_point.x = max_point.x + dim_tem/2 + x1;
      max_point.y = max_point.y + dim_tem/2 + y1;

      features_new.push_back(max_point);

    } // end of looping through features


    drawFeatures(frame_in, features_new);
    features_cur.clear();
    features_cur = features_new;

    frame_gray_old = frame_gray.clone();<commit_msg>cleanup<commit_after>#include "sfm.h"

using namespace cv;


void StructureFromMotion::newFrame(Mat frame_in)
{
  // this method is to be overwritten by the derived class
  // it is supposed to perform logic based on the frame id to determine if
  // the current frame is a keyframe
  std::cout << "base class method needs to be overwritten!" << std::endl;
}

void StructureFromMotion::featureTracker(Mat frame_in)
{
  // convert to intensity image
  Mat frame_gray;
  cvtColor(frame_in, frame_gray, CV_BGR2GRAY);


  if (features_cur.size()==0)
  {
    // no features have been initialized
    // populate the feature vector

    int max_points = 1000;
    double quality = 0.01;
    double min_dist = 10;
    Mat mask;
    int blockSize = 3;
    bool useHarris = false;
    double k = 0.04;
    goodFeaturesToTrack(frame_gray, features_cur, max_points, quality, min_dist, mask, blockSize, useHarris, k);

    drawFeatures(frame_in, features_cur);

    // remember the last iteration's frame
    frame_gray_old = frame_gray.clone();

    // REMEMBER TO PUSH BACK ON features_all !!!!!!!!!!!!!!!!!!!!!!!!
  }
  else
  {
    // features have been initialized, perform tracking, remove outliers (from all feature sets of all idx)
    // look into the "keep" command with masking

    // template matching
    int dim_win = 81;
    int dim_tem = 31;

    // rename features_cur to old
    std::vector<Point2f> features_new;
    for (int i=0; i<features_cur.size(); i++)
    {

      // create a window from current image for the current feature
      int x1 = features_cur[i].x - (dim_win-1)/2;
      x1 = std::max(x1, 0);
      x1 = std::min(x1, frame_in.cols-dim_win);
      int y1 = features_cur[i].y - (dim_win-1)/2;
      y1 = std::max(y1, 0);
      y1 = std::min(y1, frame_in.rows-dim_win);
      Mat win = frame_gray(Rect(x1, y1, dim_win, dim_win));

      // create a template from previous image for the current feature
      int x2 = features_cur[i].x - (dim_tem-1)/2;
      x2 = std::max(x2, 0);
      x2 = std::min(x2, frame_in.cols-dim_tem);
      int y2 = features_cur[i].y - (dim_tem-1)/2;
      y2 = std::max(y2, 0);
      y2 = std::min(y2, frame_in.rows-dim_tem);
      Mat tem = frame_gray_old(Rect(x2, y2, dim_tem, dim_tem));

      // THIS ALLOWS OFF-CENTER FEATURES!!! which makes the max_point logic
      // wrong lines 89 and 90

      // match the current template of image 1 to window of image 2
      Mat output;
      matchTemplate(win, tem, output, TM_CCORR_NORMED);

      // normalize the intensity output
      normalize(output, output, 0, 1, NORM_MINMAX,  -1, Mat());

      // locate the position of highest correlation
      Point max_point;
      minMaxLoc(output, 0, 0, 0, &max_point, Mat());

      // represent the max point in original image coordinates
      max_point.x = max_point.x + dim_tem/2 + x1;
      max_point.y = max_point.y + dim_tem/2 + y1;

      features_new.push_back(max_point);

    } // end of looping through features


    drawFeatures(frame_in, features_new);
    features_cur.clear();
    features_cur = features_new;

    frame_gray_old = frame_gray.clone();
{'fraction_non_alphanumeric': 0.11542610571736785, 'fraction_numerical': 0.022653721682847898, 'mean_word_length': 2.5915882678472606, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1617045', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2135, 'n_words': 638}