图片使用哪种数据类型?

时间:2020-06-02 07:12:07

标签: c python-3.x image-processing pyopencl

我正在推导NDVI(归一化植被指数),它是(NIR-R)/(NIR + R)的比率,其中NIR是近红外波段,R是红色波段。该索引的范围是-1到1。因此,我编写了pyopencl代码,这是我所做的并观察到的。

Python代码:

import pyopencl as cl
import cv2
from PIL import Image
import numpy as np
from time import time
import matplotlib.pyplot as plt


#get kernel file
def getKernel():
    kernel = open('kernel.c').read()
    return kernel

#return images as numpy int32 arrays
def convToArray(im_r,im_nir):
    a = np.asarray(im_r).astype(np.int32)
    b = np.asarray(im_nir).astype(np.int32)

    return a,b

#processing part
def getDerivation(platform,device,im_r,im_nir):

    #setting device
    pltfrm = cl.get_platforms()[platform]
    dev = pltfrm.get_devices()[device]
    cntx = cl.Context([dev])
    queue = cl.CommandQueue(cntx)

    #get 2Darrays
    r,nir = convToArray(im_r,im_nir)

    #shape of array
    x = r.shape[1]

    mf = cl.mem_flags

    bs = time()

    #input images buffer
    inR = cl.Buffer(cntx,mf.READ_ONLY | mf.COPY_HOST_PTR,hostbuf=r)
    inIR = cl.Buffer(cntx,mf.READ_ONLY | mf.COPY_HOST_PTR,hostbuf=nir)

    #output image buffers
    ndvi = cl.Buffer(cntx,mf.WRITE_ONLY,r.nbytes)

    be = time()
    print("Buffering time: " + str(be-bs) + " sec")
    ts = time()

    #load kernel
    task = cl.Program(cntx,getKernel()%(x)).build()

    #execute the process
    task.derive(queue,r.shape,None,inR,inIR,ndvi)

    #create empty buffer to store result
    Vout = np.empty_like(r)

    #dump output buffers to empty arrays
    cl.enqueue_copy(queue,Vout,ndvi)

    te = time()

    #convert arrays to gray - image compatible formate
    NDVI = Vout.astype(np.uint8)

    print("Processing time: " + str(te - ts) + " On: " + pltfrm.name + " --> " + dev.name)

    return NDVI

def process(platform,device,im_r,im_nir):
    NDVI,NDBI,NDWI = getDerivation(platform,device,im_g,im_r,im_nir,im_swir)
    print(NDVI)
    cv2.imshow("NDVI",NDVI)
    cv2.waitKey(0)

if __name__ == '__main__':

    R = cv2.imread("BAND3.jpg",0)
    NIR = cv2.imread("BAND4.jpg",0)

    print(R.dtype) #returns uint8

    process(0,0,R,NIR) #(0,0) is my intel gpu

内核代码(C):

__kernel void derive(__global int* inR,__global int* inIR,__global int* ndvi){

    int x = get_global_id(0);
    int y = get_global_id(1);

    int width = %d;
    int index = x + y*width;

    //ndvi ratio (-1 to 1)

    int a = ((inIR[index] - inR[index])/(inIR[index] + inR[index])) * (256);

    a = (a <   (0) ?   (-1*a)  :   (a));
    a = (a >   (255) ?   (255) :   (a));

    ndvi[index] = (a);

}

输入图片R: Red band image

输入图像NIR: Near-Infrared image

两个图像的位深度均为8

但是我只是空白图像。最初,出于调试原因,我在命令行上写了结果, 命令行输出:

(1151, 1151)
Buffering time: 0.015959739685058594 sec
Processing time: 0.22115755081176758 On: Intel(R) OpenCL --> Intel(R) HD Graphics 520
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

现在我认为我可能没有为图像使用适当的数据类型吗?同样,在内核中,行((inIR[index] - inR[index])/(inIR[index] + inR[index]))将给出一个浮点值,我将其乘以256以得到该浮点值的像素值。那有问题吗?有谁知道我要去哪里错了?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好吧...我明白了。我只是将函数a = np.asarray(im_r).astype(np.int32)中行convToArray()的数据类型更改为float32,并在内核文件中,将参数类型更改为float并添加了{{1} }进行计算。但是, 我需要一个解释 ,为什么这行得通,而不是相反...我大概可以想像一下,计算后得到的结果{{1 }}类型从int a = (int)((((float)(inIR[index] - inR[index])/(float)(inIR[index] + inR[index]))+1)*127.5);转换时丢失数据...对吗?

相关问题