100% found this document useful (1 vote)
626 views

Computer Vision 8th Sem Lab Manual

The document describes experiments related to image processing and computer vision using OpenCV. Experiment 1 discusses basic image handling operations like reading, displaying, converting formats and to grayscale. Experiment 2 covers geometric transformations including rotation, translation, scaling and affine transformations. Experiment 3 computes homography matrices to map source and destination points, for applications like perspective removal. Experiment 4 applies perspective transformations using computed homography matrices. Experiment 5 discusses camera calibration to determine intrinsic and extrinsic camera parameters relating 3D real-world points to 2D pixel coordinates.

Uploaded by

Jaikishan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
626 views

Computer Vision 8th Sem Lab Manual

The document describes experiments related to image processing and computer vision using OpenCV. Experiment 1 discusses basic image handling operations like reading, displaying, converting formats and to grayscale. Experiment 2 covers geometric transformations including rotation, translation, scaling and affine transformations. Experiment 3 computes homography matrices to map source and destination points, for applications like perspective removal. Experiment 4 applies perspective transformations using computed homography matrices. Experiment 5 discusses camera calibration to determine intrinsic and extrinsic camera parameters relating 3D real-world points to 2D pixel coordinates.

Uploaded by

Jaikishan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

lOMoARcPSD|245 368 66

Experiment No. 1
Aim: Perform basic Image Handling and processing operations on the image.

Objectives: The objective of this lab is to introduce the student to OpenCV, especially for
image processing.
 Reading an image in python
 Convert Images to another format
 Convert an Image to Grayscale
 Play a video file

Some Useful Commands:


1. To slice a 2D array: x = y [row_start: row_ end, col_start: col_end]
2. To create a 2D array of zeros using NumPy: my_array = numpy.zeros ((row, columns),
dtype=numpy.uint8)
3. To create a 2D array of ones using NumPy: my_array = numpy.ones ((row, columns),
dtype=numpy.uint8)
4. To check the size of a 2D array: size = numpy.shape(my_array)
5. To join a sequence of arrays along an existing axis: F = np.concatenate ((a, b), axis=0);
6. To assemble an array from nested lists of blocks. img = np.block ([[np.ones ((2, 2)),
np.zeros ((2, 3))], [np.zeros ((2, 2)), np.ones ((2, 3))]]) Note: all the input array
dimensions except for the concatenation axis must match exactly
7. Reading an image using OpenCV: my_image = cv2.imread(<test_image.jpg=,0) The
second argument determines whether the image is read as a grayscale image or a
colored image. 0 is used for reading an image as grayscale and while 1 is used for
reading in color. If no argument is passed then the image is read as is.
8. Displaying an image using OpenCV: cv2.imshow (<Title of the window=, my_image).
Two more commands that need to be used while displaying an image are
cv2.waitKey(x), cv2.destroyAllWindows (). The waitKey () function waits for a key
being pressed for x number of milliseconds. If 0 is passed to waitKey () as an argument,
it will wait indefinitely for a key press. cv2.destroyAllWindows () closes are the open
image windows.
9. Writing an image to disk: cv2.imwrite(<image_name.jpg=, my_image)

Lab Exercise: The combination of Python (the language), Numpy (the numerical array lib),
SciPy (scientific libs), and Matplotlib (the graphical plot lib) will serve as our computational
lOMoARcPSD|245 368 66

basis to learn image processing and computer vision. Where possible and needed we will use
other libraries as well.

1. Reading, displaying, and writing an image using OpenCV.


2. Convert the image to another format using OpenCV.
3. Perform the image resizing using OpenCV.
4. Convert a colored image into a grayscale image using OpenCV.
5. Scaling, rotation and shifting operation on the image using OpenCV.
6. Play a video using OpenCV.
7. Extract images from the video.
8. Using the following formula f (i, j) = sin (2 π f (i + j)) where i and j indices of a pixel,
draw an image with different frequencies (input from the user).

Post Lab Exercise:


1. Apply all the above image handling and processing operations on any image using the
different library and shows the result.
2. Write 3 different Python functions that can create the images given below. Code them
in such so that the size of the image itself, size of boxes, size of lines, and a number of
horizontal and vertical lines are entered by the user.
lOMoARcPSD|245 368 66

Experiment No. 2
Aim: Geometric Transformation

Objectives: The objective of this lab is to introduce Geometric Transformation and apply it
to images.
 Affine Transformation
 Rotation, Translation and Scaling Transformation

Some Useful Commands:


1. cv2.resize()
2. cv2.warpAffine()
3. cv2.getRotationMatrix2D()
4. cv2.warpPerspective()
5. cv2.getAffineTransform()
6. cv2.getPerspectiveTransform()

Lab Exercise:

1. Perform scaling, rotation and shifting operations on an image using OpenCV().


2. Perform image reflection on an image using OpenCV().
3. Perform Image shearing on an image using OpenCV().
4. Apply the affine transformation on an image using OpenCV().

1. Write the affine transformation yourself and call cv2.warpAffine(image, A, output_shape)

The code below shows the overall affine matrix that would give the same results as above. A good exercise
would be to derive the formulation yourself!
def get_affine_cv(t, r, s):
sin_theta = np.sin(r)
cos_theta = np.cos(r)

a_11 = s * cos_theta
a_21 = -s * sin_theta

a_12 = s * sin_theta
a_22 = s * cos_theta

a_13 = t[0] * (1 - s * cos_theta) - s * sin_theta * t[1]


a_23 = t[1] * (1 - s * cos_theta) + s * sin_theta * t[0]return np.array([[a_11, a_12, a_13],
[a_21, a_22, a_23]])A2 = get_affine_cv((tx, ty), angle, scale)
warped = cv2.warpAffine(image, A2, (width, height))

2. Rely on OpenCV to return the affine transformation matric using cv2.getRotationMatrix2D(center, angle, scale).

This function rotates the image about the point center with angle and scale it with scale
A3 = cv2.getRotationMatrix2D((tx, ty), np.rad2deg(angle), scale)warped = cv2.warpAffine(image, b3, (width, height),
flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0)
lOMoARcPSD|245 368 66

Experiment No. 3
Aim: Compute Homography Matrix

Objectives:
 Estimate the Homography matrix without Singular Value Decomposition
 Implement Direct Linear Transformation (DLT) algorithm to estimate the Homography
 Remove the Projective Distortion in the image using DLT

Lab Exercise:

This experiment will demonstrate the basic concepts of homography with some codes. For
detailed explanations of the theory, please refer to video recordings of a computer vision course
or a computer vision book.

The images used in this experiment can be found here:


https://github.com/opencv/opencv/tree/master/samples/data

Lab Exercise

1. Compute the Homography matrix for a given 4 data points without SVD and
transformed the point using the computed homography matrix.
(51,791) -- (1,900)
(63,143) -- (1,1)
(444,211) -- (501,1)
(426,719) -- (501,900)
2. Compute the Homography matrix for a given 4 data points using DLT and transformed
the point using the computed homography matrix.
(51,791) -- (1,900)
(63,143) -- (1,1)
(444,211) -- (501,1)
(426,719) -- (501,900)

Post Lab Exercise:


1. A source image will be transformed into the desired perspective view by computing
the homography using DLT that maps the source points into the desired points.
lOMoARcPSD|245 368 66

Experiment No. 4
Aim: Perspective Transformation

Objectives:
 Remove the Projective Distortion in the image using Homography

Lab Exercise

1. For perspective transformation, you need a 3x3 transformation matrix. Straight lines
will remain straight even after the transformation. To find this transformation matrix,
you need 4 points on the input image and corresponding points on the output image.
Among these 4 points, 3 of them should not be collinear. Then the transformation
matrix can be found by the function cv2.getPerspectiveTransform(). Then
apply cv2.warpPerspective() with this 3x3 transformation matrix.

Code:

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

img = cv2.imread('nature.jpg')
rows,cols,ch = img.shape

pt1 = np.float32([[50,65],[370,52],[30,387],[390,390]])
pt2 = np.float32([[0,0],[310,0],[0,310],[310,310]])

matrix_aff = cv2.getPerspectiveTransform(pt1,pt2)
dst = cv2.warpPerspective(img,matrix_aff,(cols,rows))

plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')

plt.show()

2. Write a function <perspective transform(f, x1, y1, x2, y2, x3, y3, x4, y4, width, height)” that
warps a quadrilateral with vertices at (x1,y1), (x2,y2), (x3,y3) and (x4,y4) to a new
image of given width and height. The mapping is given in the following table:
lOMoARcPSD|245 368 66

Calculation of the parameters of the perspective transform should be done by your code (do
not use the code from OpenCV or other sources).
Be sure that your code works with:
3. image of unequal width and height
4. scalar and color images

In your report present the code and the formulas on which it is based together with some
examples showing that it works as intended. You should at least be able to redo the following
example (make a reasonable choice for width and height of the new image assuming the flyer
is of A4 shape).
lOMoARcPSD|245 368 66

Experiment No. 5
Aim: Camera Calibration

Objectives:
 Calibrate the camera and extract the intrinsic and extrinsic parameters of the camera

Camera Calibration:

Camera Calibration is nothing but estimating the parameters of a camera, parameters about the camera
are required to determine an accurate relationship between a 3D point in the real world and its
corresponding 2D projection (pixel) in the image captured by that calibrated camera.

Some pinhole cameras introduce significant distortion to images. Two major kinds of distortion are
radial distortion and tangential distortion.

Radial distortion causes straight lines to appear curved. Radial distortion becomes larger the farther
points are from the center of the image. For example, one image is shown below in Figure 1 which two
edges of a chess board are marked with red lines. But, you can see that the border of the chess board is
not a straight line and doesn't match the red line. All the expected straight lines are bulged out.

Figure 1: Example of radial distortion

Similarly, tangential distortion occurs because the image-taking lense is not aligned perfectly
parallel to the imaging plane. So, some areas in the image may look nearer than expected.
lOMoARcPSD|245 368 66

In addition to this, we need some other information, like the intrinsic and extrinsic parameters
of the camera. Intrinsic parameters are specific to a camera. They include information like focal
length ( fx, fy) and optical centers ( cx, cy). The focal length and optical centers can be used to
create a camera matrix, which can be used to remove distortion due to the lenses of a specific
camera. The camera matrix is unique to a specific camera, so once calculated, it can be reused
on other images taken by the same camera. It is expressed as a 3x3 matrix:

Extrinsic parameters correspond to rotation and translation vectors which translate the
coordinates of a 3D point to a coordinate system.

To find these parameters, we must provide some sample images of a well-defined pattern (e.g.
a chess board). We find some specific points of which we already know the relative positions
(e.g. square corners on the chess board). We know the coordinates of these points in real-world
space and we know the coordinates in the image, so we can solve for the distortion coefficients.
For better results, we need at least 10 test patterns.

Code

As mentioned above, we need at least 10 test patterns for camera calibration. OpenCV comes
with some images of a chess board (see samples/data/left01.jpg 3 left14.jpg), so we will utilize
these. Consider an image of a chess board. The important input data needed for the calibration
of the camera is the set of 3D real-world points and the corresponding 2D coordinates of these
points in the image. 2D image points are OK which we can easily find from the image. (These
image points are locations where two black squares touch each other on chess boards)

What about the 3D points from real-world space? Those images are taken from a static camera
and chess boards are placed at different locations and orientations. So we need to know (X, Y,
Z) values. But for simplicity, we can say the chess board was kept stationary at XY plane, (so
Z=0 always) and the camera was moved accordingly. This consideration helps us to find only
X and Y values. Now for X, and Y values, we can simply pass the points as (0,0), (1,0), and
(2,0), ... which denotes the location of the points. In this case, the results we get will be on the
scale of the size of the chess board square. But if we know the square size, (say 30 mm), we
can pass the values as (0,0), (30,0), (60,0), Thus, we get the results in mm. (In this case, we
don't know square size since we didn't take those images, so we pass in terms of square size).

3D points are called object points and 2D image points are called image points.
lOMoARcPSD|245 368 66

Camera Calibration can be done in a step-by-step approach:


Step 1: First define real-world coordinates of 3D points using the known size of the
checkerboard pattern.
Step 2: Different viewpoints of the check-board image are captured.
Step 3: findChessboardCorners() is a method in OpenCV and used to find pixel
coordinates (u, v) for each 3D point in different images
Step 4: Then calibrateCamera() method is used to find camera parameters.

Setup

So to find pattern in chess board, we can use the function, cv.findChessboardCorners(). We


also need to pass what kind of pattern we are looking for, like an 8x8 grid, 5x5 grid, etc. In this
example, we use a 7x6 grid. (Normally a chess board has 8x8 squares and 7x7 internal corners).
It returns the corner points and retval which will be True if the pattern is obtained. These
corners will be placed in an order (from left-to-right, top-to-bottom)

Once we find the corners, we can increase their accuracy using cv.cornerSubPix(). We can also
draw the pattern using cv.drawChessboardCorners(). All these steps are included below code:

import numpy as np

import cv2 as cv import

glob

# termination criteria

criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ............................................... ,(6,5,0)

objp = np.zeros((6*7,3), np.float32) objp[:,:2] =

np.mgrid[0:7,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.objpoints = [] # 3d point

in real world space

imgpoints = [] # 2d points in image plane.images =


glob.glob('*.jpg')

for fname in images:


img = cv.imread(fname)

gray = cv.cvtColor(img, cv.C0L0R_BGR2GRAY)

# Find the chess board corners

ret, corners = cv.findChessboardCorners(gray, (7,6), None)


lOMoARcPSD|245 368 66

# If found, add object points, image points (after refining them)

if ret == True:
objpoints.append(objp)

corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)

imgpoints.append(corners)

# Draw and display the corners cv.drawChessboardCorners(img,

(7,6), corners2, ret)cv.imshow('img', img)

cv.waitKey(500)
cv.destroyAllWindows()

Calibration

Now that we have our object points and image points, we are ready to go for calibration. We
can use the function, cv.calibrateCamera() which returns the camera matrix, distortion
coefficients, rotation and translation vectors, etc.

ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints,


gray.shape[::-1], None, None)

Undistortion

Now, we can take an image and undistort it. OpenCV comes with two methods for doing this.
However, first, we can refine the camera matrix based on a free scaling parameter
using cv.getOptimalNewCameraMatrix(). If the scaling parameter alpha=0, it returns an
undistorted image with minimum unwanted pixels. So it may even remove some pixels at
image corners. If alpha=1, all pixels are retained with some extra black images. This function
also returns an image ROI which can be used to crop the result.

img = cv.imread('left12.jpg')

h, w = img.shape[:2]

newcameramtx, roi = cv.get0ptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))

Using cv.undistort()

Just call the function and use ROI obtained above to crop the result.

# undistort

dst = cv.undistort(img, mtx, dist, None, newcameramtx)

# crop the image


lOMoARcPSD|245 368 66

x, y, w, h = roi

dst = dst[y:y+h, x:x+w]

cv.imwrite('calibresult.png', dst)

# Display the final result


cv2.imshow('chess board', np.hstack((img, undistortedImg)))
cv2.waitKey(0)

cv2.destroyAllWindows()

Lab Exercise

1. Perform the camera calibration and compute the intrinsic and extrinsic parameters of
the camera.
2. Use the camera calibration parameters to undistort the image.
lOMoARcPSD|245 368 66

Experiment No. 6
Aim: Compute Fundamental Matrix

Objectives:
 Find fundamental matrix, epipoles, epipolar lines.
 Plot epipole lines on the images.
 Find the projection matrix of the second camera position using the fundamental matrix.

Lab Exercise

1. Compute the Fundamental Matrix using the Least Square Minimization


2. Compute the Fundamental Matrix using the 8-point algorithm.
3. Plotting epipole lines using the Fundamental Matrix computed in (1).
4. Finding the projection matrix of the second camera using the Fundamental Matrix
(assuming that the first camera is calibrated).

he normalized eight-point algorithm is used to compute the fundamental matrix given point
correspondences x = (u, v) and x' = (u', v') in the left and right images, respectively. Each point
correspondence generates one constraint on the fundamental matrix F and must satisfy the epipolar
constraint equation

Expanding the matrices out by multiplication, we obtain the following equation for n point
correspondences

where A is the n x 9 equation matrix, and f is a 9-element column vector containing the entries of the
fundamental matrix F.

From here, the least-squares solution f is easily computed by performing singular value decomposition
(SVD) on the matrix A=UDVT. It is well-known that the vector f that minimizes ||Af|| such that ||f|| = 1 can
be found along the column of V corresponding to the least singular value. Next, we rearrange the 9 entries
of f to create the 3x3 fundamental matrix F. Then, we perform SVD on F to obtain

We set the smallest singular value of D f to 0 to create matrix D'f, thus reducing the rank of the matrix
from 3 to 2, and from there we can recompute the rank-2 fundamental matrix as

Using the version of the eight-point algorithm without prior coordinate normalization to compute the
fundamental matrix on the laboratory image pair, we obtain the following fundamental matrix F
(truncated to 4 decimal places) and the epipolar lines for both images:

If you look closely, you'll notice that the epipolar lines don't pass exactly through the center of all the
point correspondences. How can we improve this?

However, in order to improve the accuracy of epipolar lines generated in Part III, we want to modify the
'vanilla' eight-point algorithm to recenter the point correspondences xi = (ui, vi) and x'i = (u'i, v'i) in both
lOMoARcPSD|245 368 66

images to their respective centroids before proceeding to compute the least-squares solution for f. After
recentering the image points, we must scale the points to be a fixed squared distance from the origin. The
coordinate normalization steps can be summarized in the following steps:

1. Compute the centroid of all corresponding points in a single image:

2. Recenter by subtracting the mean u and v coordinates from the original point correspondences to
obtain

3. Define the scale term s and s' to be the average distances of the centered points from the origin in
both the left and right images:

4. Construct the transformation matrices Ta and Tb:

5. The normalized correspondence points can be computed from

6. Solve for the fundamental matrix F by applying the eight-point algorithm on the normalized set of
point correspondences computed in the previous step.
7. After obtaining the normalized fundamental matrix Fnorm, retrieve the fundamental matrix in the
original coordinate frame using the following formula

Using the normalized eight-point algorithm on the laboratory image pair, we get
lOMoARcPSD|245 368 66

and the fundamental matrix (truncated to 4 decimal places)


lOMoARcPSD|245 368 66

Experiment No. 7
Aim: Edge Detection, Line Detection and Corner Detection

Objectives:
 Find the edges in the image
 Corner detection with Harris Corner Detector
 Line detection

Lab Exercise

1. Compute the edge detection using Sobel, Prewitt and canny operator.
2. Implement the Harris Corner detector algorithm to determine the corner in the image.
3. Implement the Harris Corner Detector algorithm without the inbuilt OpenCV()
function.
4. Detect the line using Hough Transform

OpenCv functions: cv.Sobel, cv2.filter2D,cv2.cornerHarris(), cv2.HoughLines()

1. Compute the edge detection using Sobel, Prewitt and canny operator.
Prewitt Operator
The Prewitt operator was developed by Judith M. S. Prewitt. Prewitt operator is used for edge detection in an image.
Prewitt operator detects both types of edges, these are:
 Horizontal edges or along the x-axis,
 Vertical Edges or along the y-axis.
Prewitt operator provides us two masks one for detecting edges in the horizontal direction and another for detecting edges
in a vertical direction.
Prewitt Operator [X-axis] = [ -1 0 1; -1 0 1; -1 0 1]
Prewitt Operator [Y-axis] = [-1 -1 -1; 0 0 0; 1 1 1]
Steps:
 Read the image.
 Convert into grayscale if it is colored.
 Convert into the double format.
 Define the mask or filter.
 Detect the edges along X-axis.
 Detect the edges along Y-axis.
 Combine the edges detected along the X and Y axes.
 Display all the images.
Imtool() is the inbuilt function in Matlab. It is used to display the image. It takes 2 parameters; the first is the image
variable and the second is the range of intensity values. We provide an empty list as the second argument which means the
complete range of intensity has to be used while displaying the image.
Example:

% MATLAB code for prewitt

% operator edge detection

k=imread("logo.png");

k=rgb2gray(k);
lOMoARcPSD|245 368 66

k1=double(k);

p_msk=[-1 0 1; -1 0 1; -1 0 1];

kx=conv2(k1, p_msk, 'same');

ky=conv2(k1, p_msk', 'same');

ked=sqrt(kx.^2 + ky.^2);

% display the images.

imtool(k,[]);

% display the edge detection along x-axis.

imtool(abs(kx), []);

% display the edge detection along y-axis.

imtool(abs(ky),[]);

% display the full edge detection.

imtool(abs(ked),[]);

Output:
lOMoARcPSD|245 368 66

Scharr Operator
This is a filtering method used to identify and highlight gradient edges/features using the first derivative. Performance is
quite similar to the Sobel filter.
Scharr Operator [X-axis] = [-3 0 3; -10 0 10; -3 0 3];
Scharr Operator [Y-axis] = [ 3 10 3; 0 0 0; -3 -10 -3];
Example:

%Scharr operator -> edge detection

k=imread("logo.png");

k=rgb2gray(k);

k1=double(k);

s_msk=[-3 0 3; -10 0 10; -3 0 3];

kx=conv2(k1, s_msk, 'same');

ky=conv2(k1, s_msk', 'same');

ked=sqrt(kx.^2 + ky.^2);

%display the images.

imtool(k,[]);

%display the edge detection along x-axis.

imtool(abs(kx), []);

%display the edge detection along y-axis.

imtool(abs(ky),[]);

%display the full edge detection.

imtool(abs(ked),[]);

Output:
lOMoARcPSD|245 368 66

Sobel Operator
It is named after Irwin Sobel and Gary Feldman. Like the Prewitt operator Sobel operator is also used to detect two kinds of
edges in an image:

 Vertical direction
 Horizontal direction
The difference between Sobel and Prewitt Operator is that in Sobel operator the coefficients of masks are adjustable
according to our requirement provided they follow all properties of derivative masks.

Sobel-X Operator = [-1 0 1; -2 0 2; -1 0 1]


Sobel-Y Operator = [-1 -2 -1; 0 0 0; 1 2 1]
Example:

% MATLAB code for Sobel operator

% edge detection

k=imread("logo.png");

k=rgb2gray(k);
lOMoARcPSD|245 368 66

k1=double(k);

s_msk=[-1 0 1; -2 0 2; -1 0 1];

kx=conv2(k1, s_msk, 'same');

ky=conv2(k1, s_msk', 'same');

ked=sqrt(kx.^2 + ky.^2);

%display the images.

imtool(k,[]);

%display the edge detection along x-axis.

imtool(abs(kx), []);

%display the edge detection along y-axis.

imtool(abs(ky),[]);

%display the full edge detection.

imtool(abs(ked),[]);

Output:
lOMoARcPSD|245 368 66

Harris Corner Detection


Import resources and display image
In [1]:
import matplotlib.pyplot as plt
import numpy as np
import cv2

%matplotlib inline

# Read in the image


image = cv2.imread('images/waffle.jpg')

# Make a copy of the image


image_copy = np.copy(image)

# Change color to RGB (from BGR)


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

plt.imshow(image_copy)
Out[1]:
<matplotlib.image.AxesImage at 0x7ffb1f306eb8>

Detect corners
In [2]:
# Convert to grayscale
gray = cv2.cvtColor(image_copy, cv2.COLOR_RGB2GRAY)
gray = np.float32(gray)

# Detect corners
dst = cv2.cornerHarris(gray, 2, 3, 0.04)

# Dilate corner image to enhance corner points


dst = cv2.dilate(dst,None)

plt.imshow(dst, cmap='gray')
Out[2]:
<matplotlib.image.AxesImage at 0x7ffb1f2b2828>

Extract and display strong corners


In [3]:
# This value vary depending on the image and how many corners you want to detect
# Try changing this free parameter, 0.1, to be larger or smaller and see what happens
thresh = 0.1*dst.max()

# Create an image copy to draw corners on


lOMoARcPSD|245 368 66

corner_image = np.copy(image_copy)

# Iterate through all the corners and draw them on the image (if they pass the threshold)
for j in range(0, dst.shape[0]):
for i in range(0, dst.shape[1]):
if(dst[j,i] > thresh):
# image, center pt, radius, color, thickness
cv2.circle( corner_image, (i, j), 1, (0,255,0), 1)

plt.imshow(corner_image)
Out[3]:
<matplotlib.image.AxesImage at 0x7ffae844cac8>
lOMoARcPSD|245 368 66

Experiment No. 8
Aim: SIFT feature descriptors

Objectives:
 To understand the concepts of SIFT algorithm
 To find the key points and descriptors

Lab Exercise

1. Write a program to compute the SIFT feature descriptors of the image.


2. Write a program to generate a panorama image using SIFT feature descriptor.

OpenCV functions: cv.SIFT_create(), sift.detect, cv.drawKeypoints, sift.detectAndComputeMedium

# Important NOTE: Use opencv <= 3.4.2.16 as

# SIFT is no longer available in

# opencv > 3.4.2.16

#import cv2

# Loading the image

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

# Converting image to grayscale

gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Applying SIFT detector

sift = cv2.xfeatures2d.SIFT_create()

kp = sift.detect(gray, None)

# Marking the keypoint on the image using circles

img=cv2.drawKeypoints(gray ,

kp ,

img ,

flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
lOMoARcPSD|245 368 66

cv2.imwrite('image-with-keypoints.jpg', img)

Output:

The image on left is the original, the image on right shows the various highlighted interest points on the image

2. Write a program to generate a panorama image using SIFT feature descriptor.

import cv2
image_paths=['1.jpg','2.jpg','3.jpg']
# initialized a list of images
imgs = []

for i in range(len(image_paths)):
imgs.append(cv2.imread(image_paths[i]))
imgs[i]=cv2.resize(imgs[i],(0,0),fx=0.4,fy=0.4)
# this is optional if your input images isn't too large
# you don't need to scale down the image
# in my case the input images are of dimensions 3000x1200
# and due to this the resultant image won't fit the screen
# scaling down the images
# showing the original pictures
cv2.imshow('1',imgs[0])
cv2.imshow('2',imgs[1])
cv2.imshow('3',imgs[2])

stitchy=cv2.Stitcher.create()
(dummy,output)=stitchy.stitch(imgs)

if dummy != cv2.STITCHER_OK:
# checking if the stitching procedure is successful
# .stitch() function returns a true value if stitching is
# done successfully
print("stitching ain't successful")
else:
print('Your Panorama is ready!!!')

# final output
cv2.imshow('final result',output)

cv2.waitKey(0)

Output:
lOMoARcPSD|245 368 66

Experiment No. 9
Aim: SURF and HOG feature descriptors

Objectives:
 To understand the concepts of SURF and HOG algorithm
 To compute the SURF and HOG features

Lab Exercise

1. Write a program to compute the SURF feature descriptors of the image.


2. Write a program to compute the HOG feature descriptors of the image.
3. Write a program to detect the pedestrians in an image using HOG.

1. Write a program to compute the SURF feature descriptors of the image.


Syntax : surf.surf(img)
Argument : It takes image object as argument
Return : It returns numpy.ndarray

Example 1 :

 Python3

# importing various libraries

import mahotas

import mahotas.demos

import mahotas as mh

import numpy as np

from pylab import imshow, show

from mahotas.features import surf

# loading nuclear image

nuclear = mahotas.demos.nuclear_image()

# filtering image

nuclear = nuclear[:, :, 0]

# adding gaussian filter

nuclear = mahotas.gaussian_filter(nuclear, 4)

# showing image

print("Image")
lOMoARcPSD|245 368 66

imshow(nuclear)

show()

# getting Speeded-Up Robust Features

spoints = surf.surf(nuclear)

print("No of points: {}".format(len(spoints)))

Output :

No of points: 217
Example 2 :

 Python3

# importing required libraries

import numpy as np

import mahotas

from pylab import imshow, show

from mahotas.features import surf

# loading image

img = mahotas.imread('dog_image.png')

# filtering the image

img = img[:, :, 0]

# setting gaussian filter

gaussian = mahotas.gaussian_filter(img, 5)

# showing image
lOMoARcPSD|245 368 66

print("Image")

imshow(gaussian)

show()

# getting Speeded-Up Robust Features

spoints = surf.surf(gaussian)

print("No of points: {}".format(len(spoints)))

Output :

No of points: 364

2. Write a program to compute the HOG feature descriptors of the image.

we can use scikit-image library to extract HOG features from images.

First, let's install the necessary libraries for this tutorial:

pip3 install scikit-image matplotlib

I'm gonna perform HOG on a cute cat image, get it here and put it in the current working directory (you can
use any image you want, of course). Let's load the image and show it:

#importing required libraries


from skimage.io import imread
from skimage.transform import resize
from skimage.feature import hog
from skimage import exposure
import matplotlib.pyplot as plt

# reading the image


img = imread('cat.jpg')
plt.axis("off")
plt.imshow(img)
print(img.shape)
Output:
lOMoARcPSD|245 368 66

(1349, 1012, 3)
3. Write a program to detect the pedestrians in an image using HOG.
Requirements
opencv-python 3.4.2

imutils 0.5.3
To install the above modules type the below command in the terminal.
pip install moudle_name
Example 1:
Lets make the program to detect pedestrians in an Image:
Image Used:

import cv2

import imutils

# Initializing the HOG person

# detector

hog = cv2.HOGDescriptor()

hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# Reading the Image

image = cv2.imread('img.png')

# Resizing the Image

image = imutils.resize(image,

width=min(400, image.shape[1]))

# Detecting all the regions in the

# Image that has a pedestrians inside it

(regions, _) = hog.detectMultiScale(image,

winStride=(4, 4),

padding=(4, 4),
lOMoARcPSD|245 368 66

scale=1.05)

# Drawing the regions in the Image

for (x, y, w, h) in regions:

cv2.rectangle(image, (x, y),

(x + w, y + h),

(0, 0, 255), 2)

# Showing the output Image

cv2.imshow("Image", image)

cv2.waitKey(0)

cv2.destroyAllWindows()

Output:

Example 2: Lets make the program to detect pedestrians in a video:

import cv2

import imutils

# Initializing the HOG person

# detector

hog = cv2.HOGDescriptor()

hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

cap = cv2.VideoCapture('vid.mp4')

while cap.isOpened():

# Reading the video stream

ret, image = cap.read()


lOMoARcPSD|245 368 66

if ret:

image = imutils.resize(image,

width=min(400, image.shape[1]))

# Detecting all the regions

# in the Image that has a

# pedestrians inside it

(regions, _) = hog.detectMultiScale(image,

winStride=(4, 4),

padding=(4, 4),

scale=1.05)

# Drawing the regions in the

# Image

for (x, y, w, h) in regions:

cv2.rectangle(image, (x, y),

(x + w, y + h),

(0, 0, 255), 2)

# Showing the output Image

cv2.imshow("Image", image)

if cv2.waitKey(25) & 0xFF == ord('q'):

break

else:

break

cap.release()

cv2.destroyAllWindows()

Output:
Video Player
00:00

You might also like