OpenCVUtil.C

Go to the documentation of this file.
00001 /*!@file Image/OpenCVUtil.C OpenCV IPL image conversions
00002  */
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/OpenCVUtil.C $
00036 // $Id: OpenCVUtil.C 14605 2011-03-15 02:25:06Z dparks $
00037 //
00038 
00039 #ifdef HAVE_OPENCV
00040 #include "Image/OpenCVUtil.H"
00041 
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Util/log.H"
00045 
00046 // ######################################################################
00047 IplImage* img2ipl(const Image< PixRGB<byte> > &img)
00048 {
00049   IplImage* ret_img =
00050     cvCreateImageHeader(cvSize(img.getWidth(), img.getHeight()),
00051                         IPL_DEPTH_8U, 3);
00052 
00053   ret_img->imageData = (char*)(img.getArrayPtr());
00054 
00055   return ret_img;
00056 }
00057 
00058 // ######################################################################
00059 IplImage* img2ipl(const Image<byte> &img)
00060 {
00061   IplImage* ret_img =
00062     cvCreateImageHeader(cvSize(img.getWidth(), img.getHeight()),
00063                         IPL_DEPTH_8U, 1);
00064 
00065   ret_img->imageData = (char*)(img.getArrayPtr());
00066 
00067   return ret_img;
00068 }
00069 
00070 // ######################################################################
00071 IplImage* img2ipl(const Image<float> &img)
00072 {
00073   IplImage* ret_img =
00074     cvCreateImageHeader(cvSize(img.getWidth(), img.getHeight()),
00075                         IPL_DEPTH_32F, 1);
00076 
00077   ret_img->imageData = (char*)(img.getArrayPtr());
00078 
00079   return ret_img;
00080 }
00081 
00082 // ######################################################################
00083 Image<byte> ipl2gray(const IplImage* img)
00084 {
00085   if (img->depth != IPL_DEPTH_8U)
00086     LFATAL("IplImage must have depth==IPL_DEPTH_8U"
00087            "for conversion to Image<byte>");
00088 
00089   if (img->nChannels != 1)
00090     LFATAL("IplImage must have nChannels==1"
00091            "for conversion to Image<byte>");
00092 
00093   return Image<byte>(reinterpret_cast<const byte*>(img->imageData),
00094                      img->width, img->height);
00095 }
00096 
00097 // ######################################################################
00098 Image<PixRGB<byte> > ipl2rgb(const IplImage* img)
00099 {
00100   if (img->depth != IPL_DEPTH_8U)
00101     LFATAL("IplImage must have depth==IPL_DEPTH_8U"
00102            "for conversion to Image<PixRGB<byte>>");
00103 
00104   if (img->nChannels != 3)
00105     LFATAL("IplImage must have nChannels==3"
00106            "for conversion to Image<PixRGB<byte>>");
00107 
00108   if (img->dataOrder != 0)
00109     LFATAL("IplImage must have dataOrder==0 (interleaved)"
00110            "for conversion to Image<PixRGB<byte>>");
00111 
00112   return Image<PixRGB<byte> >
00113     (reinterpret_cast<const PixRGB<byte>*>(img->imageData),
00114     img->width, img->height);
00115 }
00116 
00117 // ######################################################################
00118 Image<float> ipl2float(const IplImage* img)
00119 {
00120   if (img->depth != IPL_DEPTH_32F)
00121     LFATAL("IplImage must have depth==IPL_DEPTH_32F"
00122            "for conversion to Image<float>");
00123 
00124   if (img->nChannels != 1)
00125     LFATAL("IplImage must have nChannels==1"
00126            "for conversion to Image<float>");
00127 
00128   return Image<float>(reinterpret_cast<const float*>(img->imageData),
00129                       img->width, img->height);
00130 }
00131 
00132 #endif
00133 
00134 // ######################################################################
00135 // ######################################################################
00136 /* So things look consistent in everyone's emacs... */
00137 /* Local Variables: */
00138 /* indent-tabs-mode: nil */
00139 /* End: */
Generated on Sun May 8 08:05:14 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3