📜  clahe opencv - Python (1)

📅  最后修改于: 2023-12-03 15:14:09.683000             🧑  作者: Mango

CLAHE Opencv - Python

Introduction

CLAHE (Contrast Limited Adaptive Histogram Equalization) is a computer algorithm for enhancing the contrast of images. It is similar to the histogram equalization algorithm, but it limits the contrast enhancement in local regions. CLAHE prevents the over-enhancement of noise in regions with low contrast, and it also prevents the under-enhancement of regions with high contrast.

OpenCV provides an implementation of the CLAHE algorithm, which can be used in Python. In this tutorial, we will see how to use the CLAHE algorithm in OpenCV using Python.

Installation

To use the CLAHE algorithm in OpenCV, you first need to install OpenCV on your system. You can install OpenCV using pip, which is the Python package installer. Open a command prompt and run the following command to install OpenCV:

Syntax:

pip install opencv-python
Implementation

Before implementing the CLAHE algorithm in OpenCV, we need to import the necessary libraries. We will also read an image using OpenCV's imread() function.

import cv2
import numpy as np

# Reading the image
img = cv2.imread('input.jpg', cv2.IMREAD_GRAYSCALE)

# Displaying the image
cv2.imshow('Original Image', img)
cv2.waitKey(0)

In the above code, we have used the cv2.imread() function to read our input image. We have also used cv2.imshow() function to display the image on the screen.

Now, we will apply the CLAHE algorithm on the image using OpenCV's createCLAHE() function. This function creates a CLAHE object, which can be used to apply the CLAHE algorithm on the input image.

# Creating a CLAHE object
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))

# Applying CLAHE on the image
img_clahe = clahe.apply(img)

# Displaying the image after CLAHE
cv2.imshow('CLAHE Image', img_clahe)
cv2.waitKey(0)

# Saving the image
cv2.imwrite('output.jpg', img_clahe)

In the above code, we have created a CLAHE object using cv2.createCLAHE() function. We have set the clipLimit parameter to 2.0, which limits the contrast enhancement. We have also set the tileGridSize parameter to (8,8), which defines the size of the tiles used for histogram equalization.

We have then applied the CLAHE algorithm on the input image using the apply() method of the CLAHE object. Finally, we have displayed and saved the CLAHE enhanced image.

Conclusion

In this tutorial, we have seen how to use the CLAHE algorithm in OpenCV using Python. We have learned how to install OpenCV, read an image, create a CLAHE object, apply the CLAHE algorithm on the image, and display and save the enhanced image.