Is it possible to do partial gamma adjust using OpenCV's LUT?

Viewed 205

According to OpenCV Gamma Correction, we could adjust the whole image's gamma by OpenCV's LUT():

def adjust_gamma(image, gamma=1.0):
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
        for i in np.arange(0, 256)]).astype("uint8")

    return cv2.LUT(image, table)

I was wondering if is possible to do partial gamma correction?

For example, here's an under exposed doll:

Underexposure doll

I just want to adjust the gamma of the doll, and keep other part still the same.

Ad hoc demo

I've read the official doc of OpenCV, it seems that there's no parameter to specific image's partial position.

1 Answers

You could pass a Numpy slice of your image to your function, but you'll get an ugly discontinuity around the edges.

#!/usr/bin/env python3

import cv2
import numpy as np

def adjust_gamma(image, gamma=1.0):
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
        for i in np.arange(0, 256)]).astype("uint8")

    return cv2.LUT(image, table)

image = cv2.imread('doll.jpg')
image[200:1600, 800:2100] = adjust_gamma(image[200:1600, 800:2100], 3)

enter image description here

Related