2016-03-30 24 views
0

Ich versuche, ein Multi-Band-Bild in einem Python-Code zu lesen. Meine Anforderung ist eine Nachbarschaftsmatrix zu bilden. Also muss ich die Matrix mit einer Nummer auffüllen, um die Nachbarschaft für jedes Element zu bilden. Bsp. a ist eine Matrix, Polsterung mit 0Padding numpy Array/Matrix mit String

a= |1 2 3 | 
    |4 5 6 | 
    |7 8 9 | 
neighborhood matrix = |0 0 0| 
         |0 1 2| 
         |0 4 5| 

Ich verwende numpy.pad(below) für dieses und es funktioniert perfekt mit Single-Band. Aber für Multi-Band, konvertiert es NoDataValue zu seinem Äquivalent in 0-255 und Pads damit, was ich nicht will.

pixels = np.pad(a, (padding,padding), mode='constant', constant_values=(noDataValue)) 

wo padding = 1, noDataValue = -999.0 aber es Umwandlung automatisch auf 125. Und dies geschieht nur für Multiband. So würde jede Hilfe geschätzt werden.

Oder

Wenn ich kann Pad-Matrix mit einer Schnur, das wäre toll. Ich konnte keine Funktion finden, die das Auffüllen mit String unterstützt.

Update 1:

enter image description here

Antwort

1

a in einen umzuwandeln, die den Wert noDataValue sein kann

z.B.

import numpy as np 
# .... 
a = [[1,2,3],[4,5,6],[7,8,9]] 
a = np.array(a).astype(np.float32) 
padding = 2 
noDataValue = -999.0 
pixels = np.pad(a, (padding,padding), mode='constant', constant_values=(noDataValue)) 

Es funktioniert hier

+0

ich Attribute bin immer: 'list' Objekt hat kein Attribut 'AsType'. Wird es mit irgendeiner Nummer auffüllen? –

+0

Entschuldigung, Sie müssen 2 Sekunden in 2D numpy Array konvertieren. Arbeite jetzt? –

+0

Nun, ich habe das versucht, a = [[1,2,3], [4,5,6], [7,8,9]] Alpha = np.reshape (a, (-1, 3)) Alpha = Alpha.astype (np.int64) Fehler: KeyboardInterrupt –