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