(Digital Image Processing MATLAB+Python) Chapter 5 Image Enhancement - Section 3: Image Enhancement Based on Illuminance Reflection Model

Image enhancement based on the illuminance reflection model : The image enhancement method based on the illuminance reflection model is to improve the image quality by adjusting the brightness and contrast in the image. This method assumes that the brightness of the image can be decomposed into two parts: illuminance and reflection . The illuminance is the distribution of light intensity produced by the light source , and the reflection is determined by the reflectance of the object surface and the light intensity.

  • i ( x , y ) i (x, y) i(x,y ) describes the slow transformation of scene lighting, which is approximated asa low-frequency component
  • r ( x , y ) r (x, y) r(x,y ) describes the rapid change of scene content, which is approximated asa high-frequency component

insert image description here

The image enhancement method based on the illumination reflection model mainly includes the following steps

  • Estimate the illuminance distribution of the image : This step aims to isolate the illuminance in the image so that the reflection distribution can be adjusted later. Commonly used methods include methods based on histogram equalization, methods based on Gaussian models, and methods based on Retinex theory, etc.
  • Calculate the reflection distribution of the image : After obtaining the illumination distribution, the reflection distribution can be obtained by dividing the original image by the illumination distribution, which represents the reflectance information of the object surface
  • Adjust the reflection distribution of the image : The contrast and brightness of the image can be adjusted by linear or nonlinear transformation of the reflection distribution to improve the quality of the image. Commonly used methods include gamma correction, logarithmic transformation, and histogram matching, etc.
  • Multiply the adjusted reflectance distribution with the illuminance distribution to get the enhanced image

One: Enhancement based on homomorphic filtering

(1 Overview

  • Background : If the object is subject to uneven illumination, it will be difficult to distinguish the details of the part of the image corresponding to the dark illumination
  • Main purpose : Eliminate the influence of uneven illumination and enhance image details

Enhancement based on homomorphic filtering : It is a method that can effectively improve image quality. The main idea is to convert the image to the frequency domain for processing, and achieve enhancement by adjusting the amplitude and phase information of the image in the frequency domain. Specifically, image enhancement based on homomorphic filtering can be expressed by the following formula :

g ( x , y ) = H ( u , v ) F ( u , v ) g(x,y) = H(u,v)F(u,v) g(x,y)=H(u,v)F(u,v)

where, g ( x , y ) g(x,y)g(x,y ) represents the enhanced image,F ( u , v ) F(u,v)F(u,v ) represents the Fourier transform of the original image in the frequency domain,H ( u , v ) H(u,v)H(u,v ) is a filter function used to adjust the magnitude and phase information of the image in the frequency domain,uuuwa vv_v represent the horizontal and vertical coordinates in the frequency domain, respectively

The specific form of the homomorphic filter can be expressed as:

H ( u , v ) = H l p ( u , v ) H h p ( u , v ) H(u,v) = H_{lp}(u,v)H_{hp}(u,v) H(u,v)=Hlp(u,v)Hhp(u,v)

Among them, H lp ( u , v ) H_{lp}(u,v)Hlp(u,v ) sumH hp ( u , v ) H_{hp}(u,v)Hhp(u,v ) represent low-pass and high-pass filters, respectively, which are used to adjust the low-frequency and high-frequency information of the image in the frequency domain. In general,H lp ( u , v ) H_{lp}(u,v)Hlp(u,v ) Using a Gaussian filter,H hp ( u , v ) H_{hp}(u,v)Hhp(u,v ) Using a Butterworth filter. When performing homomorphic filtering, the image can be logarithmically transformed, and the multiplication operation can be converted into an addition operation, thereby simplifying the calculation. Finally, by inversely transforming the enhanced image, the enhanced image in the spatial domain can be obtained

(2) Program

insert image description here


matlab implementation :

Image=double(rgb2gray(imread('gugong1.jpg')));  %打开图像并转换为double数据
imshow(uint8(Image)),title('故宫');
logI=log(Image+1);  %对数运算
sigma=1.414;  filtersize=[7 7];  %高斯滤波器参数
lowfilter=fspecial('gaussian',filtersize,sigma);  %构造高斯低通滤波器
highfilter=zeros(filtersize);
highpara=1; lowpara=0.4;    %控制滤波器幅度范围的系数  
highfilter(ceil(filtersize(1,1)/2),ceil(filtersize(1,2)/2))=1;
highfilter=highpara*highfilter-(highpara-lowpara)*lowfilter; %高斯低通滤波器转换为高斯高通滤波器
highpart=imfilter(logI,highfilter,'replicate','conv'); %时域卷积实现滤波
NewImage=exp(highpart); %指数变换恢复图像
top=max(NewImage(:)); bottom=min(NewImage(:));
NewImage=(NewImage-bottom)/(top-bottom);     %数据的映射处理,符合人眼视觉特性
NewImage=1.5.*(NewImage);
figure,imshow((NewImage));title('基于同态滤波的增强图像');
imwrite(NewImage,'tongtai.bmp');

Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']

# 读取图像并转换为double数据类型
img = cv2.imread('gugong1.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(np.float64)

# 显示原始图像
plt.imshow(img, cmap='gray')
plt.title('故宫')
plt.show()

# 对图像进行对数变换
log_img = np.log(img + 1)

# 构造高斯低通滤波器
sigma = 1.414
filter_size = (7, 7)
low_filter = cv2.getGaussianKernel(filter_size[0], sigma)
low_filter = low_filter @ low_filter.T

# 构造高斯高通滤波器
high_filter = np.zeros(filter_size)
high_para = 1
low_para = 0.4
high_filter[filter_size[0] // 2, filter_size[1] // 2] = 1
high_filter = high_para * high_filter - (high_para - low_para) * low_filter

# 对图像进行高斯高通滤波
high_part = cv2.filter2D(log_img, -1, high_filter, borderType=cv2.BORDER_REPLICATE)

# 进行指数变换并进行数据映射
new_img = np.exp(high_part)
new_img = (new_img - np.min(new_img)) / (np.max(new_img) - np.min(new_img))
new_img = 1.5 * new_img

# 显示增强后的图像并保存
plt.imshow(new_img, cmap='gray')
plt.title('基于同态滤波的增强图像')
plt.show()

cv2.imwrite('tongtai.bmp', new_img * 255)

Two: Retinex theory

(1) Overview of Retinex theory

Retinex theory : An image processing theory proposed by Professor Edwin H. Land of Stanford University in 1971. Retinex is a compound word, and its composition is retina (retina) + cortex (cortex) → Retinex. Over the years, the Retinex algorithm has been improved from a single-scale Retinex algorithm (SSR) to a multi-scale weighted average Retinex algorithm (MSR), and then developed into a multi-scale Retinex algorithm with color restoration (MSRCR). Although these algorithms are not the same, their basic principles are very similar. They all obtain the illuminance image by Gaussian filtering on the original image, and obtain the illuminance image as accurately as possible, and finally separate the illuminance image from the original image to obtain reflective image. The theoretical basis of the Retinex model is the three-color theory and color constancy , that is, the color of an object is determined by the object's ability to reflect long-wave (red), medium-wave (green), and short-wave (blue) light, not by reflection Determined by the absolute value of the light intensity, the color of the object is not affected by the non-uniformity of the light, and has consistency, that is, Retinex is based on the consistency of color perception (color constancy). The core arguments of the theory are as follows

  • The human eye's perception of the color of an object is closely related to the reflective properties of the object's surface, that is, objects with low reflectivity appear darker, and objects with high reflectivity appear brighter.
  • The human eye has a consistent perception of the color of the object and is not affected by changes in light

Retinex theory believes that the image I ( x , y ) I(x,y)I(x,y ) is composed of illuminance image and reflection image, as follows, according to the illuminance-reflection model, the core equation in Retinex theory is

  • R ( x , y ) R(x,y) R(x,y ) represents object reflection information
  • L ( x , y ) L(x,y) L(x,y ) represents the incident component information

I ( x , y ) = R ( x , y ) ∗ L ( x , y ) I(x,y)=R(x,y)*L(x,y) I(x,y)=R(x,y)L(x,y)

insert image description here

The development process of Retinex theory is as follows, roughly going through two stages

  • Iterative-based Retinex algorithm (has a lot of flaws)
  • Center Surround Based Retinex Algorithm

insert image description here

(1) SSR (single-scale Retinex algorithm)

SSR : It is an image enhancement algorithm based on Retinex, which only operates on a single scale. The algorithm steps are as follows

  • Read original image data S ( x , y ) S(x,y)S(x,y ) , and convert the integer to double type
  • Determine the scale parameter σ \sigmaThe size of σ is determined to satisfy the condition∬ G ( x , y ) dxdy = 1 \iint G(x,y)dxdy=1G(x,y)dxdy=1 λ\lambdaLambda value
  • 根据公式 r ( x , y ) = log ⁡ S ( x , y ) − log ⁡ [ G ( x , y ) ⋅ S ( x , y ) ] r(x,y)=\log S(x,y)-\log[G(x,y)\cdot S(x,y)] r(x,y)=logS(x,y)log[G(x,y)S(x,y)] r ( x , y ) r(x,y) r(x,y ) value
  • r ( x , y ) r(x,y) r(x,y ) values ​​are converted to the real field R by pair belonging toR ( x , y ) R(x,y)R(x,y)
  • R ( x , y ) R(x,y) R(x,y ) do linear stretch processing, and the output shows

For grayscale images, the grayscale value can be directly processed by the above steps; for color images, it needs to be decomposed into three grayscale images of R, G, and B, then processed by the above steps, and finally synthesized into a color image

insert image description here

Achieve the following effects
insert image description here

matlab implementation :

clear all;
close all;
clc;
I = imread('origin.jpg');%读图
 
% 提取原始图像的红色通道,将其转换为双精度类型,并进行log变换和二维傅里叶变换
R = I(:, :, 1);
[N1, M1] = size(R);
R0 = double(R);
Rlog = log(R0+1);
Rfft2 = fft2(R0);
 
% 设定高斯核参数sigma,并生成与图像大小相同的高斯核。然后将高斯核进行二维傅里叶变换
sigma = 80;
F = fspecial('gaussian', [N1,M1], sigma);
Efft = fft2(double(F));
 
% 进行卷积操作,然后将该结果进行傅里叶反变换,得到卷积结果的空域图像
DR0 = Rfft2.* Efft;%卷积
DR = ifft2(DR0);%反变换到空域
 
% 将卷积结果的空域图像进行log变换,然后将其与原始图像的log变换相减,得到增强后的图像的log变换
DRlog = log(DR +1);
Rr = Rlog - DRlog;

% 将卷积结果的空域图像进行log变换,然后将其与原始图像的log变换相减,得到增强后的图像的log变换
EXPRr = Rr;
MIN = min(min(EXPRr));
MAX = max(max(EXPRr));
EXPRr = 255*(EXPRr - MIN)/(MAX - MIN);%线性拉伸

 
G = I(:, :, 2);
 
G0 = double(G);
Glog = log(G0+1);
Gfft2 = fft2(G0);
 
DG0 = Gfft2.* Efft;
DG = ifft2(DG0);
 
DGlog = log(DG +1);
Gg = Glog - DGlog;

EXPGg = Gg;
MIN = min(min(EXPGg));
MAX = max(max(EXPGg));
EXPGg = 255*(EXPGg - MIN)/(MAX - MIN);

 
B = I(:, :, 3);
 
B0 = double(B);
Blog = log(B0+1);
Bfft2 = fft2(B0);
 
DB0 = Bfft2.* Efft;
DB = ifft2(DB0);
 
DBlog = log(DB+1);
Bb = Blog - DBlog;

EXPBb = Bb;
MIN = min(min(EXPBb));
MAX = max(max(EXPBb));
EXPBb = 255*(EXPBb - MIN)/(MAX - MIN);

 
result = cat(3, EXPRr, EXPGg, EXPBb);
subplot(121), imshow(I);
subplot(122), imshow(uint8(result));


Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']

# 读取图像
I = cv2.imread('origin.jpg')

# 提取红色通道,将其转换为双精度类型,并进行log变换和二维傅里叶变换
R = I[:, :, 2]
N1, M1 = R.shape
R0 = np.double(R)
Rlog = np.log(R0+1)
Rfft2 = np.fft.fft2(R0)

# 设定高斯核参数sigma,并生成与图像大小相同的高斯核。然后将高斯核进行二维傅里叶变换
sigma = 80
F = cv2.getGaussianKernel(max(N1,M1),sigma)
F = np.outer(F,F)
Efft = np.fft.fft2(np.double(F),[N1,M1])

# 进行卷积操作,然后将该结果进行傅里叶反变换,得到卷积结果的空域图像
DR0 = Rfft2 * Efft
DR = np.fft.ifft2(DR0)

# 将卷积结果的空域图像进行log变换,然后将其与原始图像的log变换相减,得到增强后的图像的log变换
DRlog = np.log(DR + 1)
Rr = Rlog - DRlog

# 线性拉伸像素值到0-255之间
EXPRr = Rr
MIN = np.min(EXPRr)
MAX = np.max(EXPRr)
EXPRr = 255 * (EXPRr - MIN) / (MAX - MIN)
EXPRr = np.uint8(EXPRr)

G = I[:, :, 1]

G0 = np.double(G)
Glog = np.log(G0+1)
Gfft2 = np.fft.fft2(G0)

DG0 = Gfft2 * Efft
DG = np.fft.ifft2(DG0)

DGlog = np.log(DG+1)
Gg = Glog - DGlog

EXPGg = Gg
MIN = np.min(EXPGg)
MAX = np.max(EXPGg)
EXPGg = 255 * (EXPGg - MIN) / (MAX - MIN)
EXPGg = np.uint8(EXPGg)

B = I[:, :, 0]

B0 = np.double(B)
Blog = np.log(B0+1)
Bfft2 = np.fft.fft2(B0)

DB0 = Bfft2 * Efft
DB = np.fft.ifft2(DB0)

DBlog = np.log(DB+1)
Bb = Blog - DBlog

EXPBb = Bb
MIN = np.min(EXPBb)
MAX = np.max(EXPBb)
EXPBb = 255 * (EXPBb - MIN) / (MAX - MIN)
EXPBb = np.uint8(EXPBb)

# 合并增强后的通道,形成增强后的图像
result = cv2.merge((EXPRr, EXPGg, EXPBb))

# 显示原始图像和增强后的图像
fig, ax = plt.subplots(1, 2)
ax[0].imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
ax[0].set_title('Original Image

(2) MSR (multi-scale Retinex algorithm)

MSR : To put it simply, MSR uses several scale parameters of different sizes to filter the three components of RGB separately, and then linearly weighted and normalizes to obtain the MSR algorithm. The algorithm steps are as follows

  • Read original image data S ( x , y ) S(x,y)S(x,y ) , and convert the integer to double type
  • Determine three scale parameters σ 1 , σ 2 . σ 3 \sigma_{1},\sigma_{2}.\sigma_{3}p1,p2. p3Size, determine to satisfy the condition ∬ G k ( x , y ) dxdy = 1 \iint G_{k}(x,y)dxdy=1Gk(x,y)dxdy= 1, λ 2 , λ 3 \lambda_{1},\lambda_{2},\lambda_{3}l1,l2,l3the value of
  • Level ratio r ( x , y ) = ∑ k = 1 K qk { log ⁡ S ( x , y ) − log ⁡ [ G k ( x , y ) ∗ S ( x , y ) ] } r(\mathrm{x }, \mathrm{y})=\sum_{k=1}^{K} q_{k}\left\{\log \mathrm{S}(\mathrm{x}, \mathrm{y})-\ log \left[\mathrm{G}_{k}(\mathrm{x}, \mathrm{y}) *\mathrm{~S}(\mathrm{x}, \mathrm{y})\right]\ right\}r(x,y)=k=1Kqk{ logS(x,y)log[Gk(x,y) S(x,y)]} r ( x , y ) r(x,y) r(x,y ) value
  • r ( x , y ) r(x,y) r(x,y ) values ​​are converted to the real field R by pair belonging toR ( x , y ) R(x,y)R(x,y)
  • R ( x , y ) R(x,y) R(x,y ) do linear stretch processing, and the output shows

insert image description here

Achieve the following effects

Please add a picture description


matlab implementation :

close all; clear all; clc
I = imread('origin.jpg');
I_r = double(I(:,:,1));
I_g = double(I(:,:,2));
I_b = double(I(:,:,3));
I_r_log = log(I_r+1);
I_g_log = log(I_g+1);
I_b_log = log(I_b+1);
Rfft1 = fft2(I_r);
Gfft1 = fft2(I_g);
Bfft1 = fft2(I_b);[m,n] = size(I_r);
sigma1 = 15;
sigma2 = 80;
sigma3 = 200;
f1 = fspecial('gaussian', [m, n], sigma1);
f2 = fspecial('gaussian', [m, n], sigma2);
f3 = fspecial('gaussian', [m, n], sigma3);
efft1 = fft2(double(f1));
efft2 = fft2(double(f2));
efft3 = fft2(double(f3));
D_r1 = ifft2(Rfft1.*efft1);
D_g1 = ifft2(Gfft1.*efft1);
D_b1 = ifft2(Bfft1.*efft1);
D_r_log1 = log(D_r1 + 1);
D_g_log1 = log(D_g1 + 1);
D_b_log1 = log(D_b1 + 1);
R1 = I_r_log - D_r_log1;
G1 = I_g_log - D_g_log1;
B1 = I_b_log - D_b_log1;
D_r2 = ifft2(Rfft1.*efft2);
D_g2 = ifft2(Gfft1.*efft2);
D_b2 = ifft2(Bfft1.*efft2);
D_r_log2 = log(D_r2 + 1);
D_g_log2 = log(D_g2 + 1);
D_b_log2 = log(D_b2 + 1);
R2 = I_r_log - D_r_log2;
G2 = I_g_log - D_g_log2;
B2 = I_b_log - D_b_log2;
D_r3 = ifft2(Rfft1.*efft3);
D_g3 = ifft2(Gfft1.*efft3);
D_b3 = ifft2(Bfft1.*efft3);
D_r_log3 = log(D_r3 + 1);
D_g_log3 = log(D_g3 + 1);
D_b_log3 = log(D_b3 + 1);
R3 = I_r_log - D_r_log3;
G3 = I_g_log - D_g_log3;
B3 = I_b_log - D_b_log3;
R = 0.1*R1 + 0.4*R2 + 0.5*R3;
G = 0.1*G1 + 0.4*G2 + 0.5*G3;
B = 0.1*B1 + 0.4*B2 + 0.5*B3;
R = exp(R);
MIN = min(min(R));
MAX = max(max(R));
R = (R - MIN)/(MAX - MIN);
R = adapthisteq(R);
G = exp(G);
MIN = min(min(G));
MAX = max(max(G));
G = (G - MIN)/(MAX - MIN);
G = adapthisteq(G);
B = exp(B);
MIN = min(min(B));
MAX = max(max(B));
B = (B - MIN)/(MAX - MIN);
B = adapthisteq(B);
J = cat(3, R, G, B);
figure;
subplot(121);imshow(I);
subplot(122);imshow(J,[]);

Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']

# 读取图像
I = cv2.imread('origin.jpg')

# 将图像拆分为R,G,B通道
I_r = I[:, :, 0].astype(float)
I_g = I[:, :, 1].astype(float)
I_b = I[:, :, 2].astype(float)

# 对每个通道进行对数变换
I_r_log = np.log(I_r + 1)
I_g_log = np.log(I_g + 1)
I_b_log = np.log(I_b + 1)

# 获取图像大小
m, n = I_r.shape

# 定义三个不同的高斯滤波核
sigma1 = 15
sigma2 = 80
sigma3 = 200
f1 = cv2.getGaussianKernel(m, sigma1)
f2 = cv2.getGaussianKernel(m, sigma2)
f3 = cv2.getGaussianKernel(m, sigma3)
f1 = f1.dot(f1.T)
f2 = f2.dot(f2.T)
f3 = f3.dot(f3.T)

# 对高斯滤波核进行FFT
efft1 = np.fft.fft2(f1)
efft2 = np.fft.fft2(f2)
efft3 = np.fft.fft2(f3)

# 对R,G,B通道进行FFT
Rfft1 = np.fft.fft2(I_r)
Gfft1 = np.fft.fft2(I_g)
Bfft1 = np.fft.fft2(I_b)

# 对每个通道进行频域滤波
D_r1 = np.fft.ifft2(Rfft1 * efft1)
D_g1 = np.fft.ifft2(Gfft1 * efft1)
D_b1 = np.fft.ifft2(Bfft1 * efft1)
D_r_log1 = np.log(D_r1 + 1)
D_g_log1 = np.log(D_g1 + 1)
D_b_log1 = np.log(D_b1 + 1)
R1 = I_r_log - D_r_log1
G1 = I_g_log - D_g_log1
B1 = I_b_log - D_b_log1

D_r2 = np.fft.ifft2(Rfft1 * efft2)
D_g2 = np.fft.ifft2(Gfft1 * efft2)
D_b2 = np.fft.ifft2(Bfft1 * efft2)
D_r_log2 = np.log(D_r2 + 1)
D_g_log2 = np.log(D_g2 + 1)
D_b_log2 = np.log(D_b2 + 1)
R2 = I_r_log - D_r_log2
G2 = I_g_log - D_g_log2
B2 = I_b_log - D_b_log2

D_r3 = np.fft.ifft2(Rfft1 * efft3)
D_g3 = np.fft.ifft2(Gfft1 * efft3)
D_b3 = np.fft.ifft2(Bfft1 * efft3)
D_r_log3 = np.log(D_r3 + 1)
D_g_log3 = np.log(D_g3 + 1)
D_b_log3 = np.log(D_b3 + 1)
R3 = I_r_log - D_r_log3
G3 = I_g_log - D_g_log3
B3 = I_b_log - D_b_log3

# 对每个通道

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/130243593