gpt4 book ai didi

c++ - CUDA图像处理错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:14:02 24 4
gpt4 key购买 nike

我正在从事一个小型图像处理项目。我想运行一个执行图像减法的 CUDA 程序。所以你有图像背景和具有相同背景但上面有一些其他东西的图​​像。一旦你减去图像,你就会得到剩下的东西。两张图片都是480*360,我的gpu是GTX780。我的程序抛出错误 ./main': free(): invalid next size (normal): 0x000000000126bd70 ***
中止(核心转储)
并且输出图像错误。我一直在努力解决这个问题。这是代码:

内核:

__global__ void add(unsigned char* a, unsigned char* b, unsigned char* c, int numCols, int numWidth) {
int i = blockIdx.x * blockDim.x + threadIdx.x; //Column
int j = blockIdx.y * blockDim.y + threadIdx.y; //Row
if(i < numWidth && j < numCols)
{
int idx = j * numCols + i;
c[idx] = b[idx] - a[idx];
}
}

和主要功能:

int main() {
CImg<unsigned char> img1("1.bmp");
CImg<unsigned char> img2("2.bmp");
//both images have the same size
int width = img1.width();
int height = img1.height();

int size = width * height * 3; //both images of same size

dim3 blockSize(16, 16, 1);
dim3 gridSize((width + blockSize.x - 1) / blockSize.x, (height + blockSize.y - 1) / blockSize.y, 1);

unsigned char *dev_a, *dev_b, *dev_c;

cudaMalloc((void**)&dev_a, size * (sizeof(unsigned char)));
cudaMalloc((void**)&dev_b, size * (sizeof(unsigned char)));
cudaMalloc((void**)&dev_c, size * (sizeof(unsigned char)));

cudaMemcpy(dev_a, img1, size * (sizeof(unsigned char)), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, img2, size * (sizeof(unsigned char)), cudaMemcpyHostToDevice);

add<<<gridSize, blockSize>>>(dev_a, dev_b, dev_c, height, width);

cudaMemcpy(img2, dev_c, size * (sizeof(unsigned char)), cudaMemcpyDeviceToHost);

img2.save("out.bmp");
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
return 0;
}

图像加载了 CImg图书馆。

最佳答案

问题在于在主机代码中错误地使用了 cimg 容器。根据documentation , 图像数据指针是通过data() 方法访问的,这意味着宿主代码中的cudaMemcpy 调用应该由img1.data()< 提供img2.data()

[此答案根据评论汇总并添加为社区 wiki 条目]

关于c++ - CUDA图像处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36343854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com