1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <opencv2/opencv.hpp> int main() { cv::Mat srcImage = cv::imread("circles.jpg"); if( !srcImage.data ) return 1; cv::imshow("srcImage",srcImage); cv::Mat srcGray; cv::cvtColor(srcImage,srcGray,cv::COLOR_BGR2GRAY); const int channels[1]={0}; const int histSize[1]={256}; float hranges[6]={0, 60, 120, 160, 220, 255}; const float* ranges[1] = {hranges}; cv::MatND hist; cv::calcHist( &srcGray, 1, channels, cv::Mat(), hist, 1, histSize, ranges ); double maxHist=0; cv::minMaxLoc(hist, 0, &maxHist, 0 ,0); int hist_Size = hist.rows; cv::Mat histImg(hist_Size, hist_Size, CV_8U, cv::Scalar(255)); for(int h = 0; h < hist_Size; h++) { float binVal = hist.at<float>(h); int intensity = static_cast<int>(binVal * hist_Size / maxHist); cv::line(histImg, cv::Point(h, hist_Size), cv::Point(h, hist_Size - intensity), cv::Scalar::all(0)); } cv::imshow("histImg", histImg); cv::waitKey(0); return 0; }
|