<commit_before>// __BEGIN_LICENSE__
// 
// Copyright (C) 2006 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA).  All Rights Reserved.
// 
// Copyright 2006 <NAME>. All rights reserved.
// 
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3.  The NOSA has been approved by the Open Source
// Initiative.  See the file COPYING at the top of the distribution
// directory tree for the complete NOSA document.
// 
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
// 
// __END_LICENSE__

/// \file WeightedHistogram.h
/// 
/// Functions for constructing histograms from pixels in a given image.
/// 
#ifndef _WEIGHTED_HISTOGRAM_H_
#define _WEIGHTED_HISTOGRAM_H_

#include <vw/Image/ImageView.h>
#include <vw/Image/Filter.h>
#include <vw/Image/Manipulation.h>
#include <vector>
#include <algorithm>
#include <math.h>

// Yet another magic number from Lowe.
#define INTEREST_POINT_MODE_THRESHOLD (0.8)
#define INTEREST_POINT_MODE_LIMIT (5)

namespace vw { namespace ip {

/// Construct histogram from pixels in given image
template <class ViewT1, class ViewT2>
inline
void weighted_histogram(ImageViewBase<ViewT1> const& val_image,
                        ImageViewBase<ViewT2> const& weight_image,
                        std::vector<float>& histo,
                        float min, float max, unsigned n_bins) {
  // Make sure images are same size
  assert( val_image.impl().cols() == weight_image.impl().cols() );
  assert( val_image.impl().rows() == weight_image.impl().rows() );

  histo.resize(n_bins);
  std::fill(histo.begin(),histo.end(),0.0);
  float s = float(n_bins-1)/(max-min);

  typename ViewT2::iterator weight_iter = weight_image.begin();
  for (typename ViewT1::iterator iter = val_image.begin(); iter != val_image.end(); ++iter, ++weight_iter) {
    int index = int(0.5+s*(float(*iter) - min));
    if (index>=0 && (unsigned)index<n_bins) histo[index] += *weight_iter;
  }
}

/// Construct a 2D Gaussian kernel. This is templatized so that kernel
/// could be any 2D array that provides the appropriate accessors and
/// resize method.  Note that for Gaussian filtering of images, the
/// separable method is preferred with two 1D kernels.
/// The kernel size may be specified; if it is not then the default
/// value of zero indicates the kernel size should be computed from
/// sigma.
template <class KernelT>
int make_gaussian_kernel_2d( KernelT& kernel, float sigma, int usewidth=0 ) {
  std::vector<float> kernel_1d;
  generate_gaussian_kernel(kernel_1d, sigma, usewidth);
  int kerwidth = kernel_1d.size();
  kernel.set_size(kerwidth,kerwidth);
  int halfwidth = (kerwidth-1)/2;
  
  // Put in Gaussian values
  for (int j=0; j<kerwidth; j++)
    for (int i=0; i<kerwidth; i++)
      kernel(i,j) = kernel_1d[i] * kernel_1d[j];

  return 0;
}

/// Compute (gaussian weight) * (edge magnitude) kernel.
template <class ViewT1, class ViewT2>
void weighted_magnitude(ImageViewBase<ViewT1>& weight,
                        ImageViewBase<ViewT2> const& mag_image,
                        int x, int y, float sigma, int usewidth=0) {
  ImageView<float> kernel;
  make_gaussian_kernel_2d(kernel, sigma, usewidth);
  int width = kernel.rows();
  int halfwidth = (width - 1) / 2;

  weight = crop(mag_image, x - halfwidth, y - halfwidth, width,
                width) * kernel;
}

/// Adds a sample to the orientation histogram using linear
/// interpolation.
inline
void orientation_interpolate(std::vector<float>& histo,
                             unsigned n_bins, float ori, float mag) {
  float bin_size = (float)2 * M_PI / (float)n_bins;
  float ratio = (ori - bin_size) / bin_size;
  int n = (int)floor((ori + M_PI) / bin_size);
  histo[n] += ((float)1 - ratio) * mag;
  histo[(n+1) % n_bins] += ratio * mag;
}

/// Construct orientation histogram for a region
template <class ViewT1, class ViewT2>
void orientation_histogram(ImageViewBase<ViewT1> const& ori_image,
                           ImageViewBase<ViewT2> const& mag_image,
                           std::vector<float>& histo, int x, int y,
                           float sigma, unsigned n_bins) {
  ImageView<float> weight;
  weighted_magnitude(weight, mag_image, x, y, sigma);
  int width = weight.rows();<commit_msg>Changed casting to remove compiler warning<commit_after>// __BEGIN_LICENSE__
// 
// Copyright (C) 2006 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA).  All Rights Reserved.
// 
// Copyright 2006 <NAME> University. All rights reserved.
// 
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3.  The NOSA has been approved by the Open Source
// Initiative.  See the file COPYING at the top of the distribution
// directory tree for the complete NOSA document.
// 
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
// 
// __END_LICENSE__

/// \file WeightedHistogram.h
/// 
/// Functions for constructing histograms from pixels in a given image.
/// 
#ifndef _WEIGHTED_HISTOGRAM_H_
#define _WEIGHTED_HISTOGRAM_H_

#include <vw/Image/ImageView.h>
#include <vw/Image/Filter.h>
#include <vw/Image/Manipulation.h>
#include <vector>
#include <algorithm>
#include <math.h>

// Yet another magic number from Lowe.
#define INTEREST_POINT_MODE_THRESHOLD (0.8)
#define INTEREST_POINT_MODE_LIMIT (5)

namespace vw { namespace ip {

/// Construct histogram from pixels in given image
template <class ViewT1, class ViewT2>
inline
void weighted_histogram(ImageViewBase<ViewT1> const& val_image,
                        ImageViewBase<ViewT2> const& weight_image,
                        std::vector<float>& histo,
                        float min, float max, unsigned n_bins) {
  // Make sure images are same size
  assert( val_image.impl().cols() == weight_image.impl().cols() );
  assert( val_image.impl().rows() == weight_image.impl().rows() );

  histo.resize(n_bins);
  std::fill(histo.begin(),histo.end(),0.0);
  float s = float(n_bins-1)/(max-min);

  typename ViewT2::iterator weight_iter = weight_image.begin();
  for (typename ViewT1::iterator iter = val_image.begin(); iter != val_image.end(); ++iter, ++weight_iter) {
    int index = int(0.5+s*(float(*iter) - min));
    if (index>=0 && (unsigned)index<n_bins) histo[index] += *weight_iter;
  }
}

/// Construct a 2D Gaussian kernel. This is templatized so that kernel
/// could be any 2D array that provides the appropriate accessors and
/// resize method.  Note that for Gaussian filtering of images, the
/// separable method is preferred with two 1D kernels.
/// The kernel size may be specified; if it is not then the default
/// value of zero indicates the kernel size should be computed from
/// sigma.
template <class KernelT>
int make_gaussian_kernel_2d( KernelT& kernel, float sigma, int usewidth=0 ) {
  std::vector<float> kernel_1d;
  generate_gaussian_kernel(kernel_1d, sigma, usewidth);
  int kerwidth = kernel_1d.size();
  kernel.set_size(kerwidth,kerwidth);
  int halfwidth = (kerwidth-1)/2;
  
  // Put in Gaussian values
  for (int j=0; j<kerwidth; j++)
    for (int i=0; i<kerwidth; i++)
      kernel(i,j) = kernel_1d[i] * kernel_1d[j];

  return 0;
}

/// Compute (gaussian weight) * (edge magnitude) kernel.
template <class ViewT1, class ViewT2>
void weighted_magnitude(ImageViewBase<ViewT1>& weight,
                        ImageViewBase<ViewT2> const& mag_image,
                        int x, int y, float sigma, int usewidth=0) {
  ImageView<float> kernel;
  make_gaussian_kernel_2d(kernel, sigma, usewidth);
  int width = kernel.rows();
  int halfwidth = (width - 1) / 2;

  weight = crop(mag_image, x - halfwidth, y - halfwidth, width,
                width) * kernel;
}

/// Adds a sample to the orientation histogram using linear
/// interpolation.
inline
void orientation_interpolate(std::vector<float>& histo,
                             unsigned n_bins, float ori, float mag) {
  float bin_size = (float)(2 * M_PI / n_bins);
  float ratio = (ori - bin_size) / bin_size;
  int n = (int)floor((ori + M_PI) / bin_size);
  histo[n] += ((float)1 - ratio) * mag;
  histo[(n+1) % n_bins] += ratio * mag;
}

/// Construct orientation histogram for a region
template <class ViewT1, class ViewT2>
void orientation_histogram(ImageViewBase<ViewT1> const& ori_image,
                           ImageViewBase<ViewT2> const& mag_image,
                           std::vector<float>& histo, int x, int y,
                           float sigma, unsigned n_bins) {
  ImageView<float> weight;
  weighted_magnitude(weight, mag_image, x, y, sigma);
  int width = weight.rows();
{'fraction_non_alphanumeric': 0.11261119832548404, 'fraction_numerical': 0.011512297226582941, 'mean_word_length': 3.3495675921711423, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 51, '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': '5437947', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2898, 'n_words': 979}