Python image enhancement histogram equalization (global histogram equalization, local histogram equalization)

 Image enhancement overview

Image enhancement is to purposefully emphasize the overall or local characteristics of the image, make the original unclear image clear or emphasize certain features of interest, expand the differences between the features of different objects in the image, suppress uninteresting features, and make the image more interesting . It improves image quality, enriches information, enhances image interpretation and recognition effects, and meets the needs of certain special analyses.

Image enhancement is usually divided into categories as shown in the figure

Histogram Equalization Overview

   Histogram equalization is a method in the field of image processing that uses image histograms to adjust contrast. This way the brightness is better distributed on the histogram. This can be used to enhance local contrast without affecting the overall contrast. Histogram equalization achieves this function by effectively expanding the commonly used brightness.

  • Global histogram equalization

 The main advantages of this method are simple algorithm, fast speed, and automatic imaging. The disadvantages are that it is sensitive to noise, easy to lose detailed information, and causes over-enhancement problems in some result areas.

  • local histogram equalization

   The advantage of this method is local adaptation, which can maximize image details; the disadvantage is that it is difficult to control the enhanced image quality and will introduce noise.

Code

Global histogram equalization

#encoding:utf-8
import cv2  
import numpy as np  
import matplotlib.pyplot as plt
 
#读取图片
img = cv2.imread('1.bmp',cv2.IMREAD_GRAYSCALE)

#灰度转换
gray = img #cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
 
#直方图均衡化处理
result = cv2.equalizeHist(gray)

#显示图像
fig = plt.figure(figsize=(10, 10))#设置大小
plt.subplot(221)
plt.imshow(gray, cmap=plt.cm.gray, vmin=0, vmax=255), plt.axis("off"), plt.title('(a)') 
plt.subplot(222)
plt.imshow(result, cmap=plt.cm.gray, vmin=0, vmax=255), plt.axis("off"), plt.title('(b)') 
plt.subplot(223)
plt.hist(img.ravel(), 256), plt.title('(c)') 
plt.subplot(224)
plt.hist(result.ravel(), 256), plt.title('(d)') 
plt.show()
fig.savefig('fig-equal.jpg',bbox_inches='tight')

local histogram equalization

#encoding:utf-8
import cv2  
import numpy as np  
import matplotlib.pyplot as plt
 
#读取图片
img = cv2.imread('E:/python/CSDN/image/image_process/1.bmp',cv2.IMREAD_GRAYSCALE)

#灰度转换
gray = img #cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
#局部直方图均衡化处理
clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(10,10))

#将灰度图像和局部直方图相关联, 把直方图均衡化应用到灰度图 
result = clahe.apply(gray)

#显示图像
fig = plt.figure(figsize=(10, 10))#设置大小
plt.subplot(221)
plt.imshow(gray, cmap=plt.cm.gray, vmin=0, vmax=255), plt.axis("off"), plt.title('(a)') 
plt.subplot(222)
plt.imshow(result, cmap=plt.cm.gray, vmin=0, vmax=255), plt.axis("off"), plt.title('(b)') 
plt.subplot(223)
plt.hist(img.ravel(), 256), plt.title('(c)') 
plt.subplot(224)
plt.hist(result.ravel(), 256), plt.title('(d)') 
plt.show()
fig.savefig('E:/python/CSDN/image/image_process/fig-clahe.jpg',bbox_inches='tight')

Show results

Global histogram equalization

 

local histogram equalization

 

CSDN Topic Challenge Issue 2 Entry
Topic: Study Notes

Guess you like

Origin blog.csdn.net/L888666Q/article/details/127208352