【问题标题】:Is there a way to fix value-error problem有没有办法解决价值错误问题
【发布时间】:2021-06-03 13:18:36
【问题描述】:

大家好,我正在尝试实现一种算法来从水下图像中去除水并使图像更加引人注目,但是我得到了一个错误ValueError: max() arg is an empty sequence ,在这一行的函数同态r = max(np.ravel(result[:,:i])),错误是由于结果数组是空的,但我把它填在上面。下面是代码。

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)

感谢您的任何帮助或建议。

【问题讨论】:

    标签: python arrays list sorting


    【解决方案1】:

    在这个循环中for i in range(3):i 的第一个值将是0

    这将在稍后导致此r = max(np.ravel(result[:,:0])),其中切片的结果将为空。

    您可能希望像这样将range 向前移动:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 2022-09-27
      • 2022-06-11
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多