KEMBAR78
CVAR Lab Manual | PDF | Digital Signal Processing | Computer Engineering
0% found this document useful (0 votes)
7 views40 pages

CVAR Lab Manual

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)
7 views40 pages

CVAR Lab Manual

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/ 40

COMPUTER VISION & ROBOTICS LAB MANUAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


(INTERNET OF THINGS)
COMPUTER VISION & ROBOTICS LAB MANUAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


(INTERNET OF THINGS)

Name : ________________________________

Roll No.: ________________________________

Branch : ________________________________

Year & Semester : _______________________________


Exp. No. Name of the experiment Date Faculty sign.
Familiarization of the tool used for computer
1
vision.

2 Implement basic image operations.

3 Implementing smoothing filters on an image.

4 Demonstrate Fourier Transformations.

Implement histogram calculation and

5 equalization for the given image.

Implement morphological operations like


dilation, erosion, opening and closing on the
6
given image.

Implement edge detection on images using any


7 two edge detection masks.

To detect motion from structure.


8

Implement texture extraction of a given image.


9

Implement object detection like recognizing


10
pedestrians.

Implement face recognition of an image using


11
K-Means clustering.

Implement dimensionality reduction using PCA


12
for the given images.
Demonstrate model based reconstruction using
13 tensor flow.
Exp. 1 : Familiarization of the tool used for computer vision.
Aim : To familiarize of OpenCV with Python, the tool used for computer vision.

Apparatus:

Hardware required: Desktop / Laptop with min. 4GB RAM

Software required: Operating Systems – Windows 7 & later

Open CV can be implemented in Python 3.8 or lower versions of Python.

Procedure:

Installation of OpenCV for Python on Windows

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.

If Python is not present, go through How to install Python on Windows? and


follow the instructions provided. PIP is a package management system used
to install and manage software packages/libraries written in Python. These
files are stored in a large “on-line repository” termed as Python Package Index
(PyPI). To check if PIP is already installed on your system, just go to the
command line and execute the following command:

pip -V

Downloading and Installing OpenCV:

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

• Collecting Information and downloading


data:
• Installing
Packages:

• Finished
Installation:

To check if OpenCV is correctly installed, just run the following commands to


perform a version check:
python
>>>import cv2
>>>print(cv2.__version__)
Exp. 2 : Implement basic image operations.

a) Loading and displaying an image

Aim : To load and display an image in OpenCV with Python.

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

Aim : To display the image in different colour 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

Aim : To do the enhancement of a given image.

Procedure:

#Import the necessary libraries


import cv2
import matplotlib.pyplot as plt
import numpy as np

# Load the image


image = cv2.imread('GFG.jpeg')

#Plot the original image


plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)

# Adjust the brightness and contrast


# Adjusts the brightness by adding 10 to each pixel value
brightness = 10
# Adjusts the contrast by scaling the pixel values by 2.3
contrast = 2.3
image2 = cv2.addWeighted(image, contrast, np.zeros(image.shape,
image.dtype), 0, brightness)

#Save the image


cv2.imwrite('modified_image.jpg', image2)
#Plot the contrast image
plt.subplot(1, 2, 2)
plt.title("Brightness & contrast")
plt.imshow(image2)
plt.show()
Exp. 3 : Implementing smoothing filters on an image.

a) Aim: To implement smoothing filter using Gauss filters.

Procedure:
# importing libraries
import cv2 import
numpy as np

image = cv2.imread('C://Geeksforgeeks//image_processing//fruits.jpg')

cv2.imshow('Original Image', image)


cv2.waitKey(0)

# 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.

• Low intensity edges are removed.

• 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.

• Gaussian Blurring: Gaussian blur is the result of blurring an image by a Gaussian


function. It is a widely used effect in graphics software, typically to reduce image noise
and reduce detail. It is also used as a preprocessing stage before applying our machine
learning or deep learning models.

E.g. of a Gaussian kernel(3×3).

• 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;

public class BlurTest {


public static void main(String args[]) {
// Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME
);
// Reading the Image from the file and storing it in to a Matrix object
String file ="C:/EXAMPLES/OpenCV/sample.jpg"; Mat src =
Imgcodecs.imread(file);

// Creating an empty matrix to store the


result
Mat dst = new Mat();

// Creating the Size and Point objects


Size size = new Size(45, 45); Point point = new
Point(20, 30);

// Applying Blur effect on the Image Imgproc.blur(src,


dst, size, point,
Core.BORDER_DEFAULT);

// 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

#Managing the imports

import cv2

import numpy as np

#reading a image from computer and taking dimensions

img = cv2.imread('noisy.jpg')

rows, cols = img.shape[:2]

#Kernel Blurring using filter2D()

kernel_25 = np.ones((25,25), np.float32) / 625.0

output_kernel = cv2.filter2D(img, -1, kernel_25)

#Boxfilter and blur function blurring

output_blur = cv2.blur(img, (25,25))

output_box = cv2.boxFilter(img, -1, (5,5), normalize=False)

#gaussian Blur

output_gaus = cv2.GaussianBlur(img, (5,5), 0)

#median Bur (reduction of noise)

output_med = cv2.medianBlur(img, 5)

#Bilateral filtering (Reduction of noise + Preserving of edges)

output_bil = cv2.bilateralFilter(img, 5, 6, 6)

cv2.imshow('kernel blur', output_kernel)

cv2.imshow('Blur() output', output_blur)

cv2.imshow('Box filter', output_box)

cv2.imshow('Gaussian', output_gaus)
cv2.imshow('Bilateral', output_bil)

cv2.imshow('Median Blur', output_med)

cv2.imshow('Original', img)

cv2.waitKey(0)

#Sharpening.py

#Getting the imports

import cv2

import numpy as np

#Reading the image

img = cv2.imread('nature.jpg')

#Gauusian kernel for sharpening

gaussian_blur = cv2.GaussianBlur(img, (7,7), 2)

#Sharpening using addweighted()

sharpened1 = cv2.addWeighted(img,1.5, gaussian_blur, -0.5, 0)

sharpened2 = cv2.addWeighted(img,3.5, gaussian_blur, -2.5, 0)

sharpened3 = cv2.addWeighted(img,7.5, gaussian_blur, -6.5, 0)

#Showing the sharpened Images

cv2.imshow('Sharpened 3', sharpened3)

cv2.imshow('Sharpened 2', sharpened2)

cv2.imshow('Sharpened 1', sharpened1)

cv2.imshow('original', img)

cv2.waitKey(0)
Exp. 4 : Demonstrate Fourier Transformations.

Aim: To 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.

Definition of one-dimensional continuous Fourier transform

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

the following discrete Fourier transform to transform the picture.

Definition of one-dimensional discrete Fourier transform

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/*")

for image_title in list_images:

img = cv2.imread(image_title, cv2.IMREAD_GRAYSCALE)

f = np.fft.fft2(img)

fshift = np.fft.fftshift(f)

magnitude_spectrum = 20*np.log(np.abs(fshift))

magnitude_spectrum = np.asarray(magnitude_spectrum, dtype=np.uint8)

img_and_magnitude = np.concatenate((img, magnitude_spectrum), axis=1)

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

value found in that image.

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

histogram can be constructed.


Procedure :
import cv2

import matplotlib.pyplot as plt

In [2]:

path = "D:\\Dataset\\"

imgpath = path + "4.2.03.tiff"

img = cv2.imread(imgpath, 1)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

R, G, B = cv2.split(img)

output1_R = cv2.equalizeHist(R)

output1_G = cv2.equalizeHist(G)

output1_B = cv2.equalizeHist(B)

output1 = cv2.merge((output1_R, output1_G, output1_B))

# Contrast Limited Adaptive Histogram Equalization

clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))

output2_R = clahe.apply(R)

output2_G = clahe.apply(G)

output2_B = clahe.apply(B)

output2 = cv2.merge((output2_R, output2_G, output2_B))

output = [img, output1, output2]

titles = ['Original Image',

'Adjusted Histogram', 'CLAHE']

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.

Aim : To implement morphological operations like dilation, erosion, opening


and closing on the given image.

Theory :

OpenCV Morphological Operations

Morphological operations are simple transformations applied to binary or grayscale images.


More specifically, we apply morphological operations to shapes and structures inside of
images.

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.

Morphological operations “probe” an image with a structuring element. This structuring


element defines the neighbourhood to be examined around each pixel. And based on the given
operation and the size of the structuring element we are able to adjust our output image.
Morphological operations

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

An opening is an erosion followed by a dilation.

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

The exact opposite to an opening would be a closing. A closing is a dilation followed by


an erosion.

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

from matplotlib import pyplot as plt

img = cv2.imread('smarties.png', cv2.IMREAD_GRAYSCALE)

_, mask = cv2.threshold(img, 220, 255, cv2.THRESH_BINARY_INV)

kernal = np.ones((5,5), np.uint8)

dilation = cv2.dilate(mask, kernal, iterations=2)

erosion = cv2.erode(mask, kernal, iterations=1)

opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal)

closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernal)

mg = cv2.morphologyEx(mask, cv2.MORPH_GRADIENT, kernal)

th = cv2.morphologyEx(mask, cv2.MORPH_TOPHAT, kernal)

titles = ['image', 'mask', 'dilation', 'erosion', 'opening', 'closing', 'mg', 'th']

images = [img, mask, dilation, erosion, opening, closing, mg, th]

for i in range(8):

plt.subplot(2, 4, i+1), plt.imshow(images[i], 'gray')

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.

Edge detection works by detecting discontinuities in brightness. It is used for image


segmentation and data extraction in areas such as image processing, computer vision, and
machine vision.

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

from matplotlib import pyplot as plt

img = cv2.imread("lena.jpg")

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

canny = cv2.Canny(img, 100, 200)

titles = ['image', 'canny']

images = [img, canny]

for i in range(2):

plt.subplot(1, 2, i+1), plt.imshow(images[i], 'gray')

plt.title(titles[i])
plt.xticks([]),plt.yticks([])

plt.show()

#image_gradients_and_canny_edge_detection.py

import cv2

import numpy as np

from matplotlib import pyplot as plt

img = cv2.imread("messi5.jpg", cv2.IMREAD_GRAYSCALE)

lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3)

lap = np.uint8(np.absolute(lap))

sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0)

sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1)

edges = cv2.Canny(img,100,200)

sobelX = np.uint8(np.absolute(sobelX))

sobelY = np.uint8(np.absolute(sobelY))

sobelCombined = cv2.bitwise_or(sobelX, sobelY)

titles = ['image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined', 'Canny']

images = [img, lap, sobelX, sobelY, sobelCombined, edges]

for i in range(6):

plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')

plt.title(titles[i])

plt.xticks([]),plt.yticks([])

plt.show()
Exp. 8 : To detect motion from structure.

Aim : 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))

frame_height =int( cap.get( cv2.CAP_PROP_FRAME_HEIGHT))

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)

for contour in contours:


(x, y, w, h) = cv2.boundingRect(contour)

if cv2.contourArea(contour) < 900:


continue
cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame1, "Status: {}".format('Movement'), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 0, 255), 3)
#cv2.drawContours(frame1, contours, -1, (0, 255, 0), 2)
image = cv2.resize(frame1, (1280,720))
out.write(image)
cv2.imshow("feed", frame1)
frame1 = frame2
ret, frame2 = cap.read()
if cv2.waitKey(40) == 27:
break
cv2.destroyAllWindows()
cap.release()
out.release()
Exp. 9 Implement texture extraction of a given image.
Aim : To implement texture extraction of a given image.

Theory :

Machines store images in the form of a matrix of numbers. The size of this matrix

depends on the number of pixels we have in any given image.

Let’s say the dimensions of an image are 180 x 200 or n x m. These dimensions are

basically the number of pixels in the image (height x width).

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

to 255) denote white.

A colored image is typically composed of multiple colors and almost all colors can

be generated from three primary colors – red, green and blue.

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

intensity of the color for that pixel.


Procedure:
# to read the Image Data
from skimage.io import imread, imshow

image = imread('image_8_original.png', as_gray=True)

# import matplotlib.pyplot as plt

# %matplotlib inline

#imshow(image)

#checking image shape

print('Shape of the image is = ',image.shape)

# image matrix

print('\n\nImage matrix\n\n',image)

Method #1: Grayscale Pixel Values as Features

#Grayscale Pixel Values as Features

# importing library and read the image

import numpy as np

from skimage.io import imread, imshow

image = imread('puppy.jpeg', as_gray=True)

# shape of the image

print('\n\nShape of the image = ',image.shape)

# create features
features = np.reshape(image, (660*450))

# shape of feature array

print('\n\nShape of the feature array = ',features.shape)

print('\n\nFeature Array\n\n',features)

Method #2: Extracting Edge Features

#importing the required libraries

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

#reading the image


image = imread('puppy.jpeg',as_gray=True)

#calculating horizontal edges using prewitt kernel


edges_prewitt_horizontal = prewitt_h(image)
#calculating vertical edges using prewitt kernel
edges_prewitt_vertical = prewitt_v(image)

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

popular open-source library for computer vision tasks.

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.

OpenCV provides a range of functions and methods to implement object detection

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()

# Our operations on the frame come here


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
humans = human_cascade.detectMultiScale(gray, 1.9, 1)

# Display the resulting frame


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

cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture


cap.release()
cv2.destroyAllWindows()
Exp. 11 Implement face recognition of an image using K-Means clustering

Aim: To implement face recognition of an image using K-Means clustering.


Theory :
K-Means clustering is an unsupervised learning algorithm used to classify a set of n-

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

cluster similar pixels together.

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:

from sklearn.cluster import KMeans

from skimage import io

import numpy as np

# load image

image = io.imread('image.jpg')

# reshape image to a 2D array of pixels

pixels = image.reshape(-1,3)

# initialize KMeans model

kmeans = KMeans(n_clusters=5)

# fit the model to the pixels

kmeans.fit(pixels)

# get the labels for each pixel

labels = kmeans.labels_

# reshape the labels back to the original image shape

segmented_image = labels.reshape(image.shape[0], image.shape[1])

# display the segmented image

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

the data as much as possible.


Procedure:

Implementation of image compression using PCA

#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()

Applying PCA to compress the data.

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

Storing the PCA compressed dataset.

z =pca_values[:,2]

Testing compressed new data on the k-means algorithm.

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.

Aim : To demonstrate model based reconstruction using tensor flow.


Theory :

TensorFlow is an end-to-end platform for machine learning. It supports the following:

• Multidimensional-array based numeric computation (similar to NumPy.)

• GPU and distributed processing

• Automatic differentiation

• Model construction, training, and export

Tensors

TensorFlow operates on multidimensional arrays or tensors represented as tf.Tensor objects.

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.

Modules, layers, and models

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

from sklearn.model_selection import train_test_split

df = pd.read_csv('Churn.csv')

X = pd.get_dummies(df.drop(['Churn', 'Customer ID'], axis=1))

y = df['Churn'].apply(lambda x: 1 if x=='Yes' else 0)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)

y_train.head()

1. Import Dependencies
from tensorflow.keras.models import Sequential, load_model

from tensorflow.keras.layers import Dense

from sklearn.metrics import accuracy_score

2. Build and Compile Model


model = Sequential()

model.add(Dense(units=32, activation='relu', input_dim=len(X_train.columns)))

model.add(Dense(units=64, activation='relu'))

model.add(Dense(units=1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimiz)

3. Fit, Predict and Evaluate


model.fit(X_train, y_train, epochs=200, batch_size=32)er='sgd', metrics='accuracy')

y_hat = model.predict(X_test)

y_hat = [0 if val < 0.5 else 1 for val in y_hat]

accuracy_score(y_test, y_hat)
4. Saving and Reloading
model.save('tfmodel')

del model

model = load_model('tfmodel')

You might also like