Image preprocessing for segmentation: Part 2

Amartya Hatua
2 min readJul 9, 2022

--

The second part of the satellite image preprocessing is to divide the entire image into small patches and store them in .tiff format. There are common four steps that have to be followed in order to preprocess the satellite image and the ground truth. The steps are as follows: i) convert the image into a NumPy array, ii) crop the image, iii) pad the image based on requirements, and iv) divide them into small patches.

Apart from these four steps, there is an additional step that needs to be followed for the ground truth preprocessing i.e. to convert each pixel information into a one-hot vector based on the RGB value of the pixel [1]. A pre-determined map is used for this conversion and then stored in tiff format.

TIFF image to NumPy array:

To convert the image into a NumPy array, we need to read the image first using gdal. I did not face any issue in reading the ground truth image using gdal once its shape is changed using the method mentioned in Part 1 of this series. The image has four-band, each band is separately accessed and stacked using NumPy.

Crop image:

In this step, the image is divided into small patches. Before dividing, I padded the image with a stride size of 32. Padding increases the height and width of the image and helps in dividing both images.

One-hot vector:

For the ground truth image, each pixel value of small patches is converted to a one-hot vector. In this case, I have used an image that has nine different color classes.

Store image:

Finally storing small images in two different directories and we are ready for the training and testing. The entire code is available in [2]

Please visit Part 1 of this series.

References:

[1] https://github.com/Kalit31/Land-Cover-Analysis/blob/master/Sentinel-2-MSI-1C:4-bands/RGBOneHotConv.py

[2]https://gist.github.com/amartyahatua/6aa28eeeca192fb59d69d41191d57789

--

--