Home > Backend Development > Python Tutorial > How to perform histogram equalization on images using Python

How to perform histogram equalization on images using Python

WBOY
Release: 2023-08-26 21:40:46
Original
1366 people have browsed it

How to perform histogram equalization on images using Python

How to use Python to perform histogram equalization on images

Introduction:
Histogram equalization is a common image enhancement method. The pixel values ​​are adjusted to make the grayscale distribution of the image more uniform, thus enhancing the contrast of the image. In this article, we will learn how to implement histogram equalization of images using the OpenCV library in Python.

1. Import the necessary libraries
First, we need to import some necessary libraries in Python: numpy is used to process arrays, matplotlib is used to display images, and cv2 is used for image processing.

import numpy as np
import cv2
from matplotlib import pyplot as plt
Copy after login

2. Load image
We use the cv2.imread() function to load an image. This function accepts the path to the image file as a parameter and returns an array representing the image.

# 加载图像
img = cv2.imread('image.jpg', 0)
Copy after login

3. Calculate the histogram
We use the cv2.calcHist() function to calculate the histogram of the image. This function accepts an image array, channel index, mask, histogram size, and histogram range as arguments and returns an array representing the histogram of the image.

# 计算直方图
hist = cv2.calcHist([img],[0],None,[256],[0,256])
Copy after login

4. Draw the original image and histogram
Use the plt.subplot() function in the matplotlib library to draw the original image and histogram respectively.

# 绘制原始图像和直方图
plt.subplot(121), plt.imshow(img, 'gray')
plt.subplot(122), plt.plot(hist)
Copy after login

5. Perform histogram equalization
Use the cv2.equalizeHist() function to perform histogram equalization on the image. This function accepts an image array as a parameter and returns an image array after histogram equalization.

# 进行直方图均衡化
equ = cv2.equalizeHist(img)
Copy after login

6. Draw the equalized image and histogram
Also use the plt.subplot() function to draw the equalized image and histogram respectively.

# 绘制均衡化后的图像和直方图
plt.subplot(121), plt.imshow(equ, 'gray')
plt.subplot(122), plt.plot(hist)
Copy after login

7. Display results
Use the plt.show() function to display all drawn images.

# 显示图像
plt.show()
Copy after login

8. Complete code example

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

# 加载图像
img = cv2.imread('image.jpg', 0)

# 计算直方图
hist = cv2.calcHist([img],[0],None,[256],[0,256])

# 绘制原始图像和直方图
plt.subplot(121), plt.imshow(img, 'gray')
plt.subplot(122), plt.plot(hist)

# 进行直方图均衡化
equ = cv2.equalizeHist(img)

# 绘制均衡化后的图像和直方图
plt.subplot(121), plt.imshow(equ, 'gray')
plt.subplot(122), plt.plot(hist)

# 显示图像
plt.show()
Copy after login

Conclusion:
By using the OpenCV library in Python, we can easily perform histogram equalization on images. Histogram equalization is a simple and effective method that can enhance the contrast of an image and make it clearer and more vivid. I hope this article can help readers who are interested in image processing.

The above is the detailed content of How to perform histogram equalization on images using Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template