-
Notifications
You must be signed in to change notification settings - Fork 903
/
example_13-01.cpp
56 lines (45 loc) · 1.4 KB
/
example_13-01.cpp
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
53
54
55
56
// Example 13-1. Histogram computation and display
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
int main( int argc, char** argv ){
if(argc != 2) {
cout << "\n// Example 13-1. Histogram computation and display" << endl;
cout << "\nComputer Color Histogram\nUsage: " <<argv[0] <<" <imagename>\n" << endl;
return -1;
}
cv::Mat src = cv::imread( argv[1],1 );
if( src.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; }
// Compute the HSV image, and decompose it into separate planes.
//
cv::Mat hsv;
cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);
float h_ranges[] = {0, 180}; // hue is [0, 180]
float s_ranges[] = {0, 256};
const float* ranges[] = {h_ranges, s_ranges};
int histSize[] = {30, 32}, ch[] = {0, 1};
cv::Mat hist;
// Compute the histogram
//
cv::calcHist(&hsv, 1, ch, cv::noArray(), hist, 2, histSize, ranges, true);
cv::normalize(hist, hist, 0, 255, cv::NORM_MINMAX);
int scale = 10;
cv::Mat hist_img(histSize[0]*scale, histSize[1]*scale, CV_8UC3);
// Draw our histogram.
//
for( int h = 0; h < histSize[0]; h++ ) {
for( int s = 0; s < histSize[1]; s++ ){
float hval = hist.at<float>(h, s);
cv::rectangle(
hist_img,
cv::Rect(h*scale,s*scale,scale,scale),
cv::Scalar::all(hval),
-1
);
}
}
cv::imshow("image", src);
cv::imshow("H-S histogram", hist_img);
cv::waitKey();
return 0;
}