cv::cuda::calcHist() giving zero array of length 256 as output

I am getting a strange result while calculating histogram using cv::cuda::calcHist(). The result what I am getting is an array 256 zeros. This problem is persisting when using CUDA version of calcHist() but not in CPU version cv::calcHist(). Following is the code snap I am utilising.

cv::cuda::GpuMat input_g, gray_img_g, hist_gray_g;            // GPU Mat variables
cv::Mat hist_gray_c;                                          // CPU Mat variables 
cv::cuda::cvtColor(input_g, gray_img_g, cv::COLOR_BGR2GRAY);  // getting gray-scale image 
cv::cuda::calcHist(gray_img_g, hist_gray_g);                  // calculating histogram on GPU
hist_gray_g.download(hist_gray_c);                            // downloading the histogram to CPU memory

// checking the histogram values
for (int i = 0; i < 256; i++){ 
    std::cout<<hist_gray_c.at<float>(i)<<std::endl;
}

The data type for the bins should be int not float. e.g.

for (int i = 0; i < 256; i++){ 
    std::cout<<hist_gray_c.at<int>(i)<<std::endl;
}

From the docs

hist Destination histogram with one row, 256 columns, and the CV_32SC1 type.

using int in-place of float resolved my issue. Thank you.

1 Like