IndexError:索引超出范围

时间:2019-05-07 03:29:23

标签: python numpy

我正在编写一个程序,通过使用图像缝来增加图像文件的大小。但是,当找到最小成本时(findMinHorzCost函数),我收到一个IndexError,说索引超出范围,而且我不完全确定错误在哪里。

def getHorzCost(self, imagePixel, samplePixel):
    horzCost = np.zeros((overlapWidth, patchSize), np.int)
    for i in range(overlapWidth):
        for j in range(patchSize):
            if j == patchSize - 1:
                horzCost[i, j] = self.colourCorrection((i - overlapWidth, j), imagePixel, samplePixel)
            elif i == 0:
                horzCost[i, j] = self.colourCorrection((i - overlapWidth, j), imagePixel, samplePixel) + min(self.colourCorrection((i - overlapWidth, j + 1), imagePixel, samplePixel), self.colourCorrection((i + 1 - overlapWidth, j + 1), imagePixel, samplePixel))
            elif i == overlapWidth - 1:
                horzCost[i, j] = self.colourCorrection((i - overlapWidth, j), imagePixel, samplePixel) + min(self.colourCorrection((i - overlapWidth, j + 1), imagePixel, samplePixel), self.colourCorrection((i - 1 - overlapWidth, j + 1), imagePixel, samplePixel))
            else:
                horzCost[i, j] = self.colourCorrection((i - overlapWidth, j), imagePixel, samplePixel) + min(self.colourCorrection((i - overlapWidth, j + 1), imagePixel, samplePixel), self.colourCorrection((i + 1 - overlapWidth, j + 1), imagePixel, samplePixel), self.colourCorrection((i - 1 - overlapWidth, j + 1), imagePixel, samplePixel))
    return horzCost

def findMinHorzCost(self, horzCost):
    Bound = np.zeros((patchSize), np.int)
    Matrix = np.zeros((overlapWidth, patchSize), np.int)
    for j in range(1, patchSize):
        for i in range(overlapWidth):
            if i == 0:
                Matrix[i, j] = i if horzCost[i, j - 1] < horzCost[i + 1, j - 1] else i + 1
            elif i == overlapWidth - 1:
                Matrix[i, j] = i if horzCost[i, j - 1] < horzCost[i - 1, j - 1] else i - 1
            else:
                currentMin = i if horzCost[i, j - 1] < horzCost[i - 1, j - 1] else i - 1
                Matrix[i, j] = currentMin if horzCost[currentMin, j - 1] < horzCost[i - 1, j - 1] else i + 1
            horzCost[i, j] += horzCost[Matrix[i, j], j - 1]
    minimum = 0
    for i in range(1, overlapWidth):
        minimum = minimum if horzCost[minimum, patchSize - 1] < horzCost[i, patchSize - 1] else i
    Bound[patchSize - 1] = minimum
    for j in range(patchSize - 1, 0, -1):
        Bound[j - 1] = Matrix[Bound[j], j]
    return Bound

控制台中显示的错误是这样的:

  

如果horzCost [i,j-1]

我相信引起错误的索引是Matrix [i],因为当我更改overlayWidth变量时,错误中索引的大小也会发生变化。

1 个答案:

答案 0 :(得分:0)

如OP在其评论中所述,horzCost的形状为(15,10),但Matrix的形状为(10,15),这导致IndexError < / p>

此外,在两次for循环之后,您需要使用overlapWidth代替patchSize

patchSize = 15
overlapWidth = 10
Bound = np.zeros((patchSize), np.int)
Matrix = np.zeros((overlapWidth, patchSize), np.int)
horzCost = np.zeros((patchSize, overlapWidth), np.int)
print(Matrix.shape)
#(10, 15)
print(horzCost.shape)
#(15,10)

因此,Matrix的形状需要根据需要进行更改以匹配horzCost的形状,反之亦然。

此外,根据需要,j应该运行到overlapWidthi应该运行到patchSize,反之亦然

另外 因此,代码将更改为

def findMinHorzCost(self, horzCost):

    Bound = np.zeros((patchSize), np.int)

    # Fixed the dimensions to patchSize x overlapWidth
    Matrix = np.zeros((patchSize, overlapWidth), np.int)

    # Fixed the range stop to overlapWidth
    for j in range(1, overlapWidth):
        # Fixed the range stop to patchSize
        for i in range(patchSize):
            if i == 0:
                Matrix[i, j] = i if horzCost[i, j - 1] < horzCost[i + 1, j - 1] else i + 1
            elif i == overlapWidth - 1:
                Matrix[i, j] = i if horzCost[i, j - 1] < horzCost[i - 1, j - 1] else i - 1
            else:
                currentMin = i if horzCost[i, j - 1] < horzCost[i - 1, j - 1] else i - 1
                Matrix[i, j] = currentMin if horzCost[currentMin, j - 1] < horzCost[i - 1, j - 1] else i + 1
    minimum = 0
    for i in range(1, overlapWidth):
        minimum = minimum if horzCost[minimum, overlapWidth - 1] < horzCost[i, overlapWidth - 1] else i
    Bound[overlapWidth - 1] = minimum
    for j in range(overlapWidth - 1, 0, -1):
        Bound[j - 1] = Matrix[Bound[j], j]