Advertisement
OscarAHB

Untitled

Sep 14th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include "opencv2/objdetect/objdetect.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. using namespace cv;
  8.  
  9. int main(int, char**)
  10. {
  11.     Mat gray = imread("../data/12.PNG", 0);
  12.     namedWindow("Gray", 1);    imshow("Gray", gray);
  13.  
  14.     // Initialize parameters
  15.     int histSize = 256;    // bin size
  16.     float range[] = { 0, 255 };
  17.     const float *ranges[] = { range };
  18.  
  19.     // Calculate histogram
  20.     MatND hist;
  21.     calcHist(&gray, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false);
  22.  
  23.     // Show the calculated histogram in command window
  24.     double total;
  25.     total = gray.rows * gray.cols;
  26.     for (int h = 0; h < histSize; h++)
  27.     {
  28.         float binVal = hist.at<float>(h);
  29.         cout << " " << binVal;
  30.     }
  31.  
  32.     // Plot the histogram
  33.     int hist_w = 512; int hist_h = 400;
  34.     int bin_w = cvRound((double)hist_w / histSize);
  35.  
  36.     Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(0, 0, 0));
  37.     normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
  38.  
  39.     for (int i = 1; i < histSize; i++)
  40.     {
  41.         line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(hist.at<float>(i - 1))),
  42.             Point(bin_w*(i), hist_h - cvRound(hist.at<float>(i))),
  43.             Scalar(255, 0, 0), 2, 8, 0);
  44.     }
  45.  
  46.     namedWindow("Result", 1);    imshow("Result", histImage);
  47.  
  48.     waitKey(0);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement