0% found this document useful (0 votes)
7 views12 pages

Contrast S2

Uploaded by

aya hamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Contrast S2

Uploaded by

aya hamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Section 2 Contrast

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

synthetic_image = np.zeros((height, width))

In [199]:

# Fill the right half with white


synthetic_image[:, width // 2:] = 1 # Set the right half to white

In [200]:

# Display the image


plt.imshow(synthetic_image, cmap='gray', vmin=0, vmax=1)
plt.axis('off')
plt.show()
2. Read image

In [201]:
# grayed image
g = cv2.imread("grayed.jpg")
plt.axis('off')
plt.imshow(g)

Out [201]: <matplotlib.image.AxesImage at 0x1549929a710>


In [213]:
# Increase brightness
gg = np.array([ x*0.3 for x in g])

# Display the image


plt.imshow(gg, cmap='gray', vmin=0, vmax=256)
plt.axis('off')
plt.show()

In [203]:
hist = cv2.calcHist([g], [0], None, [256], [0, 256])

# plot the histogram


plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])

Out [203]: (0.0, 256.0)


In [204]:
# Apply linear contrast stretching
min_val = g.min()
max_val = g.max()
stretched_g = (((g - min_val) / (max_val - min_val)) * 255).astype(n

plt.axis('off')
plt.imshow(stretched_g)

# cv2.calcHist(images, channels, mask, histSize, ranges)

hist_g = cv2.calcHist([stretched_g], [0], None, [256], [0, 256])

# plot the histogram


plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist_g)
plt.xlim([0, 256])

Out [204]: (0.0, 256.0)


In [205]:
# Read an Image
image = cv2.imread("forest.png")

plt.axis('off')
plt.imshow(image)

Out [205]: <matplotlib.image.AxesImage at 0x15499b8b750>


3. Showing histogram for a colored image
In [214]:
# Get histogram for a colored image
def show_hist(image):
# Convert the image from BGR to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Split the image into its three channels


red_channel = image[:, :, 0]
green_channel = image[:, :, 1]
blue_channel = image[:, :, 2]

# Calculate histograms for each channel


red_hist = cv2.calcHist([red_channel], [0], None, [256], [0, 256
green_hist = cv2.calcHist([green_channel], [0], None, [256], [0,
blue_hist = cv2.calcHist([blue_channel], [0], None, [256], [0, 2

# Plotting histograms
plt.figure(figsize=(16, 8))

# Red channel histogram


plt.subplot(3, 1, 1)
plt.plot(red_hist, color='red')
plt.title('Histogram for Red Channel')
plt.xlim([0, 256])
# Green channel histogram
plt.subplot(3, 1, 2)
plt.plot(green_hist, color='green')
plt.title('Histogram for Green Channel')
plt.xlim([0, 256])

# Blue channel histogram


plt.subplot(3, 1, 3)
plt.plot(blue_hist, color='blue')
plt.title('Histogram for Blue Channel')
plt.xlim([0, 256])

# Show the plot


plt.tight_layout()
plt.show()

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()

stretched_image = ((image - min_val) / (max_val - min_val) * 255


show_hist(stretched_image)
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(stretched_image)
plt.axis('off')

In [215]: linear_contrast_stretching(image)

In [210]: # Hist equalization

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)

# Display the original and equalized images


plt.figure(figsize=(12, 6))
show_hist(g)
# Original Image
plt.subplot(1, 2, 1)
plt.imshow(g, cmap='gray')
plt.title('Original Grayscale Image')
plt.axis('off')

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()

<Figure size 1200x600 with 0 Axes>


In [218]:
# Original

hist = cv2.calcHist([g], [0], None, [256], [0, 256])

# plot the histogram


plt.figure(figsize=(6,4))
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])

hist_g = cv2.calcHist([equalized_image], [0], None, [256], [0, 256])

# plot the histogram


plt.figure(figsize=(6,4))
plt.title("Equalized Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist_g)
plt.xlim([0, 256])

Out [218]: (0.0, 256.0)

You might also like