OpenCV-python的gamma校正问题

2024-06-01 04:07:26 发布

您现在位置:Python中文网/ 问答频道 /正文

# USAGE
# python adjust_gamma.py 

# import the necessary packages
from __future__ import print_function
import numpy as np
import argparse
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)

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="C:\Users\mesco\Desktop\h.jpg")
args = vars(ap.parse_args())

# load the original image
original = cv2.imread(args["image"])

# loop over various values of gamma
for gamma in np.arange(0.0, 3.5, 0.5):
    # ignore when gamma is 1 (there will be no change to the image)
    if gamma == 1:
        continue

    # apply gamma correction and show the images
    gamma = gamma if gamma > 0 else 0.1
    adjusted = adjust_gamma(original, gamma=gamma)
    cv2.putText(adjusted, "g={}".format(gamma), (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
    cv2.imshow("Images", np.hstack([original, adjusted]))
    cv2.waitKey(0)

我犯了一个错误,告诉-->

File "C:\Users\mesco\Desktop\adjust_gamma.py", line 30, in <module>
    adjusted = adjust_gamma(original, gamma=gamma)
  File "C:\Users\mesco\Desktop\adjust_gamma.py", line 12, in adjust_gamma
    return cv2.LUT(image, table)
 cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:406: error: (-215:Assertion failed) m.dims >= 2 in function 'cv::Mat::Mat'

请帮忙,我正在为我的硕士学位论文做伽马校正。有人能帮我解决这个问题吗?我已经在笔记本电脑上安装了OpenCV 4.1.2,我找不到任何解决方案来处理这个错误。谢谢你的帮助


Tags: theinpyimageimportparsenptable