Example #1
0
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())
Example #2
0
    def vglClBinConway(self, img_input, img_output):

        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/vglClBinConway.cl", "vglClBinConway")
        kernel_run = _program.vglClBinConway

        mobj_img_shape = img_input.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)

        _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())

        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_oclPtr().shape, None)

        vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
Example #3
0
    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())
Example #4
0
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())
Example #5
0
def vglCheckContext(img, context):
    if (not vglIsContextUnique(context)):
        print("vglCheckContext: Error: context =", context,
              "is not unique or invalid")
        exit()

    if (vglIsInContext(img, context)):
        print("vglCheckContext: image already in context", context)
        return context
    """
		HERE STARTS THE CASE-LIKE SEQUENCE FROM 
		vglContext.vglCheckContext(VglImage img, int context)
	"""
    # IF THE CONTEXT IS IN RAM
    if (context is vl.VGL_RAM_CONTEXT()):
        # AND IS A BLANK IMAGE, IT IS ON RAM. JUST SET IT IN CONTEXT.
        if (vglIsInContext(img, vl.VGL_BLANK_CONTEXT())):
            vglAddContext(img, vl.VGL_RAM_CONTEXT())
        # AND THE CONTEXT IS IN CL-DEVICE, DOWNLOAD IT BACK TO RAM.
        if (vglIsInContext(img, vl.VGL_CL_CONTEXT())):
            vl.vglClDownload(img)
    # IF THE CONTEXT IS IN CL-DEVICE
    elif (context is vl.VGL_CL_CONTEXT()):
        # AND IS A BLANK-IMAGE, IT IS ON RAM. UPLOAD IT TO CL-DEVICE!
        if (vglIsInContext(img, vl.VGL_BLANK_CONTEXT())):
            vl.vglClUpload(img)
        # AND THE CONTEXT IS IN RAM, UPLOAD IT TO CL-DEVICE!
        if (vglIsInContext(img, vl.VGL_RAM_CONTEXT())):
            vl.vglClUpload(img)
    else:
        print("vglCheckContext: Error: Trying to copy to invalid context =",
              context)
        exit()

    return img.inContext
Example #6
0
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())
Example #7
0
    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())
Example #8
0
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())
Example #9
0
    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())
Example #10
0
    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())
Example #11
0
    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())
Example #12
0
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())
Example #13
0
    def vglCl3dNot(self, img_input, img_output):
        print("# Running vglCl3dNot")
        vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT())
        vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT())

        _program = self.cl_ctx.get_compiled_kernel("../CL/vglCl3dNot.cl",
                                                   "vglCl3dNot")
        kernel_run = _program.vglCl3dNot

        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_oclPtr().shape, None)

        vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
Example #14
0
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())
Example #15
0
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())
Example #16
0
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())
Example #17
0
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())
Example #18
0
    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())
Example #19
0
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()
Example #20
0
    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())
Example #21
0
    def vglClBinThreshold(self, img_input, img_output, thresh):

        vl.vglCheckContext(img_input, vl.VGL_CL_CONTEXT())
        vl.vglCheckContext(img_output, vl.VGL_CL_CONTEXT())

        if (not isinstance(thresh, np.float32)):
            print(
                "vglClBinThreshold: Warning: thresh not np.float32! Trying to convert..."
            )
            try:
                thresh = np.float32(thresh)
            except Exception as e:
                print(
                    "vglClBinThreshold: Error!! Impossible to convert thresh as a np.float32 object."
                )
                print(str(e))
                exit()

        _program = self.cl_ctx.get_compiled_kernel(
            "../CL_BIN/vglClBinThreshold.cl", "vglClBinThreshold")
        kernel_run = _program.vglClBinThreshold

        kernel_run.set_arg(0, img_input.get_oclPtr())
        kernel_run.set_arg(1, img_output.get_oclPtr())
        kernel_run.set_arg(2, thresh)

        _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())

        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_oclPtr().shape, None)

        vl.vglSetContext(img_output, vl.VGL_CL_CONTEXT())
Example #22
0
    img_input2 = vl.VglImage("bin.pgm", vl.VGL_IMAGE_2D_IMAGE())
    vl.vglLoadImage(img_input2)
    #if( img_input2.getVglShape().getNChannels() == 3 ):
    #	vl.rgb_to_rgba(img_input2)

    #img_input_3d = vl.VglImage("3d.tif", vl.VGL_IMAGE_3D_IMAGE())
    #vl.vglLoadImage(img_input_3d)
    #vl.vglClUpload(img_input_3d)

    #img_input2_3d = vl.VglImage("3d-2.tif", vl.VGL_IMAGE_3D_IMAGE())
    #vl.vglLoadImage(img_input2_3d)

    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_output_3d = vl.create_blank_image_as(img_input_3d)
    #img_output_3d.set_oclPtr( vl.get_similar_oclPtr_object(img_input_3d) )
    #vl.vglAddContext(img_output_3d, vl.VGL_CL_CONTEXT())

    convolution_window_2d = np.ones((5, 5), np.float32) * (1 / 25)
    convolution_window_3d = np.ones((5, 5, 5), np.float32) * (1 / 125)

    morph_window_2d = np.ones((3, 3), np.uint8) * 255
    morph_window_2d[0, 0] = 0
    morph_window_2d[0, 2] = 0
    morph_window_2d[2, 0] = 0
    morph_window_2d[2, 2] = 0

    morph_window_3d = np.zeros((3, 3, 3), np.uint8)
Example #23
0
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())
Example #24
0
"""
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()
	vl.vglCheckContext(img_output, vl.VGL_RAM_CONTEXT())
	vl.vglSaveImage("img-vglNdCopy.jpg", img_output)
	msg = msg + "Tempo de execução do método vglClNdCopy:\t" +str( round( (fim-inicio), 9 ) ) +"s\n"

	inicio = t.time()
	vglClNdConvolution(img_input, img_output, window)
	fim = t.time()