0

Trying to find the HSL per pixel value from a frame of input video but it's taking too much time about 0.2s but want to reduce the time to at least within 0.1s.

def colorize(im, h, s, l_adjust):
    result = Image.new('RGBA', im.size)
    pixin = im.load()
    pixout = result.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            currentR, currentG, currentB = pixin[x, y][0]/255 , pixin[x, y][1]/255, pixin[x, y][2]/255
            #luminance
            lum = (currentR * 0.2126) + (currentG * 0.7152) + (currentB * 0.0722)
            if l_adjust > 0:
                lum = lum * (1 - l_adjust)
                lum = lum + (1.0 - (1.0 - l_adjust))
            else:
                lum = lum * (l_adjust + 1)
            l = lum
            r, g, b = colorsys.hls_to_rgb(h, l, s)
            r, g, b = int(r * 255.99), int(g * 255.99), int(b * 255.99)
            pixout[x, y] = (r, g, b, 255)
    return result

Is there any faster way to do this without using these 2 loops? Looking for something like LUT(Look up table) or something with NumPy shortcut to avoid those 2 loops. Thanks

MSI
  • 105
  • 9

1 Answers1

1

Use the built-in cv.cvtColor(im, cv2.COLOR_RGB2HLS) (or the CUDA accelerated equivalent) as that will be coded in C++ and filled with optimisations for your processor.

talonmies
  • 70,661
  • 34
  • 192
  • 269
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • In my case, I am providing the h, l, s value externally! In your solution how you will do that? And l_adjust is adjusting the luminance value per pixel. – MSI Jul 13 '21 at 12:25