(Digital Image Processing MATLAB+Python) Chapter 5 Image Enhancement - Section 2: Image Enhancement Based on Histogram Correction

Image enhancement based on histogram correction : It is a common image processing method. This method adjusts the pixel value distribution of the image to improve the visual effects such as contrast and brightness of the image. Specifically, the histogram correction method converts the pixel values ​​of the image into a new value range, so that the distribution of pixel values ​​is more uniform, thereby enhancing the details and contrast of the image. This method usually involves the following steps

  • Calculate the pixel value histogram of the image, that is, divide the pixel value into several intervals and count the number of pixels in each interval;
  • Calculate a cumulative distribution function (CDF) from the pixel value histogram;
  • Map the pixel values ​​according to the CDF, and map the pixel values ​​of the original image to the new value range to generate an enhanced image

The histogram correction method can effectively improve the visual quality of the image, but it may also cause some problems, such as excessive enhancement or noise increase in the enhanced image. Therefore, in practical applications, it is necessary to select the appropriate histogram correction method and parameters according to the specific situation

One: grayscale histogram

(1) Definition

Gray histogram : It represents the statistical relationship between each gray level in a digital image and its frequency of occurrence (that is, the number of pixels appearing on the gray level). Specifically, it uses gray-level pixel values ​​and their frequency (or probability) of appearance in the image as coordinates on the vertical and horizontal axes to form a histogram

p ( r k ) = n k N p(r_{k})=\frac{n_{k}}{N} p(rk)=Nnk

For example, in the image below, you can get its grayscale histogram

insert image description here

(2) Program

insert image description here

MATLAB implementation : the relevant functions are as follows, for detailed explanation, please refer to the MATLAB help manual

  • imhist(I,N)
Image=rgb2gray(imread('couple.bmp'));
histgram=zeros(256);                                    
[h w]=size(Image);
for x=1:w
    for y=1:h                                         %循环扫描
        histgram(Image(y,x)+1)=histgram(Image(y,x)+1)+1;   %统计并累加
    end
end
imwrite(Image,'Gcouple.bmp');
imshow(Image);title('couple灰度图像');
figure;stem(histgram(),'.'); 
axis tight;
colormap(gray)
figure;imhist(Image);
axis tight;
colormap(gray)

Python implementation :

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

import cv2
import matplotlib.pyplot as plt
import numpy as np

# 读入图像并转换为灰度图
Image = cv2.imread('couple.bmp')
Image = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)

# 初始化直方图
histogram = np.zeros(256)

# 统计每个像素值的数量
h, w = Image.shape[:2]
for x in range(w):
    for y in range(h):
        histogram[Image[y, x]] += 1

# 保存图像
cv2.imwrite('Gcouple.bmp', Image)

# 显示图像和直方图
plt.subplot(121)
plt.imshow(Image, cmap='gray')
plt.title('couple灰度图像')

plt.subplot(122)
plt.stem(histogram, use_line_collection=True)
plt.axis('tight')
plt.title('灰度直方图')
plt.show()

(3) Nature

Gray histogram properties :

  • Histograms are not spatially specific. The histogram cannot reflect the spatial position information of image pixels
  • The histogram reflects the general description of the image
  • An image uniquely corresponds to the corresponding histogram, and different images can have the same histogram
  • If an image can be divided into multiple sub-regions, the sum of the histograms of the multiple sub-regions is equal to the corresponding full-image histogram

In general, different histograms cause different visual effects, and the same histogram can correspond to different images

insert image description here

insert image description here

Two: Histogram correction method theory

The theory of histogram correction method : the gray dynamic range of the image is too small or its histogram is concentrated in a certain gray interval , and the visual effect is not ideal; when the image histogram occupies all the gray intervals , and all gray intervals The probability distribution is close, that is, the image whose histogram is evenly distributed will have the most ideal visual effect. On the other hand, from the perspective of random signals and information volume, when the probability distribution of each gray level is equal, the information volume is the largest and the entropy is the largest . Therefore, it is necessary to find this transformation to make the transformed image histogram uniform, which is histogram equalization

Let 0 ≤ r ≤ 1 0\leq r \leq 10r1 is to enhance the pixel gray level,0 ≤ s ≤ 1 0 \leq s \leq 10s1 is the new gray level after enhancement, the histogram correction formula iss = T ( r ) s=T(r)s=T ( r ) (orr = T − 1 ( s ) r=T^{-1}(s)r=T1 (s)), whereT ( r ) T(r)T ( r ) is a transformation function, which needs to meet the following two conditions

  • T ( r ) T(r) T(r) 0 ≤ r ≤ 1 0\leq r \leq 1 0rSingle increase in 1 area to ensure that the order of gray levels from black to white does not appear reversed
  • T ( r ) T(r)T(r) 0 ≤ r ≤ 1 0\leq r \leq 1 0r1 area satisfies0 ≤ s ≤ 1 0\leq s \leq 10s1 , to ensure that the transformed pixels are still within the allowed gray level range

Three: Histogram equalization

Histogram equalization : also known as histogram equalization. Its purpose is to make the relative frequency (probability) of all gray levels appear the same , and the image contains the largest amount of information at this time. That is, the gray histogram of the original image is corrected to a uniformly distributed histogram, and the global overall homogenization of the image is realized.

(1) Solution of histogram equalization transformation function T®

Let pr ( r ) p_{r}(r)pr( r ) andps ( s ) p_{s}(s)ps( s ) represent rrrespectivelyr andssThe gray probability density function of s , then before and after the transformation, there are

p s ( s ) d s = p r ( r ) d r ∫ 0 s p s ( s ) d s = ∫ 0 r p r ( r ) d r \begin{array}{l}p_{s}(s) d s=p_{r}(r) d r \\\int_{0}^{s} p_{s}(s) d s=\int_{0}^{r} p_{r}(r) d r\end{array} ps(s)ds=pr( r ) d r0sps(s)ds=0rpr( r ) d r

Because after equalization, ps ( s ) = 1 p_{s}(s)=1ps(s)=1 , so the histogram equalization change function iss = T ( r ) = ∫ 0 rpr ( ω ) d ω s=T(r)=\int_{0}^{r} p_{r}(\omega) d \omegas=T(r)=0rpr( ω ) d ω

(2) Histogram equalization of digital images

A: Overview

Histogram equalization of digital images : a discrete digital image, a total of LLL gray levels, where thekkthk gray levelsrk r_{k}rkThe number of pixels that appear is nk n_{k}nk, the total number of pixels in the image is NNN. _ Then thekkthThe probability of occurrence of k gray levels is

P ( r k ) = n k N 0 ≤ r k ≤ 1 , k = 0 , 1 , … , L − 1 P\left(r_{k}\right)=\frac{n_{k}}{N} \quad 0 \leq r_{k} \leq 1, k=0,1, \ldots, L-1 P(rk)=Nnk0rk1,k=0,1,,L1

Transformation function T ( r ) T(r) for equalizationT ( r ) is

  • r k = T − 1 ( s k ) r_{k}=T^{-1}(s_{k}) rk=T1(sk)

s k = T ( r k ) = ∑ j = 0 k p r ( r j ) = ∑ j = 0 k n j N s_{k}=\boldsymbol{T}\left(r_{k}\right)=\sum_{j=0}^{k} p_{r}\left(r_{j}\right)=\sum_{j=0}^{k} \frac{n_{j}}{N} sk=T(rk)=j=0kpr(rj)=j=0kNnj

The steps of the histogram equalization algorithm for digital images are as follows:

  • Statistical raw image histogram
  • Calculate the new gray level: sk = T ( rk ) = ∑ j = 0 kpr ( rj ) = ∑ j = 0 knj N s_{k}=\boldsymbol{T}\left(r_{k}\right)= \sum_{j=0}^{k} p_{r}\left(r_{j}\right)=\sum_{j=0}^{k} \frac{n_{j}}{N}sk=T(rk)=j=0kpr(rj)=j=0kNnj
  • fix sk s_{k}skfor a reasonable grayscale
  • find new histogram
  • Replace the grayscale before processing with the new grayscale after processing to generate a new image

B: Example

As follows, given a 64×64 8-level grayscale image, its grayscale distribution is shown in the table, and its histogram equalization is performed on it

insert image description here

First calculate the new gray level

  • s 0 = T ( r 0 ) = ∑ j = 0 0 p r ( r j ) = P r ( r 0 ) = 0.19 s_{0}=T\left(r_{0}\right)=\sum_{j=0}^{0} p_{r}\left(r_{j}\right)=P_{r}\left(r_{0}\right)=0.19 s0=T(r0)=j=00pr(rj)=Pr(r0)=0.19
  • s 1 = T ( r 1 ) = ∑ j = 0 1 P r ( r j ) = P r ( r 0 ) + P r ( r 1 ) = 0.19 + 0.25 = 0.44 s_{1}=T\left(r_{1}\right)=\sum_{j=0}^{1} P_{r}\left(r_{j}\right)=P_{r}\left(r_{0}\right)+P_{r}\left(r_{1}\right)=0.19+0.25=0.44 s1=T(r1)=j=01Pr(rj)=Pr(r0)+Pr(r1)=0.19+0.25=0.44
  • s 2 = 0.65 s_{2}=0.65 s2=0.65
  • s 3 = 0.81 s_{3}=0.81 s3=0.81
  • s 4 = 0.89 s_{4}=0.89 s4=0.89
  • s 5 = 0.95 s_{5}=0.95 s5=0.95
  • s 6 = 0.98 s_{6}=0.98 s6=0.98
  • s 7 = 1 s_{7}=1 s7=1

Then correct sk s_{k}skFor a reasonable gray level sk ′ s^{\prime}_{k}sk

  • s 0 = 0.19 ≈ 1 7 s_{0}=0.19\approx \frac{1}{7} s0=0.1971
  • s 1 = 0.44 ≈ 3 7 s_{1}=0.44\approx \frac{3}{7} s1=0.4473
  • s 2 = 0659 ≈ 5 7 s_{2}=0659\approx \frac{5}{7} s2=065975
  • s 3 = 0.81 ≈ 6 7 s_{3}=0.81\approx \frac{6}{7} s3=0.8176
  • s 4 = 0.89 ≈ 6 7 s_{4}=0.89\approx \frac{6}{7} s4=0.8976$
  • s 5 = 0.95 ≈ 1 s_{5}=0.95\approx 1 s5=0.951
  • s 6 = 0.98 ≈ 1 s_{6}=0.98\approx 1 s6=0.981
  • s 7 = 1 s_{7}=1 s7=1

Then the new image corresponds to only 5 different gray levels

  • s 0 ′ = 1 7 s_{0}^{\prime}=\frac{1}{7} s0=71
  • s 1 ′ = 3 7 s_{1}^{\prime}=\frac{3}{7} s1=73
  • s 2 ′ = 5 7 s_{2}^{\prime}=\frac{5}{7} s2=75
  • s 3 ′ = 6 7 s_{3}^{\prime}=\frac{6}{7} s3=76
  • s 4 ′ = 1 s_{4}^{\prime}=1 s4=1

Then compute the new histogram

  • P s ( s 0 ′ ) = P s ( 1 7 ) = P r ( r 0 ) = 0.19 P_{s}\left(s_{0}^{\prime}\right)=P_{s}\left(\frac{1}{7}\right)=P_{r}\left(r_{0}\right)=0.19 Ps(s0)=Ps(71)=Pr(r0)=0.19
  • P s ( s 1 ′ ) = P s ( 3 7 ) = P r ( r 1 ) = 0.25 P_{s}\left(s_{1}^{\prime}\right)=P_{s}\left(\frac{3}{7}\right)=P_{r}\left(r_{1}\right)=0.25 Ps(s1)=Ps(73)=Pr(r1)=0.25
  • P s ( s 2 ′ ) = P s ( 5 7 ) = P r ( r 2 ) = 0.21 P_{s}\left(s_{2}^{\prime}\right)=P_{s}\left(\frac{5}{7}\right)=P_{r}\left(r_{2}\right)=0.21 Ps(s2)=Ps(75)=Pr(r2)=0.21
  • P s ( s 3 ′ ) = P s ( 6 7 ) = P r ( r 3 ) + P r ( r 4 ) = 0.16 + 0.08 = 0.24 P_{s}\left(s_{3}^{\prime}\right)=P_{s}\left(\frac{6}{7}\right)=P_{r}\left(r_{3}\right)+P_{r}\left(r_{4}\right)=0.16+0.08=0.24 Ps(s3)=Ps(76)=Pr(r3)+Pr(r4)=0.16+0.08=0.24
  • P s ( s 4 ′ ) = P s ( 1 ) = P r ( r 5 ) + P r ( r 6 ) + P r ( r 7 ) = 0.06 + 0.03 + 0.02 = 0.11 P_{s}\left(s_{4}^{\prime}\right)=P_{s}(1)=P_{r}\left(r_{5}\right)+P_{r}\left(r_{6}\right)+P_{r}\left(r_{7}\right)=0.06+0.03+0.02=0.11 Ps(s4)=Ps(1)=Pr(r5)+Pr(r6)+Pr(r7)=0.06+0.03+0.02=0.11

Finally generate a new image

insert image description here

Comparison of old and new histograms

insert image description here

C: program

insert image description here

MATLAB implementation : the relevant functions are as follows, for detailed explanation, please refer to the MATLAB help manual

  • histeq(I,N)
Image=rgb2gray(imread('couple.bmp'));
histgram =imhist(Image);    %统计图像直方图
[h w]=size(Image);
NewImage1=zeros(h,w);
NewImage2=zeros(h,w);
s=zeros(256);
s(1)=histgram(1);
for t=2:256
    s(t)=s(t-1)+histgram(t);  %计算新的灰度值    
end
for x=1:w
    for y=1:h
        NewImage1(y,x)=s(Image(y,x)+1)/(w*h);                 %生成新图像
   end
end
NewImage2=histeq(Image,256);%调用Matlab函数
imshow(Image);title('couple灰度图像');
figure;imhist(Image);title('couple灰度图像的直方图'); 
axis tight;
figure;imshow(NewImage1);title('全局直方图均衡化处理后图像');
figure;imhist(NewImage1);title('全局直方图均衡化处理后图像的直方图');
axis tight;
figure;imshow(NewImage2);title('Matlab函数全局均衡化处理后图像');
figure;imhist(NewImage2);title('Matlab函数全局均衡化处理后图像的直方图');
axis tight;

Python implementation :

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

# 读取图像
img = cv2.imread('couple.bmp')
# 转换为灰度图像
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 统计图像直方图
histogram = cv2.calcHist([gray_img], [0], None, [256], [0, 256])

# 计算新的灰度值
s = np.zeros((256,))
s[0] = histogram[0]
for t in range(1, 256):
    s[t] = s[t-1] + histogram[t]

# 生成新图像
new_img1 = np.zeros_like(gray_img)
h, w = gray_img.shape[:2]
for y in range(h):
    for x in range(w):
        new_img1[y, x] = s[gray_img[y, x]] / (w * h)

# 调用OpenCV自带的全局均衡化函数
new_img2 = cv2.equalizeHist(gray_img)

# 显示图像和直方图
plt.subplot(2, 2, 1)
plt.imshow(gray_img, cmap='gray')
plt.title('couple灰度图像')

plt.subplot(2, 2, 2)
plt.hist(gray_img.ravel(), 256, [0, 256])
plt.title('couple灰度图像的直方图')
plt.axis('tight')

plt.subplot(2, 2, 3)
plt.imshow(new_img1, cmap='gray')
plt.title('全局直方图均衡化处理后图像')

plt.subplot(2, 2, 4)
plt.hist(new_img1.ravel(), 256, [0, 256])
plt.title('全局直方图均衡化处理后图像的直方图')
plt.axis('tight')

plt.show()

# 显示OpenCV自带函数处理后的图像和直方图
cv2.imshow('Matlab函数全局均衡化处理后图像', new_img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.figure()
plt.hist(new_img2.ravel(), 256, [0, 256])
plt.title('Matlab函数全局均衡化处理后图像的直方图')
plt.axis('tight')
plt.show()

(3) Local histogram equalization

A: Overview

Local histogram equalization : Define the gray level transformation function according to the local histogram statistical characteristics of the region, and perform equalization processing, which is local histogram equalization. Given a digital image, the chosen size is w × hw × hw×The rectangular subblockSS of hS , subblockSSPerform histogram equalization processing in S , with

s k = T ( r k ) = ∑ j = 0 k p S ( r j ) = ∑ j = 0 k n j w × h s_{k}=\boldsymbol{T}\left(r_{k}\right)=\sum_{j=0}^{k} p_{S}\left(r_{j}\right)=\sum_{j=0}^{k} \frac{n_{j}}{w \times h} sk=T(rk)=j=0kpS(rj)=j=0kw×hnj

Can be divided into the following three categories

  • Subblock non-overlapping local histogram equalization :
    • Divide the image into a series of non-overlapping sets of adjacent rectangular sub-blocks { S i ∣ i = 1 , 2 , . . . , num } \{S_{i}|i=1,2,...,num\ }{ Sii=1,2,...,n u m } , independently perform histogram equalization on all pixels in each sub-block and output
    • Due to the large statistical difference in the gray distribution of each divided sub-block, the output image after enhancement processing has obvious block effects
  • Subblock overlapping local histogram equalization :
    • Use the histogram information of the divided sub-blocks to perform histogram equalization processing on the sub-blocks, and then use the value of the center pixel of the equalized sub-block as the output value of the pixel, and move the sub-blocks pixel by pixel in the image , Repeat the above process until all pixels in the image are traversed
    • Algorithms are less efficient
  • Partially overlapped sub-blocks with local histogram equalization :
    • The partition size is w × hw × hw×The sub-block of h performs histogram equalization,moves the sub-block in the image according to a certain horizontal step size wstep and vertical step size hstep, and repeats the above process until all pixels in the image are traversed. Take the average of the results of multiple equalization processes in the overlapping area as the output value of the pixels in the overlapping area
    • The algorithm is frequently used

B: program

insert image description here


MATLAB implementation :

Image=(rgb2gray(imread('couple.bmp')));
imshow(Image);title('原始图像');
result1=blkproc(Image,[32 32],@histeq);
figure,imshow(result1);title('无重叠的局部直方图均衡化图像');
imwrite(result1,'NLHE.bmp');
[height,width]=size(Image);
result2=zeros(height,width);
n=16;%邻域模板半径
hh=height+2*n;  ww=width+2*n;
ff=zeros(hh,ww);%图像对外边缘扩充ff
ff(n+1:hh-n,n+1:ww-n)=Image;
ff(1:n,n+1:ww-n)=Image(1:n,:);
ff(hh-n+1:hh,n+1:ww-n)=Image(height-n+1:height,:);
ff(:,1:n)=ff(:,n+1:n*2);
ff(:,ww-n+1:ww)=ff(:,ww+1-n*2:ww-n);
ff=uint8(ff);
for i=n+1:hh-n
    for j=n+1:ww-n  
        lwc=histeq(ff(i-n:i+n,j-n:j+n),256);
        result2(i-n,j-n)=lwc(n+1,n+1);%实现对子块中心像素点的均衡化处理
    end
end
figure,imshow(uint8(result2));title('重叠的局部直方图均衡化图像');
imwrite(uint8(result2),'LHE.bmp');
sumf=int16(zeros(hh,ww));%%转化成int16型数据
num=zeros(hh,ww);
for i=n+1:8:hh-n
    for j=n+1:8:ww-n 
        lwc=int16(histeq(ff(i-n:i+n,j-n:j+n),256));%计算子块的局部直方图均衡化
        sumf(i-n:i+n,j-n:j+n)=sumf(i-n:i+n,j-n:j+n)+lwc;%像素的均衡化结果进行累加
        num(i-n:i+n,j-n:j+n)=num(i-n:i+n,j-n:j+n)+1;%像素被均衡化的累加次数
    end
end
result3(:,:)=double(sumf(n+1:hh-n,n+1:ww-n));
result3(:,:)=result3(:,:)./num(n+1:hh-n,n+1:ww-n);%像素的均衡化结果取平均值
rr(:,:)=uint8(result3(:,:));
figure,imshow(uint8(result3(:,:)));title('部分重叠的局部直方图均衡化图像');
imwrite(uint8(result3(:,:)),'POSHE.bmp');

Python implementation :

import cv2
import numpy as np
from matplotlib import pyplot as plt

# 读入图像
Image = cv2.imread('couple.bmp')
Image = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)

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

# 无重叠的局部直方图均衡化
result1 = cv2.normalize(cv2.boxFilter(Image, -1, (32, 32)), None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
plt.imshow(result1, cmap='gray')
plt.title('无重叠的局部直方图均衡化图像')
plt.show()
cv2.imwrite('NLHE.bmp', result1)

# 重叠的局部直方图均衡化
n = 16
hh, ww = Image.shape[0] + 2 * n, Image.shape[1] + 2 * n
ff = np.zeros((hh, ww), dtype=np.uint8)
ff[n:hh-n, n:ww-n] = Image
ff[0:n, n:ww-n] = Image[0:n, :]
ff[hh-n:hh, n:ww-n] = Image[Image.shape[0]-n:Image.shape[0], :]
ff[:, 0:n] = ff[:, n:2*n]
ff[:, ww-n:ww] = ff[:, ww-2*n:ww-n]

result2 = np.zeros_like(Image, dtype=np.uint8)
for i in range(n, hh-n):
    for j in range(n, ww-n):
        lwc = cv2.equalizeHist(ff[i-n:i+n+1, j-n:j+n+1])
        result2[i-n, j-n] = lwc[n, n]

plt.imshow(result2, cmap='gray')
plt.title('重叠的局部直方图均衡化图像')
plt.show()
cv2.imwrite('LHE.bmp', result2)

# 部分重叠的局部直方图均衡化
result3 = np.zeros_like(Image, dtype=np.float32)
num = np.zeros((hh, ww), dtype=np.uint8)
for i in range(n, hh-n, 8):
    for j in range(n, ww-n, 8):
        lwc = cv2.equalizeHist(ff[i-n:i+n+1, j-n:j+n+1]).astype(np.float32)
        result3[i-n:i+n+1, j-n:j+n+1] += lwc
        num[i-n:i+n+1, j-n:j+n+1] += 1

result3 /= num[n:hh-n, n:ww-n]
result3 = result3.astype(np.uint8)

plt.imshow(result3, cmap='gray')
plt.title('部分重叠的局部直方图均衡化图像')
plt.show()
cv2.imwrite('POSHE.bmp', result3)

Guess you like

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