Python坐标超出多边形的范围

2024-10-02 10:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,他的工作是梳理一个充满像素值的CSV文件。我使用了一个json文件来查找我预先说明的多边形的尺寸,并且我正在尝试计算该对象中的平均像素值。像素温度列表是与图像尺寸相同的[512][640]。我正在使用skimage的多边形函数来计算多边形点内的所有像素

# Find all points within polygon
                        r = np.array([y1, y2, y3, y4])
                        c = np.array([x1, x2, x3, x4])
                        x_axis, y_axis = draw.polygon(r, c)

注意:我的JSON中的x和y混淆了,这就是为什么我首先声明y

这会产生两个长度相等的nparray(x\u轴,y\u轴)(通过随机点进行测试,它们也是坐标,所以需要成对出现)我尝试同时遍历这两个列表,然后计算我的值。这适用于我的大多数多边形,但我随机击中一个

Traceback (most recent call last):
  File "u-valueV2.py", line 232, in <module>
    main()
  File "u-valueV2.py", line 228, in main
    parseJSON(jsonFilePath)
  File "u-valueV2.py", line 113, in parseJSON
    average_u_value = parseCSVPolygon(csvFilePath, csvFileName[0], x_axis, y_axis)
  File "u-valueV2.py", line 197, in parseCSVPolygon
    total += u_value_calculation(emissivity, pixel_temperature[x[item]][y[item]])
IndexError: list index out of range

由于列表长度相等,并且我的像素临时列表包含所有512×640的值,我不明白为什么会出现这个错误。你知道吗

错误是在parseCSVPolygon函数中引用此块

fileName += ".csv"
path = csvFilePath + '/' + fileName
with open(path) as read_csv:
    csvData = csv.reader(read_csv, delimiter=',', quotechar='|')  # Seperated by comma (hence csv)
    for i, data in enumerate(csvData):  # i holds the index and increments each loop while data holds cells value
        length = len(data)
        if (length == 0):  # Incase the length of a row is 0 (empty cells)
            continue
        else:
            index = length - 1  # Extract the last data point in the row
        if (i == 2):
            emissivity = float(data[index])
            #print(emissivity)
        if (i >= 10):
            pixel_temperature.append(data[1:])  # Pixel temperature now holds 512 indexes. Each index

#TODO: Figure out how to calculate the doors u-value (or Polygons)
for item in range(len(y)):
    total += u_value_calculation(emissivity, pixel_temperature[x[item]][y[item]])
average = total / (len(x) * len(y))
return average

我对这个错误的根源感到困惑。有人能帮我吗?你知道吗


Tags: csvtheinpy列表dataindexvalue

热门问题