himage2iplimage.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  * 
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018  
00019 #ifndef HIMAGE2IPLIMAGE_H
00020 #define HIMAGE2IPLIMAGE_H
00021 
00025 #include <Halcon.h>
00026 
00027 
00028 
00029 /*/ here are following private macros for the next function: HImage2IplImage
00030 //Copy all channels in the composed pixels that are expected by OpenCv**/
00031 template<class type> IplImage* CPYIMG2CV(Hlong** himg, CvSize size, INT depth, INT channels, INT width, INT height)                                     
00032 {                                                                                                                                                               
00033         IplImage* iplimg;
00034     type* pointer[3];                                                                                                                   
00035         int pixelItLines = 1;                                                                                                           
00036         int pixelItColumns = 1;                                                                                                         
00037         int channelIt = 0;                                                                                                                      
00038         iplimg = cvCreateImage(size, depth, channels);                          
00039         if(channels == 1 && (sizeof(type) * width * height) == iplimg->imageSize)       
00040         {                                                                                                                                                       
00041                 memcpy(iplimg->imageData, (void*)himg[0], sizeof(type) * width * height);
00042         }                                                                                                                                                       
00043         else                                                                                                                                            
00044         {                                                                                                                                                       
00045                 for (channelIt = 0; channelIt < channels; channelIt++)                                  
00046                         pointer[channelIt] = (type*)himg[channelIt];                                    
00047                 for (pixelItLines = 0; pixelItLines < height ;pixelItLines++ )                  
00048         {                                                                                                                                               
00049                         for (pixelItColumns = 0;pixelItColumns < width; pixelItColumns++)       
00050                         {                                                                                                                                       
00051                                 CvScalar csv;                                                                                                   
00052                                 for (channelIt = 0; channelIt < channels; channelIt++)                  
00053                                 {                                                                                                                               
00054                                         csv.val[(channels-1) - channelIt] = pointer[channelIt][pixelItLines * width +  pixelItColumns];
00055                 }                                                                                                                       
00056                                 cvSet2D(iplimg,pixelItLines,pixelItColumns,csv);                                
00057                         }                                                                                                                                       
00058                 }                                                                                                                                               
00059         }
00060     return iplimg;
00061 }                                                                                                                                                       
00062 
00063 /*/Copy all channels in the composed pixels that are expected by OpenCv, special macro for the H(U)Int2Pixel Struct*/
00064 #define CPYIMG_STRUCT2CV(depth, channels, width, height, type)                  \
00065 {                                                                                                                                                               \
00066         int pixelItLines = 1;                                                                                                           \
00067         int pixelItColumns = 1;                                                                                                         \
00068         int channelIt = 0;                                                                                                                      \
00069         iplimg = cvCreateImage(size, depth, channels);                          \
00070         for (pixelItLines = 0; pixelItLines < height ;pixelItLines++ )                          \
00071         {                                                                                                                                                       \
00072                 for (pixelItColumns = 0;pixelItColumns < width; pixelItColumns++)               \
00073                 {                                                                                                                                               \
00074                         CvScalar csv;                                                                                                           \
00075                         for(channelIt = 0; channelIt < channels; channelIt++)                           \
00076                                 csv.val[(channels - 1) - channelIt] = himg[channelIt][pixelItLines * width +  pixelItColumns]; \
00077                         cvSet2D(iplimg,pixelItLines,pixelItColumns,csv);                                        \
00078                 }                                                                                                                                               \
00079         }                                                                                                                                                       \
00080 }
00081 
00082 /*/Copy all channels in the composed pixels that are expected by OpenCv, special macro for the HComplexPixel Struct*/
00083 #define CPYIMG_COMPLEX2CV(depth, channels, width, height, type)                 \
00084 {                                                                                                                                                               \
00085         int pixelItLines = 1;                                                                                                           \
00086         int pixelItColumns = 1;                                                                                                         \
00087         int channelIt = 0;                                                                                                                      \
00088         iplimg = cvCreateImage(size, depth, channels);                          \
00089         for (pixelItLines = 0; pixelItLines < height ;pixelItLines++ )                          \
00090         {                                                                                                                                                       \
00091                 for (pixelItColumns = 0;pixelItColumns < width; pixelItColumns++)               \
00092                 {                                                                                                                                               \
00093                         CvScalar csv;                                                                                                           \
00094                         for(channelIt = 0; channelIt < channels / 2; channelIt++)                       \
00095                         {                                                                                                                                       \
00096                                 csv.val[((channels/2- 1) - channelIt) * 2    ] = himg[channelIt][pixelItLines * width +  pixelItColumns]; \
00097                                 csv.val[((channels/2- 1) - channelIt) * 2 + 1] = himg[channelIt][pixelItLines * width +  pixelItColumns]; \
00098                         }                                                                                                                                       \
00099                         cvSet2D(iplimg,pixelItLines,pixelItColumns,csv);                                        \
00100                 }                                                                                                                                               \
00101         }                                                                                                                                                       \
00102 }
00103 
00104 
00114 IplImage *HImage2IplImage(Hlong** himg, INT channels, INT width, INT height, INT type)
00115 {
00116         IplImage* iplimg;
00117         CvSize size;
00118         size.width = width;
00119         size.height = height;
00120         if(channels > 3)
00121                 channels = 3;
00122         
00123         switch(type)
00124         {
00125         case BYTE_IMAGE:
00126                 /*/ - unsigend char*/
00127                 iplimg = CPYIMG2CV<HBYTE>(himg,size, IPL_DEPTH_8U, channels, width, height);
00128                 break;
00129         case INT4_IMAGE:
00130                 /*/ - signed 32-bit integers*/
00131                 iplimg = CPYIMG2CV<INT4>(himg,size, IPL_DEPTH_32S, channels, width, height);
00132                 break;
00133         case FLOAT_IMAGE:
00134                 /*/ - single precision floating-point numbers*/
00135                 iplimg = CPYIMG2CV<float>(himg,size, IPL_DEPTH_32F, channels, width, height);
00136                 /*/ TODO Check if there are used double in halcon instead of float?*/
00137                 /*/depth = IPL_DEPTH_64F;// - double precision floating-point numbers*/
00138                 break;
00139         case DIR_IMAGE: /*/TOCHECK: Byte? 0..180*/
00140                 iplimg = CPYIMG2CV<HBYTE>(himg,size, IPL_DEPTH_8U, channels, width, height);
00141                 break;
00142         case CYCLIC_IMAGE:/*/TOCHECK: nothing compareable in opencv*/
00143                 /*/TOCHECK: 0..255*/
00144                 iplimg = CPYIMG2CV<HBYTE>(himg,size, IPL_DEPTH_8U, channels, width, height);
00145                 break;
00146         case INT1_IMAGE:
00147                 /*/ - signed 8-bit integers*/
00148                 iplimg = CPYIMG2CV<INT1>(himg,size, IPL_DEPTH_8S, channels, width, height);
00149                 break;
00150         case COMPLEX_IMAGE: /*/TODO: not supported in open CV*/
00151                 CPYIMG_COMPLEX2CV(IPL_DEPTH_32F, channels * 2, width, height, float*)
00152                 break;
00153         case INT2_IMAGE:
00154                 /*/ - signed 16-bit integers*/
00155                 CPYIMG_STRUCT2CV(IPL_DEPTH_16S, channels, width, height, HInt2Pixel)
00156                 break;
00157         case UINT2_IMAGE:
00158                 /*/ - unsigned 16-bit integers*/
00159                 CPYIMG_STRUCT2CV(IPL_DEPTH_16U, channels, width, height, HUInt2Pixel)
00160                 break;
00161         }
00162         return iplimg;
00163 }
00164 
00165 
00166 #endif /*/HIMAGE2IPLIMAGE_H*/


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Mon Oct 6 2014 10:48:45