Cupy - 类型错误:不支持的类型<类型'numpy.ndarray'>



我想尝试cupy用于图像曝光融合过程,因为我们在算法中使用了大量numpy。以下代码部分显示了导致错误的函数和行。

image_stack是具有不同曝光的图像的列表。创建image_stack的代码是:

def load_images(path, mode='color'):
"""
FUNCTION: load_images
Call to load images colored or grayscale and stack them. 
INPUTS:
path = location of image
mode = 'grayscale' or 'colored'
OUTPUTS:
read data file
"""
image_stack = []; i = 0
for filename in os.listdir(path):
print("Loading... /" + filename + "...as Image_stack["+str(i)+"]")
if mode == 'color':
image = cv2.imread(os.path.join(path, filename), cv2.IMREAD_COLOR)
else: #mode == 'gray':
image = cv2.imread(os.path.join(path, filename), cv2.IMREAD_GRAYSCALE)
image_stack.append(cv2.resize(image,(864,576), interpolation = cv2.INTER_AREA))
#image_stack.append(image)
i += 1
print("n")
return image_stack

下面的函数假定基于数学方法计算标量权重图。我直接把import numpy as np改成了import cupy as np。然而,我面临着来自这个功能的一个问题。

def scalar_weight_map(image_stack, weights=[1,1,1]):
"""
FUNCTION: scalar_weight_map
Call to forcefully "AND"-combine all quality measures defined 
INPUTS:
image_measures = stack of quality measures computed for image i 
image_measures[contrast, saturation, exposedness]
weights = weight for each quality measure : weights[wc, ws, we]
OUTPUTS:
scalar_weight_map for particular image
"""
H = np.shape(image_stack[0])[0]; 
W = np.shape(image_stack[0])[1]; 
D = len(image_stack);
Wijk = np.zeros((H,W,D), dtype='float64')
wc = weights[0]
ws = weights[1]
we = weights[2]
print("Computing Weight Maps from Measures using: C=%1.1d, S=%1.1d, E=%1.1d" %(wc,ws,we))
epsilon = 0.000005
for i in range(D):
C  = contrast(image_stack[i])
S  = saturation(image_stack[i])
E  = exposedness(image_stack[i])
Wijk[:,:,i] = (np.power(C,wc)*np.power(S,ws)*np.power(E,we)) + epsilon
normalizer = np.sum(Wijk,2)
for i in range(D):
Wijk[:,:,i] = np.divide(Wijk[:,:,i], normalizer)
print(" *Done");print("n")
return Wijk.astype('float64')

以及导致以下函数错误的lambda函数:

def exposedness(image, sigma=0.2):
"""
FUNCTION: exposedness
Call to compute third quality measure - exposure using a gaussian curve
INPUTS:
image = input image (colored)
sigma = gaussian curve parameter
OUTPUTS:
exposedness measure
"""
image = cv2.normalize(image, None, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_64F)
gauss_curve = lambda i : np.exp(-((i-0.5)**2) / (2*sigma*sigma))
R_gauss_curve = gauss_curve(image[:,:,2])
G_gauss_curve = gauss_curve(image[:,:,1])
B_gauss_curve = gauss_curve(image[:,:,0])
E = R_gauss_curve * G_gauss_curve * B_gauss_curve
return E.astype('float64')

我面临的错误:

Traceback (most recent call last):
File "C:UsersarDesktopAbexposure_exposure_fusion-masterMain.py", line 36, in <module>
weight_map      = ef.scalar_weight_map(image_stack, weights = [1,1,1])
File "C:UsersarDesktopAbexposure_exposure_fusion-masterexposureFusion_.py", line 202, in scalar_weight_map
E  = exposedness(image_stack[i])
File "C:UsersarDesktopAbexposure_exposure_fusion-masterexposureFusion_.py", line 163, in exposedness
R_gauss_curve = gauss_curve(image[:,:,2])
File "C:UsersarDesktopAbexposure_exposure_fusion-masterexposureFusion_.py", line 162, in <lambda>
gauss_curve = lambda i : np.exp(-((i-0.5)**2) / (2*sigma*sigma))
File "cupy_core_kernel.pyx", line 1222, in cupy._core._kernel.ufunc.__call__
File "cupy_core_kernel.pyx", line 138, in cupy._core._kernel._preprocess_args
File "cupy_core_kernel.pyx", line 124, in cupy._core._kernel._preprocess_arg
TypeError: Unsupported type <class 'numpy.ndarray'>

如果有任何帮助,我将不胜感激。提前感谢!

这是因为您正在将numpy.ndarray(CPU内存上的数据(传递给CuPy。CuPy只接受cupy.ndarray(GPU内存上的数据(,因此在使用gauss_curve之前需要将image转换为cupy.ndarray

最新更新