在python中反转伽玛校正

时间:2018-11-01 15:50:33

标签: python opencv gamma

我想使用

<!DOCTYPE html>
<html>

<head>
    <title>Einfache-Rezepte</title>
    <link href="https://fonts.googleapis.com/css?family=Anton|Baloo+Bhaijaan|Gloria+Hallelujah|PT+Sans+Narrow|Righteous|Titillium+Web|Yanone+Kaffeesatz" rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <link rel="stylesheet" href="CSS/normalize.css">
    <link rel="stylesheet" href="CSS/style.css">
    <meta name="description" content="Diese Seite wird im Laufe der Zeit (hoffentlich) Rezepte beinhalten">
</head>

<body>
    <div id="Wrapper">

        <header>
            <img src="logo.png">
            <h1 id="branding">Einfache-Rezepte</h1>
            <ul class="nav">
                <li class="navitem active"><a href="index.html">Startseite</a></li>
                <li class="navitem"><a href="#">Rezepte</a></li>
                <li class="navitem"><a href="contact.html">Kontakt</a></li>
            </ul>
        </header>
        <main>       

            <h2>Einfache Rezeptideen für jeden leicht zu machen!</h2><br>

            <h4>Top Rezepte der Woche</h4>

            <table>
                <tbody>
                    <tr>
                        <th>Nummer</th>
                        <th>Gericht</th>
                        <th>Zeit</th>
                        <th>Bild</th>
                    </tr>
                    <tr>
                        <td>1</td>
                        <td>Lendchen - herzhaft gefüllt</td>
                        <td>25 Minuten</td>
                        <td><img src="lendchenpg.jpg"></td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td>Hähnchenkeulen alla Ossobuco</td>
                        <td>18 Minuten</td>
                        <td><img src="haehnchenkeulen.jpg"></td>
                    </tr>
                    <tr>
                        <td>3</td>
                        <td>Filetspitzenragout und Paprika-Pesto-Reis</td>
                        <td>90 Minuten</td>
                        <td><img src="filetspitzenragout.jpg"></td>
                    </tr>
                </tbody>


            </table>

        </main>
    </div>
</body>

</html>

进行反伽马校正,但是我的数据为[0,4095]格式,因此我修改了代码,将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 / 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)

但是当尝试在随机的png图像上调用该函数时:

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)

我得到了错误:

adjust_gamma(cv2.imread('myimage.png'), gamma=2.0)

2 个答案:

答案 0 :(得分:0)

从较高的角度来看,我认为您的问题仅仅是方法兼容性的问题之一:LUT适用于标准的8位格式。您必须先将图像转换为该格式,然后再将其提供给方法。或者,您可以编写自己的12位LUT方法,用4095/4096替换原始代码中的所有上限。

答案 1 :(得分:0)

对于带宽不是Gamma Correction或不是用cv2类型制定的数据,您可以在8bit的情况下不使用integer

import numpy as np

def gamma_correction(img: np.ndarray, gamma: float=1.0):
  igamma = 1.0 / gamma
  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
  return img_c

用法示例

import imageio
import matplotlib.pyplot as plt
import numpy as np

test = imageio.imread('test.png')
test_g = gamma_correction(test, gamma=2.2).astype(np.uint8)

plt.subplot(1, 2, 1)
plt.imshow(test)
plt.subplot(1, 2, 2)
plt.imshow(test_g)
plt.show()

示例结果

enter image description here