0

I am trying to read a multi-band image in a python code. My requirement is to form a neighborhood matrix. So I need to pad the matrix with with some number so as to be able to form neighborhood for each element. Ex. a is a matrix, padding with 0

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

I am using numpy.pad(below) for this and it works perfectly with single band. But for multi-band, it converts noDataValue to its equivalent in 0-255 and pads with it, which I do not want.

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

where padding = 1, noDataValue = -999.0 but it automatically converting it to 125. And this is happening only for multiband. So any help would be appreciated.

Or

If I can pad matrix with a string, that would be great. I could not find any function that helps padding with String.

Update 1:

enter image description here

1 Answer 1

1

convert a to a type which can be the value noDataValue

e.g.

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

It works here

Sign up to request clarification or add additional context in comments.

20 Comments

I am getting AttributeError: 'list' object has no attribute 'astype'. Will it pad with any number?
Sorry, you need to convert to 2D numpy array, 2 secs. Working now?
Well I tried this, a=[[1,2,3],[4,5,6],[7,8,9]] Alpha = np.reshape(a, (-1, 3)) Alpha = Alpha.astype(np.int64) Error: KeyboardInterrupt
@AN_SH The above works here. You cannot reshape a list. It needs to be a Numpy array
Its working on simple array, which was already working. let me check on multiband array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.