I want to try cupy
for image exposure fusion process because we use a lot of numpy
in algorithms. The following code parts shows the functions and lines that causes errors.
image_stack
is a list of images that have different exposures. The code for creating image_stack
is:
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
The function below supposed to calculate scalar weight map based on mathematical approach. I directly changed import numpy as np
to import cupy as np
. However, I faced an issue from this function.
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')
And the lambda
function that causes error from the below function:
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')
The error I faced:
Traceback (most recent call last):
File "C:\Users\ar\Desktop\Ab\exposure_\exposure_fusion-master\Main.py", line 36, in <module>
weight_map = ef.scalar_weight_map(image_stack, weights = [1,1,1])
File "C:\Users\ar\Desktop\Ab\exposure_\exposure_fusion-master\exposureFusion_.py", line 202, in scalar_weight_map
E = exposedness(image_stack[i])
File "C:\Users\ar\Desktop\Ab\exposure_\exposure_fusion-master\exposureFusion_.py", line 163, in exposedness
R_gauss_curve = gauss_curve(image[:,:,2])
File "C:\Users\ar\Desktop\Ab\exposure_\exposure_fusion-master\exposureFusion_.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'>
I would be grateful for any help. Thanks in advance!
CodePudding user response:
This is because you are passing numpy.ndarray
(data on CPU memory) to CuPy. CuPy only accepts cupy.ndarray
(data on GPU memory), so you need to convert image
to cupy.ndarray
before using gauss_curve
.