opencl - Copying an Image using PyOpenCL -
i've been having trouble making copy of image using pyopencl. wanted try copying want other processing, im not able understand basic task of accessing every pixel. please me catch error make sure works.
here program
import pyopencl cl import numpy import image import sys img = image.open(sys.argv[1]) img_arr = numpy.asarray(img).astype(numpy.uint8) dim = img_arr.shape host_arr = img_arr.reshape(-1) ctx = cl.create_some_context() queue = cl.commandqueue(ctx) mf = cl.mem_flags a_buf = cl.buffer(ctx, mf.read_only | mf.copy_host_ptr, hostbuf=host_arr) dest_buf = cl.buffer(ctx, mf.write_only, host_arr.nbytes) kernel_code = """ __kernel void copyimage(__global const uint8 *a, __global uint8 *c) { int rowid = get_global_id(0); int colid = get_global_id(1); int ncols = %d; int npix = %d; //number of pixels, 3 rgb 4 rgba int index = rowid * ncols * npix + colid * npix; c[index + 0] = a[index + 0]; c[index + 1] = a[index + 1]; c[index + 2] = a[index + 2]; } """ % (dim[1], dim[2]) prg = cl.program(ctx, kernel_code).build() prg.copyimage(queue, (dim[0], dim[1]) , none, a_buf, dest_buf) result = numpy.empty_like(host_arr) cl.enqueue_copy(queue, result, dest_buf) result_reshaped = result.reshape(dim) img2 = image.fromarray(result_reshaped, "rgb") img2.save("new_image_gpu.bmp")
the image gave input
however, output program
i'm not able make sense of why black lines appear. please me solve bug.
thank you
ok ! i've found solution. changed uint8 int, , in numpy array removed "astype(numpy.uint8)". dont know why, tried , worked. explanation why helpful. also, mean take more memory ? works, think takes more memory. workaround using uint8 helpful.
there mismatch between datatypes using in python , opencl. in numpy, uint8
8-bit unsigned integer (which presume after). in opencl, uint8
8-element vector of 32-bit unsigned integers. correct datatype 8-bit unsigned integer in opencl uchar
. so, astype(numpy.uint8)
fine, should accompanied arrays of __global const uchar*
in opencl kernel.
if dealing images, recommend looking opencl's dedicated image types, can take advantage of native support handling images available in hardware.
Comments
Post a Comment