Мой текущий код Python (ниже) разрезает изображение на n фрагментов и возвращает каждую координату среза в строке/порядок столбцов.Нарезка сетки по спиральному движению, возвращение xMin, xMax, yMin, yMax для каждого фрагмента
Вместо того, чтобы начинать с 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]? – butch
Звучит правильно, да. – fredrik