"Digital Image Processing-OpenCV/Python" Serial (50) Nonlinear Grayscale Transformation

"Digital Image Processing-OpenCV/Python" Serial (50) Nonlinear Grayscale Transformation


This book’s JD discount purchase link: https://item.jd.com/14098452.html
This book’s CSDN exclusive serial column: https://blog.csdn.net /youcans/category_12418787.html

Insert image description here


Chapter 7 Grayscale Transformation of Images


Grayscale transformation modifies the grayscale value of the pixel according to the grayscale mapping function, thereby changing the dynamic range of the image grayscale. Grayscale transformation can expand the dynamic range of the image and enhance the contrast of the image, making the image clearer and more obvious.


Summary of this chapter

  • Introduce the linear grayscale transformation of images and understand the impact of linear stretching on the grayscale dynamic range.
  • Introduce commonly used nonlinear grayscale transformation methods, such as logarithmic transformation, power law transformation and piecewise linear transformation.
  • Adjust image levels through grayscale transformation to understand and correct the tonal range and color balance of images.

7.3 Nonlinear grayscale transformation

Nonlinear grayscale transformation refers to using nonlinear functions to adjust the grayscale range of the original image. Commonly used methods include logarithmic transformation and power law transformation. The mapping relationship between logarithmic transformation and power law transformation is shown in Figure 7-4.

Insert image description here
During the operation of nonlinear grayscale transformation, pixel values ​​must be calculated as real numbers, and the calculation results are also real numbers. Pay attention to the conversion of image data types.


7.3.1 Logarithmic transformation

Logarithmic transformation refers to mapping a low gray level with a narrow input range into a gray level with a wide range, so as to enhance the contrast of darker areas and improve the dark details of the image.

The logarithmic transformation can be described by the following formula:

d s t = c ∗ l o g ( 1 + s r c ) dst = c*log(1+src) dst=clog(1+src)

In the formula, src and dst represent the grayscale values ​​of the original image and the transformed image respectively; c is the proportion coefficient.

Logarithmic transformation achieves the effect of expanding low gray levels and compressing high gray levels, and is widely used in the display of spectrum images. A typical application is the display of Fourier spectrum.


7.3.2 Power law transformation

Power law transformation, also called gamma transformation, can improve dark details and correct images that are white (overexposed) or too dark (underexposed).

Gamma transform can be described by the following formula:

d s t = c ∗ s r c γ , γ > 0 dst = c*src^{\gamma}, \gamma>0 dst=csrcγ,c>0

In the formula, src and dst represent the grayscale values ​​of the original image and the transformed image respectively; γ \gamma γ is the gamma coefficient; c is the proportional coefficient.

0 < γ < 1 0<\gamma<1 0<c<When 1, the low gray level of the image is stretched, the high gray level of the image is compressed, and the contrast of the image is weakened; when γ \gamma When γ>1, the high gray level of the image is stretched, the low gray level of the image is compressed, and the contrast of the image is enhanced.

Gamma transformation compensates for human visual characteristics through non-linear transformation and can maximize the use of gray-scale bandwidth. The brightness curves of many shooting, display and printing devices conform to the gamma curve, so gamma transformation is widely used in displays. The adjustment of the equipment is called gamma correction.


[Routine 0704] Logarithmic transformation of grayscale transformation

This routine is the application of logarithmic transformation of image grayscale changes in Fourier spectrum display.


# 【0704】灰度变换之对数变换
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

if __name__ == '__main__':
    gray = cv.imread("../images/Fig0602.png", flags=0)  # 读取为灰度图像

    fft = np.fft.fft2(gray)  # 傅里叶变换
    fft_shift = np.fft.fftshift(fft)  # 将低频部分移动到图像中心
    amp = np.abs(fft_shift)  # 傅里叶变换的频谱
    ampNorm = np.uint8(cv.normalize(amp, None, 0, 255, cv.NORM_MINMAX))  # 归一化为 [0,255]
    ampLog = np.abs(np.log(1.0 + np.abs(fft_shift)))  # 对数变换, c=1
    ampLogNorm = np.uint8(cv.normalize(ampLog, None, 0, 255, cv.NORM_MINMAX))

    plt.figure(figsize=(9, 3.2))
    plt.subplot(131), plt.title("1. Original"), plt.axis('off')
    plt.imshow(gray, cmap='gray', vmin=0, vmax=255)
    plt.subplot(132), plt.title("2. FFT spectrum"), plt.axis('off')
    plt.imshow(ampNorm, cmap='gray', vmin=0, vmax=255)
    plt.subplot(133), plt.title("3. LogTrans of FFT"), plt.axis('off')
    plt.imshow(ampLogNorm, cmap='gray', vmin=0, vmax=255)
    plt.tight_layout()
    plt.show()


Program description:
(1) Running results, the logarithmic transformation of the Fourier spectrum is shown in Figure 7-5. Figure 7-5(2) shows the Fourier spectrum diagram of Figure 7-5(1), and Figure 7-5(3) shows the logarithmic transform image of Figure 7-5(2).
(2) Due to the wide dynamic range of the Fourier spectrum, Figure 7-5(2) can only show a bright spot in the center of the image (the bright spot only has one pixel, and it cannot actually be seen) , losing a lot of dark details.
(3) Figure 7-5(3) uses logarithmic transformation to nonlinearly compress the dynamic range of Figure 7-5(2), so the spectrum characteristics are clearly displayed.


Insert image description here

Figure 7-5 Logarithmic transformation of Fourier spectrum


Copyright statement:
youcans@xupt original work, reprints must be marked with the original link: (https://blog.csdn.net/youcans/article/details/134726151) Crated:2023-12-01
Copyright 2023 youcans, XUPT

Welcome to follow this book’s CSDN exclusive serialization column
"Digital Image Processing-OpenCV/Python" serialization: https://blog.csdn.net/youcans/category_12418787.html

Guess you like

Origin blog.csdn.net/youcans/article/details/134726151