c++ - 使用cuda进行图像处理的Hello world

标签 c++ .net visual-studio opencv cuda

我正在尝试让 cuda 在我的网络摄像头视频上执行内核。

我希望它能够从我的网络摄像头检索数据,将其发送到我的 GPU,处理内核,然后将生成的图像发回以进行显示。

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <Windows.h>
#include "Bitmap.h"

#include "OpenCVTest.h"

#include "OpenCVTest.h"
#include <opencv2/opencv.hpp>

using namespace cv;

#define Pixel unsigned char


__global__ void TestKernel(unsigned char * img)
{
    int index = threadIdx.x + blockIdx.x * blockDim.x;
    img[index] = 100;
}

int main(void) 
{
    VideoCapture cap(0); 
    Mat input;
    Mat frame;
    Mat Output;
    cap >> frame;
    //cap >> Output;
    cvtColor(frame, Output, CV_BGR2GRAY);
    uchar *d_frame;
    size_t size = (int) (640 * 480);
    cudaMalloc((void **)&d_frame, size);

    namedWindow("Window",1);
    for(;;)
    {
        cap >> input; 
        cvtColor(input, frame, CV_BGR2GRAY);        

        cudaMemcpy(d_frame, frame.data, size, cudaMemcpyHostToDevice);

        TestKernel<<<640 * 480, 1>>>( d_frame );

        cudaMemcpy(Output.data, d_frame, size, cudaMemcpyDeviceToHost);

        imshow("Window", Output);
        if(waitKey(30) >= 0) break;
    }

    cudaFree(d_frame);

    return 0;
}

我刚刚编写了一个非常基本的测试内核作为开始。但似乎内核没有被执行,因为我显示的图像只是来 self 的网络摄像头的灰度视频。

编辑

正如 Robert 所说,我添加了一些错误检查

gpuErrchk( cudaPeekAtLastError() );

调用内核之后

gpuErrchk 在哪里

#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

GPUError

最佳答案

640 * 480 = 307200

除非您已经为 cc 3.0 或更高版本的 GPU 编译并运行了代码,否则对于内核的第一个配置参数,这不是一个可接受的选择:

    TestKernel<<<640 * 480, 1>>>( d_frame );

对于 cc3.0 之前的设备,第一个参数(即 线程 block 网格的最大 x 维度)is limited to 65535对于 dim3 数量的前 2 个维度。

如果你做了 proper cuda error checking你会发现你的内核没有运行(和/或其他错误)。你也可以尝试使用 cuda-memcheck 运行你的代码作为快速测试。

关于c++ - 使用cuda进行图像处理的Hello world,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24051719/

相关文章:

.net - ASP.Net MVC 中的 JsonResult 又名 Json 函数中的控制序列化器?

c# - 如何使用System.CommandLine绑定(bind)NodaTime.Duration?

c# - 执行代码修复后如何将插入符号移动到特定位置

c++ - 设置 Visual Studio 库项目依赖项

c++ - 构建依赖于非 const-correct 库的 const-correct (C++) 库

C++,构造读取两个实数和一个字符的程序

c++ - 异步 Wt::Http::Client 响应和请求匹配

c# - 向基于模型的列表添加值

linux - 在 netbeans 中部署 java 桌面应用程序

c++ - istream 末尾的字符问题