1 #include "LEDDetector.hh"
2 #include <Base/Image/ImageConvert.hh>
7 #define MASK_X 17 // filter size
8 #define RADIUS_SPOT_RANGE 70 // max. space between spots
9 #define MASK_WHATISMINBRIGHT 150 // gray values for thresholding
10 #define MASK_WAHTISMAXDARK 50 // gray values for thresholding
22 if (lookup)
delete[] lookup;
28 neighbours = nextpixel;
32 GetAttributes (std::vector<double> &minX, std::vector<double> &maxX,
33 std::vector<double> &maxValues)
37 maxValues = maxvalues;
43 if (!results.empty()) {
44 resultpixel_old.clear();
45 for (
unsigned int i = 0; i < results.size(); i++) {
46 resultpixel_old.push_back(results[i]);
56 width = gray_threshold->width;
57 height = gray_threshold->height;
58 if (lookupsize < width * height) {
60 lookupsize = width * height;
61 lookup =
new unsigned char[lookupsize];
63 memset(lookup, 0, width * height *
sizeof(
unsigned char));
68 CvScalar m = cvAvg(gray_orig, NULL);
69 mean = (double)m.val[0];
71 cvThreshold(gray_orig, gray_threshold, 210, 255, CV_THRESH_BINARY);
72 step = gray_threshold->widthStep /
sizeof(
unsigned char);
73 data = (
unsigned char *)gray_threshold->imageData;
77 if (!resultpixel_old.empty())
79 if (res != 0 || resultpixel_old.empty())
81 results = resultpixel;
85 cvReleaseImage(&gray_threshold);
86 cvReleaseImage(&gray_orig);
91 int LEDDetector::Recycle_()
93 bool foundatold =
false;
95 for (
unsigned int i = 0; i < resultpixel_old.size(); i++) {
99 int fx = (int)f[0], fy = (
int)f[1];
100 if (data[fy*step + fx] > mean-10 && lookup[fy*width+fx] == 0) {
108 int res = CheckNeighboursIfWhite_(fx, fy, a, b);
109 if (res == 0 && a != -1 && b != -1) {
121 resultpixel_old.clear();
122 return (!foundatold && !foundnn) ? -1 : 0;
125 int LEDDetector:: CheckNeighboursIfWhite_(
int x,
int y,
int& a,
int& b)
129 while (!found && h < 100) {
130 int res = CheckNeighboursIfWhite_inter_(x+h, y);
132 found =
true; a = x+h; b=y;
134 res = CheckNeighboursIfWhite_inter_(x+h, y-h);
136 found =
true; a = x+h; b=y-h;
138 res = CheckNeighboursIfWhite_inter_(x+h, y+h);
140 found =
true; a = x+h; b=y+h;
142 res = CheckNeighboursIfWhite_inter_(x, y+h);
144 found =
true; a = x; b = y+h;
146 res = CheckNeighboursIfWhite_inter_(x, y-h);
148 found =
true; a = x; b = y-h;
150 res = CheckNeighboursIfWhite_inter_(x-h, y+h);
152 found =
true; a=x-h;b=y+h;
154 res = CheckNeighboursIfWhite_inter_(x-h, y);
156 found =
true; a = x-h; b=y;
158 res = CheckNeighboursIfWhite_inter_(x-h, y-h);
160 found =
true; a = x-h; b = y-h;
171 return found ? 0 : -1;
174 int LEDDetector::CheckNeighboursIfWhite_inter_(
int x,
int y)
176 if (x > 0 && y > 0 && x < (gray_threshold->width)-1 && y < (gray_threshold->height)-1)
177 if (data[y*step+x] > MASK_WHATISMINBRIGHT && lookup[y*width+x] == 0)
182 void LEDDetector::Search_(
int x,
int y)
184 if (lookup[y*width+x] == 0) {
190 pixelweightvaluex = 0;
191 pixelweightvaluey = 0;
195 int res = CheckNeighbours_(x, y);
197 minxcoords.push_back(minx);
198 maxxcoords.push_back(maxx);
199 maxvalues.push_back(maxvalue);
202 double o = pixelweightvaluex / count;
203 double p = pixelweightvaluey / count;
204 double u = valuesum / count;
220 CvScalar w = cvGet2D(gray_orig, g, h);
221 erg[2] = (double)w.val[0];
223 n.val[0] = 255; n.val[1] = 0; n.val[2] = 0; n.val[3] = 0;
224 cvSet2D(gray_threshold, g, h, n);
225 lookup[y*width+x] = 255;
226 nextpixel.push_back(whitepixel);
228 resultpixel.push_back(erg);
234 int LEDDetector::CheckNeighbours_(
int x,
int y)
237 if (recursivedepth < 1000 && blobsize < 100) {
238 if (x > 0 && y > 0 && x < (gray_threshold->width)-1 && y < (gray_threshold->height)-1) {
239 if (data[y*step+x]> MASK_WAHTISMAXDARK && lookup[y*width+x] == 0) {
245 lookup[y*width+x] = 127;
250 CvScalar s = cvGet2D(gray_orig, y, x);
254 if (value > maxvalue)
258 whitepixel.push_back(a);
260 pixelweightvaluex+=value*y;
261 pixelweightvaluey+=value*x;
263 CheckNeighbours_( x-1, y);
264 CheckNeighbours_( x+1, y);
265 CheckNeighbours_( x, y-1);
266 CheckNeighbours_( x, y+1);
274 void LEDDetector::TemplateMatching_()
277 int y = gray_threshold->height;
278 int x = gray_threshold->width*gray_threshold->nChannels;
279 int s = (MASK_X-1) / 2;
280 for (
int i = s; i < y-s; i+=2) {
281 for (
int j = s; j < x-s; j+=2) {
285 if (data[i*step+j] > MASK_WAHTISMAXDARK && lookup[i*width+j] == 0){
290 for (; xh < xd; xh++) {
292 if (data[xh*step+yh] > MASK_WAHTISMAXDARK) {
298 for (; yh < yd; yh++) {
299 if (data[xh*step+yh] > MASK_WAHTISMAXDARK) {
307 for (; xh > xd; xh--) {
308 if (data[xh*step+yh] > MASK_WAHTISMAXDARK) {
315 for (; yh > yd; yh--) {
316 if (data[xh*step+yh] > MASK_WAHTISMAXDARK) {
323 for (
int t = -3; t < 5; t++) {
324 for (
int r = -3; r < 5; r++) {
325 if (data[(i+r)*step+(j+t)] > MASK_WAHTISMAXDARK) {
336 if (repeat && inside > 3) {
339 lookup[i*width+j] = 255;
void Release()
reimplemented from ImageBase
void clear()
stl conform interface
static int BIAS2ipl(const Image< StorageType > &source, IplImage *&dest)
Returns a new, separate IplImage for use with OpenCV created rom the source BIAS Image.
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey