画像の区分的線形変換(Piecewise Linear Transformation)は、異なる領域のピクセル値に異なる線形変換を適用することで、画像のコントラストと明るさを調整する画像処理技術です。これは、画像の特定の領域の詳細を強調したり、画像全体の外観を調整したりするためによく使用されます。数学的には、区分的線形変換は次の形式で表すことができます:
入力画像の各ピクセル点(x, y) (x, y)( x ,y )、その出力値f ' ( x , y ) f'(x, y)f' (x,y )は次の分節線性関数によって計算できます:
f ' ( x , y ) = { a 1 ⋅ f ( x , y ) + b 1 、 f ( x , y ) < x 1 a 2 ⋅ f ( x , y ) + b 2 、 x 1 ≤ f ( x , y ) < x 2 の場合 ⋮ an ⋅ f ( x , y ) + bn 、 xn − 1 ≤ f ( x , y ) < xn f'(x, y) = \begin{cases} a_1 \cdot f(x, y) + b_1, & \text{if } f(x, y) < x_1 \\ a_2 \cdot f(x, y) + b_2, & \ text{if } x_1 \leq f(x, y) < x_2 \\ \vdots \\ a_n \cdot f(x, y) + b_n, & \text{if } x_{n-1} \leq f(x , y) < x_n \\ \end{cases}f' (x,y )=⎩
⎨
⎧ある1⋅f ( x ,y )+b1、ある2⋅f ( x ,y )+b2、⋮あるん⋅f ( x ,y )+bん、if f ( x ,y )<バツ1×の場合 1≤f ( x ,y )<バツ2×の場合 n − 1≤f ( x ,y )<バツん
其中, f ( x , y ) f(x, y) f ( x ,y )は、元の画像のピクセル点(x, y) (x, y)( x ,y )グレー値、( x 1 , x 2 , … , xn ) (x_1, x_2, \ldots, x_n)( ×1、バツ2、…、バツん)は分割点です( a 1 , a 2 , … , an ) (a_1, a_2, \ldots, a_n)( _1、ある2、…、あるん)は傾きです( b 1 , b 2 , … , bn ) (b_1, b_2, \ldots, b_n)( b1、b2、…、bん)は切片です。
画像の区分的線形変換に OpenCV ライブラリを使用した Python コードの例を次に示します。
import cv2
import numpy as np
def piecewise_linear_transform(image, breakpoints, slopes, intercepts):
# 创建一个空白图像,用于存储变换后的结果
transformed_image = np.zeros_like(image)
for i in range(len(breakpoints) + 1):
lower_bound = breakpoints[i - 1] if i > 0 else 0
upper_bound = breakpoints[i] if i < len(breakpoints) else 255
# 对当前分段内的像素应用线性变换
mask = (image >= lower_bound) & (image < upper_bound)
transformed_image[mask] = slopes[i] * image[mask] + intercepts[i]
return transformed_image
# 读取图像
input_image = cv2.imread('input.jpg', cv2.IMREAD_GRAYSCALE)
# 定义分段点、斜率和截距
breakpoints = [100, 150]
slopes = [1.5, 0.7, 1.2]
intercepts = [-100, 50, 0]
# 应用分段线性变换
output_image = piecewise_linear_transform(input_image, breakpoints, slopes, intercepts)
# 显示原始图像和变换后的图像
cv2.imshow('Original Image', input_image)
cv2.imshow('Transformed Image', output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()