使用PyOpenCL进行边缘检测

问题描述 投票:0回答:1

我在编写python脚本以使用PyOpenCl检测边缘时遇到问题。我是OpenCL的新手,在尝试时遇到了一个问题,经过多次调整,我无法解决。下面是python代码:

edge.py:

import numpy as np
import pyopencl as cl
from PIL import Image
from time import time


def getKernel(krnl):
    kernel = open(krnl).read()
    return kernel


def findedges(p,d,image):

    data = np.asarray(image).astype(np.uint8)

    platform = cl.get_platforms()[p]
    device = platform.get_devices()[d]
    cntx = cl.Context([device])
    queue = cl.CommandQueue(cntx)

    mf = cl.mem_flags
    im = cl.Buffer(cntx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=data)
    out = cl.Buffer(cntx,mf.WRITE_ONLY,data.nbytes)

    prgm = cl.Program(cntx,getKernel('edge.c')%(data.shape[1],data.shape[0])).build()

    prgm.detectedge(queue,data.shape,None,im,out)

    result = np.empty_like(data)

    cl.enqueue_copy(queue,result,out)
    result = result.astype(np.uint8)
    print(result)

    img = Image.fromarray(result)
    #img.show()
    img.save('coinsedge.png')


if __name__ == '__main__':

    image = Image.open('coins.png')
    #(1,0) is my platform 1, device 0 = "AMD gpu"  
    #(0,0) for intel processor 
    findedges(1,0,image)

和我的内核文件:edge.c


__kernel void detectedge(__global int *im,__global int *out){
      int j = get_global_id(1);
      int i = get_global_id(0);
      int width = %d;
      int rown = %d;
      int value;


              value = -im[(i)*width + j] -  0* im[(i)*width + j+1] + im[(i)*width + j+2]
                      -2*im[(i+1)*width + j] +  0*im[(i+1)*width + j+1] + 2*im[(i+1)*width + j+2]
                      -im[(i+2)*width + j] -  0*im[(i+2)*width + j+1] + im[(i+2)*width + j+2];

              value = (value < 0   ? 0   : value);
              value = (value > 255 ? 255 : value);
              out[i*width + j] = value;

  }

现在没有运行时警告/错误,但输出是我所期望的。这是输入及其输出:这是我的输入图片:

“输入”

这是我的输出:

“输出”

python image-processing python-imaging-library opencl pyopencl
1个回答
0
投票

我认为问题在于传递给内核的输入和输出缓冲区的大小。图像是每个像素一个uint8,您可以根据data.nbytes调整缓冲区大小。内核正在读写4字节像素。

我只需要更改此行即可使用:

data = np.asarray(image).astype(np.int32)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.