CV Lab Manual - Harsh Mehta
CV Lab Manual - Harsh Mehta
IT DEPARTMENT
Laboratory Manual
Year: 2024-2025
CERTIFICATE
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.
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.
OUTPUT:
WRITING IMAGE
The function imwrite saves the image to the specified file. The image format is
chosen based on the filename extension
OUTPUT:
Image written to file-system: True
CONVERSION IMAGE
The function is used for converting RGB image to gray scale image.
OUTPUT:
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.
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.
PRACTICAL: 2
Aim: Implement contrast adjustment of an image. Implement Histogram processing and
equalization.
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.
OUTPUT:
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.
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.
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.
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.
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.
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.
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.
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.
PRACTICAL: 5
Aim: Implement Edge detection. (Prewitt, Sobel, Laplacian, Canny)
plt.imshow(img)
OUTPUT:
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 |
OUTPUT:
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:
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.
OUTPUT:
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).
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()
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.
OUTPUT:
GRADIENT
It is the difference between dilation and erosion of an image. The result will look like
the outline of the object.
OUTPUT:
TOP HAT
It is the difference between input image and Opening of the image. Below example is
done for a 13x13 kernel
OUTPUT;
BLACK HAT
It is the difference between the closing of the input image and input image.
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.
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)
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.
PRACTICAL: 8
def optical_flow(video_path):
# Create a VideoCapture object
cap = cv2.VideoCapture(video_path)
while True:
# Read a new frame
ret, frame2 = cap.read()
if not ret:
break
# 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)
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.
PRACTICAL: 9
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.
OUTPUT:
OBSERVATION:
In this practical I learn about Lane detection process and I implement Lane detection code in
cv2.
PRACTICAL: 10
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.
OUTPUT:
OBSERVATION:
In this practical I learn about object detection process and I implement object detection code in
cv2.