COMPUTER VISION LAB
LIST OF PROGRAMS
1. Perform the image transformations that include the geometric and morphological
transformations.
2. Perform the image enhancement by applying contrast limited adaptive histogram Equalization
3. Perform the Contours and Region based segmentation in images
4. Perform the Wavelet Transforms on image using PyWavelets.
5. Perform the K-Means clustering for Image segmentation using CV2 library.
6. Perform basic motion detection and tracking using python and OpenCV
7. Perform face detection using OpenCV library
8. Perform Foreground Extraction in an image
9. Perform Pedestrian Detection using OpenCV and Python.
PROGRAM – 1
PERFORM THE K-MEANS CLUSTERING FOR IMAGE SEGMENTATION USING CV2
LIBRARY
IN COMMAND PROMPT:
# Check for pip
pip --version
# Install opencv library
pip install opencv-python
IN IDLE (Python):
PROGRAM:
import numpy as np
import cv2
import matplotlib.pyplot as plt
# Read the original image
original_image = cv2.imread("F:/Suhaana Folders/Downloads/image.jpg")
# Convert the image from BGR to RGB
img = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
# Reshape the image to a 2D array of pixels
vectorized = img.reshape((-1, 3))
# Convert to float32 for k-means
vectorized = np.float32(vectorized)
# Define criteria and apply k-means
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 3 # Number of clusters
attempts = 10 # Number of times the algorithm is executed using different initial labellings
ret, label, center = cv2.kmeans(vectorized, K, None, criteria, attempts,
cv2.KMEANS_PP_CENTERS)
# Convert the center values back to uint8
center = np.uint8(center)
# Map the centers to the labels to get the segmented image
res = center[label.flatten()]
result_image = res.reshape((img.shape))
# Define the size of the figures
figure_size = 15
# Detect edges using Canny edge detection
edges = cv2.Canny(img, 150, 200)
# Plot the original and segmented images
plt.figure(figsize=(figure_size, figure_size))
plt.subplot(1, 2, 1), plt.imshow(img)
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(result_image)
plt.title('Segmented Image when K = %i' % K), plt.xticks([]), plt.yticks([])
# Plot the original and edge-detected images
plt.figure(figsize=(figure_size, figure_size))
plt.subplot(1, 2, 1), plt.imshow(img)
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(edges, cmap='gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()
OUTPUT:
Figure 1 – Original Image and Segmented Image
Figure 2 – Original Image and Edge Image
PROGRAM – 2
PERFORM THE WAVELET TRANSFORMS ON IMAGE USING PYWAVELETS
IN COMMAND PROMPT:
# Download the PyWavelets library
pip install PyWavelets
IN IDLE (Python):
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import pywt
import pywt.data
# Load the example image provided by PyWavelets
original = pywt.data.camera()
# Titles for the subplots
titles = ['Approximation', 'Horizontal detail', 'Vertical detail', 'Diagonal detail']
# Perform a 2D discrete wavelet transform
coeffs2 = pywt.dwt2(original, 'bior1.3')
# Extract the approximation and detail coefficients
LL, (LH, HL, HH) = coeffs2
# Create a figure to display the coefficients
fig = plt.figure(figsize=(12, 3))
# Plot the approximation and detail coefficients
for i, a in enumerate([LL, LH, HL, HH]):
ax = fig.add_subplot(1, 4, i+1)
ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
ax.set_title(titles[i], fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
# Adjust layout and display the plots
fig.tight_layout()
plt.show()
OUTPUT:
Figure
PROGRAM – 3
PERFORM THE IMAGE TRANSFORMATIONS THAT INCLUDE THE GEOMETRIC AND
MORPHOLOGICAL TRANSFORMATIONS
IN IDLE (Python):
import cv2
import numpy as np
image = cv2.imread('F:/Suhaana Folders/Downloads/newpic.jpg')
# Display the original image
cv2.imshow('Original Image', image)
cv2.waitKey(0)
resized_image = cv2.resize(image, (300, 300))
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated_image = cv2.warpAffine(image, M, (w, h))
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cropped_image = image[50:200, 50:200]
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
blurred_image = cv2.GaussianBlur(image, (15, 15), 0)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
edges = cv2.Canny(image, 100, 200)
cv2.imshow('Edge Detected Image', edges)
cv2.waitKey(0)
_, binary_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
cv2.imshow('Binary Image', binary_image)
cv2.waitKey(0)
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(binary_image, kernel, iterations = 1)
cv2.imshow('Erosion', erosion)
cv2.waitKey(0)
dilation= cv2.dilate(binary_image, kernel, iterations = 1)
cv2.imshow('Dilation', dilation)
cv2.waitKey(0)
opening = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel)
cv2.imshow('Opening', opening)
cv2.waitKey(0)
closing = cv2.morphologyEx(binary_image, cv2.MORPH_CLOSE, kernel)
cv2.imshow('Closing', closing)
cv2.waitKey(0)
rows, cols = image.shape[:2]
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M_affine = cv2.getAffineTransform(pts1, pts2)
affine_transformed = cv2.warpAffine(image, M_affine, (cols, rows))
cv2.imshow('Affine Transformation', affine_transformed)
cv2.waitKey(0)
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M_perspective = cv2.getPerspectiveTransform(pts1, pts2)
perspective_transformed = cv2.warpPerspective(image, M_perspective, (300, 300))
cv2.imshow('Perspective Transformation', perspective_transformed)
cv2.waitKey(0)
# Close all OpenCV windows
cv2.destroyAllWindows()
OUTPUT:
Original Image
Resized Image
Rotated Image
Cropped Image
Grayscale Image
Blurred Image
Edge Detected Image
Binary Image
Erosion
Dilation
Opening
Closing
Affine Transformation
Perspective Transformation