日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

PyOpenCL图像处理:Box模糊

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyOpenCL图像处理:Box模糊 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

# -*- coding: utf-8 -*-from __future__ import absolute_import, print_function import numpy as np import pyopencl as cl import cv2 from PIL import Imagedef RoundUp(groupSize, globalSize): r = globalSize % groupSize; if r == 0: return globalSizeelse: return globalSize + groupSize - r# 創(chuàng)建Context # 如果有多個(gè)設(shè)備,則會(huì)提示選擇 ctx = cl.create_some_context() # 創(chuàng)建CommandQueue queue = cl.CommandQueue(ctx)mf = cl.mem_flags# 通過(guò)字符串內(nèi)容編譯OpenCL的Program prg = cl.Program(ctx, """ __kernel void box_blur_filter(__read_only image2d_t input,__write_only image2d_t output){const sampler_t sampler = CLK_FILTER_NEAREST |CLK_NORMALIZED_COORDS_FALSE |CLK_ADDRESS_CLAMP_TO_EDGE;const int2 size = get_image_dim(input);int2 coord = (int2)(get_global_id(0),get_global_id(1));float blurSize =256.0f; float imageWidth = size.x;float4 sum;for(int i = 0; i < 40; i++){sum += read_imagef(input,sampler, coord + (int2)(0, convert_int((i-20) * blurSize / imageWidth)));}/*for(int i = 0; i < 40; i++){sum += read_imagef(input,sampler, coord + (int2)(convert_int((i-20) * blurSize / size.y),0));}*/sum = sum / 40;write_imagef(output,coord,sum); } """).build()# 打開(kāi)圖片文件 src1 = Image.open('temp/images/f2.png') print(src1.size) dist = Image.new('RGBA',(640,480),(255,255,255))# OpenCL處理的圖片文件格式RGBA,unit8 imageFormat = cl.ImageFormat(cl.channel_order.RGBA,cl.channel_type.UNSIGNED_INT8)# 將圖片從Host復(fù)制到Device img1 = cl.Image(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR,imageFormat,src1.size,None,src1.tobytes()) output = cl.Image(context=ctx,flags=mf.WRITE_ONLY,format=imageFormat,shape=src1.size)# 根據(jù)圖片大小定義WorkSize localWorkSize = ( 8, 8 ) globalWorkSize = ( RoundUp(localWorkSize[0], src1.size[0]), RoundUp(localWorkSize[1], src1.size[1])) # 執(zhí)行Kernel prg.box_blur_filter(queue,globalWorkSize,localWorkSize,img1,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 ) # 將處理好的圖片從設(shè)備復(fù)制到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()

?

轉(zhuǎn)載于:https://my.oschina.net/wujux/blog/1625579

總結(jié)

以上是生活随笔為你收集整理的PyOpenCL图像处理:Box模糊的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。