Skip to content
Advertisement

Is there a way to fix value-error problem

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.

import numpy as np
import cv2

def homomorpic(img):
    img = np.float32(img)
    #img = img/255
    rows , cols , dim = img.shape
    (rh,rl,cutoff) = 1.3, 0.8, 32
    b,g,r = cv2.split(img)
    
    y_log_b = np.log(b + 0.01)
    y_log_g = np.log(g + 0.01)
    y_log_r = np.log(r + 0.01)
    
    y_fft_b= np.fft.fft2(y_log_b)
    y_fft_g= np.fft.fft2(y_log_g)
    y_fft_r= np.fft.fft2(y_log_r)
    
    y_fft_b_shift = np.fft.fftshift(y_log_b)
    y_fft_g_shift = np.fft.fftshift(y_log_g)
    y_fft_r_shift = np.fft.fftshift(y_log_r)
    
    D0=cols/cutoff
    H= np.ones((rows,cols))
    B= np.ones((rows,cols))
    
    for i in range(rows):
        for j in range(cols):
            H[i][j] = ((rh-rl)* (1-np.exp(-((i-rows/2)**2+(j-cols/2)**2)/(2*D0**2))))+rl
            
    result_filter_b = H* y_fft_b_shift
    result_filter_g = H* y_fft_g_shift
    result_filter_r = H* y_fft_r_shift
    
    result_b_intern = np.real(np.fft.ifft2(np.fft.ifftshift(result_filter_b)))
    result_g_intern = np.real(np.fft.ifft2(np.fft.ifftshift(result_filter_g)))
    result_r_intern = np.real(np.fft.ifft2(np.fft.ifftshift(result_filter_r)))
    
    result_b = np.exp(result_b_intern)
    result_g = np.exp(result_g_intern)
    result_r = np.exp(result_r_intern)
    
    result = np.zeros((rows,cols,dim))
    result[:,:,0] = result_b
    result[:,:,1] = result_g
    result[:,:,2] = result_r
    
    ma = -1
    mi = 500
    
    for i in range(3):
        r = max(np.ravel(result[:,:i]))
        x = min(np.ravel(result[:,:i]))
        
        if r > ma :
            ma = r
        if x < mi :
            mi = x
    return(result)

image = cv2.imread("eg.png")
image2 = homomorpic(image)

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:

for i in range(1, 3+1):
Advertisement