Iva Lab Manual
Iva Lab Manual
O
.
•/ S”
ESTD. 2001
LAB MANU
PREPARED BY
B.Gunasundari,
Assistant Professor /
CSE
CCS349 IMAGE AND VIDEO ANALYTICS LAB
Exp.no. 1
T-pyramid of an image
Aim:
To Write a program that computes the T-pyramid of an image.
Algorithm:
1. Import the required libraries, OpenCV (cv2) and matplotlib.
2. Load an image "img l .jpg" from the specified file path.
3. Initialize a variable layer as a copy of the loaded image. This ccpy ’11 be repeatedly
downsampled to create the pyramid levels.
4. Iterate through a loop four times (from i - 0 to 3).
5. Inside the loop:
• Create a subplot in a 2x2 grid (total of 4 subplotsJ.
• Downsample the layer using cv2.pyrDown(). cooperation reduces the
image size by half, creating a new level of t yramid.
• Display the downsampled image using p ow() within the current
subplot. oN
6. After displaying all four levels of the ima • amid, the code attempts to display
each level individually using cv2.imsh ith a window title based on the current
loop index i. However, there is a mist n the code where the window title is not
updated correctly, and it always d’ s "str(i)" as the title.
7. Finally, the code calls cv2.wai 0) to keep the OpenCV windows open until a key
is pressed, and then it closed
penCV windows with cv2.destroyAllWindows().
Program:
import cv2
import md lib.pyplot as plt
layer = img.copy()
for i in range(4):
plt.subplot(2, 2, i + 1)
layer =
cv2.pyrDown(layer)
plt.imshow(layer)
cv2.imshow("str(i)",
layer) cv2.waitKey(0)
cv2.destroyAl1Windows()
Result:
Thus the prog hat computes the T-pyramid of an image is executed successfully.
Exp. No.2
QUAD TREE
Aim:
To Write a program that derives the quad tree representation of an image using the
homogeneity criterion of equal intensity
Algorithm:
1. Define a Quadtree Node structure to represent each node in the quadtree. Each node
should contain the following information:
• Position (x, y): The top-left corner of the node within the image.
• Size: The width and height of the node.
• Color: The dominant color of the node.
• Children: An array or a dictionary to store child nodes.
• Termination Condition: A condition that determines when to stop subdividing.
2. Initialize the quadtree by creating the root node, which represents the entire image.
3. Define the termination condition, which could be based on a threshold for color
similarity, a maximum depth, or any other criterion. If the termination condition is met,
mark the current node as a leaf node.
4. If the termination condition is not met, subdivide the current node into four quadrants,
each representing a subregion of the image:
• Divide the current node's size by 2.
• Create four child nodes, one for each quadrant.
Determine the dominant color for each quadrant.
Recursively apply the quadtree algorithm to each child not“
5. Repeat t nation condition is
met for
Program:
import numpy as np
import cv2
from PIL import Image, ImageDraw
MAX DEPTH = S
DETAIL THRESHOLD =
SIZE MULT = 1
def average_colour(i
4 convert image array
image_arr = n y(image)
def weighted_average(hist):
total = sum(hist)
error = value = 0
if total > 0:
value = sum(i * x for i, x in enumerate(hist)) / total
error = sum(x * (value - i) ** 2 for i, x in enumerate(hist)) / total
error = error ** 0.5
return error
def get_detail(hist):
red_detail = weighted_average(hist[:256])
green_detail = weighted_average(hist[25fi:512])
blue_detail = weighted_average(hist[512:76S])
return detail_intensity
class Quadrant():
def init (self, image, bbox, depth):
se11\bbox = bbox
self.depth = depth
self.children = None
self.leaf‘ = False
self.detail = get_detail(hist
self.colour = average_c image)
# start
compression
self.start(image)
dren in . ildren:
self.bu 1 1 image)
return image
quandrants = []
return quandrants
def recursive_search(self, tree, quadrant, max_depth, appen ):
# append i1’quadrant is a leaf
i1’quadrant.leaf == True or quadrant.depth == max
append_leaf’(quadrant)
gif[0].save( file_n
ame,
save_a1l=True,
append_images=gi1{1:],
duration=duration, loop=1oop)
if name == ' main '.
#image ath = "./images/eye.jpg"
image ath = "E:\Backup 14.4.23\image\img1.jpg"
# load image
image = Image.open(image ath)
image = image.resize((image.size[0] * SIZE_MULT, image.size[1] * SIZE_MULT))
# create quadtree
quadtree = QuadTree(image)
Result:
Thus the program that derives the quad tree representation of an image using the homogeneity
criterion of equal intensity is executed successfully.
Exp. No.3
GEOMETRIC TRANSFORMATION OF IMAGE
Aim:
To develop programs for the following geometric transforms: (a) Rotation (b) Change
of scale (c) Skewing (d) Affine transform calculated from three pairs of corresponding
points
Algorithm:
(a) Rotation
(c) Skewing
(a) Rotation
rotated_image2 = Original_lmage.transpose(Image.ROTATE_90)
rotated_image3 = Original_lmage.rotate(60)
rotated_image1.show()
rotated_image2.show()
rotated_image3.show()
(b) Change of
# resize image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA1
(c ) Skewing
import numpy as np
from skimage impo
from skimage.color i rgb2gray
from skimage.tr rm import
rotate
image = io.imread('E:\imageoutput.jpg')
grayscale = rgb2gray(image)
angle = determine_skew(grayscale)
rotated = rotate(image, angle, resize=True) * 255
io.imsave('E:\imageoutput1.jpg', rotated.astype(np.uints))
plt.show()
output:
In put Output
0
100 100
200 200
30 300
0
400
400
500
500 0 1DO 300 300 400 5f00 600 D 1D0 200 3D0 400 500 600
Result:
Thus the programs for the geometric trans for Rotation (b) Change o1’ scale (c)
’+K
Skewing (d) Affine transform calculated fr e pairs o1‘corresponding
points is executed successfully.
Exp. No.4
Object Detection and Recognition
Aim:
Algorithm:
1. Import necessary libraries:
• cv2 for OpenCV functions.
• google.colab.patches for displaying images in a Colab notebook.
2. Load and resize an input image:
• Read an image from a file named 'image.jpg'.
• Resize the image to a size of 640x4fi0 pixels.
3. Define the paths to the model and class label tiles:
• weights contains the path to the frozen inference graph file (the pre-trained
model).
• model contains the path to the model configuration file.
• coco names.txt contains the class labels for the COCO dataset.
4. Load the MobileNet SSD model:
• Use cv2.dnn.readNetFromTensorflow to load the model using the provided
weights and model files.
5. Load class labels:
• Read class labels from the 'coco_names.txt’ file and store them in the
class_names list.
6. Create a blob from the input image:
• Prepare the image for inference using cv2.dnn.blobFromImage.
7. Pass the blob through the network:
• Set the blob as input to the network.
• Use net.forward() to obtain the output predictions.
fi. Process the detection results:
• Loop over the detected objects in the output.
• For each detection, check the confidence score (probability“
• If the confidence is below 50%, continue to the next demon.
9. Draw bounding boxes and labels:
• Extract the (x, y) coordinates of the bounding b
• Draw a green rectangle around the detected ’e
• Extract the class ID to identify the objec ' e.
• Draw the object's name and the prob as text above the bounding box.
10. Display the resulting image:
• Use cv2_imshow to display th ge with bounding boxes and labels.
• cv2.waitKey() waits for a Press
Program:
cv2_imshow(image)
cv2.waitKey()
Result:
Thus the program to implObject Detection and Recognition is executed
successfully and outp verified.
Exp. No. S
Algorithm:
import cv2
import numpy as np
cap = cv2.VideoCapture('/content/yolodetection.mp4')
frame_width = int( cap.get(cv2.CAP_PROP_FRAME_WIDTH))
= cv2.VideoWriter_fourcc('X','V','I','D')
il’cv2.waitKey(40) ==
27: break
cv2.destroyAllWindows()
cap.release()
out.release()
Result:
Thus the program for is verified.
i n analysis using moving edges is executes successfully and output
Exp. No.6
import face_recognition as fr
import cv2
import numpy as np
import os
path = "/content/drive/MyDrive/facer/train/"
known_names = []
known_name_encodings = []
images = os.listdir(path)
for _ in images:
image = fr.load_image_file(path +
_) image ath = ath +
encoding = fr.face_encodings(image)[0]
known_name_encodings.append(e g)
known_names.append(os.path.s xt(os.path.basename(image ath))[0].capitalize())
print(known_names)
face_locations = 1r.face_locations(image)
face_encodings = fr.face_encodings(image, face_locations)
if matches[best_match]:
name = known_names[best_match]
cv2_imshow(image)
cv2.imwrite("/content/drive/MyDrive/1acer/output.jpg", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result:
Thus the program for Facial Detection and Recognition is executed successfully and
output is verified.
Exp. no:7
Aim:
Algorithm:
import cv2
import mediapipe as mp
if multiLandMarks:
handPoints = []
for handLms in multiLandMarks:
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
for idx, lm in
enumerate(handLms.landmark): #
print(idx,lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(1m.y * h)
handPoints.append((cx, cy))
upCount = 0
for coordinate in fingerCoordinates:
if handPoints[coordinate[0]][ l] < handPoints[coordinate[l]][1]:
upCount += 1
il’handPoints[thumbCoordinate[0]][0] > handPoints[thumbCoordinate[ l]][0]:
upCount += 1
Output:
Result:
Thus the program to recognize hand gesture is executed successfully and output is
verified.
ADDITIONAL EXPERIMENTS
Exp. No.:S
Aim:
Algorithm:
1. Import the OpenCV library:
• The code starts by importing the OpenCV library.
2. Load an image:
• It loads an image named "penguin.jpg" using cv2.imread assigns it to the
image variable.
3. Apply Canny edge detection:
• The Canny edge detection algorithm is applied tO oaded image using the
cv2.Canny function. The parameters 200 and used as the low and
thresholds, respectively, for edge detection. high
4. Save the resulting image:
• The edge-detected image is saved e name 'edges_Penguins.jpg' using
cv2.imwrite.
5. Display the edge-detected image:
• The code uses cv2.imshow play the edge-detected image.
Program:
import cv2
image cv2.imread(I guin.jpg")
cv2.imwrite('ed enguins.jpg',cv2.Canny(image,200,300))
Output:
Result:
Thus the program for detecting an edges o1’an image is executed suc fully and output
is verified.
Exp. No.9
SMOO3 G .URMxG
Algorithm:
and NumPy:
code starts by importing the OpenCV library as cv2 and the NumPy library
2. Read an image:
• It reads an image from the file path "E:\Backup 14.4.23\image\lab\pen.jpg"
using cv2.imread and stores it in the image variable.
3. Create a kernel for averaging (blur):
• The code defines a 5x5 kernel o1‘ones (all values set to 1) using NumPy.
• The division by 25 is to normalize the kernel so that the sum of the values is 1,
making it an average filter.
4. Apply the filter to the image:
• The cv2.filter2D function is used to apply the filter to the input image. It takes
the source image (image), the depth (ddepth), and the kernel (kernel2) as
parameters. The ddepth o1’ -1 indicates that the output image should have the
same depth as the input image.
5. Display the original and filtered images:
• The code displays both the original image and the filtered (blurred) image using
cv2.imshow.
6. Wait for a key press and close the windows:
• The code waits for a key press with cv2.waitKey().
• It then closes all OpenCV windows using cv2.destroyAllWindows().
Program:
import cv2
import numpy as np # Reading the image
image = cv2.imread("E:\Backup 14.4.23\image\lab\pen.jpg
Original
Result:
Thus the program to apply smoothing and blurring to an image is executed successfully
and output is verified.