2015-10-05 214 views
1

我想計算累計直方圖,我已經完成了直方圖計算,下面是它的代碼。計算累積直方圖

我已經轉換了iamge爲YCbCr通道和Y通道應用直方圖

感謝您的幫助

#include "opencv2/core/core.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "iostream" 

#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <stdio.h> 

using namespace cv; 
using namespace std; 

void histogramcalculation(const Mat &Image, Mat &histoImage) 
{ 
    int histSize = 255; 
// Set the ranges (for B,G,R)) 

float range[] = { 0, 256 } ; 
const float* histRange = { range }; 
bool uniform = true; bool accumulate = false; 
Mat b_hist, g_hist, r_hist; 
vector<Mat> bgr_planes; 
split(Image, bgr_planes); 

// Compute the histograms: 

calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate); 


// Draw the histogram 

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

    // Normalize the result to [ 0, histImage.rows ] 

normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); 


    // Draw 

    for(int i = 1; i < histSize; 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); 



} 
histoImage= histImage; 

} 

    int main() 
    { 

Mat src, imageq,ycbcr; 
Mat histImage; 

// Read original image 

    src = imread("3.jpg"); 
    if(! src.data) 
    { printf("Error imagen\n"); exit(1); } 

    cvtColor(src, ycbcr, CV_RGB2YCrCb); 
    vector <Mat> planes; 
    split(ycbcr,planes); 

    // Separate the image in 3 places (B, G and R) 



    // Display results 

    imshow("Source image", src); 

    // Calculate the histogram to each channel of the source image 

    histogramcalculation(planes[0], histImage); 

    // Display the histogram for each colour channel 

    imshow("Colour Image Histogram", histImage); 

    // Wait until user exits the program 

    waitKey(); 
    return 0; 
    } 
+1

和你的問題是...? – Mariano

+1

您需要'accumulate = true;'來執行累積直方圖 – Miki

+0

如何計算累積直方圖 –

回答

2

OpenCV的cv::calcHistaccumulate標誌,但這並不做一個累積性直方圖,它只是不會在開始時將直方圖設置爲零,因此您可以在多個圖像上累積直方圖。

你想要做的是在得到直方圖之後,通過將所有先前的直方圖箱的總和加到每個箱中來自己累積它。

cv::Mat hist; 

cv::calcHist(&eyeROI, 1, 0, Mat(), hist, 1, &histSize, &histRange); 
cv::Mat accumulatedHist = hist.clone(); 
for (int i = 1; i < histSize; i++) { 
    accumulatedHist.at<float>(i) += accumulatedHist.at<float>(i - 1); 
    std::cout << "Accumulated : " << accumulatedHist.at<float>(i) << ", original = " << hist.at<float>(i) << std::endl; 
} 

std :: cout添加,所以你可以看看結果。

+1

我真的認爲這是一個常見操作,它將在opencv上實現。謝謝! –