Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的反转伽马校正_Python_Opencv_Gamma - Fatal编程技术网

python中的反转伽马校正

python中的反转伽马校正,python,opencv,gamma,Python,Opencv,Gamma,我想用 import cv2 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

我想用

import cv2

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)
要进行反转伽马校正,但我的数据是[04095]格式的,因此我调整代码,将255替换为4095,将256替换为4096:

import cv2

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 / 4095.0) ** invGamma) * 4095
        for i in np.arange(0, 4096)]).astype("uint8")

    # apply gamma correction using the lookup table
    return cv2.LUT(image, table)
但在尝试对随机png图像调用函数时:

adjust_gamma(cv2.imread('myimage.png'), gamma=2.0)
我得到一个错误:

OpenCV(3.4.3) /io/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 'LUT'

从较高的层次上讲,我认为您的问题只是方法兼容性的问题:
LUT
在标准8位格式上工作。在将图像提供给方法之前,必须将图像转换为该格式。或者,您可以编写自己的12位
LUT
方法,用4095/4096替换原始代码中的所有上限。

您可以对带宽不是
8bit
或不是用
integer
类型表示的数据执行
伽马校正

将numpy导入为np
def伽马_校正(img:np.ndarray,gamma:float=1.0):
igamma=1.0/伽马
imin,imax=img.min(),img.max()
img_c=img.copy()
img_c=((img_c-imin)/(imax-imin))**igamma
img_c=img_c*(imax-imin)+imin
返回img_c
用法示例
导入图像IO
将matplotlib.pyplot作为plt导入
将numpy作为np导入
test=imageio.imread('test.png')
测试g=伽马校正(测试,伽马=2.2)。A类型(np.uint8)
plt.子地块(1,2,1)
plt.imshow(测试)
plt.子地块(1、2、2)
plt.imshow(测试)
plt.show()
示例结果

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。如果没有扩展格式文件,我们无法复制错误或测试修复。