The linear transformation and nonlinear transformation of the image, the pixel-by-pixel operation is to convert the brightness value of each pixel of the image to a new brightness value through a certain functional relationship. This conversion can be represented by a function:
s = f ( r ) s = f( r ) s=f(r)
Among them, r is the original pixel value, s is the new pixel value, and the function usually adopted is a monotone function for transformation.
线性变换
:
s ( x , y ) = c + kr ( x , y ) s(x,y) = c+kr(x,y)s(x,y)=c+k r ( x ,y )
where c and k are both constants
非线性变换
:
s = a + l n ( r + 1 ) b l n c s=a+\frac {ln(r+1)} {blnc} s=a+b l n cl n ( r+1)
Where a, b, c are constants
Gamma变换
:
s = c r γ s = cr^γ s=crγ
Where c is a constant, usually 1, γ is also a constant, the range of r is [0,255], usually scaled to [0,1] The
picture shows the situation when γ takes different values, for example, when the pixels of the original image When the value is 0.2, when γ=1.5, the pixel value of the current image is less than 0.2, when γ=1, the pixel value of the current image is equal to 0.2. When γ=0.5, the pixel value of the current image is greater than 0.4.
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
For different grayscale images and color images, use different methods to display
and define show()
functions
def show(img):
if img.ndim == 2:
plt.imshow(img, cmap='gray',vmin=0,vmax=255)
else:
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))#cv用的BGR,需要转换为RGB
plt.show()
read pictures
img = cv.imread("./pic/cat500x480.jpg",0) #0表示读为灰度图
show(img)
线性变换
:
b = 20
k = 2
img2 = b + k * img.astype(np.int32) #img的类型为uint8,线性变换后,像素值会循环
img2 = np.clip(img2,0,255) #利用np.clip来截断
show(img2)
np.clip
It is an interception function, which is used to intercept the part of the array that is less than or greater than a certain value, and make the intercepted part equal to a fixed value.
It is also possible cv.convertScaleAbs
to implement the built-in function
alpha = 2
beta = 20
img3 = cv.convertScaleAbs(img, alpha=alpha, beta=beta)#利用内置函数来截断
show(img3)
非线性变换
:
img4 = 10 + np.log(img.astype(np.float32)+1)/ 0.1
show(img4)
Gama变换
:
img01 = img / 255
img05 = np.power(img01,0.5)*255
img15 = np.power(img01,1.5)*255
show(np.hstack([img05,img,img15]))