【问题标题】:Slicing through a grid in a spiral motion, returning xMin, xMax, yMin, yMax for each slice以螺旋运动对网格进行切片,返回每个切片的 xMin、xMax、yMin、yMax
【发布时间】:2014-02-21 05:44:08
【问题描述】:

我当前的 Python 代码(如下)将图像分割成 n 个切片,并按行/列顺序返回每个切片的坐标。

与其从 xMin/yMin 1/1 开始并一次完成一行,我希望从图像的中间开始,然后将自己螺旋出来,最终完成所有切片。如何实现?

import math


slices = 11
imageWidth = 1024
imageHeight = 576
totalPixels = imageWidth * imageHeight
print 'Slices: ' + str(slices)

# Re-calculate slices
slices = int(slices/2)*2
print 'Re-calculated slices: ' + str(slices)
print 'Total pixels in image: ' + str(totalPixels)
print 'Maximum slices allowed: ' + str(totalPixels/4)

factor = math.sqrt( slices )
print 'Factor: ' + str(factor)

if (slices > totalPixels/4):
    print 'You cannot use more than ' + int(totalPixels/4) + ' slices!'
else:

    regionWidth = int(math.ceil(imageWidth / factor))
    regionHeight = int(math.ceil(imageHeight / factor))
    print 'Region size: ' + str(int(regionWidth)) + 'x' + str(int(regionHeight))
    print 'Region width: '  + str(regionWidth)
    print 'Region height: ' + str(regionHeight)

    imageWidthRounded = int( math.ceil(factor) * math.ceil( imageWidth / factor ) )
    restWidth = imageWidthRounded - imageWidth
    imageHeightRounded = int( math.ceil(factor) * math.ceil( imageHeight / factor ) )
    restHeight = imageHeightRounded - imageHeight
    print 'Rest width: ' + str(restWidth)
    print 'Rest height: ' + str(restHeight)

    factorRounded = int(math.ceil(factor))
    print 'Factor rounded: ' + str(factorRounded)


    xMin = 0
    xMax = 0
    yMin = 0
    yMax = 0


    rows = factorRounded
    columns = factorRounded
    print 'Total rows: ' + str(rows)
    print 'Total columns: ' + str(columns)


    for column in range(1, columns+1):
        xMin = 0
        xMax = 0
        if column == columns:
            print 'Col '+ str(column) + ' (last column) '
            yMin = (column*regionHeight + 1) - regionHeight
            yMax += (regionHeight - restHeight)

        else:
            print 'Col '+ str(column)
            yMin = (column*regionHeight + 1) - regionHeight
            yMax += regionHeight


        for row in range(1, rows+1):
            if row == rows:
                xMin = (row*regionWidth + 1) - regionWidth
                xMax += (regionWidth-restWidth)

                print 'Row ' + str(row) + ':  xMin=' +str(xMin) + '\t xMax=' + str(xMax) + '\t yMin=' + str(yMin) + '\t yMax=' + str(yMax) + ' (last row)'
            else:
                xMin = (row*regionWidth + 1) - regionWidth
                xMax += regionWidth

                print 'Row ' + str(row) + ':  xMin=' +str(xMin) + '\t xMax=' + str(xMax) + '\t yMin=' + str(yMin) + '\t yMax=' + str(yMax)

【问题讨论】:

  • 假设您将网格存储为二维列表是否正确?您想要的网格 [[1, 2], [3, 4]] 的输出是否类似于 [1, 2, 4, 3]?
  • 听起来不错,是的。

标签: python math integer spiral


【解决方案1】:

首先尝试列举一些案例:

案例 1:

o

返回中心值。

案例 2:

o o
o o

从左上角按顺时针顺序返回值。

案例 3:

o o o
o o o
o o o

返回中心,然后从左上角按顺时针顺序返回值。

...

一般来说,我们看到如果网格的维度是奇数,我们需要先返回中心值。在此之后,我们可以跟踪当前切片的边界角,并迭代地将这些角扩展到下一个级别以处理所有切片。我们可以通过网格的维度来确定切片的总数(以及迭代次数)。

这是函数,实现为generator

def spiral(m):
    length = len(m[0])
    last = length - 1
    mid = length // 2
    spirals_remaining = mid

    if length % 2 == 1:
        yield m[mid][mid]
        box = [(mid-1, mid-1), (mid-1, mid+1), (mid+1, mid+1), (mid+1, mid-1)]
    else:
        box = [(mid-1, mid-1), (mid-1, mid), (mid, mid), (mid, mid-1)]

    while spirals_remaining > 0:
        # yield spiral values clockwise from top left corner
        top = m[box[0][0]][slice(box[0][1], box[1][1])]
        for x in top:
            yield x
        right = [m[i][box[1][1]] for i in range(box[1][0], box[2][0])]
        for x in right:
            yield x
        bottom = m[box[2][0]][slice(box[2][1], box[3][1], -1)]
        for x in bottom:
            yield x
        left = [m[i][box[3][1]] for i in range(box[3][0], box[0][0], -1)]
        for x in left:
            yield x

        # update bounding box for next spiral
        box[0] = box[0][0] - 1, box[0][1] - 1
        box[1] = box[1][0] - 1, box[1][1] + 1
        box[2] = box[2][0] + 1, box[2][1] + 1
        box[3] = box[3][0] + 1, box[3][1] - 1
        spirals_remaining -= 1

示例输出:

>>> m = [[1, 2, 3, 4], 
...      [5, 6, 7, 8], 
...      [9, 10, 11, 12], 
...      [13, 14, 15, 16]]
>>> list(spiral(m))
[6, 7, 11, 10, 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2018-08-07
    • 2019-11-20
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多