我是OpenCV的新手,學習繪製單通道圖像的直方圖。當我運行程序時,我只會看到一條白線。請告訴我這段代碼有什麼問題。無法在OpenCV中獲取直方圖圖像
int histSize[1] = {256};
float hranges[2] = {0.0, 255.0};
const float* ranges[1] = {hranges};
int channels[1] = {0};
cv::MatND getHistogram(const cv::Mat &image) {
cv::MatND hist;
cv::calcHist(&image,1,channels,cv::Mat(),hist,1,histSize,ranges);
return hist;
}
cv::Mat getHistogramImage(const cv::Mat &image) {
cv::MatND hist = getHistogram(image);
double minVal = 0;
double maxVal = 0;
cv::minMaxLoc(hist,&minVal,&maxVal,0,0);
cv::Mat histImg(histSize[0],histSize[0],CV_8U,cv::Scalar(255));
int hpt = static_cast<int>(0.9*histSize[0]);
for(int h=0; h<256; ++h){
float binVal = hist.at<float>(h);
int intensity = static_cast<int>(binVal*hpt/maxVal);
cv::line(histImg,cv::Point(h,histSize[0]),
cv::Point(h,histSize[0]-intensity),
cv::Scalar::all(0));
}
return histImg;
}
你看到的[官方文檔](http://docs.opencv.org/doc/教程/ imgproc /直方圖/ histogram_calculation/histogram_calculation.html)直方圖計算..?我認爲它會給你所有你需要的答案.. – scap3y
是的,我指的是同一頁。謝謝。 – Shubham
@Shubham從[該頁面](http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/cpp/tutorial_code/Histograms_Matching/calcHist_Demo.cpp)獲取代碼,進行編譯,運行它並確保它能正常工作。然後,適應單通道圖像。我相信你會發現你的錯誤。 – karlphillip