pyrsgis.ml.raster_to_chips

pyrsgis.ml.raster_to_chips(file, y_size=5, x_size=5)[source]

Image chips from raster file

This function generates images chips from single or multi band GeoTIFF file. The image chips can be used as a direct input to deep learning models (eg. Convolutional Neural Network).

This is built on the pyrsgis.ml.array_to_chips function.

Parameters
filestring

Name or path of the GeoTIFF file from which image chips will be created.

y_sizeinteger

The height of the image chips. Ideally an odd number.

x_sizeinteger

The width of the image chips. Ideally an odd number.

Returns
image_chipsarray

A 3D or 4D array containing stacked image chips. The first index represents each image chip and the size is equal to total number of cells in the input array. The 2nd and 3rd index represent the height and the width of the image chips. If the input file is a multiband raster, then image_clips will be 4D where the 4th index will represent the number of bands.

Examples

>>> from pyrsgis import raster, ml
>>> infile_2d = r'E:/path_to_your_file/your_2d_file.tif'
>>> image_chips = ml.raster_to_chips(infile_2d, y_size=7, x_size=7)
>>> print('Shape of single band generated image chips:', image_chips.shape)
Shape of single bandgenerated image chips: (4198376, 7, 7)

Not that here the shape of the input raster file is 2054 rows by 2044 columns. If the raster file is multiband:

>>> infile_3d = r'E:/path_to_your_file/your_3d_file.tif'
>>> image_chips = ml.raster_to_chips(infile_3d, y_size=7, x_size=7)
>>> print('Shape of multiband generated image chips:', image_chips.shape)
Shape of multiband generated image chips: (4198376, 7, 7, 6)