Go to the documentation of this file.00001
00002
00003
00004 import roslib
00005 roslib.load_manifest('hrl_opencv')
00006 import cv
00007 import Image
00008 import numpy as np
00009
00010 def pil2cv(pi):
00011 cv_im = cv.CreateImageHeader(pi.size, cv.IPL_DEPTH_8U, 1)
00012 cv.SetData(cv_im, pi.tostring())
00013 return cv_im
00014
00015 def cv2pil(cv_im):
00016 return Image.fromstring(cv.GetSize(cv_im), "L", cv_im.tostring())
00017
00018 def cv2array(im):
00019 depth2dtype = {
00020 cv.IPL_DEPTH_8U: 'uint8',
00021 cv.IPL_DEPTH_8S: 'int8',
00022 cv.IPL_DEPTH_16U: 'uint16',
00023 cv.IPL_DEPTH_16S: 'int16',
00024 cv.IPL_DEPTH_32S: 'int32',
00025 cv.IPL_DEPTH_32F: 'float32',
00026 cv.IPL_DEPTH_64F: 'float64',
00027 }
00028
00029 arrdtype=im.depth
00030 a = np.fromstring(
00031 im.tostring(),
00032 dtype=depth2dtype[im.depth],
00033 count=im.width*im.height*im.nChannels)
00034 a.shape = (im.height,im.width,im.nChannels)
00035 return a
00036
00037 def array2cv(a):
00038 dtype2depth = {
00039 'uint8': cv.IPL_DEPTH_8U,
00040 'int8': cv.IPL_DEPTH_8S,
00041 'uint16': cv.IPL_DEPTH_16U,
00042 'int16': cv.IPL_DEPTH_16S,
00043 'int32': cv.IPL_DEPTH_32S,
00044 'float32': cv.IPL_DEPTH_32F,
00045 'float64': cv.IPL_DEPTH_64F,
00046 }
00047 try:
00048 nChannels = a.shape[2]
00049 except:
00050 nChannels = 1
00051 cv_im = cv.CreateImageHeader((a.shape[1],a.shape[0]),
00052 dtype2depth[str(a.dtype)],
00053 nChannels)
00054 cv.SetData(cv_im, a.tostring(),
00055 a.dtype.itemsize*nChannels*a.shape[1])
00056 return cv_im
00057
00058 def array2cvmat(a):
00059 dtype2type = {
00060 'uint8': cv.CV_8UC1,
00061 'int8': cv.CV_8SC1,
00062 'uint16': cv.CV_16UC1,
00063 'int16': cv.CV_16SC1,
00064 'int32': cv.CV_32SC1,
00065 'float32': cv.CV_32FC1,
00066 'float64': cv.CV_64FC1
00067 }
00068
00069 rows = a.shape[0]
00070 cols = a.shape[1]
00071 type = dtype2type[str(a.dtype)]
00072 cvmat = cv.CreateMatHeader(rows, cols, type)
00073
00074
00075 cv.SetData(cvmat, a.tostring(), a.dtype.itemsize * a.shape[1])
00076 return cvmat
00077