Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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