Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以螺旋运动在网格中切片,为每个切片返回xMin、xMax、yMin、yMax_Python_Math_Integer_Spiral - Fatal编程技术网

Python 以螺旋运动在网格中切片,为每个切片返回xMin、xMax、yMin、yMax

Python 以螺旋运动在网格中切片,为每个切片返回xMin、xMax、yMin、yMax,python,math,integer,spiral,Python,Math,Integer,Spiral,我当前的Python代码(如下所示)将一个图像分成n个切片,并按每行/每列的顺序返回每个切片的坐标 不要在XMI/YMIN 1/1开始,一次完成一行,我希望从图像中间开始,把自己旋转出来,最终完成所有切片。如何做到这一点 import math slices = 11 imageWidth = 1024 imageHeight = 576 totalPixels = imageWidth * imageHeight print 'Slices: ' + str(slices) # Re-c

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

不要在XMI/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:

o
o o
o o
o o o
o o o
o o o
返回中心值

案例2:

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

案例3:

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

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

这是一个函数,作为一个:

示例输出:

>>> 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]

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