check if threadIdx.x > 0 && threadIdx.y > 0

This commit is contained in:
2023-06-27 20:38:59 +02:00
parent 20d8872930
commit 4d8dd20b06

View File

@@ -21,7 +21,7 @@ __global__ void tvDenoisingKernel(float* image, int width, int height, float lam
// Perform TV denoising iteratively
for (int iteration = 0; iteration < maxIterations; ++iteration)
{
// Calculate the gradients using central differences
// Calculate the gradients using central differences
gradientX[threadIdx.x][threadIdx.y] = image[index + 1] - image[index - 1];
gradientY[threadIdx.x][threadIdx.y] = image[index + width] - image[index - width];
@@ -29,10 +29,12 @@ __global__ void tvDenoisingKernel(float* image, int width, int height, float lam
__syncthreads();
// Apply TV denoising update rule
updatedImage[threadIdx.x][threadIdx.y] = image[index] + lambda * (
gradientX[threadIdx.x][threadIdx.y] - gradientX[threadIdx.x - 1][threadIdx.y] +
gradientY[threadIdx.x][threadIdx.y] - gradientY[threadIdx.x][threadIdx.y - 1]
);
if (threadIdx.x > 0 && threadIdx.y > 0) {
updatedImage[threadIdx.x][threadIdx.y] = image[index] + lambda * (
gradientX[threadIdx.x][threadIdx.y] - gradientX[threadIdx.x - 1][threadIdx.y] +
gradientY[threadIdx.x][threadIdx.y] - gradientY[threadIdx.x][threadIdx.y - 1]
);
}
// Update the global image array with the updated pixel values
image[index] = updatedImage[threadIdx.x][threadIdx.y];
@@ -65,6 +67,12 @@ extern "C" void TVDenoising(cv::Mat& image, float lambda, int maxIterations)
// Invoke the TV denoising kernel
tvDenoisingKernel<<<gridSize, blockSize>>>(d_image, width, height, lambda, maxIterations);
// Check for errors during kernel launch
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
}
// Copy the denoised image data back from device to host
cudaMemcpy(floatImage.ptr<float>(0), d_image, width * height * sizeof(float), cudaMemcpyDeviceToHost);