Hello guys i am trying to implement an algortihm to remove water from underwater images and make image more noticable , but i got an errror ValueError: max() arg is an empty sequence
, at the function homomorpic on this line r = max(np.ravel(result[:,:i]))
, the error is caused because the result array is empty but i filled it above .Here the code below.
JavaScript
x
63
63
1
import numpy as np
2
import cv2
3
4
def homomorpic(img):
5
img = np.float32(img)
6
#img = img/255
7
rows , cols , dim = img.shape
8
(rh,rl,cutoff) = 1.3, 0.8, 32
9
b,g,r = cv2.split(img)
10
11
y_log_b = np.log(b + 0.01)
12
y_log_g = np.log(g + 0.01)
13
y_log_r = np.log(r + 0.01)
14
15
y_fft_b= np.fft.fft2(y_log_b)
16
y_fft_g= np.fft.fft2(y_log_g)
17
y_fft_r= np.fft.fft2(y_log_r)
18
19
y_fft_b_shift = np.fft.fftshift(y_log_b)
20
y_fft_g_shift = np.fft.fftshift(y_log_g)
21
y_fft_r_shift = np.fft.fftshift(y_log_r)
22
23
D0=cols/cutoff
24
H= np.ones((rows,cols))
25
B= np.ones((rows,cols))
26
27
for i in range(rows):
28
for j in range(cols):
29
H[i][j] = ((rh-rl)* (1-np.exp(-((i-rows/2)**2+(j-cols/2)**2)/(2*D0**2))))+rl
30
31
result_filter_b = H* y_fft_b_shift
32
result_filter_g = H* y_fft_g_shift
33
result_filter_r = H* y_fft_r_shift
34
35
result_b_intern = np.real(np.fft.ifft2(np.fft.ifftshift(result_filter_b)))
36
result_g_intern = np.real(np.fft.ifft2(np.fft.ifftshift(result_filter_g)))
37
result_r_intern = np.real(np.fft.ifft2(np.fft.ifftshift(result_filter_r)))
38
39
result_b = np.exp(result_b_intern)
40
result_g = np.exp(result_g_intern)
41
result_r = np.exp(result_r_intern)
42
43
result = np.zeros((rows,cols,dim))
44
result[:,:,0] = result_b
45
result[:,:,1] = result_g
46
result[:,:,2] = result_r
47
48
ma = -1
49
mi = 500
50
51
for i in range(3):
52
r = max(np.ravel(result[:,:i]))
53
x = min(np.ravel(result[:,:i]))
54
55
if r > ma :
56
ma = r
57
if x < mi :
58
mi = x
59
return(result)
60
61
image = cv2.imread("eg.png")
62
image2 = homomorpic(image)
63
Thanks for any help or suggestion.
Advertisement
Answer
In this loop for i in range(3):
the first value of i
would be 0
.
This will later on lead to this r = max(np.ravel(result[:,:0]))
where the result from the slicing would be empty.
You would want to shift yourrange
forward like this:
JavaScript
1
2
1
for i in range(1, 3+1):
2