[PyOpenCL] Ghosting with bilinear Interpolation of OpenCV-Images

Andreas Tharang Andreas.Tharang at mailbox.tu-dresden.de
Fri Nov 25 10:48:59 PST 2011


It wasn't a driver bug.
It has something to do with the way OpenCV stores the image in his 
array-structure.
The following code should be the solution for this thread:

import pyopencl as cl
import numpy as np
import cv2 # OpenCV 2.3.1

ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
clImageFormat = cl.ImageFormat(cl.channel_order.R, 
cl.channel_type.UNORM_INT8)

In_host = cv2.cvtColor(cv2.imread("Test.jpg"), cv2.COLOR_BGR2GRAY)
aSize = In_host.shape[1::-1]
In_buf = cl.Image(ctx, cl.mem_flags.READ_ONLY | 
cl.mem_flags.COPY_HOST_PTR, clImageFormat, aSize, None, In_host.tostring())

Out_host = np.empty(aSize, np.uint8)
Out_buf = cl.Image(ctx, cl.mem_flags.WRITE_ONLY, clImageFormat, aSize)

prg = cl.Program(ctx, """
     const sampler_t smp = CLK_NORMALIZED_COORDS_TRUE | 
CLK_FILTER_LINEAR | CLK_ADDRESS_NONE;

     __kernel void ImageCopy(__read_only image2d_t Img, __write_only 
image2d_t Out)
     {
         const int2 dims = get_image_dim(Img);
         const int2 Coords = (int2)(get_global_id(0), get_global_id(1));
         const float2 NormCoords = (convert_float2(Coords) + 
(float2)(1.7f, 0.3f)) / convert_float2(dims); // = (x + 1.7)/h ; (y + 0.3)/w

         float4 Pixel = read_imagef(Img, smp, NormCoords);
         write_imagef(Out, Coords, Pixel);
     }""").build()

prg.ImageCopy(queue, aSize, None, In_buf, Out_buf)
cl.enqueue_read_image(queue, Out_buf, (0, 0, 0), aSize, Out_host).wait()
cv2.imwrite("out.png", np.reshape(Out_host, aSize[1::-1]))


thanks to all



More information about the PyOpenCL mailing list