python numpy quickies

Generate a 2 x 4 array of ints between 0 and 4, inclusive:

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])

see http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html

Evenly spaced values for an interval

Specfiy number of segments

numpy.linspace(startstopnum=50endpoint=Trueretstep=Falsenumpy.linspace reference

Return evenly spaced numbers over a specified interval.

May include stop value (endpoint = True)

Specify step interval size

numpy.arange([start], stop[, step], dtype=Nonenumpy.arange reference

Return evenly spaced values within a given interval.

 

 

Create Sample Positions for pixels

def genPixelSamples(width, height, numWidth, numHeight, midPoint = True):
“if midPoint = true, sample is positioned at pixel center, else in the corner”
“output with y in 0 and x in 1 of 3rd dimension”
output = np.zeros([numHeight, numWidth, 2])
#y
output[:,:,0] = np.outer( np.linspace(0, height, numHeight, False), np.ones([numWidth]) );
#x
output[:,:,1] = np.outer( np.ones([numHeight]), np.linspace(0, width, numWidth, False) );

if midPoint:
output[:,:] += 0.5 * output[1,1]

return output

 

Comments are closed.