$search
00001 #!/usr/bin/env python 00002 ################################################################# 00003 ##\file 00004 # 00005 # \note 00006 # Copyright (c) 2011-2012 \n 00007 # Fraunhofer Institute for Manufacturing Engineering 00008 # and Automation (IPA) \n\n 00009 # 00010 ################################################################# 00011 # 00012 # \note 00013 # Project name: care-o-bot 00014 # \note 00015 # ROS stack name: cob_calibration 00016 # \note 00017 # ROS package name: cob_camera_calibration 00018 # 00019 # \author 00020 # Author: Sebastian Haug, email:sebhaug@gmail.com 00021 # \author 00022 # Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de 00023 # 00024 # \date Date of creation: November 2011 00025 # 00026 ################################################################# 00027 # 00028 # Redistribution and use in source and binary forms, with or without 00029 # modification, are permitted provided that the following conditions are met: 00030 # 00031 # - Redistributions of source code must retain the above copyright 00032 # notice, this list of conditions and the following disclaimer. \n 00033 # - Redistributions in binary form must reproduce the above copyright 00034 # notice, this list of conditions and the following disclaimer in the 00035 # documentation and/or other materials provided with the distribution. \n 00036 # - Neither the name of the Fraunhofer Institute for Manufacturing 00037 # Engineering and Automation (IPA) nor the names of its 00038 # contributors may be used to endorse or promote products derived from 00039 # this software without specific prior written permission. \n 00040 # 00041 # This program is free software: you can redistribute it and/or modify 00042 # it under the terms of the GNU Lesser General Public License LGPL as 00043 # published by the Free Software Foundation, either version 3 of the 00044 # License, or (at your option) any later version. 00045 # 00046 # This program is distributed in the hope that it will be useful, 00047 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00048 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00049 # GNU Lesser General Public License LGPL for more details. 00050 # 00051 # You should have received a copy of the GNU Lesser General Public 00052 # License LGPL along with this program. 00053 # If not, see <http://www.gnu.org/licenses/>. 00054 # 00055 ################################################################# 00056 00057 import cv2 00058 import numpy as np 00059 00060 ''' 00061 Provides helper functions to convert between opencv's cv and cv2 python interface data types 00062 ''' 00063 def np2cvmat(np): 00064 ''' 00065 Converts np.array (cv2 interface) to cvmat (cv interface) 00066 ''' 00067 np_type2cvmat_type = { 00068 ('uint8', 1): cv2.CV_8UC1, 00069 ('uint8', 2): cv2.CV_8UC2, 00070 ('uint8', 3): cv2.CV_8UC3, 00071 ('uint8', 4): cv2.CV_8UC4, 00072 00073 ('int8', 1): cv2.CV_8SC1, 00074 ('int8', 2): cv2.CV_8SC2, 00075 ('int8', 3): cv2.CV_8SC3, 00076 ('int8', 4): cv2.CV_8SC4, 00077 00078 ('uint16', 1): cv2.CV_16UC1, 00079 ('uint16', 2): cv2.CV_16UC2, 00080 ('uint16', 3): cv2.CV_16UC3, 00081 ('uint16', 4): cv2.CV_16UC4, 00082 00083 ('int16', 1): cv2.CV_16SC1, 00084 ('int16', 2): cv2.CV_16SC2, 00085 ('int16', 3): cv2.CV_16SC3, 00086 ('int16', 4): cv2.CV_16SC4, 00087 00088 ('int32', 1): cv2.CV_32SC1, 00089 ('int32', 2): cv2.CV_32SC2, 00090 ('int32', 3): cv2.CV_32SC3, 00091 ('int32', 4): cv2.CV_32SC4, 00092 00093 ('float32', 1): cv2.CV_32FC1, 00094 ('float32', 2): cv2.CV_32FC2, 00095 ('float32', 3): cv2.CV_32FC3, 00096 ('float32', 4): cv2.CV_32FC4, 00097 00098 ('float64', 1): cv2.CV_64FC1, 00099 ('float64', 2): cv2.CV_64FC2, 00100 ('float64', 3): cv2.CV_64FC3, 00101 ('float64', 4): cv2.CV_64FC4 00102 } 00103 00104 try: channels = np.shape[2] 00105 except: channels = 1 00106 00107 cvmat = cv2.cv.CreateMatHeader(np.shape[1],np.shape[0], np_type2cvmat_type[(str(np.dtype), channels)]) 00108 cv2.cv.SetData(cvmat, np, cv2.cv.CV_AUTOSTEP) 00109 return cvmat 00110 00111 def cvmat2np(cvmat): 00112 ''' 00113 Converts cvmat (cv interface) data to np.array (cv2 interface) 00114 ''' 00115 cvmat_type2np_type = { 00116 cv2.CV_8UC1: 'uint8', 00117 cv2.CV_8UC2: 'uint8', 00118 cv2.CV_8UC3: 'uint8', 00119 cv2.CV_8UC4: 'uint8', 00120 00121 cv2.CV_8SC1: 'int8', 00122 cv2.CV_8SC2: 'int8', 00123 cv2.CV_8SC3: 'int8', 00124 cv2.CV_8SC4: 'int8', 00125 00126 cv2.CV_16UC1: 'uint16', 00127 cv2.CV_16UC2: 'uint16', 00128 cv2.CV_16UC3: 'uint16', 00129 cv2.CV_16UC4: 'uint16', 00130 00131 cv2.CV_16SC1: 'int16', 00132 cv2.CV_16SC2: 'int16', 00133 cv2.CV_16SC3: 'int16', 00134 cv2.CV_16SC4: 'int16', 00135 00136 cv2.CV_32SC1: 'int32', 00137 cv2.CV_32SC2: 'int32', 00138 cv2.CV_32SC3: 'int32', 00139 cv2.CV_32SC4: 'int32', 00140 00141 cv2.CV_32FC1: 'float32', 00142 cv2.CV_32FC2: 'float32', 00143 cv2.CV_32FC3: 'float32', 00144 cv2.CV_32FC4: 'float32', 00145 00146 cv2.CV_64FC1: 'float64', 00147 cv2.CV_64FC2: 'float64', 00148 cv2.CV_64FC3: 'float64', 00149 cv2.CV_64FC4: 'float64' 00150 } 00151 00152 count = cvmat.width * cvmat.height * cvmat.channels 00153 nparray = np.fromstring(cvmat.tostring(), cvmat_type2np_type[cvmat.type], count=count) 00154 nparray.shape = (cvmat.height, cvmat.width, cvmat.channels) 00155 return nparray