计算具有cuda内核的图像中黑色像素的数量

时间:2019-05-07 09:53:18

标签: c++ image-processing cuda gpu

我想让内核找到图像中最黑的像素在哪里,首先将图像阈值化,在内核中将图像划分为较小的区域,我们计算每组中黑色像素的数量以查看最大数量是多少,我正在尝试编写该内核,我是Cuda的新用户,结果是错误的,因为没有黑色像素,而所有像素都是白色?

__global__ void CudaKernel
(cuda::PtrStepSzb input,
    const int outputWidth,
    const int outputHeight,
    const int inputWidthStep,
    const float pixelGroupSizeX,
    const float pixelGroupSizeY)
{

    //2D Index of current thread
    const int outputXIndex = blockIdx.x * blockDim.x + threadIdx.x;
    const int outputYIndex = blockIdx.y * blockDim.y + threadIdx.y;


    //Only valid threads perform memory I/O
    if ((outputXIndex < outputWidth) && (outputYIndex < outputHeight))
    {


        // Compute the size of the area of pixels to be in one group
        const float pixelGroupArea = pixelGroupSizeX * pixelGroupSizeY;

        // Compute the pixel group area in the input image
        const int intputXIndexStart = int(outputXIndex * pixelGroupSizeX);
        const int intputXIndexEnd = int(intputXIndexStart + pixelGroupSizeX);
        const float intputYIndexStart = int(outputYIndex * pixelGroupSizeY);
        const float intputYIndexEnd = int(intputYIndexStart + pixelGroupSizeY);

        int blackPixelsSum = 0;
        for (int intputYIndex = intputYIndexStart; intputYIndex < intputYIndexEnd; ++intputYIndex) {
            for (int intputXIndex = intputXIndexStart; intputXIndex < intputXIndexEnd; ++intputXIndex) {
                int input_tid = intputYIndex * inputWidthStep + intputXIndex;
                if (input[input_tid] == 0) {
                    blackPixelsSum++;
                }

            }
        }

        printf("%d, ", blackPixelsSum);
    }


}

0 个答案:

没有答案