Digital image processing (18): The image gradation transformation - linear and nonlinear gradation conversion gradation conversion (gamma conversion with logarithmic transformation)

Disclaimer: This article is a blogger original article, reproduced, please indicate the link. https://blog.csdn.net/zaishuiyifangxym/article/details/89817995

table of Contents

1 gray-scale transformation Profile

Second linear gradation transformation - image reversal

3 nonlinear gradation conversion

3.1 pairs logarithmic transformation

3.2 gamma conversion

Reference material


1 gray-scale transformation Profile

Gradation transformation image enhancement is an important means for improving the image display effect, a spatial domain processing method belongs to, it can increase the dynamic range of the image, image contrast stretching, the image clearer, more obvious characteristics. Gradation transformation its essence is modified according to certain rules of each image pixel of the gradation, the gradation range of the image to change. Common image reversal gradation conversion, logarithmic transformation and gamma conversion and the like. Gradation transformation image enhancement is an important means for improving the image display effect, a spatial domain processing method belongs to, it can increase the dynamic range of the image, image contrast stretching, the image clearer, more obvious characteristics. Gradation transformation its essence is modified according to certain rules of each image pixel of the gradation, the gradation range of the image to change. Common image reversal gradation conversion, logarithmic transformation and gamma conversion and the like. Specific classification as shown below:


 

Second linear gradation transformation - image reversal

Gradation is the most common linear transformation image reversal, the gray image gray scale range [0,L-1], which is inverted formula is as follows:

                                                                                        s=L-1-r

Wherein rrepresents the original gray scale image, srepresenting a gray level after conversion.

 

The following figure shows an example of image reversal of the original image is a digital X-ray photograph of the breast, showing a small lesion, it is easy to see by the image reversal lesion area.

 

Code as follows:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('zxp.jpg')

#图像灰度转换
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#获取图像高度和宽度
height = grayImage.shape[0]
width = grayImage.shape[1]

#创建一幅图像
result = np.zeros((height, width), np.uint8)

#图像灰度反色变换 s=255-r
for i in range(height):
    for j in range(width):
        gray = 255 - grayImage[i,j]
        result[i,j] = np.uint8(gray)

#显示图像

cv2.imshow("Gray Image", grayImage)
cv2.imshow("Result", result)


#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

 

Operating results as shown below:

 


 

3 nonlinear gradation conversion

3.1 pairs logarithmic transformation

Image gray logarithmic transformation generally expressed as follows:

                                                                                    s=c\log (1+r)

Wherein rrepresents the original gray scale image, srepresenting a gray level after conversion, cit is constant.

Suppose r\ge 0, in the shape of a logarithmic curve shown in the figure indicate changes in the input transducer narrower gray value is mapped to a low gradation value in a wide range of output. Conversely, too high an input gradation value. We use this type of transform values to extend the dark pixels in the image, while compressing the higher grayscale values. Action against the log-transformed to the contrary.

 

Code as follows:

# -*- 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('test8.bmp')

#绘制对数变换曲线
log_plot(42)

#图像灰度对数变换
output = log(42, img)

#显示图像
cv2.imshow('Input', img)
cv2.imshow('Output', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

Operating results as shown below:

 

 


 

3.2 gamma conversion

Gamma conversion is also called  exponential transformation  or  a power of transform , is another common non-linear transformation gradation. Gamma conversion gradation image is generally expressed as follows:

                                                                                             s = c {{r} ^ {\ gamma}}

Wherein rrepresents the original gray scale image, srepresenting a gray level, the transformed cand \gammanormal number

1) When \gamma> 1, the image will be stretched in a high gradation region, the lower portion of the grayscale compression;

2) When \gamma<1, will stretch the image gray level in the lower region, the higher grayscale compression section;

3) When \gamma= 1, the gradation transformation is linear, the original image by changing at this time in a linear manner.

As shown below, a different \gammaconversion curve values:

 

Shown below is an example of gamma conversion of an image:

 

 

Code as follows:

# -*- 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('test9.bmp')

#绘制伽玛变换曲线
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()

 

Operating results as shown below:

 


 

Reference material

[1] https://blog.csdn.net/Eastmount/article/details/88858696

[2] https://blog.csdn.net/Eastmount/article/details/88929290

[3]  Gonzalez. Digital Image Processing (third edition) 

Guess you like

Origin blog.csdn.net/zaishuiyifangxym/article/details/89817995