CV Lab Manual PDF
CV Lab Manual PDF
DATE
TOOLS REQUIRED:
1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python
I
2. Downloading/checking version of pip
Pip is a package manager for Python used to install and manage software packages. It's a
tool that lets us install additional libraries and dependencies not distributed along with the standard
library. It connects to an online repository of public packages, Python Package Index.
To check if pip is already installed on your device, execute the following command on
the command prompt pip -version
I
3. Updating the version of pip
We need to ensure that the pip version that we are using is up-to-date (19.3 is the
minimum supported version for OpenCV) by executing the following command:
pip install --upgrade pip
By default, pip installs Python packages to a system directory. This requires root access and
hence you may not be able to upgrade the version of pip using the above command. To overcome
that problem, we need to execute the following command:
We can directly download and install OpenCV-python by using pip. If you have
previously installed a version of OpenCV, remove it before installation to avoid conflicts using the
command:
To install OpenCV-python, execute the following code via the command prompt:
pip install opencv-python
I
Ensure that you select the correct package for your environment. Installing multiple packages
has several demerits and can cause conflicts. If we have installed multiple different packages in the
same environment, we need to uninstall them all with the command given above and reinstall only
one package.
5. Installing OpenCV-contrib
Contrib modules are additional modules that constantly under development and are often used
alongside the latest releases of OpenCV. Usually, some functions get transferred to and from
OpenCV-Python and OpenCV-contrib-python.
The following command is executed on the command prompt to install OpenCV -
contribpython:
RESULT:
Thus, the installation procedure of openCV on python was studied and installation process done
successfully.
EXP.NO 02 BASIC IMAGE PROCESSING - LOADING IMAGES,
CROPPING, RESIZING, THRESHOLDING, CONTOUR
DATE ANALYSIS, BOLB DETECTION
AIM:
To write a python program to implement the following Basic Image Processing operations
1. Loading images.
2. Cropping.
3. Resizing.
4. Thresholding.
5. Contour analysis.
6. Bolb detection.
TOOLS REQUIRED:
1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python
ALGORITHM:
Step 3: Perform Cropping, Resizing, Thresholding, Contour analysis and Bolb detection over the input image.
PROGRAM:
import cv2
import numpy as np
image = cv2.imread(r'C:\Users\samsh\Downloads\ffa.jpg')
if image is None:
exit()
cv2.imwrite('Input_Image.png', image)
cv2.imwrite('Cropped_Image.jpg', cropped_image)
# Blob detection
params = cv2.SimpleBlobDetector_Params()
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(gray)
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))
cv2.waitKey(0)
cv2.destroyAllWindows()
0utput
Output:
RESULT:
Thus the python program to implement the Drawing Lines,Test circle , Rectangle
and Ellipse is
executed suscessfully.
# showing the image
res= np.hstack (( img , im g2))
# show image input vs output
cv2.imshow('Convolution', res)
cv2.waitKey(O)
OUTPUT:
RESULT:
Thus, the python program to implement the Understanding Color Spaces, Color Space Conversion,
Histogram Equalization, Convolution, Image Smoothing, Gradients and Edge Detection is executed successf
EXP.NO 05 IMAGE FEATURES, IMAGE ALIGNMENT AND IMAGE
TRANSFORMS - FOURIER, HOUGH, EXTRACT ORB IMAGE
FEATURES, FEATURE MATCHING, CLONING, FEATURE
DATE
MATCHING BASED IMAGE ALIGNMENT
AIM:
To write a python program to implement the following Image Features, Image Alignment and Image
Transforms operations.
TOOLS REQUIRED:
1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python
ALGORITHM:
Step 3: Perform Extraction of ORB image features, Feature Matching, Cloning, Feature matching based image
alignment, Fourier Transforms, Hough Transforms over the input image.
PROGRAM:
query_img_bw = cv2.cvtColor(query_img,cv2.COLOR_BGR2GRAY)
train_img_bw = cv2.cvtColor(train_img, cv2.COLOR_BGR2GRAY)
# Clone seamlessly.
output= cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
cv2.imshow(" opencv-normal-clone.jpg", output)
#cv2.imshow("opencv-mixed-clone-example.jpg", mixed_clone) cv2.waitKey(0)
b) Feature Matching, Cloning, Feature matching based image alignment, Fourier Transforms,
Hough Transforms:
# Python program to illustrate HoughLine
# method for line detection
import cv2 import numpy
as np
OUTPUT:
c)
RESULT:
Thus, the python program to implement the Extraction of ORB image features, Feature Matching,
Cloning, feature matching based image alignment, Fourier Transforms and Hough Transforms is executed
successfully.
XP.NO 06
IMAGE SEGMENTATION USING GRAPHCUT I GRABCUT
DATE
To write a python program to implement the Image segmentation process using Graph cut and Grab cut
method.
TOOLS REQUIRED:
1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python
ALGORITHM:
Step 3: Perform Image segmentation process using Graph cut and Grab cut method over the input image.
PROGRAM:
a) GRAPH CUT:
import numpy as np
import matplotlib.pyplot as plt
import maxflow
from skimage import io, color
# Display results
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image, cmap='gray')
axes[0].set_title("Original Grayscale Image")
axes[0].axis('off')
axes[1].imshow(segmentation, cmap='gray')
axes[1].set_title("Segmentation Result")
axes[1].axis('off')
plt.tight_layout()
plt.show()
b) GRABCUT:
import numpy as np
import cv2
image = cv2.imread(r'C:\Users\samsh\Downloads\VK1.jpg')
if image is None:
# Show result
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.title("Segmented Image")
plt.axis('off')
plt.show()
OUTPUT:
GRAPH CUT:
F ig u re 1
RESULT:
Thus, the python program to implement the Image segmentation process using Graph cut and
Grab cut method is executed successfully.
EXP.NO 07
CAMERA CALIBRATION WITH CIRCULAR GRID
DATE
AIM:
To write a python program to implement the concept of camera calibration with circular
grid.
APPARATUS:
1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python
ALGORITHM:
Step 3: Apply the above mentioned concept of camera calibration with circular grid in the live video.
PROGRAM:
import cv2
params = cv2.SimpleBlobDetector_Params()
detector = cv2.SimpleBlobDetector_create(params)
# Video capture from external webcam (index 1 instead of 0)
while cap.isOpened():
keypoints = detector.detect(gray)
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cap.release()
cv2.destroyAllWindow
OUTPUT:
RESULT:
Thus, the python program to implement the concept of camera calibration with circular grid is
executed successfully.
EXP.NO 08
CREATING DEPTH MAP FROM STEREO IMAGES
DATE
AIM:
To write a python program to implement the concept of creating depth map from stereo
images.
APPARATUS:
1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python
ALGORITHM:
Step 3: Apply the above mentioned concept of creating depth map from stereo image to the image.
PROGRAM:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
# Show result
plt.imshow(disparity, 'gray')
plt.title('Disparity Map')
plt.axis('off')
plt.show()
Figure 1
RESULT:
Thus, the python program to implement the concept of creating depth map from stereo images is
executed successfully.
EXP.NO 09
DATE
AIM:
To write a python program to implement the following object detection and tracking operations
APPARATUS:
1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python
ALGORITHM:
Step 3: Apply the above mentioned object detection and tracking operations on the prerecorded video or
live video from the camera.
PROGRAM:
first_point_saved = False
second_point_saved = False
track_window = (x, y, w, h)
can_track = False
# initialize tracker
# Start tracking if
can_track == True:
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
# apply camshift to get the new location
ret, track_window = cv2.CamShift( dst, track_window, term_crit)
# Draw it on image pts
= cv2.boxPoints(ret) pts
= np.int0(pts)
print(ret)
print(track_window)
cv2.imshow('roi', roi)
output= cv2.polylines(frame,[pts],True, 255,2)
else:
output = frame if
first_point_saved:
cv2.circle(output, (x, y), 5, (0, 0, 255), -1)
cv2.destroyWindow
cap.release()
cv2.destroyAllWindows()
OBJECT TRACKING:
import cv2 from Detector
import detect from
KalmanFilter import
KalmanFilter def main():
# Create opencv video capture object
VideoCap = cv2.VideoCapture('randomball.avi')
HiSpeed = 100
= KalmanFilter(0.1, 1, 1, 1, 0.1,0.1)
debugMode=l
# Predict
(x, y) = KP.predict()
# Draw a rectangle as the predicted object position cv2.rectangle(frame, (int(x -
15), int(y - 15)), (int(x + 15), int(y + 15)), (255, 0, 0), 2)
# Update
(xl, yl) = KF.update(centers[0])
# Draw a rectangle as the estimated object position cv2.rectangle(frame, (int(xl
- 15), int(yl - 15)), (int(xl + 15), int(yl + 15)), (0, 0,
255), 2)
cv2.putText(frame, "Estimated Position", (int(xl + 15), int(yl + 10)), 0, 0.5, (0, 0,
255), 2) cv2.putText(frame, "Predicted Position", (int(x + 15), int(y)), 0, 0.5, (255, 0, 0), 2)
cv2.putText(frame, "Measured Position", (int(centers[0][0] + 15), int(centers[0][l]
-
15)), 0, 0.5, (0,191,255), 2)
cv2.imshow('image', frame)
if_name_ "_main_"·
# execute main main()
OBJECT DETECTOR:
def detect(frame,debugMode):
# Convert frame from BGR to GRAY
gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if (debugMode): cv2.imshow('gray',
gray)
centers=[] for c
in contours:
# ref: https://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html
(x, y), radius= cv2.minEnclosingCircle(c)
radius = int(radius)
# S = H*P*H'+R
S = np.dot(self.H, np.dot(self.P, self.H.T)) + self.R
I= np.eye(self.H.shape[l])
OUTPUT:
Thus, the python program to implement the concept of object detection and
tracking operations is executed s