0% found this document useful (0 votes)
27 views37 pages

CV Lab Manual - Harsh Mehta

Uploaded by

masumitb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views37 pages

CV Lab Manual - Harsh Mehta

Uploaded by

masumitb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

SAL COLLEGE OF ENGINEERING

IT DEPARTMENT

COMPUTER VISION (3171614)

Laboratory Manual

Year: 2024-2025

NAME: HRISHIKESH SORATHIA


ENROLLMENT: 211130116064

COMPUTER VISION: 3171614 Page 1 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

CERTIFICATE

SAL COLLEGE OF ENGINEERING

THIS IS TO CERTIFY THAT

Mr. HRISHIKESH SORATHIA Roll No. 211130116064 of 7th Sem in INFORMATION &
TECHNOLOGY department has satisfactorily completed his term work in COMPUTER
VISION with subject code 3171614 for the academic term Winter 2024.

PLACE: AHMEDABAD ENROLLMENT: 211130116064


DATE:

SUBJECT TEACHER HEAD OF DEPARTMENT PRINCIPAL

COMPUTER VISION: 3171614 Page 2 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 1
Aim: Implementing various basic image processing operations in openCV: Reading
image, writing image, conversion of images, and complement of an image.

 READING IMAGE
The function imread loads an image from the specified file and returns it.

Open CV CODE (For Image Reading):


import cv2
from google.colab.patches import cv2_imshow
img=cv2.imread('/content/man.jpeg')
cv2_imshow(img)

OUTPUT:

 WRITING IMAGE
The function imwrite saves the image to the specified file. The image format is
chosen based on the filename extension

Open CV CODE (For Image writing):


import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/man.jpeg')
status = cv2.imwrite('/content/new.jpg',img)
print("Image written to file-system : ",status)

COMPUTER VISION: 3171614 Page 3 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OUTPUT:
Image written to file-system: True

 CONVERSION IMAGE
The function is used for converting RGB image to gray scale image.

Open CV CODE (Conversion of image):


import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/3d-rendering-cartoon-like-boy_23-2150797566.jpg')
grayimg=cv2.imread('/content/3d-rendering-cartoon-like-boy_23-
2150797566.jpg',cv2.IMREAD_GRAYSCALE)
cv2_imshow(img)
cv2_imshow(grayimg)

COMPUTER VISION: 3171614 Page 4 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OUTPUT:

COMPUTER VISION: 3171614 Page 5 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 COMPLEMENT IMAGE
In the complement of a binary image, zeros become ones and ones become zeros. Black
and white are reversed. In the complement of a grayscale or color image, each pixel
value is subtracted from the maximum pixel value supported by the class.

Open CV CODE (For Image Complement):


import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/3d-rendering-cartoon-like-boy_23-2150797566.jpg')
cv2_imshow(img)
compImg=cv2.bitwise_not(img)
cv2_imshow(compImg)

OUTPUT:

OBSERVATION:
This Practical I Learned About How Images Can be open in CV2 and how we can save images
in CV2. As well as I learn about Conversion of images like RGB to BRG and Complement of
image.

COMPUTER VISION: 3171614 Page 6 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 2
Aim: Implement contrast adjustment of an image. Implement Histogram processing and
equalization.

 CONTRAST ADJUSTMENT IMAGE


A contrast adjustment remaps the original pixel intensities or colors into a new range
of intensities or colors using a single mapping function that is applied to all pixels.

Open CV CODE (Image Contrast Adjustment):


import cv2
import numpy as np
img=cv2.imread('/content/ranbir-kapoor.jpg')
img1=img+100
img2=img-100
img3=img*100
res=np.hstack((img,img1,img2,img3))
cv2_imshow(res)

OUTPUT:

 HISTOGRAM
You can consider histogram as a graph or plot, which gives you an overall idea about
the intensity distribution of an image. It is a plot with pixel values (ranging from 0 to
255, not always) in X-axis and corresponding number of pixels in the image on Y-axis.
It is just another way of understanding the image.

Open CV CODE (For Histogram):


import cv2
from google.colab.patches import cv2_imshow
COMPUTER VISION: 3171614 Page 7 of 37
ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

from matplotlib import pyplot as plt


img=cv2.imread('/content/ranbir-kapoor.jpg',0)
cv2_imshow(img)
plt.hist(img.ravel(),256,[0,256])
plt.show()

OUTPUT:

COMPUTER VISION: 3171614 Page 8 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 EQUILIZATION:
Histogram equalization is a basic image processing technique that adjusts the global
contrast of an image by updating the image histogram’s pixel intensity distribution.
Doing so enables areas of low contrast to obtain higher contrast in the output image.

Open CV CODE (For Equalization):


import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/ranbir-kapoor.jpg',0)
equ = cv2.equalizeHist(img)
res = np.hstack((img,equ)) #stacking images side-by-side
cv2.imwrite('res.png',res)
cv2_imshow(res)

COMPUTER VISION: 3171614 Page 9 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OBSERVATION:
In This Practical I Learned About How Image Contrast Adjustment work and I add, remove,
subtract pixels from images, I also get to know about histogram and Contrast limited adaptive
histogram equalization (CLAHE) which is used for improve the visibility level of foggy image
or video.

COMPUTER VISION: 3171614 Page 10 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 3
Aim: Implement Image Filtering (Convolution, Linear Filtering & Non Linear Filtering)

 IMAGE FILTERING
Image filtering is changing the appearance of an image by altering the colors of the
pixels.Increasing the contrast as well as adding a variety of special effects to images are
some of the results of applying filters.

 CONVOLUTION
Convolution is simply the sum of element-wise matrix multiplication between the
kernel and neighborhood that the kernel covers of the input image.

Open CV CODE (Convolution):


import cv2
from google.colab.patches import cv2_imshow
import numpy as np
from matplotlib import pyplot as plt
k=cv2.imread('/content/pankaj.jpeg')
kernal=np.ones((5,5))/9
kernal
dest=cv2.filter2D(k,-1,kernal)
fig,a=plt.subplots(1,figsize=(8,8))
plt.imshow(dest)
OUTPUT:

COMPUTER VISION: 3171614 Page 11 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 LINEAR FILTERING
Linear filtering is the filtering method in which the value of output pixel is linear
combinations of the neighboring input pixels. it can be done with convolution.

1. Sharpening
2. Averaging/Blurring; This is done by convolving the image with a normalized
box filter.It simply takes the average of all the pixels under kernel area and
replaces the central element acwith this average.

Open CV CODE (Sharpening):


import cv2
from google.colab.patches import cv2_imshow
import numpy as np
from matplotlib import pyplot as plt
k=cv2.imread('/content/pankaj.jpeg')
k1=np.array([[0,-1,0],[-1,5,-1],[0,-1,0]]) #sharpening
print(k1)
dest1=cv2.filter2D(k,-1,k1)
fig,a=plt.subplots(1,figsize=(8,8))
plt.imshow(dest1)

OUTPUT:

COMPUTER VISION: 3171614 Page 12 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

Open CV CODE (Blurring/Averaging):


import cv2
from google.colab.patches import cv2_imshow
import numpy as np
from matplotlib import pyplot as plt
img1=cv2.imread('/content/pankaj.jpeg')
blur = cv2.blur(img1,(5,5))
res1 = np.hstack((img1,blur))
cv2_imshow(res1)
OUTPUT:

 NON-LINEAR FILTERING
A non-linear filtering is one that cannot be done with convolution or Fourier
multiplication. A sliding median filter is a simple example of a non-linear filter.

Median Filter: Here, the function cv2.medianBlur() computes the median of all the
pixels under the kernel window and the central pixel is replaced with this median value.
This is highly effective in removing salt-and-pepper noise.

Open CV CODE (Median Filter):


import cv2
from google.colab.patches import cv2_imshow
import numpy as np
from matplotlib import pyplot as plt
img3=cv2.imread('/content/pankaj.jpeg')
median5 = cv2.medianBlur(img3,5)

COMPUTER VISION: 3171614 Page 13 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

median9 = cv2.medianBlur(img3,9) #only odd values 1,3,5,7,9,.....


median15 = cv2.medianBlur(img3,5)
res = np.hstack((img3,median15))
cv2_imshow(res)
OUTPUT:

OBSERVATION:
In This Practical I Learned About Different types of filters and I observed difference after
applying filter to original image. Using filters we can bluer images and we also clear blurred
image using filter.

COMPUTER VISION: 3171614 Page 14 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 4
Aim: Implement Fourier Transformation.

 FOURIER TRANSFORMATION
Fourier Transform is used to analyze the frequency characteristics of various filters. For
images, 2D Discrete Fourier Transform (DFT) is used to find the frequency domain. A
fast algorithm called Fast Fourier Transform (FFT) is used for calculation of DFT.
Details about these can be found in any image processing or signal processing
textbooks.

Open CV CODE (Fourier Transformation):


import cv2
import numpy as np
from matplotlib import pyplot as plt
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/images.jpeg',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
cv2_imshow(f)
print('***************************************')
cv2_imshow(fshift)
magnitude_spectrum = 20*np.log(np.abs(fshift))
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

COMPUTER VISION: 3171614 Page 15 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OBSERVATION:
In this practical lab I learned about fourier Transformation. Also learn about how to
create magnitude spectrum of any image using fourier Transformation and numpy.

COMPUTER VISION: 3171614 Page 16 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 5
Aim: Implement Edge detection. (Prewitt, Sobel, Laplacian, Canny)

 PREWITT EDGE DETECTION


This method is a commonly used edge detector mostly to detect the horizontal and
vertical edges in images. The following are the Prewitt edge detection filters-
Vertical Kernel Horizontal kernel
|1 0 -1 | |1 1 1 |
|1 0 -1 | | 0 0 0|
|1 0 -1 | | -1 -1 -1|

Open CV CODE (Prewitt Edge Detection):


import cv2
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow
import numpy as np
#original image
img=cv2.imread('/content/tiger.jpeg')
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
fig,ax=plt.subplots(1,figsize=(10,6))
print('ORIGINAL IMG')
plt.imshow(img)
#prewitt vertical edge detection
k1=np.array([[1,0,-1],[1,0,-1],[1,0,-1]])
img=cv2.filter2D(img,0,k1)
fix,ax=plt.subplots(1,figsize=(12,8))
print('prewitt vertical edge detection IMG')
plt.imshow(img)
#prewitt horizontal edge detection
k2=np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
img=cv2.filter2D(img,0,k2)
fix,ax=plt.subplots(1,figsize=(12,8))
print('prewitt horizontal edge detection IMG')

COMPUTER VISION: 3171614 Page 17 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

plt.imshow(img)

OUTPUT:

 SOBEL EDGE DETECTION


This uses a filter that gives more emphasis to the centre of the filter. It is one of the
most commonly used edge detectors and helps reduce noise and provides

COMPUTER VISION: 3171614 Page 18 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

differentiating, giving edge response simultaneously. The following are the filters used
in this method
Vertical Kernel Horizontal kernel
|1 0 -1 | |1 2 1|
|2 0 -2 | |0 0 0|
|1 0 -1 | | -1 -2 -1 |

Open CV CODE (Sobel Edge Detection):


import cv2
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow
import numpy as np
img=cv2.imread('/content/tiger.jpeg')
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#sobel vertical edge detection
k3=np.array([[1,0,-1],[2,0,2],[1,0,-1]])
img=cv2.filter2D(img,0,k3)
fix,ax=plt.subplots(1,figsize=(12,8))
print('sobel vertical edge detection IMG')
plt.imshow(img)
#sobel horizontal edge detection
k4=np.array([[1,2,1],[0,0,0],[-1,-2,-1]])
img=cv2.filter2D(img,0,k4)
fix,ax=plt.subplots(1,figsize=(12,8))
print('sobel horizontal edge detection IMG')
plt.imshow(img)

OUTPUT:

COMPUTER VISION: 3171614 Page 19 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 LAPLACIAN EDGE DETECTION


The Laplacian edge detectors vary from the previously discussed edge detectors. This
method uses only one filter (also called a kernel). In a single pass, Laplacian edge
detection performs secondorder derivatives and hence are sensitive to noise. To avoid
this sensitivity to noise, before applying this method, Gaussian smoothing is performed
on the image.

Common Laplacian Edge detection filters


|-1 -1 -1 | |0 -1 0 |
|-1 8 -1 | |-1 4 -1|
|-1 -1 -1 | | 0 -1 0|

Open CV CODE (Laplacian Edge Detection):


#laplasian edge detection 1
k4=np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
img=cv2.filter2D(img,0,k4)
COMPUTER VISION: 3171614 Page 20 of 37
ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

fix,ax=plt.subplots(1,figsize=(12,8))
print('laplacian edge detection IMG 1')
plt.imshow(img)
#laplasian edge detection 2
k4=np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])
img=cv2.filter2D(img,0,k4)
fix,ax=plt.subplots(1,figsize=(12,8))
print('laplacian edge detection IMG 2')
plt.imshow(img)

OUTPUT:

COMPUTER VISION: 3171614 Page 21 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 CANNY EDGE DETECTION


This is the most commonly used highly effective and complex compared to many other
methods. It is a multi-stage algorithm used to detect/identify a wide range of edges. The
following are the various stages of the Canny edge detection algorithm

1. Convert the image to grayscale.


2. Reduce noise – as the edge detection that using derivatives is sensitive to noise,
we reduce it.
3. Calculate the gradient – helps identify the edge intensity and direction.
4. Non-maximum suppression – to thin the edges of the image.
5. Double threshold – to identify the strong, weak and irrelevant pixels in the
images.
6. Hysteresis edge tracking – helps convert the weak pixels into strong ones only
if they have a strong pixel around them.
Open CV CODE (Canny Edge Detection):
#canny egde detection
edges=cv2.Canny(img,100,200)
fix,ax=plt.subplots(1,figsize=(12,8))
print('CANNY IMG')
plt.imshow(edges,cmap='gray')
OUTPUT;

OBSERVATION:
In this practical I learn what is edge detection and the different methods of edge detection like
sobel edge detection, prewitt edge detection, laplacian edge detection and Canny edge
detection. I perform all the methods and observed that the canny edge detection gives clear
detection of edges.
COMPUTER VISION: 3171614 Page 22 of 37
ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 6
Aim: Implement Morphological Operations (Dilation, Erosion, Opening, Closing)

 DILATION
It is just opposite of erosion. Here, a pixel element is '1' if at least one pixel under
the kernel is '1'. So it increases the white region in the image or size of foreground object
increases. Normally, in cases like noise removal, erosion is followed by dilation.
Because, erosion removes white noises, but it also shrinks our object. So we dilate it.
Since noise is gone, they won't come back, but our object area increases. It is also useful
in joining broken parts of an object.

Open CV CODE (Dilation):


import cv2
from google.colab.patches import cv2_imshow
import numpy as np
#delation
img2=cv2.imread('/content/images.jpeg',0)
kernel = np.ones((5,5)) #structuring elemnent 5*5
dilation = cv2.dilate(img2,kernel,iterations = 1)
res=np.hstack((img2,dilation))
cv2_imshow(res)

OUTPUT:

COMPUTER VISION: 3171614 Page 23 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 EROSION
The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of
foreground object (Always try to keep foreground in white). So what it does? The kernel
slides through the image (as in 2D convolution). A pixel in the original image (either 1
or 0) will be considered 1 only if all the pixels under the kernel is 1, otherwise it is eroded
made to zero).

Open CV CODE (Erosion):


#erosion
erosion = cv2.erode(img2,kernel,iterations = 1)
res2=np.hstack((img2,erosion))
cv2_imshow(res2)

OUTPUT:

 OPENING
Opening is just another name of erosion followed by dilation. It is useful in removing
noise, as we explained above. Here we use the function, cv.morphologyEx()

Open CV CODE (Opening):


#opening
op=cv2.imread('/content/opening.jpg',0)
opening = cv2.morphologyEx(op, cv2.MORPH_OPEN, kernel)
res3=np.hstack((op,opening))
cv2_imshow(res3)

COMPUTER VISION: 3171614 Page 24 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OUTPUT:

 CLOSING
Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small
holes inside the foreground objects, or small black points on the object.

Open CV CODE (Closing):


#closing
img3=cv2.imread('/content/closing.jpg',0)
closing = cv2.morphologyEx(img3, cv2.MORPH_CLOSE, kernel)
res4=np.hstack((img3,closing))
cv2_imshow(res4)

OUTPUT:

 GRADIENT
It is the difference between dilation and erosion of an image. The result will look like
the outline of the object.

COMPUTER VISION: 3171614 Page 25 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

Open CV CODE (Gradient):


#gradient
img4=cv2.imread('/content/gradient.jpg',0)
gradient = cv2.morphologyEx(img4, cv2.MORPH_GRADIENT, kernel)
res5=np.hstack((img4,gradient))
cv2_imshow(res5)

OUTPUT:

 TOP HAT
It is the difference between input image and Opening of the image. Below example is
done for a 13x13 kernel

Open CV CODE (Top Hat):


#top hat
ker=np.ones((13,13))
tophat = cv2.morphologyEx(img4, cv2.MORPH_TOPHAT,ker)
res6=np.hstack((img4,tophat))
cv2_imshow(res6)

OUTPUT;

COMPUTER VISION: 3171614 Page 26 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

 BLACK HAT
It is the difference between the closing of the input image and input image.

Open CV CODE (Black Hat):


#black hat
blackhat = cv2.morphologyEx(img4, cv2.MORPH_BLACKHAT, kernel)
res7=np.hstack((img4,blackhat))
cv2_imshow(res7)

OUTPUT:

OBSERVATION:
In this lab learn about morphological operations and I perform practical on all the different
types of morphological operations which are erosion,dilation,opening and closing,gradient,top
hat and top black.

COMPUTER VISION: 3171614 Page 27 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 7
Aim: Implement Image Segmentation in OpenCV.

 IMAGE SEGMENTATION
Image segmentation is a process by which we partition images into different regions.
Whereas the contours are the continuous lines or curves that bound or cover the full
boundary of an object in an image. And, here we will use image segmentation technique
called contours to extract the parts of an image
Open CV CODE (Segmentation):
import numpy as np
from google.colab.patches import cv2_imshow
import cv2
from matplotlib import pyplot as plt
%matplotlib auto
#Read the image using OpenCV.
img = cv2.imread("/content/coin.jpg")
cv2_imshow(img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
cv2_imshow(thresh)

COMPUTER VISION: 3171614 Page 28 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OUTPUT:

OBSERVATION: In this lab I learn about what is image segmentation and how to extract the
specific part of any image using image segmentation technique.

COMPUTER VISION: 3171614 Page 29 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 8

Aim: Implement Optical Flow in OpenCV.


 OPTICAL FLOW
Optical flow is a task of per-pixel motion estimation between two consecutive frames
in one video. Basically, the Optical Flow task implies the calculation of the shift vector
for pixel as an object displacement difference between two neighboring images. The
main idea of Optical Flow is to estimate the object’s displacement vector caused by it’s
motion or camera movements.

Open CV CODE (Optical Flow):


import numpy as np
import cv2

def optical_flow(video_path):
# Create a VideoCapture object
cap = cv2.VideoCapture(video_path)

# Check if the video opened successfully


if not cap.isOpened():
print("Error opening video file")
return

# Read the first frame


ret, frame1 = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
return

# Convert the first frame to grayscale


prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

COMPUTER VISION: 3171614 Page 30 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

# Create a mask image for drawing purposes


hsv = np.zeros_like(frame1)
hsv[..., 1] = 255

while True:
# Read a new frame
ret, frame2 = cap.read()
if not ret:
break

# Convert the new frame to grayscale


next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)

# Calculate optical flow using the Gunnar Farneback algorithm


flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

# Convert the flow vectors to polar coordinates (magnitude and angle)


mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])

# Set the hue channel of the output image to the angle of the flow vectors
hsv[..., 0] = ang * 180 / np.pi / 2

# Set the value channel of the output image to the magnitude of the flow vectors
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)

# Convert the output image to BGR format


bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

# Display the output image


cv2.imshow('frame2', bgr)

COMPUTER VISION: 3171614 Page 31 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

# Update the previous frame


prvs = next

# Break the loop if the 'q' key is pressed


k = cv2.waitKey(30) & 0xff
if k == ord('q'):
break

# Release the video capture object and destroy all windows


cap.release()
cv2.destroyAllWindows()

# Call the function with the path to your video


optical_flow('vedio.mp4')
OUTPUT:

OBSERVATION:
In this lab I learn about what is optical flow and various methods and algorithms for
implementing optical flow. I implemented Lucas-Kanade method algorithm.

COMPUTER VISION: 3171614 Page 32 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 9

Aim: Implement Lane Detection using open cv

 LANE DETECTION
Lane Detection requires to detection of the path of self-driving cars and avoiding the
risk of entering other lanes. Lane recognition algorithms reliably identify the location
and borders of the lanes by analyzing the visual input. Advanced driver assistance
systems (ADAS) and autonomous vehicle systems both heavily rely on them.

Open CV CODE (Lane Detection)


import cv2
import numpy as np
def region_of_interest(img):
height = img.shape[0]
polygons = np.array([
[(200, height), (1100, height), (550, 250)]
])
mask = np.zeros_like(img)
cv2.fillPoly(mask, polygons, 255)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines):
line_image = np.zeros_like(img)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
return line_image
def average_slope_intercept(image, lines):
left_fit = []
right_fit = []

COMPUTER VISION: 3171614 Page 33 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

for line in lines:


x1, y1, x2, y2 = line.reshape(4)
parameters = np.polyfit((x1, x2), (y1, y2), 1)
slope = parameters[0]
intercept = parameters[1]
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
left_fit_average = np.average(left_fit, axis=0)
right_fit_average = np.average(right_fit, axis=0)
left_line = make_coordinates(image, left_fit_average)
right_line = make_coordinates(image, right_fit_average)
return np.array([left_line, right_line])

def make_coordinates(image, line_parameters):


slope, intercept = line_parameters
y1 = image.shape[0]
y2 = int(y1 * (3/5))
x1 = int((y1 - intercept) / slope)
x2 = int((y2 - intercept) / slope)
return np.array([x1, y1, x2, y2])
# Read an image
image = cv2.imread('18.jpg')
image = cv2.resize(image, (960, 540))
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian Blur
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Detect edges

COMPUTER VISION: 3171614 Page 34 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

edges = cv2.Canny(blur, 50, 150)


# Crop to region of interest
cropped_edges = region_of_interest(edges)
# Detect lines
lines = cv2.HoughLinesP(cropped_edges, 2, np.pi/180, 100, np.array([]), minLineLength=40,
maxLineGap=5)
# Average and draw lines
averaged_lines = average_slope_intercept(image, lines)
line_image = draw_lines(image, averaged_lines)
# Combine line image with original image
combo_image = cv2.addWeighted(image, 0.8, line_image, 1, 1)
# Display the final image
cv2.imshow('Result', combo_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:

OBSERVATION:
In this practical I learn about Lane detection process and I implement Lane detection code in
cv2.

COMPUTER VISION: 3171614 Page 35 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

PRACTICAL: 10

Aim: Object detection and Recognition on available online image datasets

 OBJECT DETECTION
Object Detection is a computer technology related to computer vision, image
processing, and deep learning that deals with detecting instances of objects in images
and videos.

Open CV CODE (Object Detection):


import cv2

# Load the Haar cascade XML file


face_cascade = cv2.CascadeClassifier('object_detection.xml')

# Read the input image


img = cv2.imread('watchs.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces in the image


faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30,
30))

# Draw bounding boxes around detected faces


for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Display the output


cv2.imshow('Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

COMPUTER VISION: 3171614 Page 36 of 37


ENROLLMENT NUMBER: 211130116064 NAME: HRISHIKESH SORATHIA

OUTPUT:

OBSERVATION:
In this practical I learn about object detection process and I implement object detection code in
cv2.

COMPUTER VISION: 3171614 Page 37 of 37

You might also like