【问题标题】:Python :IndexError: list index out of range How to solve this error?Python :IndexError: list index out of range 如何解决这个错误?
【发布时间】:2018-04-16 05:32:32
【问题描述】:

我正在为我的工作使用双边过滤器。我收到以下错误:

src = cv2.imread(str(sys.argv[1]), 0) IndexError: list index out of 范围

代码:双边票据

 import numpy as np
 import cv2
 import sys
 import math 


def distance(x, y, i, j):
    return np.sqrt((x-i)**2 + (y-j)**2)


def gaussian(x, sigma):
    return (1.0 / (2 * math.pi * (sigma ** 2))) * math.exp(- (x ** 2) / (2 * sigma ** 2))


def apply_bilateral_filter(source, filtered_image, x, y, diameter, sigma_i, sigma_s):
    hl = diameter/2
    i_filtered = 0
    Wp = 0
    i = 0
    while i < diameter:
        j = 0
        while j < diameter:
            neighbour_x = x - (hl - i)
            neighbour_y = y - (hl - j)
            if neighbour_x >= len(source):
                neighbour_x -= len(source)
            if neighbour_y >= len(source[0]):
                neighbour_y -= len(source[0])
            gi = gaussian(source[neighbour_x][neighbour_y] - source[x][y], sigma_i)
            gs = gaussian(distance(neighbour_x, neighbour_y, x, y), sigma_s)
            w = gi * gs
            i_filtered += source[neighbour_x][neighbour_y] * w
            Wp += w
            j += 1
        i += 1
    i_filtered = i_filtered / Wp
    filtered_image[x][y] = int(round(i_filtered))


def bilateral_filter_own(source, filter_diameter, sigma_i, sigma_s):
    filtered_image = np.zeros(source.shape)

    i = 0
    while i < len(source):
        j = 0
        while j < len(source[0]):
            apply_bilateral_filter(source, filtered_image, i, j, filter_diameter, sigma_i, sigma_s)
            j += 1
        i += 1
    return filtered_image


if __name__ == "__main__":
    src = cv2.imread(str(sys.argv[1]), 0)
    filtered_image_OpenCV = cv2.bilateralFilter(src, 5, 12.0, 16.0)
    cv2.imwrite("original_image_grayscale.png", src)
    cv2.imwrite("filtered_image_OpenCV.png", filtered_image_OpenCV)
    filtered_image_own = bilateral_filter_own(src, 5, 12.0, 16.0)
    cv2.imwrite("filtered_image_own.png", filtered_image_own)

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    sys.argv[1] 正在索引命令行参数列表的第一个元素。如果您在运行程序时没有提供参数,例如python yourfilename.py testingIndexError 将被抛出。请注意,列表的第 0 个元素是脚本的文件名。

    if __name__ == "__main__": 正下方使用print(sys.argv) 进行调试,以查看sys.argv 列表的内容。

    另外,请注意参数是作为字符串传递的,所以str() 演员表是多余的。

    【讨论】:

      【解决方案2】:

      似乎有些不符合您的预期,这个IndexError: list index out of range 表示您的列表src = cv2.imread(str(sys.argv[1]), 0) 中没有2 个元素,只有1 个存在。

      尝试调试您的代码:

      if __name__ == "__main__":
          print('This is the type of my sys.argv', type(sys.argv))
          print('This is str(sys.argv) as full list --> ', str(sys.argv)) # my guess is that it will give only 1 item in the list, maybe you first need to do some splitting or etc, but the end result will be seen from you'r debugging :)
          print('This is str(sys.argv[0]) first itme from list --> ', str(sys.argv[0]))
      
          # add break point here
          src = cv2.imread(str(sys.argv[1]), 0)
          filtered_image_OpenCV = cv2.bilateralFilter(src, 5, 12.0, 16.0)
          cv2.imwrite("original_image_grayscale.png", src)
          cv2.imwrite("filtered_image_OpenCV.png", filtered_image_OpenCV)
          filtered_image_own = bilateral_filter_own(src, 5, 12.0, 16.0)
          cv2.imwrite("filtered_image_own.png", filtered_image_own)
      

      【讨论】:

        猜你喜欢
        • 2022-10-09
        • 1970-01-01
        • 2021-02-28
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 2022-06-14
        • 1970-01-01
        • 2022-11-02
        相关资源
        最近更新 更多