0

所以我有一个我正在做的项目,它使用 OpenCV 来检测移动物体的运动。我正在尝试加快检测速度,并有一个我想使用 CUDA 加速的嵌套 for 循环。我在 Visual Basic 中设置了 CUDA 集成。这是我的 .cpp 文件中的嵌套 for 循环。

      for (int i=0; i<NumberOfFeatures; i++)
  {
    // Compute integral image.
    cvIntegral(mFeatureImgs[i], mFirstOrderIIs[i]);

    for (int j=0; j<NumberOfFeatures; j++)
    {
      // Compute product feature image.
      cvMul(mFeatureImgs[i], mFeatureImgs[j], mWorker);

      // Compute integral image.
      cvIntegral(mWorker, mSecondOrderIIs[i][j]);
    }
  }

我对 CUDA 比较陌生,所以我的问题是,有人可以向我展示一个示例,说明我将如何使用 CUDA 使这个嵌套的 for 循环运行得更快吗?

4

2 回答 2

2

正如 sgar91 所指出的,OpenCV 包括一个 GPU 模块,如下所述:

http://opencv.willowgarage.com/wiki/OpenCV_GPU

该 wiki 还建议如何在 Yahoo 的 OpenCV 帮助论坛上询问与 GPU 相关的问题。

有一个gpu加速的图像积分功能。如果您环顾四周,您可能还会发现 cvMul 的等价物。

您不能在非 GPU 代码和 GPU 版本中使用完全相同的数据类型。看看我之前发布的 wiki 页面上给出的“简短示例”示例。您将看到您需要执行以下操作来将现有数据传输到可由 GPU 操作的数据结构:

    cv::gpu::GpuMat dst, src;  // this is defining variables that can be accessed by the GPU
    src.upload(src_host);      // this is loading the src (GPU variable) with the image data

    cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);  //this is causing the GPU to act

您将需要做一些类似的事情,例如:

    cv::gpu::GpuMat dst, src;
    src.upload(src_data);

    cv::gpu::integral(src, dst);
于 2012-09-25T01:41:57.397 回答
1

cv_integral 基本上总结了两个维度上的像素值——这只能通过矩阵运算来完成。因此,如果您愿意,也可以尝试使用 arrayfire。我为您创建了一个如何使用矩阵进行图像处理的小示例:

// computes integral image
af::array cv_integral(af::array img) {

  // create an integral image of size + 1
  int w = img.dims(0), h = img.dims(1);
  af::array integral = af::zeros(w + 1, h + 1, af::f32);

  integral(af::seq(1,w), af::seq(1,h)) = img;

  // compute inclusive prefix sums along both dimensions
   integral = af::accum(integral, 0);
   integral = af::accum(integral, 1);

   std::cout << integral << "\n";

   return integral;
}

void af_test()
{
 int w = 6, h = 5; // image size
 float img_host[] = {5,2,3,4,1,7,
                    1,5,4,2,3,4,
                    2,2,1,3,4,45,
                    3,5,6,4,5,2,
                    4,1,3,2,6,9};

  //! create a GPU image (matrix) from the host data
  //! NOTE: column-major order!!
  af::array img(w, h, img_host, af::afHost);

   //! create an image from random data
   af::array img2 = af::randu(w, h) * 10;
   // compute integral images
   af::array integral = cv_integral(img);
   // elementwise product of the images
   af::array res = integral * img2;
   //! compute integral image
   res = cv_integral(res);
   af::eval(res);
   std::cout << res << "\n";
}
于 2012-10-02T15:01:00.753 回答