博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PyOpenCL图像处理:两张图片不带权重叠加
阅读量:7079 次
发布时间:2019-06-28

本文共 2348 字,大约阅读时间需要 7 分钟。

  hot3.png

# -*- coding: utf-8 -*-from __future__ import absolute_import, print_functionimport numpy as npimport pyopencl as climport cv2from PIL import Imagedef RoundUp(groupSize, globalSize):      r = globalSize % groupSize;      if r == 0:          return globalSize    else:          return globalSize + groupSize - r# 创建Context# 如果有多个设备,则会提示选择ctx = cl.create_some_context()# 创建CommandQueuequeue = cl.CommandQueue(ctx)mf = cl.mem_flags# 通过字符串内容编译OpenCL的Programprg = cl.Program(ctx, """__kernel void image_add(__read_only image2d_t input,__read_only image2d_t input2,                              __write_only image2d_t output){    //初始化采样器    const sampler_t sampler = CLK_FILTER_NEAREST |                              CLK_NORMALIZED_COORDS_FALSE |                              CLK_ADDRESS_CLAMP;    //获取图片大小    const int2 size = get_image_dim(input);    //设置当前像素    int2 coord = (int2)(get_global_id(0),get_global_id(1));    if(coord.x >= 0 && coord.x < size.x && coord.y >= 0 && coord.y < size.y){        float4 color = read_imagef(input,sampler,coord) + read_imagef(input2,sampler,coord);        color.w = 1.0f;        write_imagef(output,coord,color);    }}""").build()# 打开图片文件src1 = Image.open('temp/images/f1.png')src2 = Image.open('temp/images/f2.png')dist = Image.new('RGBA',(640,480),(255,255,255))# OpenCL处理的图片文件格式RGBA,unit8imageFormat = cl.ImageFormat(cl.channel_order.RGBA,cl.channel_type.UNSIGNED_INT8)# 将图片从Host复制到Deviceimg1 = cl.Image(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR,imageFormat,src1.size,None,src1.tobytes())img2 = cl.Image(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR,imageFormat,src2.size,None,src2.tobytes())output = cl.Image(context=ctx,flags=mf.WRITE_ONLY,format=imageFormat,shape=src1.size)# 根据图片大小定义WorkSizelocalWorkSize = ( 8, 8 )  globalWorkSize = ( RoundUp(localWorkSize[0], src1.size[0]),                      RoundUp(localWorkSize[1], src1.size[1]))# 执行Kernelprg.image_add(queue,globalWorkSize,localWorkSize,img1,img2,output)buffer = np.zeros(src1.size[0] * src1.size[1] * 4, np.uint8)  origin = ( 0, 0, 0 )  region = ( src1.size[0], src1.size[1], 1 )  # 将处理好的图片从设备复制到HOST cl.enqueue_read_image(queue, output,                        origin, region, buffer).wait()# 保存图片dist = Image.frombytes("RGBA",src1.size, buffer.tobytes())dist.save('temp/images/cl-output.png')dist.show()

程序运行结果:

001610_s6st_106657.png001630_HWB1_106657.png

转载于:https://my.oschina.net/wujux/blog/1622788

你可能感兴趣的文章
理解项目编辑器---part1:使用
查看>>
ios开发之--复制到剪切板
查看>>
jQuery的原理
查看>>
SpringBoot-06:SpringBoot增删改查一套完整的考试案例
查看>>
xtrabackup: error: last checkpoint LSN (3409281307) is larger than last copied LSN (3409274368). #2
查看>>
关于NoClassDefFoundError和ClassNotFoundException异常
查看>>
一、Autofac入门
查看>>
HTML 总结
查看>>
C# 随机列表
查看>>
C# 文件的读取与另存为(WPF)
查看>>
poj1129
查看>>
socket 简历网络连接
查看>>
App.Config操作
查看>>
并查集(涂色问题) HDOJ 4056 Draw a Mess
查看>>
20151217:Web之Repeater使用:主页面
查看>>
从一次浅尝则止的代码优化说起(一) 简单的异常分离
查看>>
BZOJ1834:[ZJOI2010]网络扩容——题解
查看>>
76.培训记录信息 Extjs 页面
查看>>
7.Maven之(七)pom.xml配置文件详解
查看>>
如何成为一名黑客
查看>>