Skip to content

Commit

Permalink
Added function to normalise image pixel values.
Browse files Browse the repository at this point in the history
  • Loading branch information
petebunting committed Jul 14, 2024
1 parent 958444d commit 0aa8ab0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/python/source/rsgislib_tools_stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ RSGISLib Stats Tools
Normalisation
----------------
.. autofunction:: rsgislib.tools.stats.standarise_img_data
.. autofunction:: rsgislib.tools.stats.normalise_img_data


Histogram Thresholds
Expand Down
19 changes: 19 additions & 0 deletions python/rsgislib/tools/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,22 @@ def standarise_img_data(img_data: numpy.array) -> numpy.array:
std_img_data = numpy.reshape(std_img_data, (img_bands, img_height, img_width))

return std_img_data


def normalise_img_data(img_data: numpy.array) -> numpy.array:
"""
Normalise the input image (data - min)/range.
:param img_data: a numpy array with the shape [bands, height, width]
:return: a numpy array with the normalised pixel values.
"""
img_bands, img_height, img_width = img_data.shape
img_data_reshp = numpy.reshape(img_data, (img_bands, img_height * img_width))
max_value = numpy.max(img_data_reshp, axis=1, keepdims=True)
min_value = numpy.min(img_data_reshp, axis=1, keepdims=True)
diff_value = max_value - min_value
nm_img = (img_data_reshp - min_value) / diff_value
nm_img = numpy.reshape(nm_img, (img_bands, img_height, img_width))

return nm_img

0 comments on commit 0aa8ab0

Please sign in to comment.