使用直方图均衡化和自适应直方图均衡化分别对图像(Fig2-1)进行增强处理。注:直方图均衡化使用函数cv.equalizeHist( );自适应直方图均衡化使用的函数主要为cv2.createCLAHE()
时间: 2024-06-16 09:06:25 浏览: 205
直方图均衡化和自适应直方图均衡化是两种常用的图像增强方法,可以提高图像的对比度和细节。下面是它们的介绍:
1. 直方图均衡化:
直方图均衡化是一种全局的图像增强方法,通过重新分布图像的像素值来增强图像的对比度。它的原理是将图像的直方图变成一个均匀分布的直方图,从而使得图像中的像素值更加均匀分布。在OpenCV中,可以使用函数cv.equalizeHist()来实现直方图均衡化。
2. 自适应直方图均衡化:
自适应直方图均衡化是一种局部的图像增强方法,它将图像分成多个小区域,并对每个小区域进行直方图均衡化。这样可以避免全局直方图均衡化带来的过度增强或者细节丢失的问题。在OpenCV中,可以使用函数cv2.createCLAHE()来创建一个自适应直方图均衡化器,然后使用该均衡化器对图像进行增强处理。
相关问题
使用给定的夜景图像,分别进行直方图均衡化和自适应直方图均衡化,输出修改后的效果和直方图。代码和效果都用屏幕
好的,以下是使用Python和OpenCV库进行直方图均衡化和自适应直方图均衡化的代码和效果。
首先,我们需要导入必要的库和读入一张夜景图像。
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('night.jpg', 0)
plt.imshow(img, cmap='gray')
plt.show()
```
读入的夜景图像如下所示:

接下来,我们可以使用OpenCV中的`equalizeHist()`函数对图像进行直方图均衡化。
```python
equ = cv2.equalizeHist(img)
plt.imshow(equ, cmap='gray')
plt.show()
```
直方图均衡化后的图像如下所示:

我们还可以使用OpenCV中的`createCLAHE()`函数进行自适应直方图均衡化。这个函数可以自动根据图像的局部区域进行直方图均衡化,从而避免了全局直方图均衡化可能带来的过度增强的问题。
```python
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe_img = clahe.apply(img)
plt.imshow(clahe_img, cmap='gray')
plt.show()
```
自适应直方图均衡化后的图像如下所示:

最后,我们还可以绘制出原始图像、直方图均衡化后的图像以及自适应直方图均衡化后的图像的直方图。
```python
fig = plt.figure(figsize=(10,10))
# 原始图像
plt.subplot(2,2,1)
plt.imshow(img, cmap='gray')
plt.title('Original Image')
# 直方图均衡化后的图像
plt.subplot(2,2,2)
plt.imshow(equ, cmap='gray')
plt.title('Equalized Image')
plt.hist(equ.ravel(),256,[0,256])
# 自适应直方图均衡化后的图像
plt.subplot(2,2,3)
plt.imshow(clahe_img, cmap='gray')
plt.title('CLAHE Image')
plt.hist(clahe_img.ravel(),256,[0,256])
plt.show()
```
绘制出的直方图如下所示:

注意,在进行`plt.hist()`操作时,我们需要将图像的像素值展成一维数组。这可以通过使用`ravel()`函数实现。`256,[0,256]`表示我们希望将像素值划分到256个区间中,并在0到256之间进行划分。
限制自适应直方图均衡化
### CLAHE Implementation and Usage in Image Processing
#### Overview of CLAHE
Contrast Limited Adaptive Histogram Equalization (CLAHE) is an advanced form of histogram equalization that overcomes the limitations associated with traditional methods by applying adaptive adjustments to local regions within images[^1]. This technique enhances contrast while preventing noise amplification.
#### Mathematical Foundation
The core idea behind CLAHE involves dividing the input image into small contextual regions called tiles or blocks. For each tile, a modified histogram equalization process takes place where clipping limits are applied to prevent excessive enhancement of noisy areas. The clipped histograms undergo redistribution before being transformed back into spatial domain through inverse transformation functions[^4].
#### Practical Application Using Python OpenCV Library
To implement CLAHE using popular libraries such as OpenCV in Python:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
def apply_clahe(image_path):
# Read the image file.
img = cv2.imread(image_path, 0)
# Create a CLAHE object.
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
# Apply CLAHE on the original grayscale image.
cl_img = clahe.apply(img)
return cl_img
if __name__ == "__main__":
enhanced_image = apply_clahe('input.jpg')
# Display results side-by-side for comparison purposes.
fig, axs = plt.subplots(nrows=1, ncols=2)
ax = axs.ravel()
ax[0].imshow(cv2.cvtColor(cv2.imread('input.jpg'), cv2.COLOR_BGR2RGB))
ax[0].set_title('Original Image')
ax[1].imshow(enhanced_image, cmap='gray')
ax[1].set_title('Enhanced Image via CLAHE')
plt.show()
```
This script demonstrates how one can easily utilize pre-built tools provided by computer vision frameworks like OpenCV to perform effective image enhancements without delving too deeply into underlying algorithms' complexities[^3].
--related questions--
1. What parameters does `cv2.createCLAHE()` accept besides clip limit?
2. How do different values of grid size affect final output quality when performing CLAHE operations?
3. Can you explain why setting appropriate clip limits helps reduce artifacts during HE processes?
4. In what scenarios might AHE be preferred over global HE but still inferior compared to CLAHE?
5. Are there any alternative approaches similar to CLAHE available today which offer better performance characteristics under certain conditions?
阅读全文
相关推荐














