Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
#if !defined OFINDER
2
#define OFINDER
3
 
5 PointedEar 4
#include <opencv2/core/core.hpp>
5
#include <opencv2/imgproc/imgproc.hpp>
3 PointedEar 6
 
7
class ObjectFinder {
8
 
9
  private:
10
 
11
        float hranges[2];
12
    const float* ranges[3];
13
    int channels[3];
14
 
15
        float threshold;
16
        cv::MatND histogram;
17
        cv::SparseMat shistogram;
18
        bool isSparse;
19
 
20
  public:
21
 
22
        ObjectFinder() : threshold(0.1f), isSparse(false) {
23
 
24
                ranges[0]= hranges; // all channels have the same range 
25
                ranges[1]= hranges;
26
                ranges[2]= hranges;
27
        }
28
 
29
        // Sets the threshold on histogram values [0,1]
30
        void setThreshold(float t) {
31
 
32
                threshold= t;
33
        }
34
 
35
        // Gets the threshold
36
        float getThreshold() {
37
 
38
                return threshold;
39
        }
40
 
41
        // Sets the reference histogram
42
        void setHistogram(const cv::MatND& h) {
43
 
44
                isSparse= false;
45
                histogram= h;
46
                cv::normalize(histogram,histogram,1.0);
47
        }
48
 
49
        // Sets the reference histogram
50
        void setHistogram(const cv::SparseMat& h) {
51
 
52
                isSparse= true;
53
                shistogram= h;
54
                cv::normalize(shistogram,shistogram,1.0,cv::NORM_L2);
55
        }
56
 
57
        // Finds the pixels belonging to the histogram
58
        cv::Mat find(const cv::Mat& image) {
59
 
60
                cv::Mat result;
61
 
62
                hranges[0]= 0.0;        // range [0,255]
63
                hranges[1]= 255.0;
64
                channels[0]= 0;         // the three channels 
65
                channels[1]= 1;
66
                channels[2]= 2;
67
 
68
                if (isSparse) { // call the right function based on histogram type
69
 
70
                   cv::calcBackProject(&image,
71
                      1,            // one image
72
                      channels,     // vector specifying what histogram dimensions belong to what image channels
73
                      shistogram,   // the histogram we are using
74
                      result,       // the resulting back projection image
75
                      ranges,       // the range of values, for each dimension
76
                      255.0         // the scaling factor is chosen such that a histogram value of 1 maps to 255
77
                   );
78
 
79
                } else {
80
 
81
                   cv::calcBackProject(&image,
82
                      1,            // one image
83
                      channels,     // vector specifying what histogram dimensions belong to what image channels
84
                      histogram,    // the histogram we are using
85
                      result,       // the resulting back projection image
86
                      ranges,       // the range of values, for each dimension
87
                      255.0         // the scaling factor is chosen such that a histogram value of 1 maps to 255
88
                   );
89
                }
90
 
91
 
92
        // Threshold back projection to obtain a binary image
93
                if (threshold>0.0)
94
                        cv::threshold(result, result, 255*threshold, 255, cv::THRESH_BINARY);
95
 
96
                return result;
97
        }
98
 
99
        cv::Mat find(const cv::Mat& image, float minValue, float maxValue, int *channels, int dim) {
100
 
101
                cv::Mat result;
102
 
103
                hranges[0]= minValue;
104
                hranges[1]= maxValue;
105
 
106
                for (int i=0; i<dim; i++)
107
                        this->channels[i]= channels[i];
108
 
109
                if (isSparse) { // call the right function based on histogram type
110
 
111
                   cv::calcBackProject(&image,
112
                      1,            // we only use one image at a time
113
                      channels,     // vector specifying what histogram dimensions belong to what image channels
114
                      shistogram,   // the histogram we are using
115
                      result,       // the resulting back projection image
116
                      ranges,       // the range of values, for each dimension
117
                      255.0         // the scaling factor is chosen such that a histogram value of 1 maps to 255
118
                   );
119
 
120
                } else {
121
 
122
                   cv::calcBackProject(&image,
123
                      1,            // we only use one image at a time
124
                      channels,     // vector specifying what histogram dimensions belong to what image channels
125
                      histogram,    // the histogram we are using
126
                      result,       // the resulting back projection image
127
                      ranges,       // the range of values, for each dimension
128
                      255.0         // the scaling factor is chosen such that a histogram value of 1 maps to 255
129
                   );
130
                }
131
 
132
        // Threshold back projection to obtain a binary image
133
                if (threshold>0.0)
134
                        cv::threshold(result, result, 255*threshold, 255, cv::THRESH_BINARY);
135
 
136
                return result;
137
        }
138
 
139
};
140
 
141
 
142
#endif