简体   繁体   English

CUDA - 将RGB图像转换为灰度

[英]CUDA - convert RGB image to Grayscale

I am starting to learn CUDA GPU programming from Udacity video course (course is 2 yrs old). 我开始从Udacity视频课程学习CUDA GPU编程(课程是2岁)。 I am using CUDA 5.5 with Visual Studio Express 2012 (students edition, so not all features of CUDA debugging is not available) on Nvidia GeForce GT 630M GPU . 我在Nvidia GeForce GT 630M GPU上使用CUDA 5.5和Visual Studio Express 2012 (学生版,因此并非所有CUDA调试功能都不可用)。

Just implemented some vector addition and other simple operations. 刚刚实现了一些向量加法和其他简单的操作。

Now I am trying to convert a RGB image to Grayscale . 现在我正在尝试将RGB图像转换为灰度图像 I am reading image with help of OpenCV. 我在OpenCV的帮助下阅读图像。 (Anyway I failed whatever methods I tried. That is why I am here) (无论如何,我失败了我尝试的任何方法。这就是我在这里的原因)

Below is my .cpp file : https://gist.github.com/abidrahmank/7020863 下面是我的.cpp文件: https//gist.github.com/abidrahmank/7020863

Below is my .cu file : https://gist.github.com/abidrahmank/7020910 以下是我的.cu文件: https ://gist.github.com/abidrahmank/7020910

My input image is a simple 64x64 color image (Actually I used 512x512 image first, didn't work, so brought down to 64x64 to check if that is the problem. It doesn't seem so) 我的输入图像是一个简单的64x64彩色图像(实际上我首先使用了512x512图像,没有工作,所以降到64x64以检查是否是问题。它似乎不是这样)

Problem 问题

My output image of CUDA implementation is a white image . 我的CUDA实现的输出图像是白色图像 All value 255. Somewhere here and there, there are some gray pixels, may be less than 1%. 所有值都是255.在这里和那里的某处,有一些灰色像素,可能小于1%。 Remaining everything is white. 剩下的一切都是白色的。

What I tried: 我尝试了什么:

For three days, I tried following things: 三天,我尝试了以下事情:

  1. I thought problem may be due image size, so that number of threads may not be optimal or something like that, So reduced image size. 我认为问题可能是由于图像大小,因此线程数可能不是最佳或类似的东西,因此缩小图像大小。 Still same result. 还是一样的结果。
  2. I tried a similar example, created a 64x64 array. 我尝试了一个类似的例子,创建了一个64x64阵列。 Take its two pixels at a time, and find the square of their sums, and it worked fine. 一次取两个像素,找到它们的总和的平方,它工作正常。 Here is the code : https://gist.github.com/abidrahmank/7021023 这是代码: https//gist.github.com/abidrahmank/7021023
  3. Started checking data one-by-one at each stage. 开始在每个阶段逐个检查数据。 Input image just before loading to GPU is fine. 在加载到GPU之前输入图像很好。 But input data, when I checked inside kernel, is always 255. (Check line 14 here ) 但是当我在内核中检查时,输入数据总是255.在这里检查第14行
  4. Finally I set all GPU data to zero using CudaMemset and checked input data inside kernel, it is still 255. 最后,我使用CudaMemset将所有GPU数据设置为零并检查内核中的输入数据,它仍然是255。

So I don't have any other option to do other asking at StackOverflow. 所以我没有任何其他选择在StackOverflow上做其他的询问。

Can anyone tell me what is the mistake I am making? 谁能告诉我我犯的错误是什么?

Your kernel signature says: 你的内核签名说:

__global__ void kernel(unsigned char* d_in, unsigned char* d_out)

But you call it like: 但你称之为:

kernel<<<rows,cols>>>(d_out, d_in);

Which one is in and which one is out ? 哪一个 ,哪一个是

Having done quite a bit of CUDA programming in the past, I would strongly recommend that you use Thrust instead of hand-crafting kernels. 在过去完成了相当多的CUDA编程之后,我强烈建议您使用Thrust而不是手工制作内核。 Even thrust::for_each is hard to beat with raw kernels. 即使thrust::for_each也很难被原始内核击败。

Besides the parameter issue indicated by DanielKO, you also have problems on thread/block settings. 除了DanielKO指出的参数问题,你还有线程/块设置问题。

Since you've already treat your 2-D image as a 1-D array, here's a good example showing how to set thread/block for data with arbitrary size. 由于您已经将二维图像视为一维数组,因此这里有一个很好的示例,说明如何为任意大小的数据设置线程/块。

https://developer.nvidia.com/content/easy-introduction-cuda-c-and-c https://developer.nvidia.com/content/easy-introduction-cuda-c-and-c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM