qualifiers.cpp

00001 
00002 /***************************************************************************
00003  *  qualifiers.cpp - Pixel qualifier
00004  *
00005  *  Created: Mon Jun 09 22:54:00 2008
00006  *  Copyright  2008  Christof Rath <c.rath@student.tugraz.at>
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <classifiers/qualifiers.h>
00024 #include <core/exceptions/software.h>
00025 #include <fvutils/color/yuv.h>
00026 
00027 #include <cstdlib>
00028 
00029 using fawkes::point_t;
00030 
00031 namespace firevision {
00032 #if 0 /* just to make Emacs auto-indent happy */
00033 }
00034 #endif
00035 
00036 /** @class Qualifier qualifiers.h <apps/nao_loc/qualifiers.h>
00037  * Abstract Qualifier for a single pixel
00038  *
00039  * @author Christof Rath
00040  */
00041 
00042 /** Default constructor
00043  */
00044 Qualifier::Qualifier()
00045 {
00046   buffer_     = 0;
00047   width_      = 0;
00048   height_     = 0;
00049   size_       = 0;
00050   colorspace_ = CS_UNKNOWN;
00051 }
00052 
00053 /** Constructor.
00054  * @param buffer containing the image
00055  * @param width of the image
00056  * @param height of the image
00057  * @param colorspace the colorspace in action
00058  */
00059 Qualifier::Qualifier(unsigned char* buffer, unsigned int width, 
00060                      unsigned int height, colorspace_t colorspace)
00061 {
00062   if (!buffer)
00063     throw fawkes::NullPointerException("Qualifier: the buffer may not be null!");
00064   if (!width || !height)
00065     throw fawkes::IllegalArgumentException("Qualifier: width and height may not be 0!");
00066 
00067   set_buffer(buffer, width, height);
00068   colorspace_ = colorspace;
00069 }
00070 
00071 
00072 /** Destructor.
00073  */
00074 Qualifier::~Qualifier()
00075 {
00076 }
00077 
00078 /** Get buffer.
00079  * @return pointer to buffer
00080  */
00081 unsigned char*
00082 Qualifier::get_buffer()
00083 {
00084   return buffer_;
00085 }
00086 
00087 /** buffer setter
00088  * @param buffer containing the image
00089  * @param width of the image (if 0 the param will be ignored)
00090  * @param height of the image (if 0 the param will be ignored)
00091  */
00092 void
00093 Qualifier::set_buffer(unsigned char* buffer, unsigned int width, 
00094                       unsigned int height)
00095 {
00096   buffer_ = buffer;
00097 
00098   if (width)
00099     width_  = width;
00100 
00101   if (height)
00102     height_ = height;
00103 
00104   if (width || height)
00105     size_ = width_ * height_;
00106 }
00107 
00108 
00109 
00110 
00111 /** Get colorspace.
00112  * @return colorspace
00113  */
00114 colorspace_t
00115 Qualifier::get_colorspace()
00116 {
00117   return colorspace_;
00118 }
00119 
00120 
00121 /** colorspace setter
00122  * @param colorspace the colorspace in action
00123  */
00124 void
00125 Qualifier::set_colorspace(colorspace_t colorspace)
00126 {
00127   colorspace_ = colorspace;
00128 }
00129 
00130 
00131 
00132 
00133 
00134 
00135 /** @class LumaQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
00136  * LumaQualifier for a single pixel.
00137  * Uses the value of the Y-channel
00138  *
00139  * @author Christof Rath
00140  */
00141 
00142 
00143 /** Constructor.
00144  * @param buffer containing the image
00145  * @param width of the image
00146  * @param height of the image
00147  * @param colorspace the colorspace in action
00148  */
00149 LumaQualifier::LumaQualifier(unsigned char* buffer, unsigned int width, 
00150                              unsigned int height, colorspace_t colorspace)
00151  :Qualifier(buffer, width, height, colorspace)
00152 {
00153 }
00154 
00155 
00156 /** Getter.
00157  * @param pixel the pixel of interest
00158  * @return a corresponding int value
00159  */
00160 int
00161 LumaQualifier::get(point_t pixel)
00162 {
00163   if (pixel.x >= width_)
00164     throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
00165   if (pixel.y >= height_)
00166     throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
00167         
00168   return buffer_[pixel.y * width_ + pixel.x];
00169 }
00170 
00171 
00172 
00173 
00174 
00175 
00176 /** @class SkyblueQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
00177  * SkyblueQualifier for a single pixel.
00178  * Uses the value of the U/V-channels
00179  *
00180  * @author Christof Rath
00181  */
00182 
00183 /** Constructor.
00184  * @param buffer containing the image
00185  * @param width of the image
00186  * @param height of the image
00187  * @param colorspace the colorspace in action
00188  */
00189 SkyblueQualifier::SkyblueQualifier(unsigned char* buffer, unsigned int width, 
00190                                    unsigned int height, colorspace_t colorspace)
00191  :Qualifier(buffer, width, height, colorspace)
00192 {
00193 }
00194 
00195 
00196 /** Getter.
00197  * @param pixel the pixel of interest
00198  * @return a corresponding int value
00199  */
00200 int
00201 SkyblueQualifier::get(point_t pixel)
00202 {
00203   if (pixel.x >= width_)
00204     throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
00205   if (pixel.y >= height_)
00206     throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
00207 
00208   unsigned int u_addr = size_ + (pixel.y * width_ + pixel.x) / 2;
00209   unsigned char u = buffer_[u_addr];
00210   unsigned char v = 255 - buffer_[u_addr + size_ / 2];
00211 
00212   if ((u < threshold_) || (v < threshold_))
00213     return 0;
00214 
00215   return u + v;
00216 }
00217 
00218 
00219 
00220 
00221 
00222 
00223 /** @class YellowQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
00224  * YellowQualifier for a single pixel.
00225  * Uses the value of the U/V-channels
00226  *
00227  * @author Christof Rath
00228  */
00229 
00230 /** Constructor.
00231  * @param buffer containing the image
00232  * @param width of the image
00233  * @param height of the image
00234  * @param colorspace the colorspace in action
00235  */
00236 YellowQualifier::YellowQualifier(unsigned char* buffer, unsigned int width, 
00237                                  unsigned int height, colorspace_t colorspace)
00238  :Qualifier(buffer, width, height, colorspace)
00239 {
00240 }
00241 
00242 
00243 /** Getter.
00244  * @param pixel the pixel of interest
00245  * @return a corresponding int value
00246  */
00247 int
00248 YellowQualifier::get(point_t pixel)
00249 {
00250   if (pixel.x >= width_)
00251     throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
00252   if (pixel.y >= height_)
00253     throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
00254 
00255   unsigned int y_addr = (pixel.y * width_ + pixel.x);
00256   unsigned int u_addr = size_ + y_addr / 2;
00257   unsigned char y = buffer_[y_addr];
00258   unsigned int u = (255 - buffer_[u_addr]) * y;
00259   unsigned int v = (255 - abs(127 - buffer_[u_addr + size_ / 2]) * 2) * y;
00260 
00261   if ((u <= threshold_) || (v <= threshold_))
00262     return 0;
00263 
00264   return (u + v);
00265 }
00266 
00267 } // end namespace firevision

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1