cudaMemcpy2D未处理的异常 我是C++新手(CUDA和OpenCV),所以我对我身边的错误感到抱歉。 我有一个使用Cuda的现有代码。最近它使用.png(被解码)作为输入,但现在我使用相机生成实时图像。这些图像是代码的新输入。这是: using namespace cv; INT height = 2160; INT width = 3840; Mat image(height, width, CV_8UC3); size_t pitch; uint8_t* image_gpu; // capture image VideoCapture camera(0); camera.set(CAP_PROP_FRAME_WIDTH, width); camera.set(CAP_PROP_FRAME_HEIGHT, height); camera.read(image); // here I checked if image is definitly still a CV_8UC3 Mat with the initial height and width; and it is cudaMallocPitch(&image_gpu, &pitch, width * 4, height); // here I use cv::Mat::data to get the pointer to the data of the image: cudaMemcpy2D(image_gpu, pitch, image.data, width*4, width*4, height, cudaMemcpyHostToDevice);
代码可以编译,但在最后一行(cudaMemcpy2D)出现“异常抛出”,错误代码如下: 在realtime.exe中的0x00007FFE838D6660(nvcuda.dll)处引发异常:0xC0000005:访问冲突读取位置0x000001113AE10000 谷歌没有给我答案,我也不知道从现在开始该怎么做cudaMemcpy2D未处理的异常 我是C++新手(CUDA和OpenCV),所以我对我身边的错误感到抱歉。 我有一个使用Cuda的现有代码。最近它使用.png(被解码)作为输入,但现在我使用相机生成实时图像。这些图像是代码的新输入。这是: using namespace cv; INT height = 2160; INT width = 3840; Mat image(height, width, CV_8UC3); size_t pitch; uint8_t* image_gpu; // capture image VideoCapture camera(0); camera.set(CAP_PROP_FRAME_WIDTH, width); camera.set(CAP_PROP_FRAME_HEIGHT, height); camera.read(image); // here I checked if image is definitly still a CV_8UC3 Mat with the initial height and width; and it is cudaMallocPitch(&image_gpu, &pitch, width * 4, height); // here I use cv::Mat::data to get the pointer to the data of the image: cudaMemcpy2D(image_gpu, pitch, image.data, width*4, width*4, height, cudaMemcpyHostToDevice);,c++,opencv,cuda,C++,Opencv,Cuda,代码可以编译,但在最后一行(cudaMemcpy2D)出现“异常抛出”,错误代码如下: 在realtime.exe中的0x00007FFE838D6660(nvcuda.dll)处引发异常:0xC0000005:访问冲突读取位置0x000001113AE10000 谷歌没有给我答案,我也不知道从现在开始该怎么做 谢谢你的提示 将OpenCV Mat复制到使用CudamAllocPictch分配的设备内存中的一种相当通用的方法是利用Mat对象的step成员。此外,在分配设备内存时,您必须有视觉直觉
谢谢你的提示 将OpenCV Mat复制到使用
CudamAllocPictch
分配的设备内存中的一种相当通用的方法是利用Mat
对象的step
成员。此外,在分配设备内存时,您必须有视觉直觉,知道如何分配设备内存以及如何将Mat
对象复制到设备内存中。下面是一个简单的示例,演示了使用VideoCapture
捕获视频帧的过程
#include<iostream>
#include<cuda_runtime.h>
#include<opencv2/opencv.hpp>
using std::cout;
using std::endl;
size_t getPixelBytes(int type)
{
switch(type)
{
case CV_8UC1:
case CV_8UC3:
return sizeof(uint8_t);
break;
case CV_16UC1:
case CV_16UC3:
return sizeof(uint16_t);
break;
case CV_32FC1:
case CV_32FC3:
return sizeof(float);
break;
case CV_64FC1:
case CV_64FC3:
return sizeof(double);
break;
default:
return 0;
}
}
int main()
{
cv::VideoCapture cap(0);
cv::Mat frame;
if(cap.grab())
{
cap.retrieve(frame);
}
else
{
cout<<"Cannot read video"<<endl;
return -1;
}
uint8_t* gpu_image;
size_t gpu_pitch;
//Get number of bytes occupied by a single pixel. Although VideoCapture mostly returns CV_8UC3 type frame thus pixelBytes is 1 , but just in case.
size_t pixelBytes = getPixelBytes(frame.type());
//Number of actual data bytes occupied by a row.
size_t frameRowBytes = frame.cols * frame.channels * pixelBytes;
//Allocate pitch linear memory on device
cudaMallocPitch(&gpu_image, &gpu_pitch, frameRowBytes , frame.rows);
//Copy memory from frame to device mempry
cudaMemcpy2D(gpu_image, gpu_pitch, frame.ptr(), frame.step, frameRowBytes, frame.rows, cudaMemcpyHostToDevice);
//Rest of the code ...
return 0;
}
#包括
#包括
#包括
使用std::cout;
使用std::endl;
大小\u t获取像素字节(整数类型)
{
开关(类型)
{
案例CV_8UC1:
案例CV_8UC3:
返回大小of(uint8_t);
打破
案例CV_16UC1:
案例CV_16UC3:
返回大小of(uint16_t);
打破
案例CV_32FC1:
案例CV_32FC3:
返回sizeof(浮动);
打破
案例CV_64FC1:
案例CV_64FC3:
返回sizeof(双倍);
打破
违约:
返回0;
}
}
int main()
{
cv::视频捕获上限(0);
cv::垫架;
if(cap.grab())
{
取回盖(框架);
}
其他的
{
这里的源节距(第四个参数)不是width
吗?如果您的像素类型是CV\u 8UC3
,那么为什么要乘以4,所以是3个通道?请验证您的总矩阵数据长度实际上是width*height*3
。您是否检查了cudamallocitch
返回值?