0% found this document useful (0 votes)
24 views

Exp 8 To 12

The document describes an experiment on morphological operations and segmentation thresholding. It loads grayscale images, performs binary segmentation using thresholding at different values, and displays the original and segmented images. Morphological dilation, erosion, opening and closing are also performed on binary images using different structuring elements.

Uploaded by

Shahanas
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)
24 views

Exp 8 To 12

The document describes an experiment on morphological operations and segmentation thresholding. It loads grayscale images, performs binary segmentation using thresholding at different values, and displays the original and segmented images. Morphological dilation, erosion, opening and closing are also performed on binary images using different structuring elements.

Uploaded by

Shahanas
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/ 17

EXPERIMENT NO:8

Histogram Equalization
Date:06/11/2023

Aim:

1. Plot the histogram of: (a) Dark image (b) Bright image (c) Gray level image
2. Perform the histogram equalization of these images and plot the equalized histogram

In [10]: import cv2


from matplotlib import pyplot as plt

# Reads an input image


img_dark = cv2.imread('C:\\Users\\WT\\Downloads\\R.jpg', 0)
img_bright = cv2.imread('C:\\Users\\WT\\Downloads\\D.jpg', 0)
img_gray = cv2.imread('C:\\Users\\WT\\Downloads\\OIP.jpg', 0)

# Find frequency of pixels in the range 0-255


hist_dark = cv2.calcHist([img_dark], [0], None, [256], [0, 256])
hist_bright = cv2.calcHist([img_bright], [0], None, [256], [0, 256])
hist_gray = cv2.calcHist([img_gray], [0], None, [256], [0, 256])

# Plotting images and histograms


plt.figure(figsize=(11, 8))

plt.subplot(231), plt.imshow(img_dark, cmap='gray')


plt.title('Dark Image')
plt.axis('off')

plt.subplot(234), plt.plot(hist_dark)
plt.title('Histogram of Dark Image')

plt.subplot(232), plt.imshow(img_bright, cmap='gray')


plt.title('Bright Image')
plt.axis('off')

plt.subplot(235), plt.plot(hist_bright)
plt.title('Histogram of Bright Image')

plt.subplot(233), plt.imshow(img_gray, cmap='gray')


plt.title('Gray Image')
plt.axis('off')

plt.subplot(236), plt.plot(hist_gray)
plt.title('Histogram of Gray Image')

plt.show()
Histogram Equalization

In [14]: # Histogram equalization using cv2.equalizeHist()


equ_dark = cv2.equalizeHist(img_dark)
equ_bright = cv2.equalizeHist(img_bright)
equ_gray = cv2.equalizeHist(img_gray)

hist_dark0 = cv2.calcHist([img_dark], [0], None, [256], [0, 256])


hist_bright0 = cv2.calcHist([img_bright], [0], None, [256], [0, 256])
hist_gray0 = cv2.calcHist([img_gray], [0], None, [256], [0, 256])

hist_dark = cv2.calcHist([equ_dark], [0], None, [256], [0, 256])


hist_bright = cv2.calcHist([equ_bright], [0], None, [256], [0, 256])
hist_gray = cv2.calcHist([equ_gray], [0], None, [256], [0, 256])

# Show image input vs output


plt.figure(figsize=(11, 16))

plt.subplot(431), plt.imshow(img_dark, cmap='gray')


plt.title('Dark Image')
plt.axis('off')

plt.subplot(437), plt.imshow(equ_dark, cmap='gray')


plt.title('Equalization of the Dark Image')
plt.axis('off')

plt.subplot(4, 3, 4), plt.plot(hist_dark0)


plt.title('Histogram of Dark Image')

plt.subplot(4, 3, 10), plt.plot(hist_dark)


plt.title('Equalized Histogram of Dark Image')

plt.subplot(432), plt.imshow(img_bright, cmap='gray')


plt.title('Bright Image')
plt.axis('off')
plt.subplot(438), plt.imshow(equ_bright, cmap='gray')
plt.title('Equalization of the Bright Image')
plt.axis('off')

plt.subplot(4, 3, 5), plt.plot(hist_bright0)


plt.title('Histogram of Bright Image')

plt.subplot(4, 3, 11), plt.plot(hist_bright)


plt.title('Equalized Histogram of Bright Image')

plt.subplot(433), plt.imshow(img_gray, cmap='gray')


plt.title('Gray Image')
plt.axis('off')

plt.subplot(439), plt.imshow(equ_gray, cmap='gray')


plt.title('Equalization of the Gray Image')
plt.axis('off')

plt.subplot(4, 3, 6), plt.plot(hist_gray0)


plt.title('Histogram of Gray Image')

plt.subplot(4, 3, 12), plt.plot(hist_gray)


plt.title('Equalized Histogram of Gray Image')

plt.show()
exp9

EXPERIMENT NO:9
KRONECKER PRODUCT OF TWO MATRCES
Aim: Find the kronecker product of two 2*2 matrix

In [5]: #using built-in function


import numpy as np
a = np.array([[2,4],[3,6]])
b = np.array([[1,2],[5,6]])
m=np.kron(a,b)
print("Kronecker Product:\n",m)

Kronecker Product:
[[ 2 4 4 8]
[10 12 20 24]
[ 3 6 6 12]
[15 18 30 36]]

In [8]: #without built-in function


cola = 2
rowa = 2
colb = 2
rowb = 2
def Kroneckerproduct( A , B ):
C = [[0]*(cola * colb) for _ in range(rowa * rowb)]
for i in range(rowa):
for k in range(cola):
for j in range(rowb):
for l in range(0,colb):
C[i * rowb + k][j * colb + l] = A[i][j] * B[k][l];

for i in range(rowa * rowb):


print(*C[i])
A = [[2,4],[3,6]];
B = [[1,2],[5,6]];

Kroneckerproduct( A , B );

2 4 4 8
10 12 20 24
3 6 6 12
15 18 30 36

Result:
Found the kronecker product of two 2*2 matrix

localhost:8888/nbconvert/html/exp10.ipynb?download=false
EXPERIMENT NO:10
EXPERIMENT NO:11
Morphological Operations
Aim:
1. To perform morphological dilation and erosion using different structuring elements.
2. To perform morphological opening and closing on binary images.
EXPERIMENT NO:11
Segmentation Thresholding
Aim:
1. To perform morphological dilation and erosion using different structuring elements.
2. To perform morphological opening and closing on binary images.
In [20]:
import cv2
import matplotlib.pyplot as plt
import numpy as np

img1 = cv2.imread('C:\\Users\\WT\\Downloads\\gray image 2.png', cv2.IMREAD_GRAYSCA


img1 = cv2.resize(img1, (275, 180))

# function to display images with subplot


def show_images(imgs, labels, figsize):
fig = plt.figure(figsize=figsize)
for i in range(len(imgs)):
ax = fig.add_subplot(1, len(imgs), i+1)
ax.set_title(labels[i])
ax.imshow(imgs[i], cmap='gray', vmax=255, vmin=0)
plt.show()

show_images([img1], ["Original Image"], (5, 5))

In [21]: threshold = 100


new_image = []

for row in img1:


temp = []
for val in row:
if val > threshold:
temp.append(val)
else:
temp.append(0)
new_image.append(temp)

show_images([img1, new_image], ["Original Image", "segmented image"], (10, 10))


Binary Segmentation using in-built function:

In [25]: import cv2


import matplotlib.pyplot as plt

img = cv2.imread('C:\\Users\\WT\\Downloads\\gray image 2.png', 0)

def segment(x):
ret, bin_img = cv2.threshold(img, x, 255, cv2.THRESH_BINARY)

plt.figure(figsize=(11, 8))

plt.subplot(121)
plt.imshow(img, cmap="gray")
plt.title('Original Image')
plt.xticks([]), plt.yticks([])

plt.subplot(122)
plt.imshow(bin_img, cmap='gray')
plt.title('Segmented Image')
plt.xticks([]), plt.yticks([])

plt.show()

segment(63)
segment(90)
segment(127)

You might also like