使用CUDA优化嵌套for循环

使用CUDA优化嵌套for循环,cuda,gpu,Cuda,Gpu,所以我有一个项目,我正在工作,它使用OpenCV来检测运动物体的运动。我正在尝试加快检测速度,并有一个嵌套的for循环,我想使用CUDA来加速它。我已经在Visual Basic中设置了CUDA集成。下面是我的.cpp文件中的嵌套for循环 for (int i=0; i<NumberOfFeatures; i++) { // Compute integral image. cvIntegral(mFeatureImgs[i], mFirstOrderIIs

所以我有一个项目,我正在工作,它使用OpenCV来检测运动物体的运动。我正在尝试加快检测速度,并有一个嵌套的for循环,我想使用CUDA来加速它。我已经在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]);
    }
  }

for(int i=0;i正如sgar91所指出的,OpenCV包括一个GPU模块,如下所述:

该维基还建议如何在雅虎的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);

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

该维基还建议如何在雅虎的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);

cv_积分基本上是沿两个维度对像素值求和-这只能通过矩阵运算来完成。因此,如果您愿意,也可以尝试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";
}
//计算积分图像
af::数组cv_积分(af::数组img){
//创建大小为+1的完整图像
int w=img.dims(0),h=img.dims(1);
数组积分=af::零(w+1,h+1,af::f32);
积分(af::seq(1,w),af::seq(1,h))=img;
//沿两个维度计算包含前缀和
积分=af::accum(积分,0);
积分=af::accum(积分,1);

cv_积分基本上是沿着两个维度对像素值求和-这只能通过矩阵运算来完成。因此,如果您愿意,也可以尝试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";
}
//计算积分图像
af::数组cv_积分(af::数组img){
//创建大小为+1的完整图像
int w=img.dims(0),h=img.dims(1);
数组积分=af::零(w+1,h+1,af::f32);
积分(af::seq(1,w),af::seq(1,h))=img;
//沿两个维度计算包含前缀和
积分=af::accum(积分,0);
积分=af::accum(积分,1);

std::你有没有考虑过使用OpenCV gpu模块?这些都是已经回答过的类似问题。你可以从中找到答案:你有没有考虑过使用OpenCV gpu模块?这些都是已经回答过的类似问题。你可以从中找到答案:谢谢你的回答,罗伯特,我以前在那里问过实际上是来StackOverflow的。仍然没有回应,已经两天了。关于如何让这个OpenCV GPU模块为integral运行有什么建议吗?当我将cvIntegral切换到cv::GPU::integral时,它给了我一个参数错误,因为我没有一个名为GPU::mat的东西?谢谢你的回复Robert,在来StackOverflow之前我在那里问过事实上。仍然没有回应,已经2天了。关于如何让这个OpenCV GPU模块为integral运行有什么建议吗?当我将cvIntegral切换到cv::GPU::integral时,它会给我一个参数错误,因为我没有称为GPU::mat的东西?