Contrast S2
Contrast S2
1. create an image
In [196]: # We will use openCv
# pip3 install opencv-python
In [197]:
# How to read an image
import cv2
import matplotlib.pyplot as plt
import numpy as np
In [198]:
# synthetic image
height, width = 100, 100
In [199]:
In [200]:
In [201]:
# grayed image
g = cv2.imread("grayed.jpg")
plt.axis('off')
plt.imshow(g)
In [203]:
hist = cv2.calcHist([g], [0], None, [256], [0, 256])
plt.axis('off')
plt.imshow(stretched_g)
plt.axis('off')
plt.imshow(image)
# Plotting histograms
plt.figure(figsize=(16, 8))
In [207]: show_hist(image)
In [208]:
# Function to apply linear contrast stretching
def linear_contrast_stretching(image):
# Convert BGR (OpenCV format) to RGB (Matplotlib format)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Compute min and max values
min_val = image.min()
max_val = image.max()
In [215]: linear_contrast_stretching(image)
def histogram_equalization(image):
equalized_image = cv2.equalizeHist(image)
return equalized_image
In [211]:
g = cv2.imread("grayed.jpg",0)
equalized_image = histogram_equalization(g)
show_hist(equalized_image)
# Equalized Image
plt.subplot(1, 2, 2)
plt.imshow(equalized_image, cmap='gray')
plt.title('Histogram Equalized Image')
plt.axis('off')
plt.tight_layout()
plt.show()