将两个1d直方图合并为一个

时间:2018-04-30 23:37:19

标签: c++ opencv image-processing

我得到了两个用标准 openCV 函数calcHist创建的直方图:

int getModels(string filename) {
    Mat src = imread(filename, 1);   
    if(!src.data) { return -1; }
    Mat imageHSV;
    cvtColor(src, imageHSV, COLOR_BGR2HSV);
    vector<Mat> bgr_planes; 
    split(imageHSV, bgr_planes);
    int histSize = 256;
    float range[] = { 0, 256 } ;
    const float* histRange = { range };
    bool uniform = true; bool accumulate = false;
    Mat g_hist, r_hist;
    calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
    calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
    cv::Mat combined_hist = g_hist + r_hist;
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound( (double) hist_w/histSize );
    Mat histImage(hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
    for( int i = 0; i < histSize; i++ )
    {
      line( histImage, Point( bin_w*(i-1), cvRound(combined_hist.at<float>(i-1)) ) ,
                       Point( bin_w*(i), cvRound(combined_hist.at<float>(i)) ),
                       Scalar( 19,90,87), 2, 8, 0  );
    }
    /// Display
        namedWindow(filename, CV_WINDOW_AUTOSIZE );
        imshow(filename, histImage );

        return 0;
    }

有没有办法将它们合并为一个combined_hist直方图?

1 个答案:

答案 0 :(得分:3)

是的,你可以。 OpenCV有cv::add,可以像这样使用:

cv::Mat combined_hist;

cv::add(g_hist, r_hist, combined_hist);

但是,由于cv::Mat重载了运算符,您可以这样做:

cv::Mat combined_hist = g_hist + r_hist;

希望对你有所帮助。