Example #1
0
 // OPTIMIZE
 mdarray_float convolve(const mdarray_float &src, const mdarray_float &kernel, int cx, int cy) {
     mdarray_float convolved(false, src.length(0), src.length(1));
     #pragma omp parallel
     #pragma omp for
     for (int y = 0;  y < src.length(1);  y++)  for (int x = 0;  x < src.length(0);  x++) {
         double s = 0.;
         for (int yy = 0;  yy < kernel.length(1);  yy++)  for (int xx = 0;  xx < kernel.length(0);  xx++) {
             s += kernel(xx, yy) * src(x + xx - cx, y + yy - cy);
         }
         convolved(x, y) = s;
     }
     return convolved;
 }
double MyModel_Pixels::calculate_total_flux(double time) const
{
    double speed = 1.0/timescale;
    double offset = x0 + speed*time;

    int j = (int)floor((offset - (x_min + 0.5*dx))/dx);
    return convolved(ni/2, j%nj);
}
 std::vector<float> WindowFunction::convolve(
   const std::vector<float>& input, const std::vector<float>& window
 ) const {
   unsigned int inputSize = input.size();
   unsigned int padding = window.size() / 2;
   std::vector<float> convolved(inputSize, 0.0);
   // TODO: this implements zero padding for boundary effects,
   // write something mean-based later.
   for (unsigned int sample = 0; sample < inputSize; sample++) {
     float convolution = 0.0;
     for (unsigned int k = 0; k < window.size(); k++) {
       int frm = (signed)sample - (signed)padding + (signed)k;
       if (frm >= 0 && frm < (signed)inputSize) // don't run off either end
         convolution += input[frm] * window[k] / window.size();
     }
     convolved[sample] = convolution;
   }
   return convolved;
 }
void MyModel_Pixels::calculate_obscurer_map()
{
    // Obscurer image
    for(size_t j=0; j<nj; ++j)
        for(size_t i=0; i<ni; ++i)
            obscurer_map(i, j) = exp(-sd*n(i, j));

    // FFT of obscurer_map
    arma::cx_mat A = arma::fft2(obscurer_map);

    // (FFT of obscurer map)*(FFT of star map)
    for(size_t j=0; j<nj; ++j)
        for(size_t i=0; i<ni; ++i)
            A(i, j) *= fft_of_star(i, j);

    // obscurer map convolved with star map
    A = arma::ifft2(A);
    for(size_t i=0; i<ni; ++i)
        for(size_t j=0; j<nj; ++j)
            convolved(i, j) = A(i, j).real();
}
Example #5
0
int main(int argc, char *argv[])
{
  if (argc != 4)
  {
    cout << "Usage: " << argv[0] << " cpu|gpu out_func out_prefix" << endl;
    return 1;
  }

  ImageParam input(UInt(8), 3, "input");
  Func clamped("clamped");
  Func convolved("convolved");
  Func sharpen("sharpen");
  Var c("c"), x("x"), y("y");

  // Algorithm
  clamped(x, y, c) = input(
    clamp(x, 0, input.width()-1),
    clamp(y, 0, input.height()-1),
    c) / 255.f;

  Image<int16_t> kernel(3, 3);
  kernel(0, 0) = -1;
  kernel(0, 1) = -1;
  kernel(0, 2) = -1;
  kernel(1, 0) = -1;
  kernel(1, 1) = 8;
  kernel(1, 2) = -1;
  kernel(2, 0) = -1;
  kernel(2, 1) = -1;
  kernel(2, 2) = -1;

  RDom r(kernel);
  convolved(x, y, c) += kernel(r.x, r.y) * clamped(x + r.x - 1, y + r.y - 1, c);
  sharpen(x, y, c) = u8(
    clamp(convolved(x, y, c)/8 + clamped(x, y, c), 0, 1) * 255
  );

  // Channel order
  input.set_stride(0, 4);
  input.set_extent(2, 4);
  sharpen.reorder_storage(c, x, y);
  sharpen.output_buffer().set_stride(0, 4);
  sharpen.output_buffer().set_extent(2, 4);

  // Schedules
  if (!strcmp(argv[1], "cpu"))
  {
    sharpen.parallel(y).vectorize(c, 4);
  }
  else if (!strcmp(argv[1], "gpu"))
  {
    sharpen.cuda_tile(x, y, 16, 4);
  }
  else
  {
    cout << "Invalid schedule type '" << argv[1] << "'" << endl;
    return 1;
  }

  compile(sharpen, input, argv[2], argv[3]);

  return 0;
}
Example #6
0
int
main (int argc, char ** argv)
{
    int viewport_source, viewport_convolved = 0;
    int direction = -1;
    int nb_threads = 0;
    char border_policy = 'Z';
    double threshold = 0.001;
    pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB> convolution;
    Eigen::ArrayXf gaussian_kernel(5);
    gaussian_kernel << 1.f/16, 1.f/4, 3.f/8, 1.f/4, 1.f/16;
    pcl::console::print_info ("convolution kernel:");
    for (int i = 0; i < gaussian_kernel.size (); ++i)
        pcl::console::print_info (" %f", gaussian_kernel[i]);
    pcl::console::print_info ("\n");

    if (argc < 3)
    {
        usage (argv);
        return 1;
    }

    // check if user is requesting help
    std::string arg (argv[1]);

    if (arg == "--help" || arg == "-h")
    {
        usage (argv);
        return 1;
    }

    // user don't need help find convolving direction
    // convolve row
    if (pcl::console::find_switch (argc, argv, "-r"))
        direction = 0;
    else
    {
        // convolve column
        if (pcl::console::find_switch (argc, argv, "-c"))
            direction = 1;
        else
            // convolve both
            if (pcl::console::find_switch (argc, argv, "-s"))
                direction = 2;
            else
            {
                // wrong direction given print usage
                usage (argv);
                return 1;
            }
    }

    // number of threads if any
    if (pcl::console::parse_argument (argc, argv, "-t", nb_threads) != -1 )
    {
        if (nb_threads <= 0)
            nb_threads = 1;
    }
    convolution.setNumberOfThreads (nb_threads);

    // borders policy if any
    if (pcl::console::parse_argument (argc, argv, "-p", border_policy) != -1 )
    {
        switch (border_policy)
        {
        case 'Z' :
            convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_IGNORE);
            break;
        case 'M' :
            convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_MIRROR);
            break;
        case 'D' :
            convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_DUPLICATE);
            break;
        default :
        {
            usage (argv);
            return (1);
        }
        }
    }
    else
        convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_IGNORE);

    // distance threshold if any
    if (pcl::console::parse_argument (argc, argv, "-d", threshold) == -1 )
    {
        threshold = 0.01;
    }
    convolution.setDistanceThreshold (static_cast<float> (threshold));

    // all set
    // we have file name and convolving direction
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());
    if (pcl::io::loadPCDFile (argv[1], *cloud) == -1)
    {
        pcl::console::print_error ("Couldn't read file %s \n", argv[1]);
        return (-1);
    }
    cloud->is_dense = false;
    convolution.setInputCloud (cloud);
    convolution.setKernel (gaussian_kernel);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr convolved (new pcl::PointCloud<pcl::PointXYZRGB> ());
    double t0;
    pcl::console::print_info ("convolving %s along \n", argv[1]);
    std::ostringstream convolved_label;
    convolved_label << "convolved along ";
    switch (direction)
    {
    case 0:
    {
        convolved_label << "rows... ";
        t0 = pcl::getTime ();
        convolution.convolveRows (*convolved);
        break;
    }
    case 1:
    {
        convolved_label << "columns... ";
        t0 = pcl::getTime ();
        convolution.convolveCols (*convolved);
        break;
    }
    case 2:
    {
        convolved_label << "rows and columns... ";
        t0 = pcl::getTime ();
        convolution.convolve (*convolved);
        break;
    }
    }
    convolved_label << pcl::getTime () - t0 << "s";
    // Display
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("Convolution"));
    // viewport stuff
    viewer->createViewPort (0, 0, 0.5, 1, viewport_source);
    viewer->createViewPort (0.5, 0, 1, 1, viewport_convolved);
    viewer->setBackgroundColor (0, 0, 0);

    // Source
    pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> color_handler_source (cloud);
    viewer->addPointCloud<pcl::PointXYZRGB> (cloud, color_handler_source, "source", viewport_source);
    viewer->addText ("source", 10, 10, "source_label", viewport_source);

    // Convolved
    pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> color_handler_convolved (convolved);
    viewer->addPointCloud<pcl::PointXYZRGB> (convolved, color_handler_convolved, "convolved", viewport_convolved);
    viewer->addText (convolved_label.str (), 10, 10, "convolved_label", viewport_convolved);
    viewer->spin ();
    pcl::PCDWriter writer;
    writer.write<pcl::PointXYZRGB> ("convolved.pcd", *convolved, false);
}