【发布时间】:2021-12-02 19:02:24
【问题描述】:
我是 CUDA 的新手,我正在尝试在图像上实现平滑卷积,到目前为止我有这个,但结果是错误的。 不确定我的偏移运动是否正确。有什么帮助吗?
__global__ void smooth(unsigned char* device_out_image, float kernel_size, unsigned char* device_input_imag, int height, int width)
{
int pos_x = threadIdx.x + blockIdx.x * blockDim.x;//x coordinate of pixel
int pos_y = threadIdx.y + blockIdx.y * blockDim.y;//y coordinate of pixel
if (pos_x < width && pos_y < height)
{
unsigned char r = device_input_imag[pos_y * width + pos_x];//absolute pixel position
unsigned char g = device_input_imag[(height + pos_y) * width + pos_x];
unsigned char b = device_input_imag[(height * 2 + pos_y) * width + pos_x];
//also mix value with the intensity instead of the range x
float sumR = float(0.0f);
float sumG = float(0.0f);
float sumB = float(0.0f);
for (int i = (-1 * 15); i <= 15; i++)
for (int j = (-1 *15); j <= 15; j++)
{
if (pos_x + j > 0 && pos_y + i > 0 && pos_x + j <= width && pos_y + i <= height)
{
sumR += (float)device_input_imag[(pos_y + i) * width + (pos_x + j)]/255.0;
sumG += (float)device_input_imag[(height + (pos_y + i)) * width + (pos_x + j)]/255.0;
sumB += (float)device_input_imag[(height * 2 + (pos_y + i)) * width + (pos_x + j)]/255.0;
}
}
sumR = sumR / (15 * 15);
sumG = sumG / (15 * 15);
sumB = sumB / (15 * 15);
device_out_image[pos_y * width + pos_x] = (unsigned char)(sumR * 255.0);
device_out_image[(height + pos_y) * width + pos_x] = (unsigned char)(sumG * 255.0) ;
device_out_image[(height * 2 + pos_y) * width + pos_x] = (unsigned char)(sumB *255.0 );
if (device_out_image[pos_y * width + pos_x] > 255)
device_out_image[pos_y * width + pos_x] = 255;
if (device_out_image[(height + pos_y) * width + pos_x] > 255)
device_out_image[(height + pos_y) * width + pos_x] = 255;
if (device_out_image[(height * 2 + pos_y) * width + pos_x] > 255)
device_out_image[(height * 2 + pos_y) * width + pos_x] = 255;
}
}
【问题讨论】:
-
请尽量更清楚地解释您的问题。您可以重新编辑您的问题。
-
你的循环从 -15 到 15,所以你有 31*31 个值:检查 Peter 提到的上限条件
标签: cuda