<commit_before>#include <opencv2/highgui.hpp>  // for imshow
#include <opencv2/imgproc.hpp>  // for blur, dilation, erosion
#include <opencv2/video.hpp>    // for backgroundsubtractor
#include <opencv2/videoio.hpp>  // for videocapture

#include <iostream>

void show(std::string title, const cv::Mat& image) {
  cv::imshow(title.c_str(), image);
  char c = static_cast<char>(cv::waitKey(1));
  if (c == 27) exit(0);
}

const auto learning_rate_divider = 10000;

void subtract(cv::Ptr<cv::BackgroundSubtractor> pMOG2, cv::Mat* image,
              cv::Mat* background, int learning_rate) {
  auto rate = learning_rate / static_cast<double>(learning_rate_divider);
  if (learning_rate <= 0) {
    rate = -1.0;
  }
  pMOG2->apply(*image, *background, rate);
}

void blur(cv::Mat* input_output, int blur_size) {
  if (blur_size <= 0) {
    return;
  }
  cv::blur(*input_output, *input_output, cv::Size(blur_size, blur_size));
}

void erode(cv::Mat* input_output, int erosion_size) {
  if (erosion_size <= 0) {
    return;
  }
  int erosion_type = cv::MORPH_RECT;
  cv::Mat element = cv::getStructuringElement(
      erosion_type, cv::Size(2 * erosion_size, 2 * erosion_size),
      cv::Point(erosion_size, erosion_size));
  cv::erode(*input_output, *input_output, element);
}

void dilate(cv::Mat* input_output, int dilation_size) {
  if (dilation_size <= 0) {
    return;
  }
  int dilation_type = cv::MORPH_RECT;
  cv::Mat element = cv::getStructuringElement(
      dilation_type, cv::Size(2 * dilation_size, 2 * dilation_size),
      cv::Point(dilation_size, dilation_size));
  cv::dilate(*input_output, *input_output, element);
}

cv::Point2f findContourMiddle(const std::vector<cv::Point>& contour) {
  /// Get the moments
  cv::Moments moments = cv::moments(contour, false);
  if (moments.m00 == 0) {
    return cv::Point2f(-1, -1);
  }

  ///  Get the mass centers:
  cv::Point2f mc =
      cv::Point2f(moments.m10 / moments.m00, moments.m01 / moments.m00);
  return mc;
}

void findContours(const cv::Mat& image, int camera_number) {
  cv::Mat canny_output;
  std::vector<std::vector<cv::Point> > contours;
  std::vector<cv::Vec4i> hierarchy;

  /// Find contours
  cv::findContours(image, contours, hierarchy, cv::RETR_TREE,
                   cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

  if (contours.size() < 1) {
    return;
  }

  cv::Mat drawing = cv::Mat::zeros(image.size(), CV_8UC3);

  int index = 0;
  double biggest_area = 0.0;
  for (size_t i = 0; i < contours.size(); i++) {
    auto area = cv::contourArea(contours[i]);
    if (area > biggest_area) {
      index = i;
      biggest_area = area;
    }
  }

  auto middle = findContourMiddle(contours[index]);
  std::cout << camera_number << ":" << static_cast<int>(middle.x) << ","
            << static_cast<int>(middle.y) << std::endl;

  cv::Scalar color = cv::Scalar(255, 255, 255);
  cv::drawContours(drawing, contours, index, color, 2, 8, hierarchy, 0,
                   cv::Point());
  show("contours", drawing);
}

int thread(int camera_number) {
  cv::Mat background;                       // background image
  cv::Ptr<cv::BackgroundSubtractor> pMOG2;  // MOG2 Background subtractor
  int blur_size = 10;
  int erosion_size = 10;
  int dilation_size = 10;
  int learning_rate = 0;

  const int bg_history = 500;
  const int threshold = 16;          // default value
  const bool detectShadows = false;  // non-default

  pMOG2 =
      cv::createBackgroundSubtractorMOG2(bg_history, threshold, detectShadows);

  auto capture = cv::VideoCapture(camera_number);
  std::string control_window_name = "control" + std::to_string(camera_number);
  cv::namedWindow(control_window_name);
  cv::createTrackbar("blur", control_window_name, &blur_size, 20);
  cv::createTrackbar("erosion", control_window_name, &erosion_size, 20);
  cv::createTrackbar("dilation", control_window_name, &dilation_size, 20);<commit_msg>Default learning rate of 5<commit_after>#include <iostream>
#include <opencv2/highgui.hpp>  // for imshow
#include <opencv2/imgproc.hpp>  // for blur, dilation, erosion
#include <opencv2/video.hpp>    // for backgroundsubtractor
#include <opencv2/videoio.hpp>  // for videocapture

void show(std::string title, const cv::Mat& image) {
  cv::imshow(title.c_str(), image);
  char c = static_cast<char>(cv::waitKey(1));
  if (c == 27) exit(0);
}

const auto learning_rate_divider = 10000;

void subtract(cv::Ptr<cv::BackgroundSubtractor> pMOG2, cv::Mat* image,
              cv::Mat* background, int learning_rate) {
  auto rate = learning_rate / static_cast<double>(learning_rate_divider);
  if (learning_rate <= 0) {
    rate = -1.0;
  }
  pMOG2->apply(*image, *background, rate);
}

void blur(cv::Mat* input_output, int blur_size) {
  if (blur_size <= 0) {
    return;
  }
  cv::blur(*input_output, *input_output, cv::Size(blur_size, blur_size));
}

void erode(cv::Mat* input_output, int erosion_size) {
  if (erosion_size <= 0) {
    return;
  }
  int erosion_type = cv::MORPH_RECT;
  cv::Mat element = cv::getStructuringElement(
      erosion_type, cv::Size(2 * erosion_size, 2 * erosion_size),
      cv::Point(erosion_size, erosion_size));
  cv::erode(*input_output, *input_output, element);
}

void dilate(cv::Mat* input_output, int dilation_size) {
  if (dilation_size <= 0) {
    return;
  }
  int dilation_type = cv::MORPH_RECT;
  cv::Mat element = cv::getStructuringElement(
      dilation_type, cv::Size(2 * dilation_size, 2 * dilation_size),
      cv::Point(dilation_size, dilation_size));
  cv::dilate(*input_output, *input_output, element);
}

cv::Point2f findContourMiddle(const std::vector<cv::Point>& contour) {
  /// Get the moments
  cv::Moments moments = cv::moments(contour, false);
  if (moments.m00 == 0) {
    return cv::Point2f(-1, -1);
  }

  ///  Get the mass centers:
  cv::Point2f mc =
      cv::Point2f(moments.m10 / moments.m00, moments.m01 / moments.m00);
  return mc;
}

void findContours(const cv::Mat& image, int camera_number) {
  cv::Mat canny_output;
  std::vector<std::vector<cv::Point> > contours;
  std::vector<cv::Vec4i> hierarchy;

  /// Find contours
  cv::findContours(image, contours, hierarchy, cv::RETR_TREE,
                   cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

  if (contours.size() < 1) {
    return;
  }

  cv::Mat drawing = cv::Mat::zeros(image.size(), CV_8UC3);

  int index = 0;
  double biggest_area = 0.0;
  for (size_t i = 0; i < contours.size(); i++) {
    auto area = cv::contourArea(contours[i]);
    if (area > biggest_area) {
      index = i;
      biggest_area = area;
    }
  }

  auto middle = findContourMiddle(contours[index]);
  std::cout << camera_number << ":" << static_cast<int>(middle.x) << ","
            << static_cast<int>(middle.y) << std::endl;

  cv::Scalar color = cv::Scalar(255, 255, 255);
  cv::drawContours(drawing, contours, index, color, 2, 8, hierarchy, 0,
                   cv::Point());
  show("contours", drawing);
}

int thread(int camera_number) {
  cv::Mat background;                       // background image
  cv::Ptr<cv::BackgroundSubtractor> pMOG2;  // MOG2 Background subtractor
  int blur_size = 10;
  int erosion_size = 10;
  int dilation_size = 10;
  int learning_rate = 50;

  const int bg_history = 500;
  const int threshold = 16;          // default value
  const bool detectShadows = false;  // non-default

  pMOG2 =
      cv::createBackgroundSubtractorMOG2(bg_history, threshold, detectShadows);

  auto capture = cv::VideoCapture(camera_number);
  std::string control_window_name = "control" + std::to_string(camera_number);
  cv::namedWindow(control_window_name);
  cv::createTrackbar("blur", control_window_name, &blur_size, 20);
  cv::createTrackbar("erosion", control_window_name, &erosion_size, 20);
  cv::createTrackbar("dilation", control_window_name, &dilation_size, 20);
{'fraction_non_alphanumeric': 0.1510597302504817, 'fraction_numerical': 0.022350674373795763, 'mean_word_length': 4.049286640726329, 'pattern_counts': {'":': 2, '<': 69, '<?xml version=': 0, '>': 37, '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': '5429085', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2731, 'n_words': 585}