How to use nppiMean_8u_C3R?

hello.
Here is my code. I want to get brightness of a image. but the result is 0.
Anyone can help? thank you.

code:

NppiSize size{0, 0};
        size = {image_width, image_height};
        
        Npp8u *input_npp_bgr_buff = nullptr;
        input_npp_bgr_buff = nppiMalloc_8u_C3(image_width, image_height, &bgr_step);
        
        //copy rgb image here
        
        Npp64f mean[3];
        cudaMalloc((void **)&d_mean, sizeof(Npp64f));
        int bufSize{ 0 };
        nppiMeanGetBufferHostSize_8u_C3R(size, &bufSize);
        cudaMalloc((void **)(&npp_device_buff), bufSize);
        npp_convert_status = nppiMean_8u_C3R(input_npp_bgr_buff, bgr_step, size, npp_device_buff, mean);
int brightness = mean[2] ;
        cout << "mean:" << mean[0] << "," << mean[1] << "," << mean[2] << endl;//print:mean:0,0,0

The mean storage in the function call must be located in device memory.

example:

# cat t12.cu
#include <nppi.h>
#include <iostream>

int main(){
        int image_width = 1024;
        int image_height = 1024;

        NppiSize size = {image_width, image_height};

        int bgr_step;
        Npp8u *input_npp_bgr_buff = nppiMalloc_8u_C3(image_width, image_height, &bgr_step);

        //copy rgb image here
        cudaMemset(input_npp_bgr_buff, 1, image_height*bgr_step);

        Npp64f mean[3];
        Npp64f *pmean;
        cudaMalloc(&pmean, 3*sizeof(Npp64f));
        int bufSize{ 0 };
        nppiMeanGetBufferHostSize_8u_C3R(size, &bufSize);
        Npp8u *npp_device_buff;
        cudaMalloc((void **)(&npp_device_buff), bufSize);
        NppStatus npp_convert_status = nppiMean_8u_C3R(input_npp_bgr_buff, bgr_step, size, npp_device_buff, pmean);
        cudaMemcpy(mean, pmean, 3*sizeof(Npp64f), cudaMemcpyDeviceToHost);
        std::cout << "mean:" << mean[0] << "," << mean[1] << "," << mean[2] << std::endl;
}
# nvcc -o t12 t12.cu -lnppist -lnppisu
# compute-sanitizer ./t12
========= COMPUTE-SANITIZER
mean:1,1,1
========= ERROR SUMMARY: 0 errors
#
1 Like

Thank you very much! It works fine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.