Я пытаюсь получить следующие значения плотности пикселей по диагонали этого изображения (линии в граничной рамке показывают, какие пиксели я хочу получить).
![enter image description here](https://i.stack.imgur.com/FyPqu.png)
Как видите, я пытаюсь найти плотность пикселей для значений, которые простираются от центральной точки.
Мой общий подход до сих пор заключался в следующем:
- Создание 2d-матрицы, пропорциональной форме изображения,
- Используя запрошенные граничные точки, создайте границу,
- Определить центральную точку и затем нарисовать линии, соединяющие центр с каждой граничной точкой,
- Выполнить итерацию по матрице, извлечь соответствующее значение pixelValue и вставить в отдельный список для каждой диагонали.
Вот код:
# Iterate across image
for j in range(columnCount):
for i in range(rowCount):
pixelMatrix[i][j] = greyscaleImage[i][j]
# Find the width, height and area of the matrix
width, height, area = len(matrix[0]), len(matrix), len(matrix[0]) * len(matrix)
squareWidth, squareHeight = min([width, height]), min([width, height])
# Find boundary positions
leftTopCorner = [int((width - squareWidth)/2), int((height - squareHeight)/2)]
leftBottomCorner = [leftTopCorner[0], leftTopCorner[1] + squareHeight]
rightTopCorner = [leftTopCorner[0] + squareWidth, leftTopCorner[1]]
rightBottomCorner = [leftTopCorner[0] + squareWidth, leftBottomCorner[1]]
centre = [(leftTopCorner[0] + (squareWidth/2)), (squareHeight/2)]
# NB: Excluding code for graphics
# Iterate across matrix (right-corner - left-corner) and descend across rows
heightCounter = 0
for i in range((boundaryCoordinates[2][0] - boundaryCoordinates[0][0])):
# Left-side => Descending/ascending from top-left and bottom-left corners to converge on centrePoint.
XCoordLeft = boundaryCoordinates[0][0] + i
topLeft.append(matrix[XCoordLeft][heightCounter])
bottomCord = boundaryCoordinates[1][1] - heightCounter
bottomLeft.append(matrix[XCoordLeft][bottomCord])
print("LEFT_TOP_COORS: " + str(XCoordLeft) + ", " + str(heightCounter) + " LEFT_BOTTOM_COORS: " + str(XCoordLeft) + ", " + str(bottomCord))
heightCounter += 1
heightCounter = 0
for i in range((boundaryCoordinates[2][0] - boundaryCoordinates[0][0])/2):
# Right-side => Descending/ascending from centrePoint to top-right and bottom-right.
XCoordRight = centrePoint[0] + i
topHeight = centrePoint[1] + heightCounter
bottomHeight = centrePoint[1] - heightCounter
topRight.append(matrix[topHeight][XCoordRight])
bottomRight.append(matrix[bottomHeight][XCoordRight])
heightCounter += 1
return [topLeft, topRight, bottomLeft, bottomRight]
Однако я продолжаю получать IndexError: list index out of range
:
![enter image description here](https://i.stack.imgur.com/MoNhA.png)
И я просто не могу понять, почему.Любая помощь будет принята с благодарностью :)