def vglCl3dDilate(img_input, img_output, convolution_window, window_size_x, window_size_y, window_size_z): print("# Running vglCl3dDilate") vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) # TRANSFORMAR EM BUFFER try: cl_convolution_window = cl.Buffer(vl.get_ocl().context, cl.mem_flags.READ_ONLY, convolution_window.nbytes) cl.enqueue_copy(vl.get_ocl().commandQueue, cl_convolution_window, convolution_window.tobytes(), is_blocking=True) convolution_window = cl_convolution_window except Exception as e: print("vglCl3dDilate: Error!! Impossible to convert convolution_window to cl.Buffer object.") print(str(e)) exit() if( not isinstance(window_size_x, np.uint32) ): print("vglCl3dDilate: Warning: window_size_x not np.uint32! Trying to convert...") try: window_size_x = np.uint32(window_size_x) except Exception as e: print("vglCl3dDilate: Error!! Impossible to convert window_size_x as a np.uint32 object.") print(str(e)) exit() if( not isinstance(window_size_y, np.uint32) ): print("vglCl3dDilate: Warning: window_size_y not np.uint32! Trying to convert...") try: window_size_y = np.uint32(window_size_y) except Exception as e: print("vglCl3dDilate: Error!! Impossible to convert window_size_y as a np.uint32 object.") print(str(e)) exit() if( not isinstance(window_size_z, np.uint32) ): print("vglCl3dDilate: Warning: window_size_z not np.uint32! Trying to convert...") try: window_size_z = np.uint32(window_size_z) except Exception as e: print("vglCl3dDilate: Error!! Impossible to convert window_size_z as a np.uint32 object.") print(str(e)) exit() _program = vl.get_ocl_context().get_compiled_kernel("../CL/vglCl3dDilate.cl", "vglCl3dDilate") kernel_run = _program.vglCl3dDilate kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) kernel_run.set_arg(2, convolution_window) kernel_run.set_arg(3, window_size_x) kernel_run.set_arg(4, window_size_y) kernel_run.set_arg(5, window_size_z) cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, kernel_run, img_output.get_oclPtr().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
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 vglCl3dThreshold(img_input, img_output, thresh, top=1.0): print("# Running vglCl3dThreshold") vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) if( not isinstance(thresh, np.float32) ): print("vglCl3dThreshold: Warning: thresh not np.float32! Trying to convert...") try: thresh = np.float32(thresh) except Exception as e: print("vglCl3dThreshold: Error!! Impossible to convert thresh as a np.float32 object.") print(str(e)) exit() if( not isinstance(top, np.float32) ): print("vglCl3dThreshold: Warning: top not np.float32! Trying to convert...") try: top = np.float32(top) except Exception as e: print("vglCl3dThreshold: Error!! Impossible to convert top as a np.float32 object.") print(str(e)) exit() _program = vl.get_ocl_context().get_compiled_kernel("../CL/vglCl3dThreshold.cl", "vglCl3dThreshold") kernel_run = _program.vglCl3dThreshold 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(vl.get_ocl().commandQueue, kernel_run, img_output.get_oclPtr().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 __init__(self, cl_ctx=None): # PYTHON-EXCLUSIVE VARIABLES self.cl_ctx: Union[None, vl.opencl_context] = cl_ctx # COMMON VARIABLES. self.ocl IS EQUIVALENT TO cl. self.ocl: Union[None, vl.VglClContext] = None if (self.cl_ctx is None): vl.vglClInit() self.ocl = vl.get_ocl() self.cl_ctx = vl.get_ocl_context() else: self.ocl = cl_ctx.get_vglClContext_attributes()
def vglClBlurSq3(img_input, img_output): print("# Running vglClBlurSq3") 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/vglClBlurSq3.cl", "vglClBlurSq3") kernel_run = _program.vglClBlurSq3 kernel_run.set_arg(0, img_input.get_oclPtr()) kernel_run.set_arg(1, img_output.get_oclPtr()) cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, kernel_run, img_output.get_oclPtr().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def __init__(self, cl_ctx=None): # PYTHON-EXCLUSIVE VARIABLES self.cl_ctx: Union[None, vl.opencl_context] = cl_ctx # COMMON VARIABLES. self.ocl IS EQUIVALENT TO cl. self.ocl: Union[None, vl.VglClContext] = None # SE O CONTEXTO OPENCL NÃO FOR DEFINIDO # ELE INSTANCIADO E DEFINIDO if (self.cl_ctx is None): vl.vglClInit() self.ocl = vl.get_ocl() self.cl_ctx = vl.get_ocl_context() else: self.ocl = cl_ctx.get_vglClContext_attributes()
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 vglClSwapRgb(src, dst): vl.vglCheckContext(src, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(dst, vl.VGL_CL_CONTEXT()) _program = vl.get_ocl_context().get_compiled_kernel( "CL/vglClSwapRgb.cl", "vglClSwapRgb") _kernel = _program.vglClSwapRgb _kernel.set_arg(0, src.get_oclPtr()) _kernel.set_arg(1, dst.get_oclPtr()) # THIS IS A BLOCKING COMMAND. IT EXECUTES THE KERNEL. cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, _kernel, src.get_oclPtr().shape, None) vl.vglSetContext(dst, vl.VGL_CL_CONTEXT())
def vglClInvert(img_input, img_output): 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/vglClInvert.cl", "vglClInvert") _kernel = _program.vglClInvert _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_oclPtr().shape, None) vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
def vglClThreshold(src, dst, thresh, top=1.0): vl.vglCheckContext(src, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(dst, vl.VGL_CL_CONTEXT()) # EVALUATING IF thresh IS IN CORRECT TYPE if (not isinstance(thresh, np.float32)): print( "vglClConvolution: Warning: thresh not np.float32! Trying to convert..." ) try: thresh = np.float32(thresh) except Exception as e: print( "vglClConvolution: Error!! Impossible to convert thresh as a np.float32 object." ) print(str(e)) exit() # EVALUATING IF top IS IN CORRECT TYPE if (not isinstance(top, np.float32)): print( "vglClConvolution: Warning: top not np.float32! Trying to convert..." ) try: top = np.float32(top) except Exception as e: print( "vglClConvolution: Error!! Impossible to convert top as a np.float32 object." ) print(str(e)) exit() _program = vl.get_ocl_context().get_compiled_kernel( "CL/vglClThreshold.cl", "vglClThreshold") _kernel = _program.vglClThreshold _kernel.set_arg(0, src.get_oclPtr()) _kernel.set_arg(1, dst.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, src.get_oclPtr().shape, None) vl.vglSetContext(dst, vl.VGL_CL_CONTEXT())
def vglClErode(img_input, img_output, convolution_window, window_size_x, window_size_y): vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT()) vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT()) # EVALUATING IF convolution_window IS IN CORRECT TYPE try: mobj_convolution_window = cl.Buffer(vl.get_ocl().context, cl.mem_flags.READ_ONLY, convolution_window.nbytes) cl.enqueue_copy(vl.get_ocl().commandQueue, mobj_convolution_window, convolution_window.tobytes(), is_blocking=True) convolution_window = mobj_convolution_window except Exception as e: print( "vglClConvolution: Error!! Impossible to convert convolution_window to cl.Buffer object." ) print(str(e)) exit() # EVALUATING IF window_size_x IS IN CORRECT TYPE if (not isinstance(window_size_x, np.uint32)): print( "vglClConvolution: Warning: window_size_x not np.uint32! Trying to convert..." ) try: window_size_x = np.uint32(window_size_x) except Exception as e: print( "vglClConvolution: Error!! Impossible to convert window_size_x as a np.uint32 object." ) print(str(e)) exit() # EVALUATING IF window_size_y IS IN CORRECT TYPE if (not isinstance(window_size_y, np.uint32)): print( "vglClConvolution: Warning: window_size_y not np.uint32! Trying to convert..." ) try: window_size_y = np.uint32(window_size_y) except Exception as e: print( "vglClConvolution: Error!! Impossible to convert window_size_y as a np.uint32 object." ) print(str(e)) exit() _program = vl.get_ocl_context().get_compiled_kernel( "CL/vglClErode.cl", "vglClErode") _kernel = _program.vglClErode _kernel.set_arg(0, img_input.get_oclPtr()) _kernel.set_arg(1, img_output.get_oclPtr()) _kernel.set_arg(2, mobj_convolution_window) _kernel.set_arg(3, window_size_x) _kernel.set_arg(4, window_size_y) # THIS IS A BLOCKING COMMAND. IT EXECUTES THE KERNEL. cl.enqueue_nd_range_kernel(vl.get_ocl().commandQueue, _kernel, img_input.get_oclPtr().shape, None) mobj_convolution_window = None vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())