text:
// Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com>.
// Copyright (C) 2004 The Trustees of Indiana University

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//   Authors: Douglas Gregor
//            Andrew Lumsdaine

// Message Passing Interface 1.1 -- Section 4.9.1. Reduce
#ifndef BOOST_MPI_REDUCE_HPP
#define BOOST_MPI_REDUCE_HPP

#include <boost/mpicxx/exception.hpp>
#include <boost/mpicxx/datatype.hpp>

// For (de-)serializing sends and receives
#include <boost/mpicxx/packed_oarchive.hpp>
#include <boost/mpicxx/packed_iarchive.hpp>

// For packed_[io]archive sends and receives
#include <boost/mpicxx/detail/point_to_point.hpp>

#include <boost/mpicxx/communicator.hpp>
#include <boost/mpicxx/environment.hpp>
#include <boost/mpicxx/detail/computation_tree.hpp>
#include <boost/mpicxx/operations.hpp>
#include <algorithm>
#include <exception>
#include <boost/assert.hpp>
#include <boost/scoped_array.hpp>

namespace boost { namespace mpicxx {

/************************************************************************
 * Implementation details                                               *
 ************************************************************************/
namespace detail {
  /**********************************************************************
   * Simple reduction with MPI_Reduce                                   *
   **********************************************************************/
  // We are reducing at the root for a type that has an associated MPI
  // datatype and operation, so we'll use MPI_Reduce directly.
  template<typename T, typename Op>
  void
  reduce_impl(const communicator& comm, const T* in_values, int n,
              T* out_values, Op /*op*/, int root, mpl::true_ /*is_mpi_op*/,
              mpl::true_/*is_mpi_datatype*/)
  {
    BOOST_MPI_CHECK_RESULT(MPI_Reduce,
                           (const_cast<T*>(in_values), out_values, n,
                            boost::mpicxx::get_mpi_datatype<T>(*in_values),
                            (is_mpi_op<Op, T>::op()), root, comm));
  }

  // We are reducing to the root for a type that has an associated MPI
  // datatype and operation, so we'll use MPI_Reduce directly.
  template<typename T, typename Op>
  void
  reduce_impl(const communicator& comm, const T* in_values, int n, Op /*op*/,
              int root, mpl::true_ /*is_mpi_op*/, mpl::true_/*is_mpi_datatype*/)
  {
    BOOST_MPI_CHECK_RESULT(MPI_Reduce,
                           (const_cast<T*>(in_values), 0, n,
                            boost::mpicxx::get_mpi_datatype<T>(*in_values),
                            (is_mpi_op<Op, T>::op()), root, comm));
  }

  /**********************************************************************
   * User-defined reduction with MPI_Reduce                             *
   **********************************************************************/

  // We are reducing at the root for a type that has an associated MPI
  // datatype but with a custom operation. We'll use MPI_Reduce
  // directly, but we'll need to create an MPI_Op manually.
  template<typename T, typename Op>
  void
  reduce_impl(const communicator& comm, const T* in_values, int n,
              T* out_values, Op op, int root, mpl::false_ /*is_mpi_op*/,
              mpl::true_/*is_mpi_datatype*/)
  {
    user_op<Op, T> mpi_op(op);
    BOOST_MPI_CHECK_RESULT(MPI_Reduce,
                           (const_cast<T*>(in_values), out_values, n,
                            boost::mpicxx::get_mpi_datatype<T>(*in_values),
                            mpi_op.get_mpi_op(), root, comm));
  }

  // We are reducing to the root for a type that has an associated MPI
  // datatype but with a custom operation. We'll use MPI_Reduce
  // directly, but we'll need to create an MPI_Op manually.
  template<typename T, typename Op>
  void
  reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
              int root, mpl::false_/*is_mpi_op*/, mpl::true_/*is_mpi_datatype*/)
  {
    user_op<Op, T> mpi_op(op);
    BOOST_MPI_CHECK_RESULT(MPI_Reduce,
                           (const_cast<T*>(in_values), 0, n,
                            boost::mpicxx::get_mpi_datatype<T>(*in_values),
                            mpi_op.get_mpi_op(), root, comm));
  }

  /**********************************************************************
   * User-defined, tree-based reduction for non-MPI data types          *
   **********************************************************************/

  // Commutative reduction
  template<typename T, typename Op>
  void
  tree_reduce_impl(const communicator& comm, const T* in_values, int n,
                   T* out_values, Op op, int root,
                   mpl::true_ /*is_commutative*/)
  {
    std::copy(in_values, in_values + n, out_values);

    int size = comm.size();
    int rank = comm.rank();

    // The computation tree we will use.
    detail::computation_tree tree(rank, size, root);

    int tag = environment::collectives_tag();

    MPI_Status status;
    int children = 0;
    for (int child = tree.child_begin();
         children < tree.branching_factor() && child != root;
         ++children, child = (child + 1) % size) {
      // Receive archive
      packed_iarchive ia(comm);
      detail::packed_archive_recv(comm, child, tag, ia, status);

      T incoming;
      for (int i = 0; i < n; ++i) {
        ia >> incoming;
        out_values[i] = op(out_values[i], incoming);
      }
    }

    // For non-roots, send the result to the parent.
    if (tree.parent() != rank) {
      packed_oarchive oa(comm);
      for (int i = 0; i < n; ++i)
        oa << out_values[i];
      detail::packed_archive_send(comm, tree.parent(), tag, oa);
    }
  }

  // Commutative reduction from a non-root.
  template<typename T, typename Op>
  void
  tree_reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
                   int root, mpl::true_ /*is_commutative*/)
  {
    scoped_array<T> results(new T[n]);
    detail::tree_reduce_impl(comm, in_values, n, results.get(), op, root,
                             mpl::true_());
  }

  // Non-commutative reduction
  template<typename T, typename Op>
  void
  tree_reduce_impl(const communicator& comm, const T* in_values, int n,
                   T* out_values, Op op, int root,
                   mpl::false_ /*is_commutative*/)
  {
    int tag = environment::collectives_tag();

    int left_child = root / 2;
    int right_child = (root + comm.size()) / 2;

    MPI_Status status;
    if (left_child != root) {
      // Receive value from the left child and merge it with the value
      // we had incoming.
      packed_iarchive ia(comm);
      detail::packed_archive_recv(comm, left_child, tag, ia, status);
      T incoming;
      for (int i = 0; i < n; ++i) {
        ia >> incoming;
        out_values[i] = op(incoming, in_values[i]);
      }
    } else {
      // There was no left value, so copy our incoming value.
      std::copy(in_values, in_values + n, out_values);
    }

    if (right_child != root) {
      // Receive value from the right child and merge it with the
      // value we had incoming.
      packed_iarchive ia(comm);
      detail::packed_archive_recv(comm, right_child, tag, ia, status);
      T incoming;
      for (int i = 0; i < n; ++i) {
        ia >> incoming;
        out_values[i] = op(out_values[i], incoming);
      }
    }
  }

  // Non-commutative reduction from a non-root.
  template<typename T, typename Op>
  void
  tree_reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
                   int root, mpl::false_ /*is_commutative*/)
  {
    int size = comm.size();
    int rank = comm.rank();

    int tag = environment::collectives_tag();

    // Determine our parents and children in the commutative binary
    // computation tree.
    int grandparent = root;
    int parent = root;
    int left_bound = 0;
    int right_bound = size;
    int left_child, right_child;
    do {
      left_child = (left_bound + parent) / 2;
      right_child = (parent + right_bound) / 2;

      if (rank < parent) {
        // Go left.
        grandparent = parent;
        right_bound = parent;
        parent = left_child;
      } else if (rank > parent) {
        // Go right.
        grandparent = parent;
        left_bound = parent + 1;
        parent = right_child;
      } else {
        // We've found the parent
        break;
      }
    } while (true);

    // Our parent is the grandparent of our children. This is a slight
    // abuse of notation, but it makes the send-to-parent below make
    // more sense.
    parent = grandparent;

    MPI_Status status;
    scoped_array<T> out_values(new T[n]);
    if (left_child != rank) {
      // Receive value from the left child and merge it with the value
      // we had incoming.
      packed_iarchive ia(comm);
      detail::packed_archive_recv(comm, left_child, tag, ia, status);
      T incoming;
      for (int i = 0; i < n; ++i) {
        ia >> incoming;
        out_values[i] = op(incoming, in_values[i]);
      }
    } else {
      // There was no left value, so copy our incoming value.
      std::copy(in_values, in_values + n, out_values.get());
    }

    if (right_child != rank) {
      // Receive value from the right child and merge it with the
      // value we had incoming.
      packed_iarchive ia(comm);
      detail::packed_archive_recv(comm, right_child, tag, ia, status);
      T incoming;
      for (int i = 0; i < n; ++i) {
        ia >> incoming;
        out_values[i] = op(out_values[i], incoming);
      }
    }

    // Send the combined value to our parent.
    packed_oarchive oa(comm);
    for (int i = 0; i < n; ++i)
      oa << out_values[i];
    detail::packed_archive_send(comm, parent, tag, oa);
  }

  // We are reducing at the root for a type that has no associated MPI
  // datatype and operation, so we'll use a simple tree-based
  // algorithm.
  template<typename T, typename Op>
  void
  reduce_impl(const communicator& comm, const T* in_values, int n,
              T* out_values, Op op, int root, mpl::false_ /*is_mpi_op*/,
              mpl::false_ /*is_mpi_datatype*/)
  {
    detail::tree_reduce_impl(comm, in_values, n, out_values, op, root,
                             is_commutative<Op, T>());
  }

  // We are reducing to the root for a type that has no associated MPI
  // datatype and operation, so we'll use a simple tree-based
  // algorithm.
  template<typename T, typename Op>
  void
  reduce_impl(const communicator& comm, const T* in_values, int n, Op op,
              int root, mpl::false_ /*is_mpi_op*/,
              mpl::false_ /*is_mpi_datatype*/)
  {
    detail::tree_reduce_impl(comm, in_values, n, op, root,
                             is_commutative<Op, T>());
  }
} // end namespace detail

template<typename T, typename Op>
void
reduce(const communicator& comm, const T* in_values, int n, T* out_values,
       Op op, int root)
{
  if (comm.rank() == root)
    detail::reduce_impl(comm, in_values, n, out_values, op, root,
                        is_mpi_op<Op, T>(), is_mpi_datatype<T>());
  else
    detail::reduce_impl(comm, in_values, n, op, root,
                        is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}

template<typename T, typename Op>
void
reduce(const communicator& comm, const T* in_values, int n, Op op, int root)
{
  BOOST_ASSERT(comm.rank() != root);

  detail::reduce_impl(comm, in_values, n, op, root,
                      is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}

template<typename T, typename Op>
void
reduce(const communicator & comm, std::vector<T> const & in_values, Op op,
       int root)
{
  reduce(comm, &in_values.front(), in_values.size(), op, root);
}

template<typename T, typename Op>
void
reduce(const communicator & comm, std::vector<T> const & in_values,
       std::vector<T> & out_values, Op op, int root)
{
  if (root == comm.rank()) out_values.resize(in_values.size());
  reduce(comm, &in_values.front(), in_values.size(), &out_values.front(), op,
         root);
}

template<typename T, typename Op>
void
reduce(const communicator& comm, const T& in_value, T& out_value, Op op,
       int root)
{
  if (comm.rank() == root)
    detail::reduce_impl(comm, &in_value, 1, &out_value, op, root,
                        is_mpi_op<Op, T>(), is_mpi_datatype<T>());
  else
    detail::reduce_impl(comm, &in_value, 1, op, root,
                        is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}

template<typename T, typename Op>
void reduce(const communicator& comm, const T& in_value, Op op, int root)
{
  BOOST_ASSERT(comm.rank() != root);

  detail::reduce_impl(comm, &in_value, 1, op, root,
                      is_mpi_op<Op, T>(), is_mpi_datatype<T>());
}

} } // end namespace boost::mpicxx

#endif // BOOST_MPI_REDUCE_HPP

max_stars_repo_name: aminiussi/mpicxx

max_issues_repo_name: aminiussi/mpicxx

max_forks_repo_name: aminiussi/mpicxx


text:
//
// Created by erik on 12/6/17.
//

#ifndef BRANCHEDFLOWSIM_USAGE_HPP
#define BRANCHEDFLOWSIM_USAGE_HPP

#include <sstream>
#include <iterator>
#include <boost/algorithm/string/case_conv.hpp>
#include "spec.hpp"
#include "argument_set.hpp"

namespace args
{
    // -----------------------------------------------------------------------------------------------------------------
    //                                               Usage String
    // -----------------------------------------------------------------------------------------------------------------

    /*! prints the usage string of `args` to `stream`.
     *
     * The usage string consists of the (optional) program name (`name` of `args`) followed by a short specification
     * of the arguments and their types. Positional arguments are marked by an uppercase version of there name whereas
     * named arguments are described by a sequence of `name DATATYPE`. Optional arguments are put in brackets and multi
     * values are marked by an ellipsis `...`.
     * The purpose of the usage string is to show the syntax of passing arguments.
     */

    void usage_string(std::ostream& stream, const ArgumentSet& args);

    /// convenience function to get a usage_string in a std::string.
    /// \sa void usage_string(std::ostream& stream, const ArgumentSet& args)
    std::string usage_string(const ArgumentSet& args);

    // -----------------------------------------------------------------------------------------------------------------
    //                                               Help Message
    // -----------------------------------------------------------------------------------------------------------------

    /*! prints a help string for the arguments in `args` to `stream`.
     *
     * The help string contains the descriptions of all arguments as well as of the ArgumentSet. It also notes the
     * expected data types.
     */
    void help_string(std::ostream& stream, const ArgumentSet& args);

    /// convenience function to get a help string in a std::string.
    /// \sa void help_string(std::ostream& stream, const ArgumentSet& args)
    std::string help_string(const ArgumentSet& args);

    // -----------------------------------------------------------------------------------------------------------------
    //                                               Python Glue
    // -----------------------------------------------------------------------------------------------------------------

    /// creates a string that is used to auto-generate python glue code.
    /// \todo writing to a more universal format would be good (maybe json)
    void argspec_string(std::ostream& stream, const args::ArgumentSet& arguments);

    /// convenience function to get a argspec string in a std::string.
    /// \sa argspec_string(std::ostream& stream, const args::ArgumentSet& arguments)
    std::string argspec_string(const args::ArgumentSet& arguments);
}

#endif //BRANCHEDFLOWSIM_USAGE_HPP

ext: hpp


text:
//
// Created by kobus on 2017/04/06.
//

#include <opencv2/imgproc.hpp>
#include <opencv2/photo.hpp>
#include <list>
#include <deque>
#include <iomanip>
#include <cv.hpp>
#include "cpuDsp.h"
#include <armadillo>
//#include <Eigen/Dense>
//#include <Eigen/Sparse>
//#include <Eigen/IterativeLinearSolvers>
//#include <Eigen/SparseLU>

void printMat(cv::Mat image)
{
    std::cout << "Mat:" << std::endl;
    for (int i = 0; i < image.cols; i++)
    {
        std::cout << "  " << std::setfill('0') << std::setw(5) << i << "  ";
    }
    std::cout << std::endl;

    for (int i = 0; i < image.rows; i++)
    {
        std::cout << "  " << std::setfill('0') << std::setw(5) << i << "  ";
        for (int j = 0; j < image.cols; j++)
        {
            std::cout << (image.at<int>(i, j) & 0xFF) << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

void printMatFloat(cv::Mat image)
{
    std::cout << "Mat:" << std::endl;
    for (int i = 0; i < image.cols; i++)
    {
        std::cout << "  " << std::setfill('0') << std::setw(5) << i << "  ";
    }
    std::cout << std::endl;

    for (int i = 0; i < image.rows; i++)
    {
        std::cout << "  " << std::setfill('0') << std::setw(5) << i << "  ";
        for (int j = 0; j < image.cols; j++)
        {
            std::cout << image.at<double>(i, j) << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

void printArray(double array[1280][720])
{
    std::cout << "Mat:" << std::endl;
    for (int i = 0; i < 1280; i++)
    {
        std::cout << "  " << std::setfill('0') << std::setw(5) << i << "  ";
    }
    std::cout << std::endl;

    for (int i = 0; i < 720; i++)
    {
        std::cout << "  " << std::setfill('0') << std::setw(5) << i << "  ";
        for (int j = 0; j < 1280; j++)
        {
            std::cout << ((int)(array[i][j])) << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

void cpuDsp::executeTiPipeline(cv::Mat& frame,
                               cv::Mat& originalFrame,
                               cv::Mat& nnFrame,
                               cv::Mat& biqFrame,
                               cv::Mat& nediFrame,
                               cv::Mat& bilinearFrame,
                               TiDspParameters params)
{
    static bool showStabilisation = false;
    static int stabFrame = 0;
    cv::Mat temp1;
    cv::Mat temp2;

    cv::cvtColor(frame, frame, CV_BGR2GRAY);
    originalFrame = frame.clone();

    if (params.outputSelect == OutputSelect::Raw)
    {
        frame.copyTo(nnFrame);
        frame.copyTo(biqFrame);
        frame.copyTo(nediFrame);
        frame.copyTo(bilinearFrame);
        return;
    }

    //    if (params.eZoomOn)
//    {
//        eZoom(frame, params);
//        if (showComparisonWindows)
//        {
//            eZoom(originalFrame, params);
//        }
//    }
//    if (params.outputSelect == OutputSelect::Ezoom)
//        frame.copyTo(outputFrame);

    if (params.downScaleOn)
    {
        if (params.ezoom == Ezoom::x2)
        {
            cv::pyrDown(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2));
        }
        else if (params.ezoom == Ezoom::x4)
        {
            cv::pyrDown(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2));
            cv::pyrDown(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2));
        }
        else if (params.ezoom == Ezoom::x8)
        {
            cv::pyrDown(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2));
            cv::pyrDown(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2));
            cv::pyrDown(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2));
        }
    }
    if (params.outputSelect == OutputSelect::Downscale)
    {
        frame.copyTo(nnFrame);
        frame.copyTo(biqFrame);
        frame.copyTo(nediFrame);
        frame.copyTo(bilinearFrame);
        return;
    }

    if (params.upscaleOn)
    {
        if (params.ezoom == Ezoom::NoZoom)
        {
            frame.copyTo(nnFrame);
            frame.copyTo(biqFrame);
            frame.copyTo(nediFrame);
            frame.copyTo(bilinearFrame);
        }
        else if (params.ezoom == Ezoom::x2)
        {
            cv::resize(frame, nnFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_NEAREST);

            cv::resize(frame, biqFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_CUBIC);

            cv::resize(frame, bilinearFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_LINEAR);

            nediUpscale(frame, nediFrame);
        }
        else if (params.ezoom == Ezoom::x4)
        {
            cv::resize(frame, temp1, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_NEAREST);
            cv::resize(temp1, nnFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_NEAREST);

            cv::resize(frame, temp1, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_CUBIC);
            cv::resize(temp1, biqFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_CUBIC);

            cv::resize(frame, temp1, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_LINEAR);
            cv::resize(temp1, bilinearFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_LINEAR);

            nediUpscale(frame, temp1);
            nediUpscale(temp1, nediFrame);
        }
        else if (params.ezoom == Ezoom::x8)
        {
            cv::resize(frame, temp1, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_NEAREST);
            cv::resize(temp1, temp2, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_NEAREST);
            cv::resize(temp2, nnFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_NEAREST);

            cv::resize(frame, temp1, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_CUBIC);
            cv::resize(temp1, temp2, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_CUBIC);
            cv::resize(temp2, biqFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_CUBIC);

            cv::resize(frame, temp1, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_LINEAR);
            cv::resize(temp1, temp2, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_LINEAR);
            cv::resize(temp2, bilinearFrame, cv::Size(originalFrame.cols, originalFrame.rows), 0, 0, cv::INTER_LINEAR);

            nediUpscale(frame, temp1);
            nediUpscale(temp1, temp2);
            nediUpscale(temp2, nediFrame);
        }
    }
    else
    {
        frame.copyTo(nnFrame);
        frame.copyTo(biqFrame);
        frame.copyTo(nediFrame);
        frame.copyTo(bilinearFrame);
    }

    if (params.outputSelect == OutputSelect::Upscale)
        return;

    if (params.edgeEnhancementOn)
    {
        edgeEnhance(originalFrame, params);
        edgeEnhance(nnFrame, params);
        edgeEnhance(biqFrame, params);
        edgeEnhance(nediFrame, params);
        edgeEnhance(bilinearFrame, params);
    }
    if (params.outputSelect == OutputSelect::EdgeEnhance)
        return;

    return;
}

void cpuDsp::edgeEnhance(cv::Mat& frame, TiDspParameters params)
{
    switch (params.edgeEnhanceType)
    {
        case EdgeEnhanceType::Sharpen:
        {
            cv::Mat kernel = (cv::Mat_<float>(3,3) <<
                     0,-1, 0,
                    -1, 5,-1,
                     0,-1, 0);

            filter2D(frame, frame, CV_32F, kernel);
        }

        case EdgeEnhanceType::Gaussian:
        {
            cv::Mat kernel = (cv::Mat_<float>(5,5) <<
                    -1,-1,-1,-1,-1,
                    -1, 2, 2, 2,-1,
                    -1, 2, 8, 2,-1,
                    -1, 2, 2, 2,-1,
                    -1,-1,-1,-1,-1) / 8.0;

            filter2D(frame, frame, CV_32F, kernel);
            break;
        }

        case EdgeEnhanceType::Unsharp_mask:
        {
            cv::Mat kernel = (cv::Mat_<float>(5,5) <<
                    1, 4,   6, 4, 1,
                    4,16,  24,16, 4,
                    6,24,-476,24, 6,
                    4,16,  24,16, 4,
                    1, 4,   6, 4, 1) / -256.0;

            filter2D(frame, frame, CV_32F, kernel);
            break;
        }

        default:
        {
            break;
        }
    }
    frame.convertTo(frame, CV_8U);
    convertScaleAbs(frame, frame);
}

void cpuDsp::eZoom(cv::Mat &frame, TiDspParameters params)
{
    cv::Mat dest;
    switch (params.ezoom)
    {
        case Ezoom::NoZoom:
            break;
        case Ezoom::x2:
            cv::pyrUp(frame, dest, cv::Size(frame.cols * 2, frame.rows * 2));
            moveFrame(frame, dest, params);
            break;
        case Ezoom::x4:
            cv::pyrUp(frame, dest, cv::Size(frame.cols * 2, frame.rows * 2));
            cv::pyrUp(dest, dest, cv::Size(dest.cols * 2, dest.rows * 2));
            moveFrame(frame, dest, params);
            break;
        case Ezoom::x8:
            cv::pyrUp(frame, dest, cv::Size(frame.cols * 2, frame.rows * 2));
            cv::pyrUp(dest, dest, cv::Size(dest.cols * 2, dest.rows * 2));
            cv::pyrUp(dest, dest, cv::Size(dest.cols * 2, dest.rows * 2));
            moveFrame(frame, dest, params);
            break;
    }

}

void cpuDsp::moveFrame(const cv::Mat &frame, const cv::Mat &dest, TiDspParameters params)
{
    double availableXPixels, availableYPixels;
    double xMove, yMove;

    availableXPixels = dest.cols - frame.cols;
    availableYPixels = dest.rows - frame.rows;
    xMove = params.horisontalOffset / 100.0;
    yMove = params.verticalOffset / 100.0;
    dest(cv::Rect(xMove * availableXPixels, yMove * availableYPixels, frame.cols, frame.rows)).copyTo(frame);
}

void cpuDsp::sharpen(cv::Mat& frame)
{
    cv::Mat blurred; double sigma = 1, threshold = 5, amount = 1;
    GaussianBlur(frame, blurred, cv::Size(), sigma, sigma);
    cv::Mat lowContrastMask = cv::abs(frame - blurred) < threshold;
    cv::Mat sharpened = frame*(1+amount) + blurred*(-amount);
    frame.copyTo(frame, lowContrastMask);
}

void cpuDsp::nediUpscale(const cv::Mat &frame, cv::Mat &dest) {
    const int upscaleFactor = 2;
    dest = cv::Mat::zeros(cv::Size(frame.cols * upscaleFactor, frame.rows * upscaleFactor), CV_8UC1);

    for (int y = 0; y < frame.rows; y++)
    {
        for (int x = 0; x < frame.cols; x++)
        {
            dest.ptr<unsigned char>(y * upscaleFactor)[x * upscaleFactor] = frame.ptr<unsigned char>(y)[x];
        }
    }

    arma::mat Y = arma::zeros(25, 1);
    arma::mat C = arma::zeros(25, 4);
    arma::mat CtC;
    arma::mat Cty;
    arma::mat Cinv;
    arma::mat a;
    int temp;
    //int m = 5; //todo review size of filter

    //Solve the "b" pixels
    //todo edge conditions not handled yet
    for (int i = 4; i < (dest.rows - 4) / 2; i++)
    {
        for (int j = 4; j < (dest.cols - 4) / 2; j++)
        {
            temp = 0;
            for (int ii = i - 2; ii <= i + 2; ii++)
            {
                for (int jj = j - 2; jj <= j + 2; jj++)
                {
                    Y(temp, 0) = (dest.ptr<unsigned char>(2*ii)[2*jj]);

                    C(temp, 0) = (dest.ptr<unsigned char>(2*ii - 2)[2*jj - 2]);
                    C(temp, 1) = (dest.ptr<unsigned char>(2*ii + 2)[2*jj - 2]);
                    C(temp, 2) = (dest.ptr<unsigned char>(2*ii + 2)[2*jj + 2]);
                    C(temp, 3) = (dest.ptr<unsigned char>(2*ii - 2)[2*jj + 2]);
                    temp++;
                }
            }

            Cty = C.t() * Y;
            CtC = C.t() * C;
            if (arma::solve(a, Cty, CtC))
            {
                (dest.ptr<unsigned char>(2*i + 1)[2*j + 1]) = (unsigned char) (a(0, 0) / 4 * (dest.ptr<unsigned char>(2*i)[2*j]) + \
                a(0, 1) / 4 * (dest.ptr<unsigned char>(2*i + 2)[2*j]) + \
                a(0, 2) / 4 * (dest.ptr<unsigned char>(2*i + 2)[2*j + 2]) + \
                a(0, 3) / 4  * (dest.ptr<unsigned char>(2*i)[2*j + 2]));
            }
            else
            {
                (dest.ptr<unsigned char>(2*i + 1)[2*j + 1]) = 0;//(unsigned char) (dest.ptr<unsigned char>(y - 1)[x - 1]);
            }
        }
    }

    for (int i = 4; i < (dest.rows - 4) / 2; i++)
    {
        for (int j = 4; j < (dest.cols - 4) / 2; j++)
        {
            temp = 0;
            for (int ii = i - 2; ii <= i + 2; ii++)
            {
                for (int jj = j - 2; jj <= j + 2; jj++)
                {
                    Y(temp, 0) = (dest.ptr<unsigned char>(2*ii + 1)[2*jj - 1]);

                    C(temp, 0) = (dest.ptr<unsigned char>(2*ii - 1)[2*jj - 1]);
                    C(temp, 1) = (dest.ptr<unsigned char>(2*ii + 1)[2*jj - 3]);
                    C(temp, 2) = (dest.ptr<unsigned char>(2*ii + 3)[2*jj - 1]);
                    C(temp, 3) = (dest.ptr<unsigned char>(2*ii + 1)[2*jj + 1]);
                    temp++;
                }
            }

            Cty = C.t() * Y;
            CtC = C.t() * C;
            if (arma::solve(a, Cty, CtC))
            {
                (dest.ptr<unsigned char>(2*i + 1)[2*j]) = (unsigned char) (a(0, 0) / 4 * (dest.ptr<unsigned char>(2*i)[2*j]) + \
                a(0, 1) / 4 * (dest.ptr<unsigned char>(2*i + 1)[2*j - 1]) + \
                a(0, 2) / 4 * (dest.ptr<unsigned char>(2*i + 2)[2*j]) + \
                a(0, 3) / 4  * (dest.ptr<unsigned char>(2*i + 1)[2*j + 1]));
            }
            else
            {
                (dest.ptr<unsigned char>(2*i + 1)[2*j]) = 0;//(unsigned char) (dest.ptr<unsigned char>(y - 1)[x - 1]);
            }
        }
    }

    for (int i = 4; i < (dest.rows - 4) / 2; i++)
    {
        for (int j = 4; j < (dest.cols - 4) / 2; j++)
        {
            temp = 0;
            for (int ii = i - 2; ii <= i + 2; ii++)
            {
                for (int jj = j - 2; jj <= j + 2; jj++)
                {
                    Y(temp, 0) = (dest.ptr<unsigned char>(2*ii + 1)[2*jj - 1]);

                    C(temp, 0) = (dest.ptr<unsigned char>(2*ii - 1)[2*jj - 1]);
                    C(temp, 1) = (dest.ptr<unsigned char>(2*ii + 1)[2*jj - 3]);
                    C(temp, 2) = (dest.ptr<unsigned char>(2*ii + 3)[2*jj - 1]);
                    C(temp, 3) = (dest.ptr<unsigned char>(2*ii + 1)[2*jj + 1]);
                    temp++;
                }
            }

            Cty = C.t() * Y;
            CtC = C.t() * C;
            if (arma::solve(a, Cty, CtC))
            {
                (dest.ptr<unsigned char>(2*i)[2*j + 1]) = (unsigned char) (a(0, 0) / 4 * (dest.ptr<unsigned char>(2*i - 1)[2*j + 1]) + \
                a(0, 1) / 4 * (dest.ptr<unsigned char>(2*i)[2*j]) + \
                a(0, 2) / 4 * (dest.ptr<unsigned char>(2*i + 1)[2*j + 1]) + \
                a(0, 3) / 4  * (dest.ptr<unsigned char>(2*i)[2*j + 2]));
            }
            else
            {
                (dest.ptr<unsigned char>(2*i)[2*j + 1]) = 0;//(unsigned char) (dest.ptr<unsigned char>(y - 1)[x - 1]);
            }
        }
    }
}

ext: cpp

max_stars_repo_name: kabousvlieg/Nedi-image-upscaling

max_issues_repo_name: kabousvlieg/Nedi-image-upscaling

max_forks_repo_name: kabousvlieg/Nedi-image-upscaling


text:
#include "utility.h"
#include "imuPreintegration.hpp"
#include "featureExtraction.hpp"
#include "imageProjection.hpp"
#include "mapOptmization.hpp"

#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <boost/foreach.hpp>

// subscribe<nav_msgs::Odometry>("lio_sam/mapping/odometry", 5, &TransformFusion::lidarOdometryHandler, this, ros::TransportHints().tcpNoDelay()); Done
// pubLaserOdometryGlobal = nh.advertise<nav_msgs::Odometry> ("lio_sam/mapping/odometry", 1); Done

// subscribe<nav_msgs::Odometry>(odomTopic+"_incremental",   2000, &TransformFusion::imuOdometryHandler,   this, ros::TransportHints().tcpNoDelay()); Done
// subscribe<nav_msgs::Odometry>(odomTopic+"_incremental", 2000, &ImageProjection::odometryHandler, this, ros::TransportHints().tcpNoDelay()); Done
// pubImuOdometry = nh.advertise<nav_msgs::Odometry> (odomTopic+"_incremental", 2000); Done

// subscribe<nav_msgs::Odometry>("lio_sam/mapping/odometry_incremental", 5, &IMUPreintegration::odometryHandler, this, ros::TransportHints().tcpNoDelay()); Done
// pubLaserOdometryIncremental = nh.advertise<nav_msgs::Odometry> ("lio_sam/mapping/odometry_incremental", 1); Done

// subscribe<lio_sam::cloud_info>("lio_sam/deskew/cloud_info", 1, &FeatureExtraction::laserCloudInfoHandler, this, ros::TransportHints().tcpNoDelay()); Done
// pubLaserCloudInfo = nh.advertise<lio_sam::cloud_info> ("lio_sam/deskew/cloud_info", 1); Done

// subscribe<lio_sam::cloud_info>("lio_sam/feature/cloud_info", 1, &mapOptimization::laserCloudInfoHandler, this, ros::TransportHints().tcpNoDelay()); Done
// pubLaserCloudInfo = nh.advertise<lio_sam::cloud_info> ("lio_sam/feature/cloud_info", 1); Done

// subscribe<sensor_msgs::PointCloud2>(pointCloudTopic, 5, &ImageProjection::cloudHandler, this, ros::TransportHints().tcpNoDelay()); Done

// subscribe<sensor_msgs::Imu>(imuTopic, 2000, &IMUPreintegration::imuHandler, this, ros::TransportHints().tcpNoDelay()); Done
// subscribe<sensor_msgs::Imu>(imuTopic, 2000, &ImageProjection::imuHandler, this, ros::TransportHints().tcpNoDelay()); Done

// subscribe<nav_msgs::Odometry>(gpsTopic, 200, &mapOptimization::gpsHandler, this, ros::TransportHints().tcpNoDelay());

// subscribe<std_msgs::Float64MultiArray>("lio_loop/loop_closure_detection", 1, &mapOptimization::loopInfoHandler, this, ros::TransportHints().tcpNoDelay());

int main(int argc, char** argv)
{
    ros::init(argc, argv, "lio_sam_offline");

    TransformFusion TF;
    IMUPreintegration ImuP;
    ImageProjection IP;
    FeatureExtraction FE;
    mapOptimization MO;

    ROS_INFO("\033[1;32m----> Offline LIO_SAM started...\033[0m");

    std::thread loopthread(&mapOptimization::loopClosureThread, &MO);
    std::thread visualizeMapThread(&mapOptimization::visualizeGlobalMapThread, &MO);

    ros::Rate rate(MO.loopRate);
    rosbag::Bag read_bag;
    read_bag.open(MO.readBag, rosbag::bagmode::Read);

    ROS_INFO("listening to:");
    ROS_INFO(MO.imuTopic.c_str());
    ROS_INFO(MO.pointCloudTopic.c_str());
    ROS_INFO(MO.gpsTopic.c_str());

    std::vector<std::string> topics;
    topics.push_back(MO.imuTopic);
    topics.push_back(MO.pointCloudTopic);
    // topics.push_back(MO.gpsTopic);

    rosbag::View view(read_bag, rosbag::TopicQuery(topics)); //note:TopicQuery;TypeQuery

    boost::shared_ptr<sensor_msgs::Imu> IMUptr;
    boost::shared_ptr<sensor_msgs::PointCloud2> RawCloudptr;

    if (ros::ok())
    {
        ROS_INFO("OK!!!!");

        BOOST_FOREACH(rosbag::MessageInstance const m, view)
        {
            ros::spinOnce();
            // ROS_INFO("Looping...");

            if(MO.pubLaserOdometryGlobalFlag)
            {
                TF.lidarOdometryHandler(MO.pubLaserOdometryGlobalPtr);
                MO.pubLaserOdometryGlobalFlag = false;
            }

            if(MO.pubLaserOdometryIncrementalFlag)
            {
                ImuP.odometryHandler(MO.pubLaserOdometryIncrementalPtr);
                MO.pubLaserOdometryIncrementalFlag = false;
            }

            if(ImuP.pubImuOdometryFlag)
            {
                TF.imuOdometryHandler(ImuP.pubImuOdometryPtr);
                IP.odometryHandler(ImuP.pubImuOdometryPtr);
                ImuP.pubImuOdometryFlag = false;
            }

            if(IP.pubLaserCloudInfoFlag)
            {
                FE.laserCloudInfoHandler(IP.pubLaserCloudInfoPtr);
                IP.pubLaserCloudInfoFlag = false;
            }

            if(FE.pubLaserCloudInfoFlag)
            {
                MO.laserCloudInfoHandler(FE.pubLaserCloudInfoPtr);
                FE.pubLaserCloudInfoFlag = false;
            }

            sensor_msgs::Imu::ConstPtr imu_msg = m.instantiate<sensor_msgs::Imu>();
            if (imu_msg)
            {
                // ROS_INFO("Pub an IMU msg");
                ImuP.imuHandler(imu_msg);
                IP.imuHandler(imu_msg);
            }

            sensor_msgs::PointCloud2::ConstPtr pointcloud_msg = m.instantiate<sensor_msgs::PointCloud2>();
            if (pointcloud_msg)
            {
                // ROS_INFO("Pub a PointCloud");
                IP.cloudHandler(pointcloud_msg);
            }

            // nav_msgs::Odometry::ConstPtr gps_msg = m.instantiate<nav_msgs::Odometry>();
            // if (gps_msg)
            // {
            //     ROS_INFO("Pub a GPS msg");
            //     MO.gpsHandler(gps_msg);
            // }
            rate.sleep();
        }

        ROS_INFO("Reached the end of the bag.");
    }

    read_bag.close();

    rosbag::Bag write_bag;
    write_bag.open(MO.writeBag, rosbag::bagmode::Write);

    ROS_INFO("Saving global map and path to %s", MO.writeBag.c_str());

    write_bag.write("/lio_sam/mapping/map_global", ros::Time::now(), MO.globalMapToSave);
    write_bag.write("/lio_sam/mapping/path", ros::Time::now(), MO.globalPath);

    ROS_INFO("Saved!");

    write_bag.close();

    loopthread.detach();
    visualizeMapThread.detach();

    ros::shutdown();

    return 0;
}


text:
#include <iostream>
#include <cmath>

#include <boost/numeric/linear_algebra/operators.hpp>
#include <boost/numeric/linear_algebra/inverse.hpp>
#include <boost/numeric/linear_algebra/is_invertible.hpp>
#include <boost/numeric/linear_algebra/new_concepts.hpp>
//#include <boost/numeric/linear_algebra/concept_maps.hpp>
#include <boost/numeric/linear_algebra/power.hpp>

#if 0 // only for testing other concepts
namespace math {
    concept_map AbelianGroup< add<float>, float > {}
    concept_map Monoid< mult<float>, float > {}
    concept_map SignedIntegral<int> {}
} // namespace math
#endif

int main(int, char* [])
{
    using math::power; using math::mult; using math::add;
    float a= 3.14;

    std::cout << "power(a, 5, mult<float>) " << power(a, 5, mult<float>()) << '\n';
#if 0
    std::cout << "power(a, 5, add<float>) " << power(a, 5, add<float>()) << '\n';
    std::cout << "power(a, 0, add<float>) " << power(a, 0, add<float>()) << '\n';
#endif

    return 0;
}


text:
#include <util/scheduler.h>
#include <util/cronexpression.h>

#include <boost/test/unit_test.hpp>
#include <boost/timer.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/local_time_adjustor.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>

#include <string>
#include <iostream>

using namespace izenelib::util;
using namespace std;

static int g_counter1 = 0;
static boost::mutex g_counter_mutex1;
void TestFuncOk1(int calltype) {
  boost::mutex::scoped_lock l(g_counter_mutex1);
  ++g_counter1;
  boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(2000));
}

static int g_counter2 = 0;
static boost::mutex g_counter_mutex2;
void TestFuncOk2(int calltype) {
  boost::mutex::scoped_lock l(g_counter_mutex2);
  ++g_counter2;
  std::cout << "running TestFuncOk2" << std::endl;
}

void TestCronExpression(int calltype)
{
    CronExpression job;
    job.setExpression("0,15,30,45  7-23    *       *       *");
    using namespace boost::posix_time;
    using namespace boost::gregorian;

    boost::posix_time::ptime now = second_clock::local_time();//boost::get_system_time();
    boost::gregorian::date date = now.date();
    boost::posix_time::time_duration du = now.time_of_day();
    int dow = date.day_of_week();
    int month = date.month();
    int day = date.day();
    int hour = du.hours();
    int minute = du.minutes();
    if(job.matches(minute, hour, day, month, dow))
    {
        cout<<"send command "<<date.year()<<" "<<month<<" "<<day<<" "<<hour<<" "<<minute<<" "<<du.seconds()<<endl;
    }
}

BOOST_AUTO_TEST_CASE(Scheduler_test)
{
    const string kTestJob = "Test";
    boost::function<void (int)> task = boost::bind(&TestCronExpression, _1);
    Scheduler::addJob(kTestJob, 1000, 0, task);
    boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(10000));
    Scheduler::removeJob(kTestJob);

    const string kTestJob1 = "Test1";
    const string kTestJob2 = "Test2";
    boost::function<void (int)> task1 = boost::bind(&TestFuncOk1, _1);
    boost::function<void (int)> task2 = boost::bind(&TestFuncOk2, _1);

    {
      g_counter1 = 0;
      std::cout << "begin test job " << std::endl;
      Scheduler::addJob(kTestJob1, 2000, 1000, task1);

      boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(10000));
      BOOST_CHECK(3==g_counter1);
      Scheduler::removeJob(kTestJob1);
      boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(5000));
      BOOST_CHECK(3==g_counter1);
    }
    {
      g_counter1 = 0;
      Scheduler::addJob(kTestJob1, 2000, 1000, task1);

      boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(10000));
      BOOST_CHECK(3==g_counter1);
      Scheduler::removeJob(kTestJob1, true);
      BOOST_CHECK(3==g_counter1);
    }
    {
      g_counter1 = 0;
      Scheduler::addJob(kTestJob1, 2000, 1000, task1);

      boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(10100));
      BOOST_CHECK(3==g_counter1);

      g_counter2 = 0;
      Scheduler::addJob(kTestJob2, 1000, 1000, task2);

      boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(3100));

      BOOST_CHECK(4==g_counter1);
      BOOST_CHECK(3==g_counter2);
      Scheduler::removeJob(kTestJob1);

      boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(3100));
      BOOST_CHECK(4==g_counter1);
      BOOST_CHECK(6==g_counter2);
    }
    Scheduler::removeJob(kTestJob2);

    boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(3100));
    BOOST_CHECK(4==g_counter1);
    BOOST_CHECK(6==g_counter2);
}

max_stars_repo_name: izenecloud/izenelib

max_issues_repo_name: izenecloud/izenelib

max_forks_repo_name: izenecloud/izenelib