Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleLEDDetector.cpp

Example for LED spot detector in images.

Author
esquivel 11/2013
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <bias_config.h>
#ifndef BIAS_HAVE_OPENCV
# error Please recompile BIAS with USE_OPENCV to use this code.
#endif
#include <Base/Common/W32Compat.hh>
#include <Base/Image/Image.hh>
#include <Base/Image/ImageConvert.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/ImageUtils/ImageDraw.hh>
#include <Base/Math/Vector2.hh>
#include <Base/Math/Vector3.hh>
#include <FeatureDetector/LEDDetector.hh>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace BIAS;
/**
@example ExampleLEDDetector.cpp
@brief Example for LED spot detector in images.
@relates LEDDetector
@ingroup g_examples
@author esquivel 11/2013
*/
int main(int argc, char** argv)
{
if (argc < 2) {
cout << "Usage : ExampleLEDDetector <image> [-c <num>]" << endl
<< " -c <num> : Work on channel <num> of color image" << endl;
return 0;
}
// load image
if (BIAS::ImageIO::Load(argv[1], image) != 0) {
cout << "Error : Failed to load image " << argv[1] << "!" << endl;
return -1;
}
// extract red channel
if (argc > 2 && strcmp(argv[2], "-c") == 0) {
int channel = argc > 3 ? atoi(argv[3]) : 0;
int channels = image.GetChannelCount();
if (channel < 0 || channel >= channels) {
cout << "Invalid channel " << channel << " given, ignoring option -c" << endl;
} else if (channels > 1) {
cout << "Working on channel " << channel << " of color image" << endl;
BIAS::ImageConvert::GetChannel<unsigned char>(image, channel);
}
}
// detect LED spots
std::vector<BIAS::Vector3<double> > pos;
detector.Compute(image, pos);
// compute radius of LED spots
std::vector<double> r(pos.size());
{
std::vector<double> minx, maxx, maxval;
detector.GetAttributes(minx, maxx, maxval);
BIASASSERT(minx.size() == pos.size() && maxx.size() == pos.size());
for (unsigned int i = 0; i < pos.size(); i++)
r[i] = 0.5 * (maxx[i] - minx[i]);
}
// find closest LED spot to image center
unsigned int i_center = 0;
double d_center = 1e8;
BIAS::Vector2<double> c(image.GetWidth() / 2, image.GetHeight() / 2);
for (unsigned int i = 0; i < pos.size(); i++) {
double d = BIAS::Vector2<double>(pos[i][0] - c[0], pos[i][1] - c[1]).NormL2();
if (d < d_center) {
i_center = i;
d_center = d;
}
}
// draw detected LED spots into image
unsigned char col[3] = { 0, 0, 0 };
cout << "Detected " << pos.size() << " LED spots : " << endl;
for (unsigned int i = 0; i < pos.size(); i++) {
cout << (i == i_center ? "X " : " ") << pos[i] << endl << flush;
if (i == i_center) col[0] = 255; else col[0] = 0;
CircleCenterFilled(image, (int)(pos[i][0]+0.5), (int)(pos[i][1]+0.5), 2, col);
CircleCenter(image, (int)(pos[i][0]+0.5), (int)(pos[i][1]+0.5), (int)(r[i]+0.5), col);
}
// save output image
const std::string filename = "ExampleLEDDetector.png";
if (BIAS::ImageIO::Save(filename, image) == 0) {
cout << "Saved output image to " << filename << endl;
} else {
cout << "Error : Failed to write image to " << filename << "!" << endl;
return -1;
}
return 0;
}