COMPUTER VISION JOURNAL
INDEX
Sr. No. Name of the Practical Date Signature
1 Perform Geometric transformations 18/03/2024
2 Perform Image Stitching 20/03/2024
3 Perform Camera Calibration 23/03/2024
Perform the following:
a. Face detection
4 b. Object detection 27/03/2024
c. Pedestrian detection
d. Face recognition
5 Construct 3D model from images 06/04/2024
Implement object detection and
6 19/04/2024
tracking from video
7 Perform Colorization 23/04/2024
Perform Text detection and
8 25/04/2024
recognition
Perform Image matting and
9 29/04/2024
Composting
Rakshit Shetty
PRACTICAL 1
Perform Geometric transformations
A. Translation
Code:
import numpy as np
import cv2 as cv
img = [Link]('[Link]')
assert img is not None, "file could not be read, check with
[Link]()"
rows,cols,_ = [Link]
M = np.float32([[1,0,100],[0,1,50]])
dst = [Link](img,M,(cols,rows))
[Link]('img',dst)
[Link](0)
[Link]()
Output:
Rakshit Shetty
B. Rotation
Code:
import numpy as np
import cv2 as cv
img = [Link]('[Link]')
assert img is not None, "file could not be read, check with
[Link]()"
rows,cols,_ = [Link]
M = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1)
dst = [Link](img,M,(cols,rows))
[Link]('img',dst)
[Link](0)
[Link]()
Output:
Rakshit Shetty
C. Affine Transformation
Code:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = [Link]('[Link]')
assert img is not None, "file could not be read, check with
[Link]()"
rows,cols,ch = [Link]
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = [Link](pts1,pts2)
dst = [Link](img,M,(cols,rows))
[Link](121),[Link](img),[Link]('Input')
[Link](122),[Link](dst),[Link]('Output')
[Link]()
Output:
Rakshit Shetty
D. Perspective Transformation
Code:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = [Link]('[Link]')
assert img is not None, "file could not be read, check with
[Link]()"
rows,cols,ch = [Link]
pts1 = np.float32([[28,31],[182,25],[14,189],[192,191]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = [Link](pts1,pts2)
dst = [Link](img,M,(300,300))
[Link](121),[Link](img),[Link]('Input')
[Link](122),[Link](dst),[Link]('Output')
[Link]()
Output:
Rakshit Shetty
PRACTICAL 2
Perform Image Stitching
Code:
import cv2
from matplotlib import pyplot as plt
# Load images for stitching
image1 = [Link]('[Link]')
image2 = [Link]('[Link]')
image1 = [Link](image1, cv2.COLOR_BGR2RGB)
image2 = [Link](image2, cv2.COLOR_BGR2RGB)
fig, ax = [Link](1,2, figsize=(14,10))
ax[0].imshow(image1)
ax[0].set_title("part-1")
ax[0].axis("off")
ax[1].imshow(image2)
ax[1].set_title("part-2")
ax[1].axis("off")
[Link]()
# Create a Stitcher object
stitcher = cv2.Stitcher_create()
# Stitch images
status, stitched_image = [Link]((image1, image2))
if status == cv2.Stitcher_OK:
# Display the stitched image
[Link](figsize = (14, 10))
[Link](stitched_image)
Rakshit Shetty
[Link]('Stitched image')
[Link]()
elif status == cv2.Stitcher_ERR_NEED_MORE_IMGS:
print('Not enough images for stitching.')
elif status == cv2.Stitcher_ERR_HOMOGRAPHY_EST_FAIL:
print('Homography estimation failed.')
else:
print('Image stitching failed!')
Output:
Rakshit Shetty
PRACTICAL 3
Perform Camera Calibration
Code:
import cv2
import numpy as np
import os
import glob
# Defining the dimensions of checkerboard
CHECKERBOARD = (6,9)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
30, 0.001)
# Creating vector to store vectors of 3D points for each
checkerboard image
objpoints = []
# Creating vector to store vectors of 2D points for each
checkerboard image
imgpoints = []
# Defining the world coordinates for 3D points
objp = [Link]((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3),
np.float32)
objp[0,:,:2] = [Link][0:CHECKERBOARD[0],
0:CHECKERBOARD[1]].[Link](-1, 2)
prev_img_shape = None
# Extracting path of individual image stored in a given directory
images = [Link]('./images/*.jpg')
for fname in images:
img = [Link](fname)
gray = [Link](img,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
# If desired number of corners are found in the image then ret
= true
ret, corners = [Link](gray, CHECKERBOARD,
cv2.CALIB_CB_ADAPTIVE_THRESH+
cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
"""If desired number of corner are detected,we refine the
pixel coordinates and display them on the images of checker
board"""
Rakshit Shetty
if ret == True:
[Link](objp)
# refining pixel coordinates for given 2d points.
corners2 = [Link](gray,corners,(11,11),(-1,-
1),criteria)
[Link](corners2)
# Draw and display the corners
img = [Link](img, CHECKERBOARD,
corners2,ret)
[Link]('img',img)
[Link](0)
[Link]()
h,w = [Link][:2]
"""Performing camera calibration by passing the value of known 3D
points (objpoints) and corresponding pixel coordinates of the
detected corners (imgpoints)"""
ret, mtx, dist, rvecs, tvecs = [Link](objpoints,
imgpoints, [Link][::-1],None,None)
print("Camera matrix : \n")
print(mtx)
print("dist : \n")
print(dist)
print("rvecs : \n")
print(rvecs)
print("tvecs : \n")
print(tvecs)
Rakshit Shetty
Output:
Rakshit Shetty
PRACTICAL 4
Perform the following
A. Face detection
Code:
import cv2
def detect_faces_from_webcam():
#Load the cascade classifier for detecting faces
face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
#Start capturing video from the webcam
cap = [Link](0)
while True:
#Read one frame from the webcam
ret, frame = [Link]()
if not ret:
print("Failed to Grab Frame")
break
#Convert the frame to grayscale - necessary for the face
detection process
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
#Detect the faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor =
1.1, minNeighbors = 4)
#Draw Rectabgle around the faces
for (x,y,w,h) in faces:
[Link](frame, (x,y), (x+w,y+h), (0,0,255), 2)
#Display the resulting Frame
Rakshit Shetty
[Link]('Face Detection', frame)
#Break the loop when user click 'q'
if [Link](1) & 0xFF == ord('q'):
break
#When everything done, release the capture
[Link]()
[Link]()
detect_faces_from_webcam()
Output:
Rakshit Shetty
B. Object detection
Code:
import cv2
image = [Link]('[Link]')
gray = [Link](image, cv2.COLOR_BGR2GRAY)
car_cascade = [Link]('haarcascade_cars.xml')
cars = car_cascade.detectMultiScale(gray, 1.1, 3)
for (x, y, w, h) in cars:
[Link](image, (x, y), (x+w, y+h), (0, 0, 255), 2)
[Link]('Detected Cars', image)
[Link](0)
[Link]()
Output:
Rakshit Shetty
C. Pedestrian detection
Code:
import cv2
cap = [Link]('[Link]')
peds_cascade = [Link]('haarcascade_fullbody.xml')
#Read until video is completed
while True:
ret, frame = [Link]()
if not ret:
print('Failed to Grab frame')
break
#Convert the frame to grayscale
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
#Detect pedestrians in the image
peds = peds_cascade.detectMultiScale(gray, scaleFactor = 1.1)
#To draw a rectangle on each pedestrian
for (x,y,w,h) in peds:
[Link](frame, (x,y), (x+w,y+h), (0,255,0), 2)
#Display result
[Link]('Video', frame)
Rakshit Shetty
#Press Q on keyboard to exit
if [Link](1) & 0xFF == ord('q'):
break
#release the video-capture object
[Link]()
#close all the frames
[Link]()
Output:
Rakshit Shetty
D. Face recognition
Code:
import cv2
def detect_faces_from_webcam():
#Load the cascade classifier for detecting faces
face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
#Start capturing video from the webcam
cap = [Link](0)
while True:
#Read one frame from the webcam
ret, frame = [Link]()
if not ret:
print("Failed to Grab Frame")
break
#Convert the frame to grayscale - necessary for the face
detection process
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
#Detect the faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor =
1.1, minNeighbors = 4)
#Draw Rectabgle around the faces
for (x,y,w,h) in faces:
[Link](frame, (x,y), (x+w,y+h), (0,0,255), 2)
#Display the resulting Frame
[Link]('Face Detection', frame)
#Break the loop when user click 'q'
if [Link](1) & 0xFF == ord('q'):
Rakshit Shetty
break
#When everything done, release the capture
[Link]()
[Link]()
detect_faces_from_webcam()
Output:
Rakshit Shetty
PRACTICAL 5
Construct 3D model from images
Code:
from PIL import Image
import numpy as np
def shift_image(img, depth_img, shift_amount=10):
img = [Link]("RGBA")
data = [Link](img)
depth_img = depth_img.convert("L")
depth_data = [Link](depth_img)
deltas = ((depth_data/255.0)*float(shift_amount)).astype(int)
shifted_data = np.zeros_like(data)
height, width, _ = [Link]
for y, row in enumerate(deltas):
for x, dx in enumerate(row):
if x + dx < width and x + dx >= 0:
shifted_data[y, x + dx] = data[y, x]
shifted_image = [Link](shifted_data.astype(np.uint8))
return shifted_image
img = [Link]("[Link]")
depth_img = [Link]("[Link]")
shifted_img = shift_image(img, depth_img, shift_amount=10)
shifted_img.show()
Output:
Rakshit Shetty
PRACTICAL 6
Implement object detection and tracking from video
Code:
import cv2
cap = [Link]('cars.mp4')
car_cascade = [Link]('haarcascade_cars.xml')
while True:
ret, frame = [Link]()
if not ret:
break
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray,1.1,3)
for (x,y,w,h) in cars:
[Link](frame, (x,y), (x+w,y+h), (0,0,255), 2)
[Link]('Video', frame)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()
Output:
Rakshit Shetty
PRACTICAL 7
Perform Colorization
Code:
import numpy as np
import cv2
from cv2 import dnn
#--------Model file paths--------#
proto_file = 'models_colorization_deploy_v2.prototxt'
model_file = 'colorization_release_v2.caffemodel'
hull_pts = 'pts_in_hull.npy'
img_path = 'bw_image.jpg'
#--------Reading the model params--------#
net = [Link](proto_file,model_file)
kernel = [Link](hull_pts)
#-----Reading and preprocessing image--------#
img = [Link](img_path)
scaled = [Link]("float32") / 255.0
lab_img = [Link](scaled, cv2.COLOR_BGR2LAB)
# add the cluster centers as 1x1 convolutions to the model
class8 = [Link]("class8_ab")
conv8 = [Link]("conv8_313_rh")
pts = [Link]().reshape(2, 313, 1, 1)
[Link](class8).blobs = [[Link]("float32")]
[Link](conv8).blobs = [[Link]([1, 313], 2.606,
dtype="float32")]
# we'll resize the image for the network
resized = [Link](lab_img, (224, 224))
# split the L channel
L = [Link](resized)[0]
# mean subtraction
L -= 50
# predicting the ab channels from the input L channel
Rakshit Shetty
[Link]([Link](L))
ab_channel = [Link]()[0, :, :, :].transpose((1, 2, 0))
# resize the predicted 'ab' volume to the same dimensions as our
# input image
ab_channel = [Link](ab_channel, ([Link][1], [Link][0]))
# Take the L channel from the image
L = [Link](lab_img)[0]
# Join the L channel with predicted ab channel
colorized = [Link]((L[:, :, [Link]], ab_channel),
axis=2)
# Then convert the image from Lab to BGR
colorized = [Link](colorized, cv2.COLOR_LAB2BGR)
colorized = [Link](colorized, 0, 1)
# change the image to 0-255 range and convert it from float32 to
int
colorized = (255 * colorized).astype("uint8")
# Let's resize the images and show them together
img = [Link](img,(640,640))
colorized = [Link](colorized,(640,640))
result = [Link]([img,colorized])
[Link]("Grayscale -> Colour", result)
[Link](0))
Output:
Rakshit Shetty
PRACTICAL 8
Perform Text detection and recognition
Code:
import pytesseract
import numpy as np
from PIL import ImageGrab
import time
import cv2
#Give path to pytesseract with tesseract location
[Link].tesseract_cmd = r'C:\Program
Files\Tesseract-OCR\[Link]'
#Read Image
img = [Link]('[Link]')
#First Way
hImg, wImg, _ = [Link]
boxes = pytesseract.image_to_boxes(img)
for b in [Link]():
print(b)
b = [Link](' ')
print(b)
x,y,w,h = int(b[1]),int(b[2]),int(b[3]),int(b[4])
[Link](img, (x,hImg-y), (w,hImg-h), (50,50,255), 2)
[Link](img, b[0], (x,hImg-y+25),
cv2.FONT_HERSHEY_SIMPLEX, 1, (50,50,255), 2)
[Link]('img',img)
[Link](0)
[Link]()
#Second Way
txt = pytesseract.image_to_string(img)
print(txt)
X = []
[Link](txt)
print(X))
Rakshit Shetty
Output:
Rakshit Shetty
PRACTICAL 9
Perform Image matting and Composting
Code:
import cv2
import [Link] as plt
import numpy as np
I = [Link]('[Link]')
I = [Link](I, cv2.COLOR_BGR2RGB)/255
[Link](I), [Link]('off')
print("[Link]")
[Link]()
alpha_ex = [Link]('matt_alpha.jpg', cv2.IMREAD_GRAYSCALE)/255
[Link](alpha_ex, cmap='gray'), [Link]('off')
print("Matt_Alpha.jpg")
[Link]();
n_rows = [Link][0]
n_cols = [Link][1]
n_pixels = n_rows * n_cols
I = [Link](I, (n_pixels, 3))
I[:, 0] = I[:, 1]
I[:, 2] = I[:, 1]
alpha_ex = [Link](alpha_ex, (n_pixels, 1))
G_B = 1
I = alpha_ex * I + (1 - alpha_ex) * [0, G_B, 0]
I = [Link](I, (n_rows, n_cols, 3))
[Link](I), [Link]('off')
print("Matt_Alpha.jpg")
[Link]();
R_I = I[:, :, 0]
G_I = I[:, :, 1]
B_I = I[:, :, 2]
alpha = (R_I - (G_I - G_B))/G_B
[Link](alpha, cmap='gray'), [Link]('off')
print("Alpha")
[Link]();
alpha = [Link](alpha, (n_pixels, 1))
error = [Link](alpha - alpha_ex)/[Link](alpha_ex)
print(f'Error (alpha): {error:.2e}')
Rakshit Shetty
K = [Link]('[Link]')
K = [Link](K, cv2.COLOR_BGR2RGB)/255
K = K[:n_rows, :n_cols, :]
[Link](K), [Link]('off')
print("\[Link]")
[Link]();
K = [Link](K, (n_pixels, 3))
R_I = [Link](R_I, (n_pixels, 1))
J = [Link](R_I, 3) + (1 - alpha) * K
J = [Link](J, (n_rows, n_cols, 3))
print("Toronto + Matt")
[Link](J), [Link]('off')
[Link]();
Output:
Rakshit Shetty