Cannot get OpenCV3.0 cuda::calcHist() to return correct values

asked 2015-06-01 08:21:17 -0500

abhiguru gravatar image

updated 2015-06-05 01:27:43 -0500

Updated code paste. I was using cuda::normalize() instead of cv2::normalize() and feeding it GpuMat from cuda::calcHist() but the values returned from calcHist were b0rked. The calcHist() without CUDA works okay and so does multiple CUDA samples. Using OpenCV3.0rc1 with Cuda 7.0 on GTX470 and Ubuntu 14.04

void displayHist2(Mat b_hist, Mat g_hist, Mat r_hist){

int hist_w = 1024;
int hist_h = 800;
int bin_w = cvRound( (double) hist_w/256 );

Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0,0,0));
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

Size s = b_hist.size();
int rows = s.height;
int cols = s.width;

if(rows == 0 && cols == 0 ) return;

for( int i = 1; i < 256 ; i++ ){
    line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
                     Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
                     Scalar( 255, 0, 0), 2, 8, 0  );
    line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
                     Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
                     Scalar( 0, 255, 0), 2, 8, 0  );
    line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
                     Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
                     Scalar( 0, 0, 255), 2, 8, 0  );
}

namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage );
waitKey(0);

}

int main(int argc, char* argv[]){

Mat h_edges;
namedWindow("edges", 1);

Mat h_frame, h_dstframe, h_result;
Mat b_hist, g_hist, r_hist;

string filename = "default.mp4";
if(argc > 1 && argv[1] != string("")) filename = argv[1];

VideoCapture h_cap(filename);
if(!h_cap.isOpened()) return -1;

while(1){

    h_cap >> h_frame;
    if( !h_frame.data ) { return -1; }

    cvtColor(h_frame, h_frame, CV_BGR2Lab);
    imshow("edges", h_frame);

    cuda::GpuMat d_bdst, d_gdst, d_rdst, d_src;
    cuda::GpuMat d_matarr[3];

    d_src.upload(h_frame);
    cuda::split(d_src,d_matarr);

    cuda::calcHist(d_matarr[0], d_bdst);
    cuda::calcHist(d_matarr[1], d_gdst);
    cuda::calcHist(d_matarr[2], d_rdst);

    d_bdst.download(b_hist);
    d_gdst.download(g_hist);
    d_rdst.download(r_hist);

    displayHist2(b_hist, g_hist, r_hist);
}

}

edit retag flag offensive close merge delete

Comments

something seems missing from your post. can you try to edit again ?

also, we can't see any problem description or error msg.

berak gravatar imageberak ( 2015-06-01 08:27:44 -0500 )edit

There is no error in code compilation or at runtime, It is the Histogram values calculated by using cuda::calcHist are incorrect for some reason. Am i doing something wrong ?

abhiguru gravatar imageabhiguru ( 2015-06-01 23:39:17 -0500 )edit

Did you ever get this to work? I'm experiencing the same problem in OpenCV 3.1. One clue is that the Mat allocated to return the data is CV_32SC1 (which matches the documentation) but I'm finding the return datatype is actually an integer Mat.

jaybo_nomad gravatar imagejaybo_nomad ( 2016-04-05 20:50:22 -0500 )edit