def vglClNdCopy(img_input, img_output): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit(1) if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit(1) vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) _program = vl.get_ocl_context().get_compiled_kernel( "CL_ND/vglClNdCopy.cl", "vglClNdCopy") _kernel = _program.vglClNdCopy _kernel.set_arg(0, img_input.get_oclPtr()) _kernel.set_arg(1, img_output.get_oclPtr()) # THIS IS A BLOCKING COMMAND. IT EXECUTES THE KERNEL. cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, _kernel, img_input.get_ipl().shape, None) vl.vglSetContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdCopy(self, img_input, img_output): print("# Running vglClNdCopy") if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) _program = self.cl_ctx.get_compiled_kernel("../CL_ND/vglClNdCopy.cl", "vglClNdCopy") kernel_run = _program.vglClNdCopy kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.get_ipl().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdThreshold(img_input, img_output, thresh, top=255): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit(1) if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit(1) vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) # EVALUATING IF thresh IS IN CORRECT TYPE if (not isinstance(thresh, np.uint8)): print( "vglClConvolution: Warning: thresh not np.uint8! Trying to convert..." ) try: thresh = np.uint8(thresh) except Exception as e: print( "vglClConvolution: Error!! Impossible to convert thresh as a np.uint8 object." ) print(str(e)) exit() # EVALUATING IF top IS IN CORRECT TYPE if (not isinstance(top, np.uint8)): print( "vglClConvolution: Warning: top not np.uint8! Trying to convert..." ) try: top = np.uint8(top) except Exception as e: print( "vglClConvolution: Error!! Impossible to convert top as a np.uint8 object." ) print(str(e)) exit() _program = vl.get_ocl_context().get_compiled_kernel( "CL_ND/vglClNdThreshold.cl", "vglClNdThreshold") _kernel = _program.vglClNdThreshold _kernel.set_arg(0, img_input.get_oclPtr()) _kernel.set_arg(1, img_output.get_oclPtr()) _kernel.set_arg(2, thresh) _kernel.set_arg(3, top) # THIS IS A BLOCKING COMMAND. IT EXECUTES THE KERNEL. cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, _kernel, img_input.get_ipl().shape, None) vl.vglSetContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdBinThreshold(self, img_input, img_output, thresh): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinThreshold: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinThreshold: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() if (not isinstance(thresh, np.uint8)): print( "vglClNdBinThreshold: Warning: thresh not np.uint8! Trying to convert..." ) try: thresh = np.uint8(thresh) except Exception as e: print( "vglClNdBinThreshold: Error!! Impossible to convert thresh as a np.uint8 object." ) print(str(e)) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) _program = self.cl_ctx.get_compiled_kernel( "../CL_BIN/vglClNdBinThreshold.cl", "vglClNdBinThreshold") kernel_run = _program.vglClNdBinThreshold mobj_img_shape_input = img_input.getVglShape().get_asVglClShape_buffer( ) mobj_img_shape_output = img_output.getVglShape( ).get_asVglClShape_buffer() kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) kernel_run.set_arg(2, thresh) kernel_run.set_arg(3, mobj_img_shape_input) kernel_run.set_arg(4, mobj_img_shape_output) _worksize_0 = img_input.getWidthIn() if (img_input.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_input.getWidthStep() if (img_output.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_output.getWidthStep() worksize = (int(_worksize_0), img_input.getHeigthIn(), img_input.getNFrames()) # ENQUEUEING KERNEL EXECUTION #cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, worksize, None) cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.ipl.shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdThreshold(self, img_input, img_output, thresh, top=255): print("# Running vglClNdThreshold") if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdThreshold: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdThreshold: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() if (not isinstance(thresh, np.uint8)): print( "vglClNdThreshold: Warning: thresh not np.uint8! Trying to convert..." ) try: thresh = np.uint8(thresh) except Exception as e: print( "vglClNdThreshold: Error!! Impossible to convert thresh as a np.uint8 object." ) print(str(e)) exit() if (not isinstance(top, np.uint8)): print( "vglClNdThreshold: Warning: top not np.uint8! Trying to convert..." ) try: top = np.uint8(top) except Exception as e: print( "vglClNdThreshold: Error!! Impossible to convert top as a np.uint8 object." ) print(str(e)) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) _program = self.cl_ctx.get_compiled_kernel( "../CL_ND/vglClNdThreshold.cl", "vglClNdThreshold") kernel_run = _program.vglClNdThreshold kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) kernel_run.set_arg(2, thresh) kernel_run.set_arg(3, top) cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.get_ipl().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdBinMin(self, img_input, img_input2, img_output): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinMin: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_input2.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinMin: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinMin: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_input2, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) if (not isinstance(window, vl.VglStrEl)): print( "vglClNdBinMin: Error: window is not a VglStrEl object. aborting execution." ) exit() _program = self.cl_ctx.get_compiled_kernel( "../CL_BIN/vglClNdBinMin.cl", "vglClNdBinMin") kernel_run = _program.vglClNdBinMin kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_input2.get_oclPtr()) kernel_run.set_arg(2, img_output.get_oclPtr()) _worksize_0 = img_input.getWidthIn() if (img_input.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_input.getWidthStep() if (img_input2.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_input2.getWidthStep() if (img_output.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_output.getWidthStep() worksize = (int(_worksize_0), img_input.getHeigthIn(), img_input.getNFrames()) # ENQUEUEING KERNEL EXECUTION #cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, worksize, None) cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.get_ipl().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdBinDilatePack(self, img_input, img_output, window): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinDilatePack: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinDilatePack: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) if (not isinstance(window, vl.VglStrEl)): print( "vglClNdBinDilatePack: Error: window is not a VglStrEl object. aborting execution." ) exit() _program = self.cl_ctx.get_compiled_kernel( "../CL_BIN/vglClNdBinDilatePack.cl", "vglClNdBinDilatePack") kernel_run = _program.vglClNdBinDilatePack # CREATING OPENCL BUFFER TO VglStrEl and VglShape mobj_window = window.get_asVglClStrEl_buffer() mobj_img_shape = img_input.getVglShape().get_asVglClShape_buffer() # SETTING ARGUMENTS kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) kernel_run.set_arg(2, mobj_img_shape) kernel_run.set_arg(3, mobj_window) _worksize_0 = img_input.getWidthIn() if (img_input.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_input.getWidthStep() if (img_output.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_output.getWidthStep() worksize = (int(_worksize_0), img_input.getHeigthIn(), img_input.getNFrames()) # ENQUEUEING KERNEL EXECUTION #cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, worksize, None) cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.get_ipl().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdConvolution(img_input, img_output, window): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit(1) if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdCopy: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit(1) # CREATING OPENCL BUFFER TO VglClShape mobj_img_shape = img_input.getVglShape().get_asVglClShape_buffer() # EVALUATING IF window IS IN CORRECT TYPE if (not isinstance(window, vl.VglStrEl)): print( "vglClNdConvolution: Error: window is not a VglClStrEl object. aborting execution." ) exit() # CREATING OPENCL BUFFER TO VglClStrEl mobj_window = window.get_asVglClStrEl_buffer() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) _program = vl.get_ocl_context().get_compiled_kernel( "CL_ND/vglClNdConvolution.cl", "vglClNdConvolution") _kernel = _program.vglClNdConvolution _kernel.set_arg(0, img_input.get_oclPtr()) _kernel.set_arg(1, img_output.get_oclPtr()) _kernel.set_arg(2, mobj_img_shape) _kernel.set_arg(3, mobj_window) # THIS IS A BLOCKING COMMAND. IT EXECUTES THE KERNEL. cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, _kernel, img_input.get_ipl().shape, None) mobj_img_shape = None vl.vglSetContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClNdBinToGray(self, img_input, img_output): if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinToGray: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdBinToGray: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) _program = self.cl_ctx.get_compiled_kernel( "../CL_BIN/vglClNdBinToGray.cl", "vglClNdBinToGray") kernel_run = _program.vglClNdBinToGray mobj_img_shape_input = img_input.getVglShape().get_asVglClShape_buffer( ) mobj_img_shape_output = img_output.getVglShape( ).get_asVglClShape_buffer() kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) kernel_run.set_arg(2, mobj_img_shape_input) kernel_run.set_arg(3, mobj_img_shape_output) _worksize_0 = img_input.getWidthIn() if (img_input.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_input.getWidthStep() if (img_output.depth == vl.IPL_DEPTH_1U()): _worksize_0 = img_output.getWidthStep() #worksize = (np.int32(_worksize_0), img_input.getHeigthIn(), img_input.getNFrames() ) # ENQUEUEING KERNEL EXECUTION #cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, worksize, None) cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.ipl.shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClDownload(img): if (vl.vglIsInContext(img, vl.VGL_CL_CONTEXT())): if (img.clForceAsBuf == vl.IMAGE_CL_OBJECT()): vglClImageDownload(img) elif (img.clForceAsBuf == vl.IMAGE_ND_ARRAY()): vglClNdImageDownload(img) vl.vglAddContext(img, vl.VGL_RAM_CONTEXT()) else: print("vglClDownload: Error: image context is not in VGL_CL_CONTEXT.") exit()
def vglClNdDilate(self, img_input, img_output, window): print("# Running vglClNdDilate") if (not img_input.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdDilate: Error: this function supports only OpenCL data as buffer and img_input isn't." ) exit() if (not img_output.clForceAsBuf == vl.IMAGE_ND_ARRAY()): print( "vglClNdDilate: Error: this function supports only OpenCL data as buffer and img_output isn't." ) exit() vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) if (not isinstance(window, vl.VglStrEl)): print( "vglClNdDilate: Error: window is not a VglStrEl object. aborting execution." ) exit() _program = self.cl_ctx.get_compiled_kernel("../CL_ND/vglClNdDilate.cl", "vglClNdDilate") kernel_run = _program.vglClNdDilate # CREATING OPENCL BUFFER TO VglStrEl and VglShape mobj_window = window.get_asVglClStrEl_buffer() mobj_img_shape = img_input.getVglShape().get_asVglClShape_buffer() # SETTING ARGUMENTS kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) kernel_run.set_arg(2, mobj_img_shape) kernel_run.set_arg(3, mobj_window) # ENQUEUEING KERNEL EXECUTION cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.get_ipl().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
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 set_oclPtr(self, img): if( self.clForceAsBuf == vl.IMAGE_CL_OBJECT() ): if( isinstance(img, cl.Image) ): self.oclPtr = img else: print("vglImage: set_oclPtr Error! This image must have a OpenCL Image object as oclPtr.") print("Image sent:") print(img) exit() elif( self.clForceAsBuf == vl.IMAGE_ND_ARRAY() ): if( isinstance(img, cl.Buffer) ): self.oclPtr = img else: print("vglImage: set_oclPtr Error: This image must have a OpenCL Buffer object as oclPtr.") exit() else: print("vglImage: set_oclPtr Error: Invalid object. oclPtr must be cl.Image or cl.Buffer objects, according to clForceAsBuf.") exit()
""" THIS BENCHMARK TOOL EXPECTS JUST 1 ARGUMENT: ARGV[1]: PRIMARY 2D-IMAGE PATH (COLORED OR GRAYSCALE) IT WILL BE USED IN ALL KERNELS AS INPUT IMAGE THE RESULT IMAGES WILL BE SAVED AS IMG-[PROCESSNAME].JPG """ if __name__ == "__main__": vl.vglClInit() msg = "" # INPUT IMAGE img_input = vl.VglImage(sys.argv[1], None, vl.VGL_IMAGE_2D_IMAGE(), vl.IMAGE_ND_ARRAY()) vl.vglLoadImage(img_input) vl.vglClUpload(img_input) # OUTPUT IMAGE img_output = vl.create_blank_image_as(img_input) img_output.set_oclPtr( vl.get_similar_oclPtr_object(img_input) ) vl.vglAddContext(img_output, vl.VGL_CL_CONTEXT()) # STRUCTURANT ELEMENT window = vl.VglStrEl() window.constructorFromTypeNdim(vl.VGL_STREL_CROSS(), 2) inicio = t.time() vglClNdCopy(img_input, img_output) fim = t.time()
cl.enqueue_nd_range_kernel(self.ocl.commandQueue, kernel_run, img_output.ipl.shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT()) """ HERE FOLLOWS THE KERNEL CALLS """ if __name__ == "__main__": wrp = cl2py_BIN_ND() # INPUT IMAGE img_input = vl.VglImage("bin.pgm", vl.VGL_IMAGE_2D_IMAGE(), None, vl.IMAGE_ND_ARRAY()) vl.vglLoadImage(img_input) vl.vglClUpload(img_input) img_input2 = vl.VglImage("bin2.pgm", vl.VGL_IMAGE_2D_IMAGE(), None, vl.IMAGE_ND_ARRAY()) vl.vglLoadImage(img_input2) vl.vglClUpload(img_input2) # OUTPUT IMAGE img_output = vl.create_blank_image_as(img_input) img_output.set_oclPtr(vl.get_similar_oclPtr_object(img_input)) vl.vglAddContext(img_output, vl.VGL_CL_CONTEXT()) img_out_aux = vl.get_similar_oclPtr_object(img_input) cl.enqueue_copy(wrp.cl_ctx.queue, img_out_aux, img_output.get_oclPtr())