def __init__(self): # CREATING CLASS DATA self.ndim = -1 self.shape = np.zeros((vl.VGL_MAX_DIM()+1), np.int32) self.offset = np.zeros((vl.VGL_MAX_DIM()+1), np.int32) self.size = -1 self.bps = 8
def vglCreateShape(self, shape, ndim, bps=8): self.ndim = ndim self.bps = bps self.size = 1 if( (bps == 1) and (shape[0] != 1) ): print("vglShape: vglCreateShape Error: Image with 1 bps and mode then one color channels(!)") exit() maxi = ndim c = shape[vl.VGL_SHAPE_NCHANNELS()] w = shape[vl.VGL_SHAPE_WIDTH()] if(ndim == 1): maxi == 2 for i in range(0, vl.VGL_MAX_DIM()+1): if(i <= maxi): self.shape[i] = shape[i] if(i == 0): self.offset[i] = 1 elif(i == 2): self.offset[i] = self.findWidthStep(bps, w, c) else: self.offset[i] = shape[i-1] * self.offset[i-1] else: self.shape[i] = 1 self.offset[i] = 0 self.size *= self.shape[maxi] * self.offset[maxi]
def constructor2DShape(self, nChannels, w, h): shape = np.ones((vl.VGL_MAX_DIM()+1), np.int32) ndim = 2 shape[0] = nChannels shape[1] = w shape[2] = h self.vglCreateShape(shape, ndim)
def asVglClShape(self): result = VglClShape() result.ndim = np.int32(self.ndim) result.size = np.int32(self.size) for i in range(0, vl.VGL_MAX_DIM()+1): result.shape[i] = np.int32(self.shape[i]) result.offset[i] = np.int32(self.offset[i]) if(self.ndim == 1): result.shape[vl.VGL_SHAPE_WIDTH()] = np.int32(self.getWidth()) result.offset[vl.VGL_SHAPE_WIDTH()] = np.int32(result.shape[vl.VGL_SHAPE_WIDTH()-1] * result.offset[vl.VGL_SHAPE_WIDTH()-1]) result.shape[vl.VGL_SHAPE_HEIGTH()] = np.int32(self.getHeigth()) result.offset[vl.VGL_SHAPE_HEIGTH()] = np.int32(result.shape[vl.VGL_SHAPE_HEIGTH()-1] * result.offset[vl.VGL_SHAPE_HEIGTH()-1]) return result
def __init__(self, imgPath="", depth=None, ndim=None, clForceAsBuf=None ): # IF THE IMAGE TYPE IS NOT SPECIFIED, A 2D IMAGE WILL BE ASSUMED # INICIALIZING DATA self.ipl = None self.ndim = ndim self.shape = np.zeros((2*vl.VGL_MAX_DIM()), np.uint8) self.vglShape = None self.depth = depth self.nChannels = 0 self.has_mipmap = 0 self.oclPtr = None self.clForceAsBuf = clForceAsBuf self.inContext = 0 self.filename = imgPath # NOT IMPLEMENTED IN PYTHON-SIDE self.fbo = -1 self.tex = -1 self.cudaPtr = None self.cudaPbo = -1 if( self.depth == None ): self.depth = vl.IPL_DEPTH_1U() if( self.clForceAsBuf is None ): self.clForceAsBuf = vl.IMAGE_CL_OBJECT() elif( not((self.clForceAsBuf is vl.IMAGE_CL_OBJECT() ) or (self.clForceAsBuf is vl.IMAGE_ND_ARRAY() ) ) ): print("VglImage: Error! Unexistent image treatment. Use vl.IMAGE_CL_OBJECT() or vl.IMAGE_ND_ARRAY()!") exit() if(self.ndim is None): self.ndim = vl.VGL_IMAGE_2D_IMAGE() print(":Assuming 2D Image!") elif(self.ndim is vl.VGL_IMAGE_2D_IMAGE()): print(":Creating 2D Image!") elif(self.ndim is vl.VGL_IMAGE_3D_IMAGE()): print(":Creating 3D Image!") else: print("vglImage: Warning! Image is not 2D or 3D. Execution will continue.") print(":::-->path", imgPath) print(":::-->dept", depth) print(":::-->ndim", ndim) print(":::-->forc", clForceAsBuf)
def asVglClStrEl(self): result = VglClStrEl() shape = self.vglShape.asVglClShape() size = self.getSize() if (size > vl.VGL_ARR_CLSTREL_SIZE()): print( "vglStrEl: asVglClStrEl Error: structuring element size > VGL_ARR_CLSTREL_SIZE. Change this value in vglClStrEl.h to a greater one." ) exit() result.ndim = np.int32(self.vglShape.getNdim()) result.size = np.int32(self.vglShape.getSize()) for i in range(0, vl.VGL_MAX_DIM() + 1): result.shape[i] = np.int32(shape.shape[i]) result.offset[i] = np.int32(shape.offset[i]) for i in range(0, size): result.data[i] = np.int32(self.data[i]) return result
def load_headers(self, filepath): #print("Loading Headers") self.kernel_file = open(filepath, "r") buildDir = self.getDir(filepath) self.build_options = "-I " + buildDir self.build_options = self.build_options + " -D VGL_SHAPE_NCHANNELS={0}".format( vl.VGL_SHAPE_NCHANNELS()) self.build_options = self.build_options + " -D VGL_SHAPE_WIDTH={0}".format( vl.VGL_SHAPE_WIDTH()) self.build_options = self.build_options + " -D VGL_SHAPE_HEIGTH={0}".format( vl.VGL_SHAPE_HEIGTH()) self.build_options = self.build_options + " -D VGL_SHAPE_LENGTH={0}".format( vl.VGL_SHAPE_LENGTH()) self.build_options = self.build_options + " -D VGL_MAX_DIM={0}".format( vl.VGL_MAX_DIM()) self.build_options = self.build_options + " -D VGL_ARR_SHAPE_SIZE={0}".format( vl.VGL_ARR_SHAPE_SIZE()) self.build_options = self.build_options + " -D VGL_ARR_CLSTREL_SIZE={0}".format( vl.VGL_ARR_CLSTREL_SIZE()) self.build_options = self.build_options + " -D VGL_STREL_CUBE={0}".format( vl.VGL_STREL_CUBE()) self.build_options = self.build_options + " -D VGL_STREL_CROSS={0}".format( vl.VGL_STREL_CROSS()) self.build_options = self.build_options + " -D VGL_STREL_GAUSS={0}".format( vl.VGL_STREL_GAUSS()) self.build_options = self.build_options + " -D VGL_STREL_MEAN={0}".format( vl.VGL_STREL_MEAN()) # READING THE HEADER FILES BEFORE COMPILING THE KERNEL while (buildDir): for file in glob.glob(buildDir + "/*.h"): #print(file) self.pgr = cl.Program(self.ctx, open(file, "r")) buildDir = self.getDir(buildDir)
def constructorFromTypeNdim(self, Type, ndim): shape = np.zeros(vl.VGL_MAX_DIM(), np.int32) shape[0] = 1 shape[2] = 1 for i in range(1, ndim + 1): # ndim+1 cuz in c++ is for i <= ndim shape[i] = 3 vglShape = vl.VglShape() vglShape.constructorFromShapeNdimBps(shape, ndim) size = vglShape.getSize() data = np.zeros((size), np.float32) index = 0 if (Type == vl.VGL_STREL_CROSS()): coord = np.zeros((vl.VGL_ARR_SHAPE_SIZE()), np.int32) for i in range(0, size): data[i] = np.float32(0.0) for d in range(1, ndim + 1): coord[d] = np.int32(1) index = vglShape.getIndexFromCoord(coord) data[index] = np.float32(1.0) for d in range(1, ndim + 1): coord[d] = np.int32(0) index = vglShape.getIndexFromCoord(coord) data[index] = np.float32(1.0) coord[d] = np.int32(2) index = vglShape.getIndexFromCoord(coord) data[index] = np.float32(1.0) coord[d] = 1 elif (Type == vl.VGL_STREL_GAUSS()): coord = np.zeros((vl.VGL_ARR_SHAPE_SIZE()), np.int32) coord[0] = np.int32(0) size = vglShape.getSize() for i in range(0, size): val = np.float32(1.0) vglShape.getCoordFromIndex(i, coord) for d in range(1, ndim + 1): if (coord[d] == 1): val = val * np.float32(0.5) else: val = val * np.float32(0.25) data[i] = val elif (Type == vl.VGL_STREL_MEAN()): for i in range(0, size): data[i] = 1.0 / size elif (Type == vl.VGL_STREL_CUBE()): for i in range(0, size): data[i] = 1.0 else: for i in range(0, size): data[i] = 1.0 self.constructorFromDataVglShape(data, vglShape)