Jetzt funktioniert es nur, wenn das große 2d-Array perfekt in gleich große Sub-Arrays geschnitten werden kann.
Der Code unten Scheiben
a ->array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
in diesem
block_array->
array([[[ 0, 1, 2],
[ 6, 7, 8]],
[[ 3, 4, 5],
[ 9, 10, 11]],
[[12, 13, 14],
[18, 19, 20]],
[[15, 16, 17],
[21, 22, 23]]])
p
ang q
bestimmen die Blockgröße
-Code
a = arange(24)
a = a.reshape((4,6))
m = a.shape[0] #image row size
n = a.shape[1] #image column size
p = 2 #block row size
q = 3 #block column size
block_array = []
previous_row = 0
for row_block in range(blocks_per_row):
previous_row = row_block * p
previous_column = 0
for column_block in range(blocks_per_column):
previous_column = column_block * q
block = a[previous_row:previous_row+p,previous_column:previous_column+q]
block_array.append(block)
block_array = array(block_array)
Könnten Sie es generischer machen, so dass Blockgrößen Variablen sind? (mit der Bedingung, dass Blöcke perfekt in das ursprüngliche Array passen) – TheMeaningfulEngineer
Danke für die Bearbeitung. Könnten Sie bitte die Gründe für den Algorithmus erklären? – TheMeaningfulEngineer
Vor ein paar Monaten gab es [eine andere Frage] (http://stackoverflow.com/a/13990648/190597), die mich auf die Idee der Verwendung von "Umformen" und "Swapaxes" hindeutete. Die 'h // nrows' sind sinnvoll, da dadurch die Zeilen des ersten Blocks zusammengehalten werden. Es macht auch Sinn, dass du 'nrows' und' ncols' brauchst, um Teil der Form zu sein. "-1" sagt Umformen, um die Zahl einzufügen, die notwendig ist, um die Umformung gültig zu machen. Bewaffnet mit der Form der Lösung habe ich einfach Dinge ausprobiert, bis ich die Formel gefunden habe, die funktioniert. Es tut mir leid, dass ich keine aufschlussreichere Erklärung für dich habe. – unutbu