Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | #if !defined COLHISTOGRAM |
2 | #define COLHISTOGRAM |
||
3 | |||
4 | #include <opencv2\core\core.hpp> |
||
5 | #include <opencv2\imgproc\imgproc.hpp> |
||
6 | |||
7 | class ColorHistogram { |
||
8 | |||
9 | private: |
||
10 | |||
11 | int histSize[3]; |
||
12 | float hranges[2]; |
||
13 | const float* ranges[3]; |
||
14 | int channels[3]; |
||
15 | |||
16 | public: |
||
17 | |||
18 | ColorHistogram() { |
||
19 | |||
20 | // Prepare arguments for a color histogram |
||
21 | histSize[0]= histSize[1]= histSize[2]= 256; |
||
22 | hranges[0]= 0.0; // BRG range |
||
23 | hranges[1]= 255.0; |
||
24 | ranges[0]= hranges; // all channels have the same range |
||
25 | ranges[1]= hranges; |
||
26 | ranges[2]= hranges; |
||
27 | channels[0]= 0; // the three channels |
||
28 | channels[1]= 1; |
||
29 | channels[2]= 2; |
||
30 | } |
||
31 | |||
32 | // Computes the histogram. |
||
33 | cv::MatND getHistogram(const cv::Mat &image) { |
||
34 | |||
35 | cv::MatND hist; |
||
36 | |||
37 | // BGR color histogram |
||
38 | hranges[0]= 0.0; // BRG range |
||
39 | hranges[1]= 255.0; |
||
40 | channels[0]= 0; // the three channels |
||
41 | channels[1]= 1; |
||
42 | channels[2]= 2; |
||
43 | |||
44 | // Compute histogram |
||
45 | cv::calcHist(&image, |
||
46 | 1, // histogram of 1 image only |
||
47 | channels, // the channel used |
||
48 | cv::Mat(), // no mask is used |
||
49 | hist, // the resulting histogram |
||
50 | 3, // it is a 3D histogram |
||
51 | histSize, // number of bins |
||
52 | ranges // pixel value range |
||
53 | ); |
||
54 | |||
55 | return hist; |
||
56 | } |
||
57 | |||
58 | // Computes the histogram. |
||
59 | cv::SparseMat getSparseHistogram(const cv::Mat &image) { |
||
60 | |||
61 | cv::SparseMat hist(3,histSize,CV_32F); |
||
62 | |||
63 | // BGR color histogram |
||
64 | hranges[0]= 0.0; // BRG range |
||
65 | hranges[1]= 255.0; |
||
66 | channels[0]= 0; // the three channels |
||
67 | channels[1]= 1; |
||
68 | channels[2]= 2; |
||
69 | |||
70 | // Compute histogram |
||
71 | cv::calcHist(&image, |
||
72 | 1, // histogram of 1 image only |
||
73 | channels, // the channel used |
||
74 | cv::Mat(), // no mask is used |
||
75 | hist, // the resulting histogram |
||
76 | 3, // it is a 3D histogram |
||
77 | histSize, // number of bins |
||
78 | ranges // pixel value range |
||
79 | ); |
||
80 | |||
81 | return hist; |
||
82 | } |
||
83 | |||
84 | // Computes the 2D ab histogram. |
||
85 | // BGR source image is converted to Lab |
||
86 | cv::MatND getabHistogram(const cv::Mat &image) { |
||
87 | |||
88 | cv::MatND hist; |
||
89 | |||
90 | // Convert to Lab color space |
||
91 | cv::Mat lab; |
||
92 | cv::cvtColor(image, lab, CV_BGR2Lab); |
||
93 | |||
94 | // Prepare arguments for a 2D color histogram |
||
95 | hranges[0]= -128.0; |
||
96 | hranges[1]= 127.0; |
||
97 | channels[0]= 1; // the two channels used are ab |
||
98 | channels[1]= 2; |
||
99 | |||
100 | // Compute histogram |
||
101 | cv::calcHist(&lab, |
||
102 | 1, // histogram of 1 image only |
||
103 | channels, // the channel used |
||
104 | cv::Mat(), // no mask is used |
||
105 | hist, // the resulting histogram |
||
106 | 2, // it is a 2D histogram |
||
107 | histSize, // number of bins |
||
108 | ranges // pixel value range |
||
109 | ); |
||
110 | |||
111 | return hist; |
||
112 | } |
||
113 | |||
114 | // Computes the 1D Hue histogram with a mask. |
||
115 | // BGR source image is converted to HSV |
||
116 | cv::MatND getHueHistogram(const cv::Mat &image) { |
||
117 | |||
118 | cv::MatND hist; |
||
119 | |||
120 | // Convert to Lab color space |
||
121 | cv::Mat hue; |
||
122 | cv::cvtColor(image, hue, CV_BGR2HSV); |
||
123 | |||
124 | // Prepare arguments for a 1D hue histogram |
||
125 | hranges[0]= 0.0; |
||
126 | hranges[1]= 180.0; |
||
127 | channels[0]= 0; // the hue channel |
||
128 | |||
129 | // Compute histogram |
||
130 | cv::calcHist(&hue, |
||
131 | 1, // histogram of 1 image only |
||
132 | channels, // the channel used |
||
133 | cv::Mat(), // no mask is used |
||
134 | hist, // the resulting histogram |
||
135 | 1, // it is a 1D histogram |
||
136 | histSize, // number of bins |
||
137 | ranges // pixel value range |
||
138 | ); |
||
139 | |||
140 | return hist; |
||
141 | } |
||
142 | |||
143 | cv::Mat colorReduce(const cv::Mat &image, int div=64) { |
||
144 | |||
145 | int n= static_cast<int>(log(static_cast<double>(div))/log(2.0)); |
||
146 | // mask used to round the pixel value |
||
147 | uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0 |
||
148 | |||
149 | cv::Mat_<cv::Vec3b>::const_iterator it= image.begin<cv::Vec3b>(); |
||
150 | cv::Mat_<cv::Vec3b>::const_iterator itend= image.end<cv::Vec3b>(); |
||
151 | |||
152 | // Set output image (always 1-channel) |
||
153 | cv::Mat result(image.rows,image.cols,image.type()); |
||
154 | cv::Mat_<cv::Vec3b>::iterator itr= result.begin<cv::Vec3b>(); |
||
155 | |||
156 | for ( ; it!= itend; ++it, ++itr) { |
||
157 | |||
158 | (*itr)[0]= ((*it)[0]&mask) + div/2; |
||
159 | (*itr)[1]= ((*it)[1]&mask) + div/2; |
||
160 | (*itr)[2]= ((*it)[2]&mask) + div/2; |
||
161 | } |
||
162 | |||
163 | return result; |
||
164 | } |
||
165 | |||
166 | }; |
||
167 | |||
168 | |||
169 | #endif |