CVR-Lib last update 20 Sep 2009

cvrMergeYCbCrToImage.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003
00003  * Department of Electronics, ITCR, Costa Rica
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   cvrMergeYCbCrToImage.h
00044  *         Merge YCbCr channels to color image
00045  * \author Pablo Alvarado
00046  * \date   04.01.2007
00047  *
00048  * $Id: cvrMergeYCbCrToImage.h,v 1.1 2007/04/05 22:56:36 alvarado Exp $
00049  */
00050 
00051 
00052 #ifndef _CVR_MERGE_Y_Cb_Cr_TO_IMAGE_H_
00053 #define _CVR_MERGE_Y_Cb_Cr_TO_IMAGE_H_
00054 
00055 #include "cvrMergeImage.h"
00056 
00057 namespace cvr {
00058 
00059   /**
00060    * Creates RGB values from given YCbCr values by merging float or ubyte
00061    * values to an rgbPixel, merging channels(floats) or channel8s(ubytes) to an
00062    * Image
00063    *
00064    * In the literature, technical and scientific, there is often confusion
00065    * among the color spaces YUV, YCbCr and YPbPr.  Poynton in
00066    * http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html explains that
00067    * YUV is usually never correctly meant, because the color space normally
00068    * used for component digital video is the YCbCr (ITU-RS601 or CCIR-601).
00069    * Other devices use the YPbPr, but the "real" YUV is rarely employed.
00070    *
00071    * The CVR-Lib provides all three spaces:
00072    *
00073    * - YCbCr: cvr::mergeYCbCrToImage used by IEEE 1394 FireWire cameras
00074    * - YPbPr: cvr::mergeYPbPrToImage used by some WebCams
00075    * - YUV:   cvr::mergeYUVToImage   did they really meant to use this?
00076    *
00077    * Here, the inverse transformation of cvr::splitImageToYCbCr is followed:
00078    *
00079    * \f[
00080    *   \begin{bmatrix}
00081    *     R \\
00082    *     G \\
00083    *     B
00084    *   \end{bmatrix}
00085    *   =
00086    *   M^{-1}
00087    *   \left(
00088    *     \begin{bmatrix}
00089    *       Y \\
00090    *       Cb \\
00091    *       Cr
00092    *     \end{bmatrix}
00093    *     -
00094    *     \begin{bmatrix}
00095    *       16 \\
00096    *       128 \\
00097    *       128
00098    *     \end{bmatrix}
00099    *   \right)
00100    * \f]
00101    * where M is the matrix given in cvr::splitImageToYCbCr.
00102    *
00103    * If you know you have a YCbCr space but it was given to you as
00104    * YUV, then the equivalences are U=Cb and V=Cr.
00105    *
00106    * A way of noticing if you have a YCbCr color space is determining the range
00107    * of the values of each channel.  Y should be in [16,235], while Cr and Cb
00108    * should be in [16,240].  If you channel Y has values in [0,255] then you
00109    * should use mergeYPbPrToImage instead.
00110    *
00111    * @ingroup gColor
00112    */
00113   class mergeYCbCrToImage : public mergeImage {
00114   public:
00115 
00116     /**
00117      * Constructor
00118      */
00119     mergeYCbCrToImage(void);
00120 
00121 
00122     /**
00123      * Destructor
00124      */
00125     ~mergeYCbCrToImage();
00126 
00127     /**
00128      * Return the name of this type
00129      */
00130     const std::string& name() const;
00131 
00132     /**
00133      * Copy data of "other" functor.
00134      *
00135      * @param other the functor to be copied
00136      * @return a reference to this functor object
00137      */
00138     mergeYCbCrToImage& copy(const mergeYCbCrToImage& other);
00139 
00140     /**
00141      * Alias for copy method.
00142      *
00143      * @param other the functor to be copied
00144      * @return a reference to this functor object
00145      */
00146     mergeYCbCrToImage& operator=(const mergeYCbCrToImage& other);
00147 
00148     /**
00149      * Returns a pointer to a clone of the functor.
00150      */
00151     virtual mergeYCbCrToImage* clone() const;
00152 
00153     /**
00154      * Returns a pointer to a new instance of this functor.
00155      */
00156     virtual mergeYCbCrToImage* newInstance() const;
00157 
00158     /**
00159      * Merge channels Y, Cr, Cb to an image.
00160      *
00161      * @param Y the Y channel, i.e. black&white
00162      * @param Cb the Cb channel, chromatic
00163      * @param Cr the Cr channel, chromatic
00164      * @param img the image to be splitted
00165      */
00166     virtual bool apply(const matrix<float>& Y,
00167                        const matrix<float>& Cb,
00168                        const matrix<float>& Cr,
00169                        image& img) const;
00170 
00171     /**
00172      * Merge  8-bit-channels Y, U, V to an image.
00173      *
00174      * @param Y the Y channel, i.e. black&white
00175      * @param Cb the Cb channel, chromatic
00176      * @param Cr the Cr channel, chromatic
00177      * @param img the image to be splitted
00178      */
00179     virtual bool apply(const matrix<ubyte>& Y,
00180                        const matrix<ubyte>& Cb,
00181                        const matrix<ubyte>& Cr,
00182                        image& img) const;
00183 
00184     /**
00185      * Merge the  values Y, Cr and Cb to a pixel.
00186      * @param Y the Y value, i.e. black&white
00187      * @param Cb the Cb value, chromatic
00188      * @param Cr the Cr value, chromatic
00189      * @param pixel the merged pixel
00190      */
00191     inline virtual bool apply(const float& Y,
00192            const float& Cb,
00193            const float& Cr,
00194            rgbaPixel& pixel) const;
00195 
00196     /**
00197      * Merge the   8-bit-values Y, U and V to a pixel.
00198      *
00199      * @param Y the Y value, i.e. black&white
00200      * @param Cb the Cb  value, chromatic
00201      * @param Cr the Cr value, chromatic
00202      * @param pixel the merged pixel
00203      */
00204     inline virtual bool apply(const ubyte& Y,
00205                               const ubyte& Cb,
00206                               const ubyte& Cr,
00207                               rgbaPixel& pixel) const;
00208   protected:
00209 
00210     /**
00211      * Initialize the Look-Up-Tables
00212      */
00213     virtual void initializeLUTs();
00214 
00215     /**
00216      * Look up tables to accelerate conversion YUV -> RGB
00217      */
00218     //@{
00219     /**
00220      * Partial Y results
00221      */
00222     static const int* lutY_;
00223 
00224     /**
00225      * Partial results with Cr (equivalent to V) for the red channel
00226      * computation.
00227      */
00228     static const int* lutVr_;
00229 
00230     /**
00231      * Partial results with Cb (equivalent to U) for the green channel
00232      * computation.
00233      */
00234     static const int* lutUg_;
00235 
00236     /**
00237      * Partial results with Cr (equivalent to V) for the green channel
00238      * computation.
00239      */
00240     static const int* lutVg_;
00241 
00242     /**
00243      * Partial results with Cb (equivalent to U) for the blue channel
00244      * computation.
00245      */
00246     static const int* lutUb_;
00247     //@}
00248 
00249     /**
00250      * Clip function
00251      *
00252      * Equivalent to min(255,max(0,val)) but maybe faster
00253      */
00254     inline ubyte clip(const int val) const;
00255 
00256   };
00257 }
00258 
00259 #include "cvrMergeYCbCrToImage_inline.h"
00260 
00261 #endif

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