def local_entropy(ocl_ctx, img, window_radius, num_bins=8):
""" compute local entropy using a sliding window """
mf = cl.mem_flags
cl_queue = cl.CommandQueue(ocl_ctx)
img_np = np.array(img).astype(np.float32)
img_buf = cl.Buffer(ocl_ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=img_np)
min_val = np.nanmin(img)
max_val = np.nanmax(img)
entropy = np.zeros_like(img,dtype=np.float32)
dest_buf = cl.Buffer(ocl_ctx, mf.WRITE_ONLY, entropy.nbytes)
cl_dir = os.path.dirname(__file__)
cl_filename = cl_dir + '/cl/local_entropy.cl'
with open(cl_filename, 'r') as fd:
clstr = fd.read()
prg = cl.Program(ocl_ctx, clstr).build()
prg.local_entropy(cl_queue, entropy.shape, None,
img_buf, dest_buf,
np.int32(img.shape[1]), np.int32(img.shape[0]),
np.int32(window_radius), np.int32(num_bins),
np.float32(min_val), np.float32(max_val))
cl.enqueue_copy(cl_queue, entropy, dest_buf)
cl_queue.finish()
return entropy
评论列表
文章目录