00001 # Copyright (c) 2008, Willow Garage, Inc. 00002 # All rights reserved. 00003 # 00004 # Redistribution and use in source and binary forms, with or without 00005 # modification, are permitted provided that the following conditions are met: 00006 # 00007 # * Redistributions of source code must retain the above copyright 00008 # notice, this list of conditions and the following disclaimer. 00009 # * Redistributions in binary form must reproduce the above copyright 00010 # notice, this list of conditions and the following disclaimer in the 00011 # documentation and/or other materials provided with the distribution. 00012 # * Neither the name of the Willow Garage, Inc. nor the names of its 00013 # contributors may be used to endorse or promote products derived from 00014 # this software without specific prior written permission. 00015 # 00016 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00019 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00020 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00021 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00022 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00023 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00024 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00025 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 # POSSIBILITY OF SUCH DAMAGE. 00027 # 00028 ## @author Hai Nguyen/hai@gatech.edu 00029 #from opencv import cv 00030 #from opencv import highgui 00031 import numpy as np 00032 import pickle as pk 00033 00034 def list_mat_to_mat(list_mat, axis=0): 00035 return np.concatenate(tuple(list_mat), axis=axis) 00036 00037 def load_pickle(filename): 00038 p = open(filename, 'r') 00039 picklelicious = pk.load(p) 00040 p.close() 00041 return picklelicious 00042 00043 def dump_pickle(object, filename): 00044 pickle_file = open(filename, 'w') 00045 pk.dump(object, pickle_file) 00046 pickle_file.close() 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 #import Image as Image 00068 00069 00070 #cv2np_type_dict = {cv.CV_16S : (np.int16, 1), 00071 # cv.CV_16SC : (np.int16, 1), 00072 # cv.CV_16SC1 : (np.int16, 1), 00073 # cv.CV_16SC2 : (np.int16, 2), 00074 # cv.CV_16SC3 : (np.int16, 3), 00075 # cv.CV_16SC4 : (np.int16, 4), 00076 # cv.CV_16U : (np.uint16, 1), 00077 # cv.CV_16UC : (np.uint16, 1), 00078 # cv.CV_16UC1 : (np.uint16, 1), 00079 # cv.CV_16UC2 : (np.uint16, 2), 00080 # cv.CV_16UC3 : (np.uint16, 3), 00081 # cv.CV_16UC4 : (np.uint16, 4), 00082 # cv.CV_32F : (np.float32, 1), 00083 # cv.CV_32FC : (np.float32, 1), 00084 # cv.CV_32FC1 : (np.float32, 1), 00085 # cv.CV_32FC2 : (np.float32, 2), 00086 # cv.CV_32FC3 : (np.float32, 3), 00087 # cv.CV_32FC4 : (np.float32, 4), 00088 # cv.CV_32S : (np.int32, 1), 00089 # cv.CV_32SC : (np.int32, 1), 00090 # cv.CV_32SC1 : (np.int32, 1), 00091 # cv.CV_32SC2 : (np.int32, 2), 00092 # cv.CV_32SC3 : (np.int32, 3), 00093 # cv.CV_32SC4 : (np.int32, 4), 00094 # cv.CV_64F : (np.float64, 1), 00095 # cv.CV_64FC : (np.float64, 1), 00096 # cv.CV_64FC1 : (np.float64, 1), 00097 # cv.CV_64FC2 : (np.float64, 2), 00098 # cv.CV_64FC3 : (np.float64, 3), 00099 # cv.CV_64FC4 : (np.float64, 4), 00100 # cv.CV_8S : (np.int8, 1), 00101 # cv.CV_8SC : (np.int8, 1), 00102 # cv.CV_8SC1 : (np.int8, 1), 00103 # cv.CV_8SC2 : (np.int8, 2), 00104 # cv.CV_8SC3 : (np.int8, 3), 00105 # cv.CV_8SC4 : (np.int8, 4), 00106 # cv.CV_8U : (np.uint8, 1), 00107 # cv.CV_8UC : (np.uint8, 1), 00108 # cv.CV_8UC1 : (np.uint8, 1), 00109 # cv.CV_8UC2 : (np.uint8, 2), 00110 # cv.CV_8UC3 : (np.uint8, 3), 00111 # cv.CV_8UC4 : (np.uint8, 4)} 00112 00113 #def numpymat2cvmat(nmat): 00114 # raise RuntimeError("numpymat2cvmat: use something else") 00115 # #cvmat = cv.cvCreateMat(nmat.shape[0],nmat.shape[1],cv.CV_32FC1) 00116 # #for i in range(nmat.shape[0]): 00117 # # for j in range(nmat.shape[1]): 00118 # # #print cvmat[i][j] 00119 # # #print nmat[i,j] 00120 # # cvmat[i,j] = nmat[i,j] 00121 # #return cvmat 00122 # 00123 #def cvmat2numpymat(cvmat): 00124 # raise RuntimeError("cvmat2numpymat: use something else") 00125 # #nmat = np.zeros((cvmat.width,cvmat.height)) 00126 # #for i in range(cvmat.width): 00127 # # for j in range(cvmat.height): 00128 # # nmat[i][j] = cvmat[i][j] 00129 # #return nmat 00130 # 00131 #def cv2np(im, format='RGB'): 00132 # raise RuntimeError("cv2np: use something else") 00133 # #if format == 'BGR': 00134 # # cv.cvCvtColor( im, im, cv.CV_BGR2RGB ) 00135 # #numpy_type, nchannels = cv2np_type_dict[cv.cvGetElemType(im)] 00136 # #array_size = [im.height, im.width, nchannels] 00137 # #np_im = np.frombuffer(im.imageData, dtype=numpy_type, 00138 # # count=im.height*im.width*nchannels*(im.depth/8)) 00139 # #return np.reshape(np_im, array_size) 00140 #def np2cv(im): 00141 # raise RuntimeError("np2cv: use something else") 00142 # #image = np2pil( im ) 00143 # #image.save('test.bmp', 'BMP') 00144 # #cvim = highgui.cvLoadImage('test.bmp') 00145 # #return cvim 00146 00147 #def np2pil( im ): 00148 # """ for grayscale - all values must be between 0 and 255. 00149 # not sure about color yet. 00150 # """ 00151 # raise RuntimeError("np2pil: moved to hrl_lib.util") 00152 # ##TODO: print 'util.np2cv: works for texseg.py' 00153 # ##TODO: print 'util.np2cv: more extensive tests would be useful' 00154 # #if len(im.shape) == 3: 00155 # # shp = im.shape 00156 # # channels = shp[2] 00157 # # height, width = shp[0], shp[1] 00158 # #elif len(im.shape) == 2: 00159 # # height, width = im.shape 00160 # # channels = 1 00161 # #else: 00162 # # raise AssertionError("unrecognized shape for the input image. should be 3 or 2, but was %d." % len(im.shape)) 00163 # # 00164 # #if channels == 3: 00165 # # image = Image.fromstring( "RGB", (width, height), im.tostring() ) 00166 # #if channels == 1: 00167 # # im = np.array(im, dtype=np.uint8) 00168 # # image = Image.fromarray(im) 00169 # # #image = Image.fromstring( "L", (width, height), im.tostring() ) 00170 # # 00171 # #return image