Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleLEDDetector.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <bias_config.h>
26 #ifndef BIAS_HAVE_OPENCV
27 # error Please recompile BIAS with USE_OPENCV to use this code.
28 #endif
29 #include <Base/Common/W32Compat.hh>
30 #include <Base/Image/Image.hh>
31 #include <Base/Image/ImageConvert.hh>
32 #include <Base/Image/ImageIO.hh>
33 #include <Base/ImageUtils/ImageDraw.hh>
34 #include <Base/Math/Vector2.hh>
35 #include <Base/Math/Vector3.hh>
36 #include <FeatureDetector/LEDDetector.hh>
37 #include <iostream>
38 #include <string>
39 #include <vector>
40 
41 using namespace std;
42 using namespace BIAS;
43 
44 /**
45  @example ExampleLEDDetector.cpp
46  @brief Example for LED spot detector in images.
47  @relates LEDDetector
48  @ingroup g_examples
49  @author esquivel 11/2013
50 */
51 int main(int argc, char** argv)
52 {
53  if (argc < 2) {
54  cout << "Usage : ExampleLEDDetector <image> [-c <num>]" << endl
55  << " -c <num> : Work on channel <num> of color image" << endl;
56  return 0;
57  }
58 
59  // load image
61  if (BIAS::ImageIO::Load(argv[1], image) != 0) {
62  cout << "Error : Failed to load image " << argv[1] << "!" << endl;
63  return -1;
64  }
65 
66  // extract red channel
67  if (argc > 2 && strcmp(argv[2], "-c") == 0) {
68  int channel = argc > 3 ? atoi(argv[3]) : 0;
69  int channels = image.GetChannelCount();
70  if (channel < 0 || channel >= channels) {
71  cout << "Invalid channel " << channel << " given, ignoring option -c" << endl;
72  } else if (channels > 1) {
73  cout << "Working on channel " << channel << " of color image" << endl;
74  BIAS::ImageConvert::GetChannel<unsigned char>(image, channel);
75  }
76  }
77 
78  // detect LED spots
79  BIAS::LEDDetector detector;
80  std::vector<BIAS::Vector3<double> > pos;
81  detector.Compute(image, pos);
82 
83  // compute radius of LED spots
84  std::vector<double> r(pos.size());
85  {
86  std::vector<double> minx, maxx, maxval;
87  detector.GetAttributes(minx, maxx, maxval);
88  BIASASSERT(minx.size() == pos.size() && maxx.size() == pos.size());
89  for (unsigned int i = 0; i < pos.size(); i++)
90  r[i] = 0.5 * (maxx[i] - minx[i]);
91  }
92 
93  // find closest LED spot to image center
94  unsigned int i_center = 0;
95  double d_center = 1e8;
96  BIAS::Vector2<double> c(image.GetWidth() / 2, image.GetHeight() / 2);
97  for (unsigned int i = 0; i < pos.size(); i++) {
98  double d = BIAS::Vector2<double>(pos[i][0] - c[0], pos[i][1] - c[1]).NormL2();
99  if (d < d_center) {
100  i_center = i;
101  d_center = d;
102  }
103  }
104 
105  // draw detected LED spots into image
106  unsigned char col[3] = { 0, 0, 0 };
107  cout << "Detected " << pos.size() << " LED spots : " << endl;
108  for (unsigned int i = 0; i < pos.size(); i++) {
109  cout << (i == i_center ? "X " : " ") << pos[i] << endl << flush;
110  if (i == i_center) col[0] = 255; else col[0] = 0;
112  CircleCenterFilled(image, (int)(pos[i][0]+0.5), (int)(pos[i][1]+0.5), 2, col);
114  CircleCenter(image, (int)(pos[i][0]+0.5), (int)(pos[i][1]+0.5), (int)(r[i]+0.5), col);
115  }
116 
117  // save output image
118  const std::string filename = "ExampleLEDDetector.png";
119  if (BIAS::ImageIO::Save(filename, image) == 0) {
120  cout << "Saved output image to " << filename << endl;
121  } else {
122  cout << "Error : Failed to write image to " << filename << "!" << endl;
123  return -1;
124  }
125  return 0;
126 }
int Compute(const BIAS::Image< unsigned char > &image, std::vector< BIAS::Vector3< double > > &results)
Detect LED spots in image.
Definition: LEDDetector.cpp:40
void GetAttributes(std::vector< double > &minX, std::vector< double > &maxX, std::vector< double > &maxValues)
Return attributes of detected LED spots.
Definition: LEDDetector.cpp:32
static int CircleCenterFilled(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const StorageType Value[])
draws a filled circle using Value
Definition: ImageDraw.cpp:1023
unsigned int GetWidth() const
Definition: ImageBase.hh:312
This class contains methods to detect (IR)-LED-Spots, with template matching (new search) or in &quot;recy...
Definition: LEDDetector.hh:50
static int CircleCenter(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const StorageType Value[]=NULL)
draws a circular line, either using Value or a good contrast value
Definition: ImageDraw.cpp:977
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141