1

I wrote some code to perform gamma adjustment on an image. To do that, I'm creating a LUT to perform gamma correction in an image. I am receiving the following error when calling cv2.LUT:

error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-52oirelq\opencv\modules\core\src\lut.cpp:368: error: (-215:Assertion failed) (lutcn == cn || lutcn == 1) && _lut.total() == 256 && _lut.isContinuous() && (depth == CV_8U || depth == CV_8S) in function 'cv::LUT'

The code I wrote is shown below which is a function for performing gamma correction:

import cv2
import numpy as np

def adjust_gamma(image, gamma=1.0):
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
        for i in np.arange(0, 256)]).astype("uint8")
    # apply gamma correction using the lookup table
    return cv2.LUT(image, table)

What am I doing wrong?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Oliver
  • 19
  • 5
  • which line of your code is throwing the error? Include the full traceback if possible. – DrBwts Oct 22 '20 at 14:59
  • Did you look at the [apply-opencv-look-up-table-lut-to-an-image](https://stackoverflow.com/questions/42169247/apply-opencv-look-up-table-lut-to-an-image)? – Ahmet Oct 22 '20 at 16:47
  • i got the erorr at the return cv2.LUT(image,table) at the gamma function – Oliver Oct 22 '20 at 16:54

1 Answers1

1

Your input image is most likely not unsigned 8-bit integer. The error message is very telling as it expects the input image into cv2.LUT to be this type. I suspect your image is floating point precision instead. In that case, the easiest solution is to scale the values by 255 and cast to uint8, run the method then convert to floating point after:

output = adjust_gamma((255 * image).astype(np.uint8), table)
output = (output.astype(np.float)) / 255
rayryeng
  • 102,964
  • 22
  • 184
  • 193