Ccs349 Iva Record - Final
Ccs349 Iva Record - Final
2023-2024
Name .………………………………
Reg.No .………………………………
GOVERNMENT COLLEGE OF ENGINEERING SRIRANGAM
SETHURAPATTI, TRICHY – 620 012.
(Approved by AICTE and Affiliated to Anna University, Chennai)
CERTIFICATE
1. COMPUTATION OF T-PYRAMID OF
AN IMAGE
QUAD TREE REPRESENTATION OF
2.
AN IMAGE USING HOMOGENEOUS
CRITERION OF EQUAL INTENSITY
3 a. GEOMETRIC TRANSFORMATION OF
AN IMAGE - ROTATION
3 b. GEOMETRIC TRANSFORMATION OF
AN IMAGE – CHANGE OF SCALE
3 c. GEOMETRIC TRANSFORMATION OF
AN IMAGE – SKEWING
AFFINE TRANSFORMATION OF AN
3 d.
IMAGE FROM THREE PAIRS OF
CORRESPONDING POINTS
BILINEAR TRANSFORMATION OF
3 e.
AN IMAGE FROM FOUR PAIRS OF
CORRESPONDING POINTS
4. OBJECT DETECTION AND
RECOGNITION
5. MOTION ANALYSIS FROM MOVING
EDGES
6. FACIAL DETECTION AND
RECOGNITION
EVENT DETECTION IN VIDEO
7.
SURVEILLANCE SYSTEM
8. MINI PROJECT
EXP NO :1
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Opencv library as cv2 for image
processing and Matplotlib as plt for displaying images.
Step 2: Load the images in the cv2.imread and store it in the img variable.
Step 3: Create a copy of the loaded image and store it in the variable as layer.
Step 4: Create a loop to generate an image pyramid and iterate four times using a
“for” loop in I in ranging 0 to 3.
Step 5: Inside the loop use plt.subplot function to set up the grid of subplots in a
2 × 2 configuration.
Step 6: Apply the cv2.pyrDown function to the Layer image and this function
reduces the size of the image by half in each dimension.
Step 7: Use the plt.imshow to display the current layer of the subplot.
Step 8: After the loop is complete close all the Opencv windows using the
cv2.destroyAllWindows.
PROGRAM:
import cv2
import numpy as np
# Define function to generate T pyramid
def T_pyramid(image, levels):
pyramid = [image]
for i in range(levels):
image = cv2.pyrDown(image)
pyramid.append(image)
return pyramid
# Load image
image = cv2.imread(r'E:\nature.jpg')
if image is None:
print("Error: Image not found.")
exit()
# Generate T pyramid
pyramid = T_pyramid(image, levels)
# Display T pyramid
for i in range(len(pyramid)):
cv2.imshow(f'Level {i} T Pyramid', pyramid[i])
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:
RESULT:
Thus, the python program to compute the T-pyramid of an image was written and the
output was verified successfully.
EXP NO :2
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Matplotlib for visualization, cv2 for
image processing, NumPy for numerical operations.
Step 2: Load the input image in the imread.
Step 3: Define a split4(image) that splits the input image into 4 equal quadrants.
Step 4: Create a 2 × 2 subplot grid using plt.subplots and display the four
quadrants of the image in the subplot using plt.imshow.
Step 5: Define a function concatenate4(north_west , north_east , south_west ,
south_east) that concatenate the four images into single image.
Step 6: Combine the four images into a single image using the concatenate4
function and display the full image using plt.imshow.
Step 7: Define a function calculate_mean to calculate the mean colour of the image
and calculate the mean value of each four quadrants and store them in the
means variable.
Step 8: Display the mean colour of the image using plt.imshow.
Step 9: Define a function checkEqual(list) to check if all elements in the list are
equal
Step 10: Create a QuadTree class for building the Quadtree structure and create a
Quad tree structure for the input image QuadTree().insert(img).
Step 11: Display the Quadtree image at different levels of details using plt.imshow.
PROGRAM:
img =cv.imread(r'E:\tree.jpg')
plt.imshow(img)
def split4(image):
half_split = np.array_split(image, 2)
res = map(lambda x: np.array_split(x, 2, axis=1), half_split)
return reduce(add, res)
split_img = split4(img)
split_img[0].shape
fig, axs = plt.subplots(2, 2)
axs[0, 0].imshow(split_img[0])
axs[0, 1].imshow(split_img[1])
axs[1, 0].imshow(split_img[2])
axs[1, 1].imshow(split_img[3])
def calculate_mean(img):
return np.mean(img, axis=(0,1))
means = np.array(list(map(lambda x:
calculate_mean(x),split_img))).astype(int).reshape(2,2,3)
print(means)
plt.imshow(means)
plt.show()
def checkEqual(myList):
first=myList[0]
return all((x==first).all() for x in myList)
class QuadTree:
if not checkEqual(img):
split_img = split4(img)
self.final = False
self.north_west = QuadTree().insert(split_img[0], level + 1)
self.north_east = QuadTree().insert(split_img[1], level + 1)
self.south_west = QuadTree().insert(split_img[2], level + 1)
self.south_east = QuadTree().insert(split_img[3], level + 1)
return self
def get_image(self, level):
if(self.final or self.level == level):
return np.tile(self.mean, (self.resolution[0], self.resolution[1], 1))
return
concatenate4(self.north_west.get_image(level),self.north_east.get_image(level),self.so
uth_west.get_image(level),self.south_east.get_image(level))
quadtree = QuadTree().insert(img)
plt.imshow(quadtree.get_image(1))
plt.show()
plt.imshow(quadtree.get_image(3))
plt.show()
plt.imshow(quadtree.get_image(7))
plt.show()
plt.imshow(quadtree.get_image(10))
plt.show()
OUTPUT:
RESULT:
Thus, the python program for Quad tree representation of an image using homogeneous
criterion of equal intensity was written and the output was verified successfully.
EXP.NO:3(a)
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Opencv libraries as cv2 and NumPy
libraries as np in image processing.
Step 2: Load the images in the cv2.imread function to read the image and store it
in the variable img.
Step 3: Extract the number of rows, columns and channels from the loaded image
using img.shape .
Step 4: Use the cv2.getRotationMatrix2D () function to create a rotation matrix
fora 90-degree clockwise rotation.
Step 5: Use the cv2.WrapAffine function to apply the rotation to the input image
and store it in the rotated_img.
Step 6: And then display the both the rotated and original image by using the
cv2.imshow function.
Step 7: Then destroyAllWindows function to closes all the displayed windows.
PROGRAM:
#Create rotation matrix using a function to rotate the image 90 degrees clockwise
matrix_rotated = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 0.6)
OUTPUT:
ROTAED IMAGE
INPUT IMAGE
RESULT:
Thus, the python program to perform geometric transformation of an image i.e. rotation
was written and the output was verified successfully.
EXP.NO:3(b)
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary library such as Opencv library as cv2 and NumPy
as np for changing the scale of the image.
Step 2: Use the cv2.imread function to read the image file and store it in the img.
Step 3: Now, we can use the cv2.resize() function to scale the input image and
we can use the parameters such as fx and fy to specify the horizontal and
vertical scaling factor.
Step 4: And then we can show the scaled image using the variable scaled_img.
Step 5: And then we can use the cv2.destroyAllWoindows to closes all the
displayed windows.
PROGRAM:
OUTPUT:
RESULT:
Thus, the python program to perform geometric transformation of an image i.e. change
of scale was written and the output was verified successfully.
EXP.NO:3(c)
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Opencv libraries as cv2 and
NumPylibraries as np in image processing.
Step 2: Load the images in the cv2.imread function to read the image and
store itin the variable img
Step 3: Retrive the number of rows , columns , and channels from the loaded
imageusing the img.shape
Step 4: Use the cv2.getPerspectiveTransform() function to calculate the
prespective transformation matrix of the image.
Step 5: And then use the im.show function to display the skewed image
with the window name ‘Transformed’.
Step 6: Use cv2.imwrite() to save the transformed image as the current directory.
PROGRAM:
#Extract rows, columns and channels of input image and store in a variable
rows, cols, ch = img.shape
#define two sets of points representing source and destination using numpy array
pts1 = np.float32([[cols*0.45, rows*.95],[cols*.90, rows*.95],[cols*.10, 0],[cols, 0]])
pts2 = np.float32([[cols*0.1, rows],[cols,rows],[0,0],[cols,0]])
OUTPUT:
RESULT:
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Opencv libraries as cv2, NumPy
libraries as np in image processing and pyplot module from the matplotlib.
Step 2: Read the image file using the cv2.imread and store the image in the img
variable.
Step 3: Retrieve the number pf rows, columns and channels of the input image
using the img.shape and store the values in the rows , cols and ch variable.
Step 4: Define the two sets of points pts1 and pts2 using the numpy arrays and
these points are used to define the transformation.
Step 5: Pts1 represents the source points and the pts2 represents the corresponding
destination points.
Step 6: Use the wrap affine function to apply the affine transformation. And use
the plot function is to display the two subplots with input and the output
images.
Step 7: And destroyAllWindows function is used to close all the output windows
which has been opened.
PROGRAM:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(r'E:\tree.jpg')
#Retrieve the number pf rows, columns and channels of the input image and store in a
variable
#define two sets of points representing source and destination using numpy arrays
M = cv2.getAffineTransform(pts1, pts2)
plt.subplot(121)
plt.imshow(img)
plt.title('Input')
plt.subplot(122)
plt.imshow(dst)
plt.title('Output')
plt.show()
cv2.destroyAllWindows()
OUTPUT:
RESULT:
Thus, the python program to perform affine transformation of an image using three
pairs of corresponding points was written and the output was verified successfully.
EXP.NO:3(e)
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Opencv libraries as cv2, NumPy
libraries as np in image processing.
Step 2: Read the image in the imread() function and store it in the img variable.
Step 3: Create a list of 4 pairs of (x,y) coordinates in the points variable and these
points represents the location in the image where bilinear interpolition will
be performed.
Step 4: Create a function called bilinear_interpolation that takes an image and the
(x,y) coordinates as input.
Step 5: And calculate the integer parts of the coordinates as x1 and y1 and their
corresponding images as x2 and y2.
Step 6: Calculate the fractional parts dx and dy by subtracting the integer parts
from the original coordinates.
Step 7: Retrieve the pixel values in the four surroundings points in the image such
as top-left(tf), top-right(tr), bottom-left(bl) and bottom-right(br).
Step 8: For each point in the list draw a red circle at the point on the image using
the circle function and this visually marks the interpolation points with a
red circle.
Step 9: Use the imshow function to display the output image with the interpolated
points marked.
Step 10: The waitKey() and the destroyAllWindows() functions is used to close all
the output images.
PROGRAM:
OUTPUT:
RESULT:
Thus, the python program to perform bilinear transformation of an image using four
pairs of corresponding points was written and the output was verified successfully.
EXP.NO:4
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries such as Opencv for the computer vision and
matplotlib.pyplot for displaying the image.
Step 2: Read the image using imread function and store it in the img variable.
Step 3: Convert the BGR colour image to grayscale image using the cvtcolor and
store it in the gray variable and the Grayscale image are often used for the
object detection because they reduce the complexity of the image.
Step 4: And then converts the BGR image to the RGB format using the cvtcolor
and store it in the rgb variable and this conversion is necessary for the
proper display in the Matplotlib.
Step 5: Load the classifier in the XML file and stores it in the data variable and the
classifier is used for detecting the cat faces in the grayscale image.
Step 6: Calls the detectMultiScale() method on the grayscale image with specified
parameters, including the minimum object size for detection.
Step 7: The detected objects are stored in the found variables as a list of rectangles
where the each rectangle is represented by the four values (x,y) coordinates
of the top-left corner,width and height.
Step 8: Checks if the length of the found list is not equal to 0, indicating the cat
faces were detected.
Step 9: Iterate through the list of detected rectangles and draw green rectangles
around each detected cat face using cv2.rectangle and the coordinates
,width and height are extracted from each rectangle in the found list and
the rectangles are drawn on the RGB image.
Step 10: Using the matplotlib to display the RGB image with detected cat faces and
the green rectangles.
Step 11: And display the image with the detected result using the plt.show function.
PROGRAM:
if amount_found != 0:
OUTPUT:
RESULT:
Thus, the python program for object detection and recognition was written and the
output was verified successfully.
EXP.NO:5
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries cv2 (OpenCV) for computer vision tasks and
numpy for numerical operations.
Step 2: Define a function motion_analysis that takes a sequence of images
(img_seq) as input.
Step 3: Convert each image in the sequence from color to grayscale and store them
in the gray_seq list.
Step 4: Initialize an empty list motion_vect to store the number of moving vectors
between frames.
Step 5: Initialize the prev_frame variable with the first frame from gray_seq. This
will be used as the reference frame.
Step 6: Use cv.goodFeaturesToTrack to find good feature points (corners) in the
prev_frame. These feature points will be tracked from frame to frame. The
prev_pts variable stores the initial set of points.
Step 7: Calculate optical flow using cv.calcOpticalFlowPyrLK between the
prev_frame and the current frame. This function estimates the motion of
the feature points.
Step 8: Filter the points based on the status returned by cv.calcOpticalFlowPyrLK.
Only points with a status of 1 are considered valid, meaning they were
successfully tracked from the previous frame to the current frame.
Step 9: Extract the valid feature points and their corresponding positions in the
current frame as next_pts.
Step 10: Append the count of valid points (number of moving vectors) to the
motion_vect list.
Step 11: Update prev_frame to be the current frame for the next iteration.
Step 12: Return the motion_vect list, which contains the number of moving vectors
for each frame in the input sequence.
Step 13: Update prev_pts with the valid feature points for the next iteration.
Step 14: Load a sequence of images (img_seq) into the list. Each image corresponds
to a frame in the video sequence.
Step 15: Call the motion_analysis function with the img_seq as input to perform
motion analysis.
Step 16: Loop through the motion_vect list and print the number of moving vectors
for each frame.
PROGRAM:
import cv2
import numpy as np
# Calculate the absolute difference between the current and previous frames
diff = cv2.absdiff(next_gray, prev_gray)
# Main function
def main():
# Path to the video file
video_path = "E:\car.mp4" # Replace this with the path to your video file
if __name__ == "__main__":
main()
OUTPUT:
RESULT:
Thus, the python program for motion analysis using moving edges was written and the
output was verified successfully.
EXP.NO:6
DATE:
AIM:
ALGORITHM:
Step 1: Import the necessary libraries deepface for face analysis , cv2 (OpenCV)
for image manipulation matplotlib.pyplot for displaying the image with
annotations.
Step 2: Load an image from a file (in this case, "m1.jpg") using OpenCV and store
it in the img variable.
Step 3: Use the DeepFace library to analyze the face in the loaded image
Step 4: DeepFace.analyze(img, actions=['age', 'gender']) performs face analysis on
the image img and specifies that you want to extract information about age
and gender.
Step 5: Extract the gender and age information from the analysis results. These
results are stored in the res variable, which is a list of dictionaries, and you
access the values using dictionary keys.
Step 6: Retrieve the face region coordinates (x, y, width, and height) from the
analysis results. This information is stored in the face_axis dictionary
within the first dictionary in the res list.
Step 7: Draw a green rectangle around the detected face using OpenCV's
cv.rectangle function. The rectangle coordinates are based on the x, y,
width, and height values obtained from the face_axis dictionary.
Step 8: Convert the image from BGR color space to RGB color space using
OpenCV's cv.cvtColor function. This step is necessary for displaying the
image correctly with Matplotlib.
Step 9: Display the image with the annotated face region using Matplotlib. The
plt.imshow function is used to show the image, and the plt.title function
adds a title with gender and age information.
Step 10: Finally, use plt.show() to display the annotated image in a pop-up window.
PROGRAM:
import cv2
import numpy as np
# Convert it to grayscale
known_face_gray = cv2.cvtColor(known_face, cv2.COLOR_BGR2GRAY)
# Extract the first face detected (assuming only one face is present)
x, y, w, h = known_face_keypoints[0]
known_face_roi = known_face_gray[y:y+h, x:x+w]
# Resize the known face ROI to match the size of the detected face ROI
known_face_resized = cv2.resize(known_face_roi, (w, h))
# Calculate the absolute difference between the known face and the detected face
diff = cv2.absdiff(roi_gray, known_face_resized)
# If the mean squared error is below the threshold, recognize the face
if mse < similarity_threshold:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, 'Recognized', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
0.9, (0, 255, 0), 2)
else:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.putText(frame, 'Unknown', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
0.9, (0, 0, 255), 2)
return frame
while True:
# Read frame from the camera
ret, frame = cap.read()
RESULT:
Thus, the python program for facial detection and recognition was written and the
output was verified successfully.
EXP.NO:7
DATE:
AIM:
ALGORITHM:
PROGRAM:
import cv2
cap = cv2.VideoCapture('E:\people.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()
while True:
if not ret:
break
# Apply background subtraction
fgmask = fgbg.apply(frame)
# Find contours
cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contour)
cv2.imshow('Frame', frame)
break
# Release video capture object and close windows
cap.release()
cv2.destroyAllWindows()
OUTPUT:
RESULT:
Thus, the python program for event detection in video surveillance system was
written and the output was verified successfully.
EX.NO:
DATE
MINI PROJECT
GAMMA CORRECTION
AIM:
To write a python program for the Gamma correction in the given image.
ALGORITHM:
Step 1: Import the necessary libraries such as cv2 for the computer vision and
numpy for the numerical operations.
Step 2: Set the path to the input image as the image_path.
Step 3: Define a function adjust_gamma to perform the gamma correction to the
input image.
Step 4: Read the input image using the cv2 and set the gamma value for the
correction.
Step 5: Apply the gamma correction to the input image and display the original
image and the gamma corrected image using the opencv.
Step 6: Wait for the key and close all the opencv windows.
PROGRAM:
import cv2
import numpy as np
image_path = "E:\lady.jpg"
return gamma_corrected
image = cv2.imread(image_path)
gamma_value = 0.6
cv2.imshow("Original", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:
RESULT:
Thus, the python program for mini project was written and the output was verified
successfully.