Cara menggunakan adaptive gamma correction python

Check out sessions from the WiML Symposium covering diffusion models with KerasCV, on-device ML, and more. Watch on demand

  • TensorFlow
  • API
  • TensorFlow v2.11.0
  • Python

tf.image.adjust_gammaStay organized with collections Save and categorize content based on your preferences.

Performs Gamma Correction.

View aliases

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.image.adjust_gamma

tf.image.adjust_gamma(
    image, gamma=1, gain=1
)

on the input image.

Also known as Power Law Transform. This function converts the input images at first to float representation, then transforms them pixelwise according to the equation Out = gain * In**gamma, and then converts the back to the original data type.

Intensity transformations are applied on images for contrast manipulation or image thresholding. These are in the spatial domain, i.e. they are performed directly on the pixels of the image at hand, as opposed to being performed on the Fourier transform of the image.

The following are commonly used intensity transformations:

  1. Image Negatives (Linear)
  2. Log Transformations
  3. Power-Law (Gamma) Transformations
  4. Piecewise-Linear Transformation Functions

Cara menggunakan adaptive gamma correction python

Spatial Domain Processes –
Spatial domain processes can be described using the equation:

Cara menggunakan adaptive gamma correction python
where
Cara menggunakan adaptive gamma correction python
is the input image, T is an operator on f defined over a neighbourhood of the point (x, y), and
Cara menggunakan adaptive gamma correction python
is the output.

Cara menggunakan adaptive gamma correction python

Image Negatives –

Image negatives are discussed in this article. Mathematically, assume that an image goes from intensity levels 0 to (L-1). Generally, L = 256. Then, the negative transformation can be described by the expression s = L-1-r where r is the initial intensity level and s is the final intensity level of a pixel. This produces a photographic negative.

Log Transformations –

Mathematically, log transformations can be expressed as s = clog(1+r). Here, s is the output intensity, r>=0 is the input intensity of the pixel, and c is a scaling constant. c is given by 255/(log (1 + m)), where m is the maximum pixel value in the image. It is done to ensure that the final pixel value does not exceed (L-1), or 255. Practically, log transformation maps a narrow range of low-intensity input values to a wide range of output values.

Consider the following input image.

Cara menggunakan adaptive gamma correction python

Below is the code to apply log transformation to the image.




import cv2

import numpy as np

  

# Open the image.

imgs = clog(1+r)0 s = clog(1+r)1s = clog(1+r)2s = clog(1+r)3

  

s = clog(1+r)5

s = clog(1+r)6s = clog(1+r)0 s = clog(1+r)8s = clog(1+r)9255/(log (1 + m))0255/(log (1 + m))1 255/(log (1 + m))2 255/(log (1 + m))3255/(log (1 + m))4255/(log (1 + m))5

255/(log (1 + m))6s = clog(1+r)0 s = clog(1+r)6255/(log (1 + m))9 import0255/(log (1 + m))1 255/(log (1 + m))2 import3

  

import5

255/(log (1 + m))6s = clog(1+r)0 import8s = clog(1+r)0 cv20

  

cv22

cv23cv24cv25

Below is the log-transformed output.

Cara menggunakan adaptive gamma correction python

Power-Law (Gamma) Transformation –

Power-law (gamma) transformations can be mathematically expressed as

Cara menggunakan adaptive gamma correction python
. Gamma correction is important for displaying images on a screen correctly, to prevent bleaching or darkening of images when viewed from different types of monitors with different display settings. This is done because our eyes perceive images in a gamma-shaped curve, whereas cameras capture images in a linear fashion. Below is the Python code to apply gamma correction.




import cv2

import numpy as np

  

# Open the image.

imgs = clog(1+r)0 s = clog(1+r)1s = clog(1+r)2s = clog(1+r)3

  

import8

import9 numpy as np0numpy as np1 numpy as np2numpy as np3numpy as np4numpy as np5numpy as np4numpy as np7numpy as np4numpy as np9 0

 

 2 3

 2 5s = clog(1+r)0  7s = clog(1+r)8255/(log (1 + m))9# Open the image.0s = clog(1+r)9 s = clog(1+r)8s = clog(1+r)3255/(log (1 + m))9255/(log (1 + m))9 # Open the image.6s = clog(1+r)0 # Open the image.8s = clog(1+r)3

  

 2img2

 2cv23img5255/(log (1 + m))2img7img8255/(log (1 + m))2s = clog(1+r)00s = clog(1+r)01

Below are the gamma-corrected outputs for different values of gamma.

Gamma = 0.1:

Cara menggunakan adaptive gamma correction python

Gamma = 0.5:

Cara menggunakan adaptive gamma correction python

Gamma = 1.2:

Cara menggunakan adaptive gamma correction python

Gamma = 2.2:

Cara menggunakan adaptive gamma correction python

As can be observed from the outputs as well as the graph, gamma>1 (indicated by the curve corresponding to ‘nth power’ label on the graph), the intensity of pixels decreases i.e. the image becomes darker. On the other hand, gamma<1 (indicated by the curve corresponding to 'nth root' label on the graph), the intensity increases i.e. the image becomes lighter.

Piecewise-Linear Transformation Functions –

These functions, as the name suggests, are not entirely linear in nature. However, they are linear between certain x-intervals. One of the most commonly used piecewise-linear transformation functions is contrast stretching.

Contrast can be defined as:

Contrast =  (I_max - I_min)/(I_max + I_min)

This process expands the range of intensity levels in an image so that it spans the full intensity of the camera/display. The figure below shows the graph corresponding to the contrast stretching.

Cara menggunakan adaptive gamma correction python

With (r1, s1), (r2, s2) as parameters, the function stretches the intensity levels by essentially decreasing the intensity of the dark pixels and increasing the intensity of the light pixels. If s = clog(1+r)02and s = clog(1+r)03, the function becomes a straight dotted line in the graph (which gives no effect). The function is monotonically increasing so that the order of intensity levels between pixels is preserved.

Below is the Python code to perform contrast stretching.




import cv2

import numpy as np

  

s = clog(1+r)09

s = clog(1+r)10 s = clog(1+r)11

 2s = clog(1+r)13 s = clog(1+r)14s = clog(1+r)15 s = clog(1+r)16s = clog(1+r)0 s = clog(1+r)18s = clog(1+r)19 s = clog(1+r)20s = clog(1+r)0 s = clog(1+r)22

s = clog(1+r)23s = clog(1+r)24 s = clog(1+r)57s = clog(1+r)8 s = clog(1+r)40 s = clog(1+r)60s = clog(1+r)9s = clog(1+r)14s = clog(1+r)8 s = clog(1+r)40 s = clog(1+r)65255/(log (1 + m))9 s = clog(1+r)47s = clog(1+r)40 s = clog(1+r)69255/(log (1 + m))2 s = clog(1+r)71