pythonopencvrangenormalizationhsv

(How) can I normalize the HSV's v value to be between 0 and 1 with the help of opencv and python?


As I want to categorize and compare photos in terms of their brightness, I want to normalize the V values (HSV) to be between 0 and 1 - with 1 being the brightest. I use opencv with python. Thank you in advance!


Solution

  • import cv2
    import numpy as np
    
    # Read an image
    frame = cv2.imread("Path/to/img")
    # Convert it to hsv
    hsv = cv2.cvtColor(frame, cv.COLOR_BGR2HSV)
    # Get the V channel
    v=hsv[:,:,2]
    # Convert it to float
    v=v.astype(np.float32)
    # Normalize to brightest value
    v/=np.amax(v)