Skip to content

Instantly share code, notes, and snippets.

@TuringNPcomplete
Created October 25, 2021 23:01
Show Gist options
  • Save TuringNPcomplete/a0920105cd6c67acee3e52ec58e63bb0 to your computer and use it in GitHub Desktop.
Save TuringNPcomplete/a0920105cd6c67acee3e52ec58e63bb0 to your computer and use it in GitHub Desktop.
A function that transforms color RGB image to the grayscale image using the three popular methods
def transf_RBG(image_arr, RBG_conv_met):
"""
Takes an image array and a conversion method as inputs
Transforms the color image to grayscale based on the specified method
"""
import cv2
blue, green, red = cv2.split(image_arr)
if RBG_conv_met == "Average":
grayscale_im = (np.round(3**-1*blue + 3**-1*green + 3**-1*red)).astype('uint8')
return grayscale_im
elif RBG_conv_met == "Weighted_average":
grayscale_im = (np.round(0.299*red + 0.587*green + 0.114*blue)).astype('uint8')
return grayscale_im
else:
grayscale_im = (np.round(0.21*red + 0.72*green + 0.07*blue)).astype('uint8')
return grayscale_im
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment