Learn Python image processing with me丨What is grayscale nonlinear transformation of images

Abstract: This article mainly explains the grayscale linear transformation, and the basic knowledge hopes to help you.

This article is shared from HUAWEI CLOUD Community " [Python Image Processing] 16. Logarithmic Transformation and Gamma Transformation of Grayscale Nonlinear Transformation of Images ", author: eastmount.

This article mainly explains nonlinear transformations, and uses custom methods to grayscale images, including logarithmic transformation and gamma transformation.

1. Image grayscale nonlinear transformation

The grayscale nonlinear transformation of an image mainly includes logarithmic transformation, power transformation, exponential transformation, and piecewise function transformation. The grayscale processing of the image is performed through a nonlinear relationship. The following mainly explains three common types of grayscale nonlinear transformation.

The gray value of the original image is nonlinearly transformed according to the formula of DB=DA×DA/255, and the code is as follows:

# -*- coding: utf-8 -*-
import cv2  
import numpy as np  
import matplotlib.pyplot as plt
#读取原始图像
img = cv2.imread('miao.png')
#图像灰度转换
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#获取图像高度和宽度
height = grayImage.shape[0]
width = grayImage.shape[1]
#创建一幅图像
result = np.zeros((height, width), np.uint8)
#图像灰度非线性变换:DB=DA×DA/255
for i in range(height):
 for j in range(width):
 gray = int(grayImage[i,j])*int(grayImage[i,j]) / 255
 result[i,j] = np.uint8(gray)
#显示图像
cv2.imshow("Gray Image", grayImage)
cv2.imshow("Result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result of the image grayscale nonlinear transformation is shown in the following figure:

2. Image grayscale logarithmic transformation

The logarithmic transformation of image grayscale is generally expressed as the formula:

where c is the scale comparison constant, DA is the original image gray value, and DB is the transformed target gray value. As shown in the figure below, it represents the change of gray value under the logarithmic curve.

Since the logarithmic curve has a large slope in the area with lower pixel value and a smaller slope in the area with higher pixel value, the contrast of the darker area will be improved after the image is logarithmically transformed. This transformation can be used to enhance dark details in an image, thereby expanding darker pixels in compressed high-value images.

Logarithmic transformation realizes the effect of expanding low grayscale values ​​and compressing high grayscale values, and is widely used in the display of spectral images. A typical application is Fourier spectrum, whose dynamic range may be as wide as 0 to 106. When the spectrum is directly displayed, the dynamic range of the image display device often cannot meet the requirements, thus losing a lot of dark details; and after using logarithmic transformation, the image's dynamic range is The dynamic range is reasonably non-linearly compressed so that it can be displayed clearly. In the image below, the untransformed spectrum has been log-transformed to increase contrast in low-gray areas, thereby enhancing detail in dark areas.

The following code implements the logarithmic transformation of the grayscale of an image.

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import cv2
#绘制曲线
def log_plot(c):
    x = np.arange(0, 256, 0.01)
    y = c * np.log(1 + x)
 plt.plot(x, y, 'r', linewidth=1)
 plt.rcParams['font.sans-serif']=['SimHei'] #正常显示中文标签
 plt.title(u'对数变换函数')
 plt.xlim(0, 255), plt.ylim(0, 255)
 plt.show()
#对数变换
def log(c, img):
 output = c * np.log(1.0 + img)
 output = np.uint8(output + 0.5)
 return output
#读取原始图像
img = cv2.imread('test.png')
#绘制对数变换曲线
log_plot(42)
#图像灰度对数变换
output = log(42, img)
#显示图像
cv2.imshow('Input', img)
cv2.imshow('Output', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

The following figure shows the effect after the logarithmic function processing. The logarithmic transformation is better for the image enhancement effect with low overall contrast and low gray value.

The corresponding logarithmic function curve is shown in the figure

3. Image grayscale gamma transformation

Gamma transform, also known as exponential transform or power transform, is another commonly used grayscale nonlinear transform. The gamma transformation of image grayscale is generally expressed as the formula:

  • When γ>1, the regions with higher gray levels in the image will be stretched and the parts with lower gray levels will be compressed.
  • When γ<1, the regions with lower gray levels in the image will be stretched and the parts with higher gray levels will be compressed.
  • When γ=1, the grayscale transformation is linear, and the original image is changed in a linear manner.

Python implements the gamma transformation code of image grayscale as follows, which is mainly implemented by calling the power function.

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import cv2
#绘制曲线
def gamma_plot(c, v):
    x = np.arange(0, 256, 0.01)
    y = c*x**v
 plt.plot(x, y, 'r', linewidth=1)
 plt.rcParams['font.sans-serif']=['SimHei'] #正常显示中文标签
 plt.title(u'伽马变换函数')
 plt.xlim([0, 255]), plt.ylim([0, 255])
 plt.show()
#伽玛变换
def gamma(img, c, v):
 lut = np.zeros(256, dtype=np.float32)
 for i in range(256):
 lut[i] = c * i ** v
 output_img = cv2.LUT(img, lut) #像素灰度值的映射
 output_img = np.uint8(output_img+0.5) 
 return output_img
#读取原始图像
img = cv2.imread('test.png')
#绘制伽玛变换曲线
gamma_plot(0.00000005, 4.0)
#图像灰度伽玛变换
output = gamma(img, 0.00000005, 4.0)
#显示图像
cv2.imshow('Imput', img)
cv2.imshow('Output', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

The following figure shows the effect after gamma transformation processing. Gamma transformation has obvious image enhancement effect when the image contrast is low and the overall brightness value is high (or due to camera overexposure).

The corresponding power-law function curve is shown in Fig.

 

Click Follow to learn about HUAWEI CLOUD's new technologies for the first time~

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/5572461