Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV3.0 cuda::calcHist()

I have been trying for the last couple of days to get histogram calculation done using CUDA but cant seem to get it to work

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); .... Then I download the hist to Mat vars and display the histogram using the same as that described in http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

But the values are of the chart

click to hide/show revision 2
No.2 Revision

updated 2015-06-01 08:26:35 -0600

berak gravatar image

OpenCV3.0 cuda::calcHist()

I have been trying for the last couple of days to get histogram calculation done using CUDA but cant seem to get it to work

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);
....

Then I download the hist to Mat vars and display the histogram using the same as that described in http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

But the values are of the chart

OpenCV3.0 cuda::calcHist()

Updated code paste. I have been trying for was using cuda::normalize() instead of cv2::normalize() and feeding it GpuMat from cuda::calcHist() but the last couple of days to get histogram calculation done using values returned from calcHist were b0rked. The calcHist() without CUDA but cant seem to get it to work 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);
}

Then I download the hist to Mat vars and display the histogram using the same as that described in http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html}

But the values are of the chart

OpenCV3.0 cuda::calcHist()cuda::calcHist() does not return correct values

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);
}

}

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

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);
}

}