Skip to content
Snippets Groups Projects
Commit 48a62940 authored by ogueler@idiap.ch's avatar ogueler@idiap.ch
Browse files

Added 24bit RGB to 8bit greyscale transformation

parent ee7b9636
No related branches found
No related tags found
2 merge requests!5Tbx11k,!4Moved code to lightning
......@@ -41,6 +41,29 @@ class SingleAutoLevel16to8:
).astype("uint8"),
).convert("L")
class RGBtoGreyscale8bit:
"""Converts a 24-bit RGB image to an 8-bit greyscale representation.
This transform assumes that the input image is RGB with 24 bits.
Converts the RGB image to a greyscale image using the well-known formula:
Y = 0.299 * R + 0.587 * G + 0.114 * B
This formula is based on the relative luminance of the RGB channels in the sRGB color space
consider such a range should be mapped to the [0,255] range of the
destination image.
"""
def __call__(self, img):
# Use the formula to convert the RGB image to a greyscale image
img_array = numpy.array(img).astype(float)
grey_array = 0.299 * img_array[:, :, 0] + 0.587 * img_array[:, :, 1] + 0.114 * img_array[:, :, 2]
# Normalize the greyscale image to the range [0, 255] and convert it to uint8
grey_array = numpy.round(255.0 * (grey_array - grey_array.min()) / (grey_array.max() - grey_array.min())).astype('uint8')
# Create a new greyscale PIL image from the normalized array
return PIL.Image.fromarray(grey_array).convert("L")
class RemoveBlackBorders:
"""Remove black borders of CXR."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment