Ccs349 Image and Video Analysis Lab Manual1 2
Ccs349 Image and Video Analysis Lab Manual1 2
LAB RECORD
Register Number :
Year / Sem :
Department :
Academic Year :
ISO 9001:2015 NABCB
Certified by IRQS QM 006
BONAFIDE CERTIFICATE
ofRecordWork(5Mar
TechnicalKnowledge(
theexperiment(5M
the
Attendance(5Marks)
Performing
Completion
S.NoDate Name of the experiment Faculty
5Marks)
arks)
with
ks)
Date
10
Aim:
This series of images forms a pyramid structure, with the base being the original image
and each higher level being a smaller and lower-resolution version of the image.
Program:
import cv2
defpyramidbuild_t_=pyramid(image,[image] levels):
for i in range(levels):
pyramid.append(image)image=cv2.pyrDown(image)
return pyramid
# Replace 'image_path' with the path to your
image image_path = 'path_to_your_image.jpg'
original_image = cv2.imread(image_path)
# Define the number of levels for the T-
pyramid num_levels = 5
# Build the T-pyramid
t_pyramid = build_t_pyramid(original_image, num_levels)
# Save the T-pyramid levels as images
forcv2.imwrite(f"leveli,imageinenumerate(t_{i}.jpg",_pyramid):image)
lOMoARcPSD|40491420
Output:
Result:
Thus the program that computes the T-pyramid of an image is successfully executed.
lOMoARcPSD|40491420
2.Quad tree representation of an image using the homogeneity criterion of equal intensity
Aim
Initialization: Loads the input image and creates a QuadNode as the root of the quadtree.
build(): Initiates the quadtree construction by calling build_tree() starting from the
root node.
visualize(): Generates an image representation of the quadtree structure for visualization.
draw_quadtree(): Recursively fills in the colors of the image based on the quadtree
structure.
4. Main Script:
Replace Image Path: Replace 'path_to_your_image.jpg' with the path to the desired image.
Create QuadTree Instance: Create an instance of the QuadTree class using the image path.
Build QuadTree: Build the quadtree representation by calling the build() method.
self.thresholdself.image=image=threshold
defself.colorcompute=_averageself.compute_color(self):_average_color()
width, height = self.image.size
r,pixelsg,b==0,list(self.image.getdata())0,0
forr pixel+=pixel[0]inpixels:
bg +=+= pixel[1]pixel[2]
total_pixels = len(pixels)
subsub__heightwidth == widthheight////22
(0,(subsub_width,_height,0, width,sub_width,sub_height),height),
lOMoARcPSD|40491420
self.children[i]sub_image=self.image=QuadNode(sub.crop((x1,_image,y1,x2, self.threshold)y2))
defself.isshould_leaf_split(self):=False
firstpixels_pixel=list(self.image.getdata())=pixels[0]
for pixel in pixels:
if pixelreturn!=Truefirst_pixel:
return False
defif buildself.should_tree(self):_split():
self.split()
forifchildchild:in self.children:
class QuadTree:child.build_tree()
def __init__(self, image_path, threshold=10):
self.thresholdself.image=Image.open(image=threshold_path)
defself.root.buildvisualize(self):_tree()
img.show()img=self.create_image()
def create_image(self):
img = Image.new("RGB", self.image.size)
returnself.drawimg_quadtree(img, self.root)
if child:self.draw_quadtree(img, child)
quadquad__tree.build()tree=QuadTree(image_path)
quad#Visualize_tree.visualize()theQuadTree
lOMoARcPSD|40491420
Output
Result
Thus the program is executed successfully.
lOMoARcPSD|40491420
3.Geometric transformations
Aim:
To understand and implement various geometric transformations such as rotation,
scaling, skewing, affine transformation, and bilinear transformation using Python
with the OpenCV library.
Algorithm:
1. Read the input image.
2. Define the rotation angle.
3. Calculate the rotation matrix using cv2.getRotationMatrix2D().
4. Apply the rotation using cv2.warpAffine() with the rotation matrix.
5. Display the original and rotated images.
Program
importimport numpycv2 as np
height,#Getimagewidthdimensions=image.shape[:2]
angle#Define=30rotation angle
# Apply rotation
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
cv2.destroyAllWindows()cv2.waitKey(0)
lOMoARcPSD|40491420
2. Scaling:
Algorithm:
1. Read the input image.
2. Define scaling factors for x and y axes.
3. Apply scaling using cv2.resize() with the specified scaling factors.
4. Display the original and scaled images.
5. Expected Result:
6. The scaled image should be displayed, resized based on the specified scaling factors.
Programimport: cv2
# Read the image
image = cv2.imread('input_image.jpg')
# Apply scaling
scaled_image = cv2.resize(image, None, fx=scale_x,
fy=scale_y, interpolation=cv2.INTER_LINEAR)
cv2.imshow('Original#Displaytheoriginal Image',andscaledimage)images
lOMoARcPSD|40491420
cv2.destroyAllWindows()cv2.waitKey(0)
3. Skewing:
Algorithm:
1. Read the input image.
2. Define pairs of corresponding points for the original and skewed images.
3. Calculate the skew matrix using cv2.getAffineTransform().
4. Apply skewing using cv2.warpAffine() with the skew matrix.
5. Display the original and skewed images.
Program:
importimport numpycv2 as np
# Apply skewing
skewed_image = cv2.warpAffine(image, skew_matrix,
(image.shape[1], image.shape[0]))
cv2.destroyAllWindows()cv2.waitKey(0)
lOMoARcPSD|40491420
4. Affine Transformation:
Algorithm:
1. Read the input image.
2. Define three pairs of corresponding points for the original and transformed images.
3. Calculate the affine matrix using cv2.getAffineTransform().
4. Apply affine transformation using cv2.warpAffine() with the affine matrix.
5. Display the original and transformed images.
Program
import cv2
import numpy as np
(image.shape[1], image.shape[0]))
5. Bilinear Transformation:
Algorithm:
1. Read the input image.
2. Define four pairs of corresponding points for the original and transformed images.
3. Calculate the perspective transform matrix using cv2.getPerspectiveTransform().
4. Apply bilinear transformation using cv2.warpPerspective() with the perspective matrix.
5. Display the original and transformed images.
6. Expected Result:
7. The transformed image should be displayed based on the provided corresponding
points using bilinear interpolation.
Program:
importimport numpycv2 as np
ptspts__dstsrc == np.array([[50,np.array([[0,0],50],[300,[200,0],50],[0,300],[50,200],[300,[200,300]],200]],dtype=np.float32)dtype=np.float32)
lOMoARcPSD|40491420
Result:
Thus the various geometric transformation is done and executed successfully.
lOMoARcPSD|40491420
Aim:
The aim of this exercise is to implement object detection and recognition using a
pre-trained deep learning model provided by the TensorFlow Object Detection API
Procedure:
Program:
importimport cv2os
importimport tensorflownumpyasnpas tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
PATHPATH__TOTO__LABELSCKPT=MODEL=os.path.join('data',_NAME+'/frozen'mscoco_inference_label__map.pbtxt')graph.pb'
category#Loadthe_indexlabel=map
label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
use_display_name=True)
withod_detectiongraph_def_graph.as=tf.compat.v1.GraphDef()_default():
tf.importod_graph__graphdef.ParseFromString(serialized_def(od_graph_def,name='')_graph)
def#Functiondetect_objects(imagetoperformobject_np):detection
with detection_graph.as_default():
with tf.compat.v1.Session(graph=detection_graph) as sess:
lOMoARcPSD|40491420
ops#Get= handlesdetectionto_graph.getinputand_operations()outputtensors
all_tensor_names = {output.name for op in ops for output in
op.outputs} tensor_dict = {}
for key in ['num_detections', 'detection_boxes',
'detection_scores', 'detection_classes']:
tensor_name = key + ':0'
if tensor_name in all_tensor_n
Output :
Result:
The implementation will result in an object detection system capable of analyzing an
input image and identifying various objects within it. The output will display the
input image with bounding boxes drawn around detected objects, accompanied by
labels specifying the type of each recognized object.
lOMoARcPSD|40491420
5.Motion analysis
Aim:
The aim of this project is to perform motion analysis using frame differencing on an
image sequence. This technique helps identify changes between consecutive frames,
revealing moving edges or objects in the sequence.
Procedure :
1. Setting up the Environment:
• Install OpenCV: Ensure OpenCV is installed to leverage its functionalities
for image processing and video manipulation.
2. Accessing Image Sequence:
• Provide Image Sequence: Have a directory containing a sequence of images
that represent consecutive frames of the video or sequence to be analyzed.
• Read Image Files: Use functions from libraries like os to access and load the
image sequence files.
3. Implementing Frame Differencing:
• Frame Differencing Function: Write a function that computes the absolute
difference between consecutive frames.
• Convert Frames to Grayscale: Convert each frame to grayscale to simplify
the comparison between frames.
• Compute Frame Difference: Use the absolute difference between grayscale frames
to capture the changes between them, emphasizing the moving edges or objects.
4. Motion Analysis:
• Iterate Through Frames: Loop through the sequence of images, applying the
frame differencing function to consecutive frames.
• Display Motion Analysis: Visualize the frame differences obtained from each
pair of consecutive frames using OpenCV.
• Control Interaction: Allow for user interaction, such as displaying frame
differences and providing an option to exit.
5. Observations and Analysis:
• Observe Motion Analysis Results: Analyze the displayed frame
differences to identify moving edges or objects within the sequence.
• Adjustments and Refinement: Make adjustments or implement
additional techniques (e.g., thresholding, morphological operations) to
refine the analysis and enhance the detection of motion in the sequence.
lOMoARcPSD|40491420
Programimport: cv2
import os
image_sequence_path = 'path/to/your/image_sequence_directory'
image_files = sorted(os.listdir(image_sequence_path))
# Display the frame difference (you can modify this part to visualize the result)
lOMoARcPSD|40491420
exit break
prev_frame = current_frame
cv2.destroyAllWindows()
Output:
Result:
Following these steps will enable the implementation of motion analysis using frame
differencing on an image sequence, allowing for the identification and visualization
of moving edges or objects across frames.
lOMoARcPSD|40491420
Procedure:
1. Setting up the Environment:
• Install Required Libraries: Use pip to install OpenCV and Dlib libraries.
2. Accessing Pre-trained Models:
• Download Pre-trained Models: Acquire pre-trained models for face detection,
facial landmark prediction, and face recognition provided by Dlib.
3. Facial Detection and Recognition Code:
• Initialize Components: Initialize the face detector, facial landmarks
predictor, and face recognition model using Dlib.
• Function for Facial Detection and Recognition:
• Convert each frame to grayscale for face detection.
• Detect faces in the grayscale image using Dlib's face detector.
• Extract facial landmarks for each detected face.
• Compute face descriptors using the face recognition model.
• Compare recognized face descriptors with known face descriptors in a
database (not implemented in this code snippet).
Program:
importimport dlibcv2
download#Download_model()Dlibmodels
lOMoARcPSD|40491420
recognition load_dlib_models()
faces#Detect=facefaces_detector(frame)usingOpenCV
shape#Getfacial= landmarks
dlib.shape_predictor("dlib_models/shape_predictor_68_face_landmarks.dat")(frame,
face)
# Recognize faces
face_encoding = face_recognizer.compute_face_descriptor(frame, shape)
namematches="Unknown"=dlib.compare_faces(known_face_encodings, face_encoding)
if True in matches:
namematch=_indexknown=_matchesface_names[match.index(True)_index]
cv2.imshow('Video', frame)
Output :
Result:
Upon running the program, the system will open a video stream capturing frames
from the webcam or a video file. It will then perform facial detection and
recognition in real-time on each frame.
lOMoARcPSD|40491420
Aim:
The aim of this program is to detect motion in a video surveillance system. It will
identify and highlight areas where significant motion occurs within the video feed.
Procedure :
1. Setting up the Environment:
• Install Required Libraries: Use pip to install OpenCV, a powerful library for
computer vision tasks.
2. Accessing Video Stream:
• Open Video Stream: Use OpenCV to access the video stream from a camera
or a video file.
3. Motion Detection Algorithm:
• Background Subtraction: Apply background subtraction techniques (e.g.,
cv2.createBackgroundSubtractorMOG2) to extract the moving foreground
from the static background.
• Thresholding and Contour Detection: Use thresholding to create a binary image
highlighting motion. Detect contours to identify distinct areas of motion.
• Bounding Box or Highlight: Draw bounding boxes or highlight the areas
where motion is detected in the frames.
4. Event Detection and Visualization:
• Iterate Through Frames: Process each frame of the video stream.
• Detect Motion: Apply the motion detection algorithm to identify areas of
motion in each frame.
• Display Motion Areas: Show the processed frames with highlighted motion
areas or bounding boxes.
lOMoARcPSD|40491420
Program :
import cv2
background#InitializeBackground_subtractor =Subtractorcv2.createBackgroundSubtractorMOG2()(Changeparametersbasedonrequirements)
while True:
ifret,notframeret: = cap.read()
break
fg_mask = background_subtractor.apply(frame)
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Output:
Result:
The program will process the video stream, highlighting areas where motion is
detected in real-time or while analyzing a pre-recorded video. It will display the
video feed with the identified motion regions or bounding boxes.