【问题标题】:Merging two 1d histograms into one将两个一维直方图合并为一个
【发布时间】:2018-05-01 08:48:31
【问题描述】:

我得到了两个使用标准 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 直方图?

【问题讨论】:

    标签: c++ opencv image-processing


    【解决方案1】:

    是的,你可以。 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;
    

    希望对你有所帮助。

    【讨论】:

    • 谢谢,没想到这么简单。您能否查看我的代码(我刚刚编辑了问题)并告诉我我在绘图时做错了什么?直方图似乎被裁剪了。
    猜你喜欢
    • 2018-11-14
    • 2014-06-14
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多