Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageConvert.hh
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  ut 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 #ifndef _BIAS_IMAGE_CONVERSION_H_
26 #define _BIAS_IMAGE_CONVERSION_H_
27 
28 #include "bias_config.h"
29 
30 #include "Image.hh"
31 #include "ImageBase.hh"
32 
33 #ifdef BIAS_HAVE_OPENCV
34 # include <opencv2/opencv.hpp>
35 #endif
36 
37 #ifdef BIAS_HAVE_DV
38 # include <dv.h>
39 #endif
40 
41 // ImageMagick support
42 #ifdef BIAS_HAVE_IMAGEMAGICKLIB
43 # include <Base/Common/BIASpragmaStart.hh>
44 # include <Magick++.h>
45 # include <Base/Common/BIASpragmaEnd.hh>
46 # if (MagickLibVersion >= 0x643)
47 # define MagickLib MagickCore
48 # endif
49 #endif
50 
51 
52 #define D_IMCONV_IPL 0x00000001
53 
54 // PointGrey XB3 special conversion (JW)
55 // format for acquired raw, packed image
56 #ifndef vidap_DEFAULT_PACKED_COLORMODEL
57 # define vidap_DEFAULT_PACKED_COLORMODEL BIAS::ImageBase::CM_PGR_XB3_F7M3_GBRG
58 #endif
59 #ifndef vidap_DEFAULT_BAYER_COLORMODEL
60 // format for deinterleaved single camera Bayer pattern images
61 # define vidap_DEFAULT_BAYER_COLORMODEL BIAS::ImageBase::CM_Bayer_GBRG
62 #endif
63 
64 namespace BIAS
65 {
66 
67  /**
68  * @brief methods for conversion from bayer to rgb
69  */
70  typedef enum {
78 
79  /** @class ImageConvert
80  @ingroup g_image_io
81  @brief class with static functions
82  for converting all kinds of color formats and storage types
83 
84  As all fucntions are static they have to be called by
85  eg. ImageConvert::Convert() and not with an object.
86 
87  @author Daniel Grest, and others Nov. 2002
88  */
89  class BIASImageBase_EXPORT ImageConvert : Debug
90  {
91  friend class ImageBase;
92  public:
93  /** \brief main general conversion function, calls desired specialized functions,
94  * always initializes the destImage(if not source==dest)
95  * \author Daniel Grest
96  * \date Nov. 2002 */
97  static int
98  Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest,
99  enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar =
100  false);
101 
102  /** \brief Function to convert the storage type of images
103  * e.g. from unsigned char to float or any other
104  * \param source[in]: the source image
105  * \param dest[out]: the target image
106  * \param targetST[in]: the storage type of the new target image
107  * \author Friso Evers */
108  static int
109  ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest,
110  ImageBase::EStorageType targetST);
111 
112  /** \brief In place conversion to gray image. No further memory is allocated,
113  * because only the the first part of the existing image is used.
114  * \author Felix Woelk **/
115  template<class StorageType>
116  static int
117  IP_ToGrey(Image<StorageType> &img);
118 
119  /**
120  * \brief wrapper for the templated function ToGrey
121  * \author woelk 02/2006 */
122  static int
123  ToGrey(const ImageBase& source, ImageBase& dest);
124 
125  /** \brief Create a grey converted copy of source image in this.
126  * Release() and Init() are called if necessary.
127  * \author Felix Woelk **/
128  template<class StorageType>
129  static int
130  ToGrey(const Image<StorageType>& source, Image<StorageType>& dest);
131 
132  /** \brief Create a new image dest by cropping one of the source images channels.
133  * Unlike GetChannel(Image<StorageType>& image, int channel), this
134  * one really creates a new single-channel image with the selected channel
135  * of the source. fkellner: Other GetChannels now have "strip" param, doing the
136  * same in-place.
137  * \author Patrick Fittkau **/
138  template<class StorageType>
139  static int
140  GetChannel(const Image<StorageType>& source, Image<StorageType>& dest,
141  const unsigned int channel);
142 
143  /** \brief in place: Crop the selected channel.
144  * (will actually copy values from selected channel to all other channels)
145  * \param image[in,out]: input image which will have only one channel, other channels are thrown away
146  * \author Christoffer Menk, fkellner **/
147  template<class StorageType>
148  static int
149  GetChannel(Image<StorageType>& image, int channel, bool strip = false);
150 
151  /** \brief in place: Crop the selected channel.
152  * (will actually copy values from selected channel to all other channels)
153  * \param image[in,out]: input image which will have only one channel, other channels are thrown away
154  * \author Christian Beder, fkellner **/
155  static int
156  GetChannel(ImageBase& image, int channel, bool strip = false);
157 
158  /** \brief Create a RGB converted copy of source image in this.
159  * Release() and Init() are called if necessary.
160  * \author Felix Woelk **/
161  template<class StorageType>
162  static int
163  ToRGB(const Image<StorageType>& source, Image<StorageType>& dest);
164 
165  /** \brief Create a RGBA converted copy of source image in dest.
166  intermediate rgb image is produced, only interleaved supported
167  @author koeser **/
168  template<class StorageType>
169  static int
170  ToRGBA(const Image<StorageType>& source, Image<StorageType>& dest);
171 
172  /** \brief Create a HSV converted copy of source image in this
173  Release() and Init() are called if necessary.
174  HSV space is implemented as a cylinder not as a cone!
175  only for float and unsigned char
176  @author Felix Woelk partially tested **/
177  template<class StorageType>
178  BIASImageBase_EXPORT
179  static int
180  ToHSV(const Image<StorageType>& source, Image<StorageType>& dest);
181 
182  /** \brief Create a HSL converted copy of source image in this
183  Release() and Init() are called if necessary.
184  only for float and unsigned char
185  @author Daniel Grest, April 2003
186  @status tested for unsigned char interleaved
187  */
188  template<class StorageType>
189  BIASImageBase_EXPORT
190  static int
191  ToHSL(const Image<StorageType>& source, Image<StorageType>& dest);
192 
193  /** \brief Create a hsL converted copy of source image in this
194  Release() and Init() are called if necessary.
195  only for unsigned char
196  similar to HSL but in euclidean coordinates for the CNCC
197  @author Daniel Grest, June 2003
198  @status tested for unsigned char interleaved
199  */
200  template<class StorageType>
201  BIASImageBase_EXPORT
202  static int
203  TohsL(const Image<StorageType>& source, Image<StorageType>& dest);
204 
205  /** \brief Takes the source image, and build an interleaved version
206  @author Ingo Thomsen
207  @date 11.06.2002
208  @status tested
209  @todo make it work for yuv **/
210  template<class StorageType>
211  BIASImageBase_EXPORT
212  static int
213  ToInterleaved(const Image<StorageType>& source, Image<StorageType>& dest);
214 
215  /** \brief Takes the three channels and composes one interleaved RGB image */
216  template<class StorageType>
217  BIASImageBase_EXPORT
218  static int
219  ToInterleavedRGB(const Image<StorageType>& red,
220  const Image<StorageType>& green, const Image<StorageType>& blue,
221  Image<StorageType>& dest);
222 
223  /** \brief Take Pointgrey X3B packed interleaved raw
224  and store there three contained images side by side
225  keeping the Bayer pattern.
226  data: abcABC --> aAbBcC
227  @author Jan Woetzel */
228  static int
229  DeinterleaveHorizontal(const BIAS::ImageBase & raw, BIAS::ImageBase & out);
230 
231  /** \brief Converts the existing image to an planar version. If source = dest in place conversion alters the source image!!!
232  @author Ingo Thomsen
233  @date 03.04.2002
234  @status none
235  case CM_YUYV422 not yet tested **/
236  template<class StorageType>
237  BIASImageBase_EXPORT
238  static int
239  ToPlanar(const Image<StorageType>& source, Image<StorageType>& dest);
240 
241  /** \brief Extracts color planes to argument images without changing source.
242  So far only implemented for CM_YUYV422
243  @author Felix Woelk
244  @status alpha (18/04/2002) */
245  template<class StorageType>
246  BIASImageBase_EXPORT
247  static int
248  FromInterleaved(const Image<StorageType>& source,
249  Image<StorageType>& Image1, Image<StorageType>& Image2, Image<
250  StorageType>& Image3);
251 
252  /**
253  @author frick
254  converts image from rgb to xyz color space
255  rgb values are requiered to be in intervall [0,scale]
256  */
257  static int
258  RGBToXYZ(const BIAS::ImageBase& source, BIAS::Image<float>& dest,float scale = 255.0);
259 
260  /**
261  @author frick
262  */
263  template<class StorageType>
264  static int
265  XYZToRGB(const BIAS::Image<float>& source, BIAS::Image<StorageType>& dest,
266  float scale = 255.0);
267 
268  /** \brief Convert XYZ image to a LUV color space image
269  * \author africk */
270  static int
271  XYZToLUV(const BIAS::Image<float>& source, BIAS::Image<float>& dest);
272 
273  /** \brief Convert LUV image to a XYZ color space image
274  * \author africk */
275  static int
276  LUVToXYZ(const BIAS::Image<float>& source, BIAS::Image<float>& dest);
277 
278  /**\brief Convert XYZ image to a LAB color space image
279  * \author bangerer
280  * \date 01/2009 */
281  static int
282  XYZToLAB(const BIAS::Image<float>& source, BIAS::Image<float>& dest);
283 
284  /** \brief Convert RGB image to a LUV color space image
285  * \author frick */
286  static int
287  RGBToLUV(const BIAS::ImageBase& source, BIAS::Image<float>& dest,
288  float scale = 255.0);
289 
290  /** \brief Convert RGB image to a LAB color space image
291  * \author frick */
292  static int
293  RGBToLAB(const BIAS::ImageBase& source, BIAS::Image<float>& dest,
294  float scale = 255.0);
295 
296  /** \brief Convert LUV image to a RGB color space image
297  * \author frick */
298  template<class StorageType>
299  static int
300  LUVToRGB(const BIAS::Image<float>& source, BIAS::Image<StorageType>& dest,
301  float scale = 255.0);
302 
303  /**
304  * Converts RGB image to Othe's principle component generalization for RGB based
305  * segmentation.
306  * \author bartczak 12/2008
307  */
308  static int RGBToI1I2I3(const BIAS::ImageBase& source, BIAS::Image<float>& dest);
309  protected:
310  template<class sourceType>
311  static void ConvertRGBToI1I2I3Worker_(const sourceType* soP, float* siP, int size);
312 
313  public:
314  /////////////////////////////////////////////////////////////////
315  /// foreign image formats, ipl, vil, ImageMagick
316  /////////////////////////////////////////////////////////////////
317 
318 #ifdef BIAS_HAVE_DV
319  /** Can specify speed/quality trade off
320  0 : monochrome , DC coeffs only
321  1 : monochrome , AC 1 coeffs
322  2 : color , DC coeffs only
323  3 : color , AC 1 coeffs
324  4 : color, AC 2 coeffs (slowest but best quality)
325  */
326  template <class StorageType>
327  static int DVToRGB_(const Image<StorageType>& source,
328  Image<StorageType>& dest,unsigned char quality=4);
329 #endif
330 
331 #ifdef BIAS_HAVE_IMAGEMAGICKLIB
332  /**
333  * \brief Returns a new, separate ImageMagick Image for use with ImageMagick
334  * created from the source BIAS Image.
335  * \return newly created ImageMagick image
336  * \author ischiller */
337  static int
338  BIAS2ImageMagick(const BIAS::ImageBase& input, Magick::Image& dest);
339  /**
340  * \brief Returns a new, separate BIAS Image
341  * created from the source ImageMagick Image.
342  * \return newly created BIAS image
343  * \author ischiller */
344  static int
345  ImageMagick2BIAS(Magick::Image& image, BIAS::ImageBase& result);
346 
347 #endif
348 
349 #ifdef BIAS_HAVE_OPENCV
350  /** Returns a new, separate IplImage for use with OpenCV created rom
351  the source BIAS Image.
352  The dest has to be a call by reference "*&" because the pointer
353  itself is changed due to new.
354  The colorChannel order is swapped because OpenCv used BGR -
355  which may be inefficient.
356  See WrapBias2Ipl class, too.
357  @return newly created IplImage
358  @todo ROIs are not yet kept, when converted to IplImages
359  @author Ingo Thomsen 04.05.2002, Jan Woetzel 2005
360  */
361  template<class StorageType>
362  static int
363  BIAS2ipl(const Image<StorageType>& source, IplImage* &dest);
364 
365  /** Takes the image information and data of an existing IplImage
366  and builds a Image. The existing data is discarded.
367 
368  @author Ingo Thomsen
369  @date 04.17.2002
370  @return -1 in case of failure
371  @param image The IplImage to be transfered
372  @status untested
373 
374  @todo Make it work for colored images
375  @todo Make it work for ROIs **/
376  template<class StorageType>
377  static int
378  Ipl2BIAS(IplImage* source, Image<StorageType>& dest);
379 #endif // BIAS_HAVE_OPENCV
380  protected:
381  template<class StorageType>
382  static int
383  IP_RGBToGrey_(Image<StorageType> &img);
384 
385  template<class StorageType>
386  static int
387  IP_YUV422ToGrey_(Image<StorageType> &img);
388 
389  template<class StorageType>
390  static int
391  BGRToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
392 
393  template<class StorageType>
394  static int
395  YUV422ToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
396 
397  template <class StorageType>
398  static int
399  HSLToRGB_(const BIAS::Image<StorageType>& source,BIAS::Image<StorageType>& dest);
400 
401  template<class StorageType>
402  static int
403  YUV420PToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
404 
405  template<class StorageType>
406  static int
407  YUV420PToGrey_(const Image<StorageType>& source,Image<StorageType>& dest);
408 
409  template<class StorageType>
410  static int
411  YUV411ToGrey_(const Image<StorageType>& source, Image<StorageType>& dest);
412  template<class StorageType>
413  static int
414  GreyToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
415 
416  template<class StorageType>
417  static int
418  RGBToGrey_(const Image<StorageType>& source, Image<StorageType>& dest);
419 
420  /**
421  not tested - use at own risk - alpha channel will be ignored
422  **/
423  template<class StorageType>
424  static int
425  BGRAToGrey_(const Image<StorageType>& source, Image<StorageType>& dest);
426 
427  template<class StorageType>
428  static int
429  YUV422ToGrey_(const Image<StorageType> &source, Image<StorageType>& dest);
430 
431  public:
432  /** added border calculation, woelk
433  @authour evers, woelk*/
434  template<class StorageType>
435  static int
436  BayerToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
437 
438  /**
439  @brief converts an image with bayer pattern to rgb
440  @author bangerer 05/2009
441  @param source[in]: source image
442  @param dest[out]: destination image
443  @param method[in]: choose the demosaicing method from several methods drom BayerDemosaicMethod enum. standard is bilinear
444  @param flip[in]: flip the image horizontal after converting it to rgb
445  */
446  template<class StorageType>
447  static int BayerToRGB(const Image<StorageType>& source, Image<StorageType>& dest, BayerDemosaicMethod method = BAYER_DEMOSAIC_METHOD_BILINEAR, bool flip = false);
448 
449  template<class StorageType>
450  static int BayerToRGBSimple(const StorageType* source, StorageType* dest, int sx, int sy, ImageBase::EColorModel tile);
451 
452  /**
453  * \brief conversion of one pixel in HSV to RGB colorspace
454  * \date 06/2010
455  * \return 0 on sucess, < 0 otherwise
456  * \param [in] h,s,v : HSV values
457  * \param [out] r,g,b : calculated RGB values
458  * \author ischiller
459  */
460  template<class StorageType> BIASImageBase_EXPORT
461  static int HSVToRGB(const StorageType h, const StorageType s, const StorageType v,
462  StorageType &r, StorageType &g, StorageType &b);
463 
464 
465  protected:
466  /** this is the right way, green interpoaltion is mostly done
467  out of two values
468  no visible difference to fast calculation as used above
469  @author woelk 03 2003 */
470  template<class StorageType>
471  static int
472  BayerToRGBSlow_(const Image<StorageType>& source,
473  Image<StorageType>& dest);
474  /** interpolates, better than above
475  @author woelk 03 2003 */
476  template<class StorageType>
477  static int
478  BayerToGrey_(const Image<StorageType>& source, Image<StorageType>& dest);
479 
480  protected:
481  // methods for demosaic bayer pattern
482  // the methods are taken from libdc1394 - 1394-Based Digital Camera Control Library, and adapted to BIAS
483  template<class StorageType>
484  static int BayerToRGBSimple_(const StorageType* source, StorageType* dest, int sx, int sy, ImageBase::EColorModel tile);
485  template<class StorageType>
486  static int BayerToRGBBilinear_(const StorageType* source, StorageType* dest, int sx, int sy, ImageBase::EColorModel tile);
487  template<class StorageType>
488  static int BayerToRGBNearestNeighbour_(const StorageType* source, StorageType* dest, int sx, int sy, ImageBase::EColorModel tile);
489  static int BayerToRGBHQLinear_(const unsigned char* source, unsigned char* dest, int sx, int sy, ImageBase::EColorModel tile);
490  static int BayerToRGBHQLinear_(const unsigned short* source, unsigned short* dest, int sx, int sy, ImageBase::EColorModel tile, int bits);
491  template<class StorageType>
492  static int BayerToRGBAHD_(const StorageType* source, StorageType* dest, int sx, int sy, ImageBase::EColorModel tile, int bits);
493  template<class StorageType>
494  static void ClearBorders_(StorageType *rgb, int sx, int sy, int w);
495 
496  template<class StorageType>
497  static void CLIP(StorageType& in, StorageType& out);
498  template<class StorageType>
499  static void CLIP16(StorageType& in, StorageType& out, StorageType bits);
500 
501  /** just copies the L channel
502  @author Daniel Grest */
503  template<class StorageType>
504  static int
505  HSLToGrey_(const Image<StorageType>& source, Image<StorageType>& dest);
506 
507  // helper function for ToPlanar(Image<StorageType>& source)
508  template<class StorageType>
509  static int
510  ToPlanarYUYV422_(const Image<StorageType>& source, Image<StorageType>& dest);
511 
512  template<class StorageType>
513  static int
514  ToPlanarRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
515 
516  // helper function for ToInterleaved(Image<StorageType>& source)
517  template<class StorageType>
518  static int
519  ToInterleavedYUYV422_(const Image<StorageType>& source,
520  Image<StorageType>& dest);
521  template<class StorageType>
522  static int
523  ToInterleavedRGB_(const Image<StorageType>& source,
524  Image<StorageType>& dest);
525 
526  template<class StorageType>
527  static int
528  FromInterleavedYUYV422_(const Image<StorageType>& source, Image<
529  StorageType>& Y, Image<StorageType>& U, Image<StorageType>& V);
530 
531  template<class StorageType>
532  static int
533  FromInterleavedRGB_(const Image<StorageType>& source,
535 
536  /// so far only implemented for StorageType=float
537  //template <class StorageType>
538  template<class StorageType>
539  static int
540  RGBToHSV_(const Image<StorageType>& source, Image<StorageType>& dest);
541 
542  template<class StorageType>
543  static int
544  HSVToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
545 
546 
547  template<class StorageType>
548  static int
549  RGBToHSL_(const Image<StorageType>& source, Image<StorageType>& dest);
550 
551  template<class StorageType>
552  static int
553  BGRToHSL_(const Image<StorageType>& source, Image<StorageType>& dest);
554 
555  template<class StorageType>
556  static int
557  BGRAToHSL_(const Image<StorageType>& source, Image<StorageType>& dest);
558 
559  template<class StorageType>
560  static int
561  RGBTohsL_(const Image<StorageType>& source, Image<StorageType>& dest);
562 
563  template<class StorageType>
564  static int
565  RGBAToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
566 
567  template<class StorageType>
568  static int
569  BGRAToRGB_(const Image<StorageType>& source, Image<StorageType>& dest);
570 
571  //in WIN32 forward declarations for specialized function templates are needed
572 #ifdef _WIN32
573  template <>
574  static int RGBToHSV_(const Image<float>& source,
575  Image<float>& dest);
576 
577  template <>
578  static int RGBToHSV_(const Image<unsigned char>& source,
579  Image<unsigned char>& dest);
580 
581  template <>
582  static int RGBToHSL_(const Image<float>& source,
583  Image<float>& dest);
584 
585  template <>
586  static int RGBToHSL_(const Image<unsigned char>& source,
587  Image<unsigned char>& dest);
588 
589  template <>
590  static int BGRToHSL_(const Image<unsigned char>& source,
591  Image<unsigned char>& dest);
592 #ifdef BUILD_IMAGE_INT
593  template <>
594  static int RGBTohsL_(const Image<int>& source,
595  Image<int>& dest);
596 #endif
597  template <>
598  static int RGBTohsL_(const Image<unsigned char>& source,
599  Image<unsigned char>& dest);
600 
601 #endif
602  static int
603  FloatToUC_(const ImageBase &source, ImageBase &dest);
604 
605 #ifdef BIAS_HAVE_DV
606  static dv_decoder_t *DVDecoder_;
607 #endif
608 
609  };
610 
611 } // namespace
612 
613 
614 #endif
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
BayerDemosaicMethod
methods for conversion from bayer to rgb
Definition: ImageConvert.hh:70
class with static functions for converting all kinds of color formats and storage types ...
Definition: ImageConvert.hh:89
static dv_decoder_t * DVDecoder_
The image template class for specific storage types.
Definition: Image.hh:78
This is the base class for images in BIAS.
Definition: ImageBase.hh:102