<commit_before>// ========================================================================== //
// This file is part of DO++, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2013 <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //

#include <DO/Features.hpp>
#include <DO/Graphics.hpp>
#include <DO/ImageProcessing.hpp>


using namespace DO;
using namespace std;


const bool draw_feature_center_only = false;
const Rgb8& c = Cyan8;


void check_affine_adaptation(const Image<unsigned char>& image,
                             const OERegion& f)
{
  int w = image.width();
  int h = image.height();
  display(image);
  f.draw(Blue8);

  Image<float> float_image(image.convert<float>());

  int r = 100;
  int patchSz = 2*r;
  Image<float> patch(w, h);
  patch.array().fill(0.f);

  OERegion rg(f);
  rg.center().fill(patchSz/2.f);
  rg.orientation() = 0.f;
  rg.shape_matrix() = Matrix2f::Identity()*4.f / (r*r);

  Matrix3d A(f.affinity().cast<double>());
  cout << "A=\n" << A << endl;

  for (int y = 0; y < patchSz; ++y)
  {
    float v = 2*float(y-r)/r;
    for (int x = 0; x < patchSz; ++x)
    {
      float u = 2*float(x-r)/r;
      Point3d pp(u, v, 1.);
      pp = A*pp;

      Point2d p;
      p << pp(0), pp(1);

      if (p.x() < 0 || p.x() >= w || p.y() < 0 || p.y() >= h)
        continue;

      patch(x,y) = interpolate(float_image, p);
    }
  }

  Window w1 = active_window();
  Window w2 = create_window(patchSz, patchSz);
  set_active_window(w2);
  set_antialiasing();
  display(patch);
  rg.draw(Blue8);
  millisleep(1000);
  close_window(w2);

  millisleep(40);
  set_active_window(w1);
}

void read_features(const Image<unsigned char>& image,
                   const string& filepath)
{
  cout << "Reading DoG features... " << endl;
  vector<OERegion> features;
  DescriptorMatrix<float> descriptors;

  cout << "Reading keypoints..." << endl;
  read_keypoints(features, descriptors, filepath);

  for (int i = 0; i < 10; ++i)
    check_affine_adaptation(image, features[i]);
<commit_msg>MAINT: simplify implementation of code sample.<commit_after>// ========================================================================== //
// This file is part of DO++, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2013 <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //

#include <DO/Features.hpp>
#include <DO/Graphics.hpp>
#include <DO/ImageProcessing.hpp>


using namespace DO;
using namespace std;


const bool draw_feature_center_only = false;
const Rgb8& c = Cyan8;


void check_affine_adaptation(const Image<unsigned char>& image,
                             const OERegion& f)
{
  int w = image.width();
  int h = image.height();
  display(image);
  f.draw(Blue8);

  Image<float> flt_image(image.convert<float>());

  float r = 100;
  float patchSz = 2*r;
  Image<float> patch(w, h);
  patch.array().fill(0.f);

  OERegion rg(f);
  rg.center().fill(patchSz/2.f);
  rg.orientation() = 0.f;
  rg.shape_matrix() = Matrix2f::Identity()*4.f / (r*r);

  Matrix3d A(f.affinity().cast<double>());
  cout << "A=\n" << A << endl;

  for (int y = 0; y < patchSz; ++y)
  {
    float v = 2*(y-r)/r;
    for (int x = 0; x < patchSz; ++x)
    {
      float u = 2*(x-r)/r;
      Point3d pp(u, v, 1.);
      pp = A*pp;

      Point2d p;
      p << pp(0), pp(1);

      if (p.x() < 0 || p.x() >= w || p.y() < 0 || p.y() >= h)
        continue;

      patch(x,y) = static_cast<float>(interpolate(flt_image, p));
    }
  }

  Window w1 = active_window();
  Window w2 = create_window(static_cast<int>(patchSz),
                            static_cast<int>(patchSz));
  set_active_window(w2);
  set_antialiasing();
  display(patch);
  rg.draw(Blue8);
  millisleep(1000);
  close_window(w2);

  millisleep(40);
  set_active_window(w1);
}

void read_features(const Image<unsigned char>& image,
                   const string& filepath)
{
  cout << "Reading DoG features... " << endl;
  vector<OERegion> features;
  DescriptorMatrix<float> descriptors;

  cout << "Reading keypoints..." << endl;
  read_keypoints(features, descriptors, filepath);

  for (int i = 0; i < 10; ++i)
    check_affine_adaptation(image, features[i]);

{'fraction_non_alphanumeric': 0.21966304116016208, 'fraction_numerical': 0.020046918319471103, 'mean_word_length': 3.13215859030837, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 38, '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': '5921', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1597, 'n_words': 425}