CVR-Lib last update 20 Sep 2009

cvrSplitImageToYUV.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2002
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrSplitImageToYUV.h
00044  *         Split color image to YUV
00045  * \author Volker Schmirgel
00046  * \author Pablo Alvarado
00047  * \date   17.12.2002
00048  *
00049  * $Id: cvrSplitImageToYUV.h,v 1.1 2007/04/05 22:56:36 alvarado Exp $
00050  */
00051 
00052 
00053 #ifndef _CVR_SPLIT_IMAGE_TO_Y_U_V_H_
00054 #define _CVR_SPLIT_IMAGE_TO_Y_U_V_H_
00055 
00056 #include "cvrSplitImage.h"
00057 
00058 namespace cvr {
00059   /**
00060    * Computes the YUV values from a given RGB color representation (rgbaPixel).
00061    *
00062    * In the literature, technical and scientific, there is a confusion between
00063    * the color spaces YUV, YCrCb and YPbPr.  Poynton in
00064    * http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html explains that
00065    * YUV is usually never correctly meant, because the color space normally
00066    * used for component digital video is the YCrCb (ITU-RS601 or CCIR-601).
00067    * Other devices use the YPbPr, but the "real" YUV is usually not employed.
00068    *
00069    * The CVR-Lib provides all three spaces:
00070    *
00071    * - YCrCb: cvr::splitImageToYCbCr used by IEEE 1394 FireWire cameras
00072    * - YPbPr: cvr::splitImageToYPbPr used by some WebCams
00073    * - YUV:   cvr::splitImageToYUV   did they really meant to use this?
00074    *
00075    * This functor splits an RGB images into the color space YUV as described
00076    * in the Poynton reference given above.
00077    *
00078    * \f[
00079    * \begin{bmatrix}
00080    * Y \\
00081    * U \\
00082    * V
00083    * \end{bmatrix}
00084    * =
00085    * \begin{bmatrix}
00086    *  0.299       &  0.587       &  0.114       \\
00087    * -0.147141189 & -0.288869157 &  0.436010346 \\
00088    *  0.614975383 & -0.514965121 & -0.100010262
00089    * \end{bmatrix}
00090    * \begin{bmatrix}
00091    * R \\
00092    * G \\
00093    * B
00094    * \end{bmatrix}
00095    * \f]
00096    *
00097    * Here, RGB are values in the intervall [0,1].  Within this range, the
00098    * excursion for Y is also from 0.0 to 1.0, the excursion of U is from -0.436
00099    * to 0.436 and V varies from -0.615 to 0.615.  These ranges are respected in
00100    * the implentation for \c float types (cvr::channel).
00101    *
00102    * If you use \c ubyte (cvr::matrix<ubyte>), then the values for U and V are
00103    * linearly mapped from 0 to 1, which results in the mapping
00104    *
00105    * \code
00106    * Y =  0.299*R + 0.587*G + 0.114*B
00107    * U = -0.169*R - 0.331*G + 0.500*B + 128.0
00108    * V =  0.500*R - 0.419*G - 0.081*B + 128.0
00109    * \endcode
00110    *
00111    * The former mapping is equivalent to the color space YPbPr scaled in such a
00112    * way that "black" is mapped to Y=0 and "white" to Y=255, and the Pb and Pr
00113    * channels, which acquire values from -0.5 to 0.5, are linearly mapped to
00114    * the inteval [0,255] as well.  In other words, for \c ubyte operations it
00115    * is equivalent to use this functor or cvr::splitImageToYPbPr (which is
00116    * maybe better named as the value ratios are at least kept there).
00117    *
00118    * @ingroup gColor
00119    */
00120   class splitImageToYUV : public splitImage {
00121   public:
00122 
00123     /**
00124      * default constructor
00125      */
00126     splitImageToYUV();
00127 
00128     /**
00129      * copy constructor
00130      * @param other the object to be copied
00131      */
00132     splitImageToYUV(const splitImageToYUV& other);
00133 
00134     /**
00135      * destructor
00136      */
00137     virtual ~splitImageToYUV();
00138 
00139     /**
00140      * returns the name of this type ("splitImageToYUV")
00141      */
00142     const std::string& name() const;
00143 
00144     /**
00145      * copy data of "other" functor.
00146      * @param other the functor to be copied
00147      * @return a reference to this functor object
00148      */
00149     splitImageToYUV& copy(const splitImageToYUV& other);
00150 
00151     /**
00152      * alias for copy member
00153      * @param other the functor to be copied
00154      * @return a reference to this functor object
00155      */
00156     splitImageToYUV& operator=(const splitImageToYUV& other);
00157 
00158     /**
00159      * returns a pointer to a clone of this functor.
00160      */
00161     virtual splitImageToYUV* clone() const;
00162 
00163     /**
00164      * Returns a pointer to a new instance of this functor.
00165      */
00166     virtual splitImageToYUV* newInstance() const;
00167 
00168 
00169     /**
00170      *  split pixel into float values (between 0 and 1)
00171      */
00172     inline virtual bool apply(const rgbaPixel& pixel,
00173                               float& c1,
00174                               float& c2,
00175                               float& c3) const;
00176 
00177     /**
00178      * Split pixel into 8-bit values (between 0 and 255)
00179      *
00180      * N.B.: when casting the transformation result to unsigned shorts
00181      * (8-bit channel) major rounding errors will occur.
00182      *
00183      * As a result, the merging operation might produce negative
00184      * values or values > 1, which are truncated subsequently.
00185      *
00186      * When accurate Y, U and V values are required, prefer float values
00187      */
00188     inline virtual bool apply(const rgbaPixel& pixel,
00189                               ubyte& c1,
00190                               ubyte& c2,
00191                               ubyte& c3) const;
00192 
00193     /**
00194      *  split pixel into float channels
00195      */
00196     virtual bool apply(const matrix<rgbaPixel>& img,
00197                        matrix<float>& c1,
00198                        matrix<float>& c2,
00199            matrix<float>& c3) const;
00200 
00201     /**
00202      * Split image into 8-bit channels.
00203      *
00204      * N.B.: when casting the transformation result to unsigned shorts
00205      * (8-bit channel) major rounding errors will occur.
00206      *
00207      * As a result, the merging operation might produce negative
00208      * values or values > 1, which are truncated subsequently.  When
00209      * accurate Y, U and V values are required, prefer float values.
00210      */
00211     virtual bool apply(const matrix<rgbaPixel>& img,
00212                        matrix<ubyte>& c1,
00213                        matrix<ubyte>& c2,
00214            matrix<ubyte>& c3) const;
00215 
00216   };
00217 
00218 }
00219 
00220 #include "cvrSplitImageToYUV_inline.h"
00221 
00222 #endif

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8