CVAR Lab Manual
CVAR Lab Manual
Name : ________________________________
Branch : ________________________________
Apparatus:
Procedure:
OpenCV is the huge open-source library for computer vision, machine learning,
and image processing and now it plays a major role in real-time operation
which is very important in today’s systems. By using it, one can process images
and videos to identify objects, faces, or even the handwriting of a human.
When it integrated with various libraries, such as Numpy, python is capable of
processing the OpenCV array structure for analysis. To Identify image patterns
and its various features we use vector space and perform mathematical
operations on these features. To install OpenCV, one must have Python and
PIP, preinstalled on their system. To check if your system already contains
Python, go through the following instructions: Open the Command line(search
for cmd in the Run dialog( + R). Now run the following command:
python --version
If Python is already installed, it will generate a message with the Python
version available.
pip -V
OpenCV can be directly downloaded and installed with the use of pip
(package manager). To install OpenCV, just go to the command-line and type
the following command:
pip install opencv-python
Beginning with the installation:
• Type the command in the Terminal and proceed
• Finished
Installation:
Procedure:
import numpy as np
import cv2
Img=cv2.imread(‘image_name’,0)
cv2.imshow(‘output’,Img)
cv2.imwrite(‘other_name’,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
b) To display the image in various Color formats
Procedure:
import numpy as np
import cv2
img=cv2.imread(‘image_name’,0)
cv2.imshow(‘output’,img)
cv2.imwrite(‘name_for_the_other_version’,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
c) Image enhancement
Procedure:
Procedure:
# importing libraries
import cv2 import
numpy as np
image = cv2.imread('C://Geeksforgeeks//image_processing//fruits.jpg')
# Gaussian Blur
Gaussian = cv2.GaussianBlur(image, (7, 7), 0)
cv2.imshow('Gaussian Blurring', Gaussian)
cv2.waitKey(0)
b) Aim: To implement smoothing filter using Median filters.
Procedure:
# Median Blur
median = cv2.medianBlur(image, 5)
cv2.imshow('Median Blurring', median)
cv2.waitKey(0)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image Blurring refers to making the image less clear or distinct. It is done with the help of
various low pass filter kernels.
Advantages of blurring:
• It helps in Noise removal. As noise is considered as high pass signal so by the application
of low pass filter kernel we restrict noise.
• It helps in smoothing the image.
• It helps in hiding the details when necessary. For e.g. in many cases police deliberately
want to hide the face of the victim, in such cases blurring is required.
• Median Blur: The Median Filter is a non-linear digital filtering technique, often used to
remove noise from an image or signal. Median filtering is very widely used in digital
image processing because, under certain conditions, it preserves edges while removing
noise. It is one of the best algorithms to remove Salt and pepper noise.
c) Aim: To implement smoothing filter using Mean filters.
Procedure:
import org.opencv.core.Core; import
org.opencv.core.Mat; import
org.opencv.core.Point; import
org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs; import
org.opencv.imgproc.Imgproc;
// blur(Mat src, Mat dst, Size ksize, Point anchor, int borderType)
// Writing the image
Imgcodecs.imwrite("E:/OpenCV/chap9/blur.jpg",
dst);
System.out.println("Image processed");
}
}
#BlurOperations.py
import cv2
import numpy as np
img = cv2.imread('noisy.jpg')
#gaussian Blur
output_med = cv2.medianBlur(img, 5)
output_bil = cv2.bilateralFilter(img, 5, 6, 6)
cv2.imshow('Gaussian', output_gaus)
cv2.imshow('Bilateral', output_bil)
cv2.imshow('Original', img)
cv2.waitKey(0)
#Sharpening.py
import cv2
import numpy as np
img = cv2.imread('nature.jpg')
cv2.imshow('original', img)
cv2.waitKey(0)
Exp. 4 : Demonstrate Fourier Transformations.
Theory:
The meaning represented by the Fourier transform is: “Any periodic wave can be divided into
many sine waves, and the meaning of the Fourier transform is to find the sine waves of each
frequency”, and the same is true for images, By converting the image into sine waves of various
frequencies, it can be converted or operated on, such as filtering out unwanted information
through a filter. This is the practical application of the Fourier transform of the image.
This is the most original definition of Fourier transform, which is used to transform continuous
signals, but the picture is not a continuous analog signal, but a discrete digital signal, so we use
This is a one-dimensional Fourier transform, but the image we want to convert is two-
dimensional, so a two-dimensional Fourier transform should be used, but if you directly write a
two-dimensional Fourier transform, the amount of calculation will be too large, so in practice,
we will perform a Fourier transform on the data vertically and horizontally to achieve the effect
of a two-dimensional Fourier transform. Similarly, the inverse Fourier transform can also be
achieved in the same way. Therefore, the formula for the two-dimensional conversion will not
be repeated here.
The left is the original image, and the right is the frequency distribution map after Fourier
transformation.
Procedure:
#Demonstrate Fourier Transformations
import cv2
import numpy as np
import glob
list_images = glob.iglob("letters/*")
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
cv2.imshow(image_title, img_and_magnitude)
cv2.waitKey(0)
cv2.destroyAllWindows()
Exp. 5 : Implement histogram calculation and equalization for the given
image.
Aim : To Implement histogram calculation and equalization for the given image.
Theory :
The histogram is a graph showing the number of pixels in an image at each different intensity
This is a simple process. The image is scanned in a single pass and a running count of the
number of pixels found at each intensity value is kept. By using this information, a relevant
In [2]:
path = "D:\\Dataset\\"
img = cv2.imread(imgpath, 1)
R, G, B = cv2.split(img)
output1_R = cv2.equalizeHist(R)
output1_G = cv2.equalizeHist(G)
output1_B = cv2.equalizeHist(B)
output2_R = clahe.apply(R)
output2_G = clahe.apply(G)
output2_B = clahe.apply(B)
for i in range(3):
plt.subplot(1, 3, i+1)
plt.imshow(output[i])
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
Exp. 6 : Implement morphological operations like dilation, erosion, opening
and closing on the given image.
Theory :
We can use morphological operations to increase the size of objects in images as well
as decrease them. We can also utilize morphological operations to close gaps between objects
as well as open them.
Erosion
An erosion in an image “erodes” the foreground object and makes it smaller. Erosion works by
defining a structuring element and then sliding this structuring element from left-to-right and
top-to-bottom across the input image.
A foreground pixel in the input image will be kept only if all pixels inside the structuring
element are > 0. Otherwise, the pixels are set to 0 (i.e., background).
Erosion is useful for removing small blobs in an image or disconnecting two connected objects.
Dilation
The opposite of an erosion is a dilation. Just like an erosion will eat away at the foreground
pixels, a dilation will grow the foreground pixels.
Dilations increase the size of foreground objects and are especially useful for joining broken
parts of an image together.
Dilations, just as an erosion, also utilize structuring elements — a center pixel p of the
structuring element is set to white if ANY pixel in the structuring element is > 0.
Opening
Performing an opening operation allows us to remove small blobs from an image: first an
erosion is applied to remove the small blobs, then a dilation is applied to regrow the size of the
original object.
Closing
As the name suggests, a closing is used to close holes inside of objects or for connecting
components together.
Morphological gradient
A morphological gradient is the difference between a dilation and erosion. It is useful for
determining the outline of a particular object of an image.
Procedure:
import cv2
import numpy as np
for i in range(8):
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
Exp. 7 : Implement edge detection on images using any two edge detection
masks.
Aim : To implement edge detection on images using any two edge detection masks.
Theory:
Edges: Edges are abrupt changes in intensity, discontinuity in image brightness or contrast;
usually edges occur on the boundary of two regions.
Some of the masks for edge detection. Prewitt Operator • Sobel Operator • Robinson
Compass Masks • Kirsch Compass Masks • Laplacian Operator. operator we can detects only
horizontal and vertical edges.
Procedure:
#canny_edge_detection_in_opencv.py
import cv2
import numpy as np
img = cv2.imread("lena.jpg")
for i in range(2):
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
#image_gradients_and_canny_edge_detection.py
import cv2
import numpy as np
lap = np.uint8(np.absolute(lap))
edges = cv2.Canny(img,100,200)
sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))
for i in range(6):
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
Exp. 8 : To detect motion from structure.
Theory :
Motion detection is the process of detecting a change in the position of an object relative to its
surroundings or a change in the surroundings relative to an object.
The structure from motion pipeline has 5 keys steps, namely key point detection, feature
matching, motion estimation triangulation and bundle adjustment.
Motion analysis is used in computer vision, image processing, high-speed
photography and machine vision that studies methods and applications in which two or more
consecutive images from an image sequences, e.g., produced by a video camera or high-speed
camera, are processed to produce information based on the apparent motion in the images. In
some applications, the camera is fixed relative to the scene and objects are moving around in
the scene, in some applications the scene is more or less fixed and the camera is moving, and
in some cases both the camera and the scene are moving.
The motion analysis processing can in the simplest case be to detect motion, i.e., find the
points in the image where something is moving. More complex types of processing can be
to track a specific object in the image over time, to group points that belong to the same rigid
object that is moving in the scene, or to determine the magnitude and direction of the motion of
every point in the image. The information that is produced is often related to a specific image in
the sequence, corresponding to a specific time-point, but then depends also on the
neighboring images. This means that motion analysis can produce time-dependent information
about motion.
Applications of motion analysis can be found in rather diverse areas, such as surveillance,
medicine, film industry, automotive crash safety,[1] ballistic firearm studies,[2] biological
science,[3] flame propagation,[4] and navigation of autonomous vehicles to name a few
examples.
Procedure:
import cv2
import numpy as np
cap = cv2.VideoCapture('vtest.avi')
frame_width = int( cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
out = cv2.VideoWriter("output.avi", fourcc, 5.0, (1280,720))
ret, frame1 = cap.read()
ret, frame2 = cap.read()
print(frame1.shape)
while cap.isOpened():
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Theory :
Machines store images in the form of a matrix of numbers. The size of this matrix
Let’s say the dimensions of an image are 180 x 200 or n x m. These dimensions are
These numbers, or the pixel values, denote the intensity or brightness of the
pixel. Smaller numbers (closer to zero) represent black, and larger numbers (closer
A colored image is typically composed of multiple colors and almost all colors can
Hence, in the case of a colored image, there are three Matrices (or channels) –
Red, Green, and Blue. Each matrix has values between 0-255 representing the
# %matplotlib inline
#imshow(image)
# image matrix
print('\n\nImage matrix\n\n',image)
import numpy as np
# create features
features = np.reshape(image, (660*450))
print('\n\nFeature Array\n\n',features)
import numpy as np
from skimage.io import imread, imshow
from skimage.filters import prewitt_h,prewitt_v
import matplotlib.pyplot as plt
%matplotlib inline
imshow(edges_prewitt_vertical, cmap='gray')
Exp. 10 Implement object detection like recognizing pedestrians.
Aim : To implement object detection like recognizing pedestrians.
Theory :
Object detection is a computer vision technique that works to identify and locate objects within
an image or video. Specifically, object detection draws bounding boxes around these detected
objects, which allow us to locate where said objects are in (or how they move through) a given
scene.
Object detection in OpenCV refers to the process of identifying and locating specific
objects within an image or a video. OpenCV (Open Source Computer Vision Library) is a
Object detection in OpenCV typically involves the use of pre-trained models, such as Haar
cascades or deep learning-based models like Single Shot MultiBox Detector (SSD) or You
Only Look Once (YOLO). These models are trained on large datasets and can detect
various objects, such as faces, pedestrians, cars, or specific objects based on the training
data.
The process of object detection involves two main steps: object localization and object
classification. Localization aims to determine the location and boundaries of the object
within the image, usually by drawing bounding boxes around the objects. Classification
involves assigning labels or identifying the specific class or category of the detected
object.
algorithms.
Procedure:
import cv2
cap = cv2.VideoCapture('in.avi')
human_cascade = cv2.CascadeClassifier('haarcascade_fullbody.xml')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
dimensional points into k clusters. It works by iteratively assigning each point to the cluster with
the closest mean, and then recalculating the mean of each cluster. This process is repeated until
the clusters no longer change. In the context of object detection, K-Means can be used to
cluster similar pixels together, which can then be used to identify objects in an image.
The K-Means algorithm is a simple and efficient method for clustering points. It works by first
randomly selecting k centroids, which represent the initial means of the clusters. Then, for each
point in the dataset, the algorithm assigns it to the cluster whose centroid is closest. After all
the points have been assigned, the centroids are recalculated as the mean of all the points in
the cluster. This process is repeated until the clusters no longer change.
One of the main advantages of K-Means is its computational efficiency. To use K-Means for
object detection, the first step is to convert the image into a set of points in a three-dimensional
color space. Each point represents a pixel in the image, with its coordinates being the pixel's
red, green, and blue values. Then, the K-Means algorithm is applied to the set of points to
Once the clustering is complete, the resulting clusters can be used to identify objects in the
image. For example, a cluster of pixels with similar colors can be used to identify a specific
object in the image. Additionally, the size of a cluster can be used to estimate the size of the
object. The process can be repeated for different values of k to get a more accurate detection.
Procedure:
import numpy as np
# load image
image = io.imread('image.jpg')
pixels = image.reshape(-1,3)
kmeans = KMeans(n_clusters=5)
kmeans.fit(pixels)
labels = kmeans.labels_
io.imshow(segmented_image)
io.show()
Exp. 12 Implement dimensionality reduction using PCA for the given images.
Aim: To implement dimensionality reduction using PCA for the given images.
Theory :
In machine learning, we need lots of data to build an efficient model, but dealing with a larger
dataset is not an easy task we need to work hard in preprocessing the data and as a data
scientist we will come across a situation dealing with a large number of variables here PCA
(principal component analysis) is dimension reduction technique helps in dealing with those
problems.
PCA is a dimensionality reduction that is often used to reduce the dimension of the variables of
a larger dataset that is compressed to the smaller one which contains most of the information
to build an efficient model. In a real-time scenario when you are working reducing the number
of variables in the dataset you need compromise on model accuracy but using PCA will give
good accuracy. The idea of PCA is to reduce the variables in the dataset and preserve data as
much as possible.
The image is a combination of pixels in rows placed one after another to form one single image
each pixel value represents the intensity value of the image, so if you have multiple images we
can form a matrix considering a row of pixels as a vector. It requires huge amounts of storage
while working with many images where we are using PCA is used to compress it and preserve
#Importing libraries.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
#Reading dataset.
wine = pd.read_csv("/content/wine.csv")
wine.describe()
pca=PCA(n_components=13)
pca_values=pca.fit_transform(wine_data)
var = pca.explained_variance_ratio_
pca.components_[0]
How compressed data is distributed.
var1 = np.cumsum(np.round(var,decimals = 4)*100)
var1
z =pca_values[:,2]
new_df = pd.DataFrame(z)
new_df
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters = 3)
kmeans.fit(new_df)
Kmeans.labels_
Exp. 13 Demonstrate model based reconstruction using tensor flow.
• Automatic differentiation
Tensors
The most important attributes of a tf.Tensor are its shape and dtype:
• Tensor.shape: tells you the size of the tensor along each of its axes.
• Tensor.dtype: tells you the type of all the elements in the tensor.
Variables
Normal tf.Tensor objects are immutable. To store model weights (or other mutable state) in
TensorFlow use a tf.Variable.
tf.Module is a class for managing your tf.Variable objects, and the tf.function objects that
operate on them. The tf.Module class is necessary to support two significant features:
1. You can save and restore the values of your variables using tf.train.Checkpoint. This is
useful during training as it is quick to save and restore a model's state.
2. You can import and export the tf.Variable values and the tf.function graphs
using tf.saved_model. This allows you to run your model independently of the Python
program that created it.
Procedure:
import pandas as pd
df = pd.read_csv('Churn.csv')
y_train.head()
1. Import Dependencies
from tensorflow.keras.models import Sequential, load_model
model.add(Dense(units=64, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimiz)
y_hat = model.predict(X_test)
accuracy_score(y_test, y_hat)
4. Saving and Reloading
model.save('tfmodel')
del model
model = load_model('tfmodel')