[파이썬] scipy 히스토그램 평활화

Histogram equalization is a powerful image processing technique used to enhance the contrast and improve the visual appearance of an image. In this blog post, we will explore how to perform histogram equalization using the Scipy library in Python.

What is Histogram Equalization?

Histogram equalization is a method that redistributes the pixel intensities of an image in order to achieve a more uniform histogram. This helps to improve the overall contrast and enhance the details in the image.

The basic idea behind histogram equalization is to stretch the range of intensity values in the image so that they cover the full dynamic range. This is done by mapping the original intensity values to new values that are spread evenly across the entire range.

Histogram Equalization with Scipy

Python’s Scipy library provides a convenient function called histogram_equalize in the exposure module, which allows us to perform histogram equalization easily. Here’s an example code snippet that demonstrates how to use this function:

import matplotlib.pyplot as plt
from skimage import data
from skimage import exposure

# Load image
image = data.camera()

# Perform histogram equalization
equalized_image = exposure.equalize_hist(image)

# Plot original and equalized images
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original Image')

ax[1].imshow(equalized_image, cmap='gray')
ax[1].set_title('Equalized Image')

for a in ax:
    a.axis('off')

plt.tight_layout()
plt.show()

In this example, we first load the image using the data.camera() function from the skimage module. Then, we use the exposure.equalize_hist() function to perform histogram equalization on the image.

Finally, we plot the original and equalized images side by side using Matplotlib. The cmap='gray' argument is used to display the images in grayscale.

Conclusion

Histogram equalization is a simple yet effective technique for enhancing the contrast and improving the appearance of an image. In this blog post, we learned how to perform histogram equalization using the Scipy library in Python.

Scipy provides an easy-to-use function exposure.equalize_hist() for performing histogram equalization. By applying this technique, we can enhance the details and improve the visual quality of our images.

I hope you found this blog post informative and helpful. Feel free to experiment with different images and explore the benefits of histogram equalization in your own projects!