UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Digital Image Processing
Lab Manual No 04
Mathematical Operations, Geometric Transformations and Image Registration
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Learning Outcomes:-
Addition and Subtraction of Images
Logical Operation on Images
Geometric Transformation of Images
Arithmetic Operations on Images using OpenCV (Addition and Subtraction):
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations (AND, OR, NOT,
XOR) can be applied to the input images. These operations can be helpful in enhancing the
properties of the input images. The image arithmetic’s are important for analyzing the input
image properties. The operated images can be further used as an enhanced input image, and
many more operations can be applied for clarifying, thresholding, dilating etc. of the image.
Addition of Image:
We can add two images by using function cv2.add(). This directly adds up image pixels in the
two images.
Syntax:
cv2.add(img1, img2)
But adding the pixels is not an ideal situation. So, we use cv2.addweighted(). Remember, both
images should be of equal size and depth.
Syntax:
cv2.addWeighted(img1, wt1, img2, wt2, gammaValue)
Parameters:
img1: First Input Image array (Single-channel, 8-bit or floating-point)
wt1: Weight of the first input image elements to be applied to the final image
img2: Second Input Image array (Single-channel, 8-bit or floating-point)
wt2: Weight of the second input image elements to be applied to the final image
gammaValue: Measurement of light
Example:
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
# Python programe to illustrate arithmetic operation of addition of two
images
# organizing imports
import cv2
import numpy as np
# path to input images are specified and images are loaded with imread
command
image1 = cv2.imread('rice.png')
image2 = cv2.imread('cameraman.jpg')
# cv2.addWeighted is applied over the image inputs with applied parameters
weightedSum = cv2.addWeighted(image1, 0.5, image2, 0.4, 0)
# the window showing output image with the weighted sum
cv2.imshow('Weighted Image', weightedSum)
# De-allocate any associated memory usage
if cv2.waitKey(0):
cv2.destroyAllWindows()
Subtraction of Image:
Just like addition, we can subtract the pixel values in two images and merge them with the help
of cv2.subtract(). The images should be of equal size and depth.
Syntax:
cv2.subtract(src1, src2)
Example:
# cv2.subtract is applied over the image inputs with applied parameters
sub = cv2.subtract(image1, image2)
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Arithmetic Operations on Images using OpenCV (Bitwise Operations on Binary Images):
Bitwise operations are used in image manipulation and used for extracting essential parts in the
image. Bitwise operations used are:
AND
OR
XOR
NOT
Bitwise operations help in image masking. Image creation can be enabled with the help of these
operations. These operations can be helpful in enhancing the properties of the input images. The
Bitwise operations should be applied on input images of same dimensions.
Bitwise AND operation on Image:
Bit-wise conjunction of input array elements.
Syntax:
cv2.bitwise_and(source1, source2, mask)
Parameters:
source1: First Input Image array (Single-channel, 8-bit or floating-point)
source2: Second Input Image array (Single-channel, 8-bit or floating-point)
mask: Operation mask, Input / output 8-bit single-channel mask
Example:
# cv2.bitwise_and is applied over the image inputs with applied parameters
dest_and = cv2.bitwise_and(img2, img1, mask = None)
Bitwise OR operation on Image:
Bit-wise disjunction of input array elements.
Syntax:
cv2.bitwise_or(source1, source2)
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Parameters:
source1: First Input Image array (Single-channel, 8-bit or floating-point)
source2: Second Input Image array (Single-channel, 8-bit or floating-point)
Example:
# cv2.bitwise_or is applied over the image inputs with applied parameters
dest_or = cv2.bitwise_or(img2, img1)
Bitwise XOR operation on Image:
Bit-wise exclusive-OR operation on input array elements.
Syntax:
cv2.bitwise_xor(source1, source2)
Parameters:
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
Example:
# cv2.bitwise_xor is applied over the image inputs with applied parameters
dest_xor = cv2.bitwise_xor(img1, img2)
Bitwise NOT operation on Image:
Inversion of input array elements.
Syntax:
cv2.bitwise_not(source)
Parameters:
source: Input Image array (Single-channel, 8-bit or floating-point)
Example:
# cv2.bitwise_not is applied over the image input with applied parameters
dest_not1 = cv2.bitwise_not(img1)
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Geometric Transformations of Images:
OpenCV provides two transformation functions, cv2.warpAffine and cv2.warpPerspective,
with which you can have all kinds of transformations. cv2.warpAffine takes a 2x3
transformation matrix while cv2.warpPerspective takes a 3x3 transformation matrix as input.
Syntax:
cv2.warpAffine(src, M, dsize)
Parameters:
src – input image.
M – 2 x 3 transformation matrix.
dsize – size of the output image.
Scaling:
Scaling is just resizing of the image. OpenCV comes with a function cv.resize() for this purpose.
The size of the image can be specified manually, or you can specify the scaling factor. Different
interpolation methods are used. Preferable interpolation methods are cv.INTER_AREA for
shrinking and cv.INTER_CUBIC (slow) & cv.INTER_LINEAR for zooming. By default,
interpolation method used is cv.INTER_LINEAR for all resizing purposes.
import cv2
import numpy as np
img = cv2.imread('messi5.jpg')
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
#OR
height, width = img.shape[:2]
res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
Translation:
Translation is the shifting of object's location. If you know the shift in (x, y) direction, let it be
(𝑡𝑡,𝑡𝑡), you can create the transformation matrix M as follows:
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
You can take make it into a Numpy array of type np.float32 and pass it into cv.warpAffine()
function. See below example for a shift of (100,50).
import cv2
import numpy as np
img = cv2.imread('messi5.jpg',0)
rows,cols = img.shape
M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow('img',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Rotation:
Rotation of an image for an angle \theta is achieved by the transformation matrix of the form.
But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at
any location you prefer. Modified transformation matrix is given by.
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
where,
To find this transformation matrix, OpenCV provides a function, cv2.getRotationMatrix2D.
Following example rotates the image by 90 degree with respect to center with a scaling factor.
M = cv2.getRotationMatrix2D(center, angle, scale)
Parameters:
center: center of the image (the point about which rotation has to happen)
angle: angle by which image has to be rotated in the anti-clockwise direction.
scale: 1.0 mean, the shape is preserved. Other value scales the image by the value provided.
Example:
import cv2
import numpy as np
img = cv2.imread("red_panda.jpg")
rows, cols, ch = img.shape
print("Height: ", rows)
print("Width: ", cols)
scaled_img = cv2.resize(img, None, fx=rows/2, fy=cols/2)
matrix_t = np.float32([[1, 0, -100], [0, 1, -30]])
translated_img = cv2.warpAffine(img, matrix_t, (cols, rows))
matrix_r = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 0.5)
rotated_img = cv2.warpAffine(img, matrix_r, (cols, rows))
cv2.imshow("Original image", img)
cv2.imshow("Scaled image", scaled_img)
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
cv2.imshow("Translated image", translated_img)
cv2.imshow("Rotated image", rotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Affine Transformation:
Before talking about affine transformations, let's see what Euclidean transformations are.
Euclidean transformations are a type of geometric transformations that preserve length and angle
measure. As in, if we take a geometric shape and apply Euclidean transformation to it, the shape
will remain unchanged. It might look rotated, shifted, and so on, but the basic structure will not
change. So technically, lines will remain lines, planes will remain planes, squares will remain
squares, and circles will remain circles.
Coming back to affine transformations, we can say that they are generalizations of Euclidean
transformations. Under the realm of affine transformations, lines will remain lines but squares
might become rectangles or parallelograms. Basically, affine transformations don't preserve
lengths and angles. In order to build a general affine transformation matrix, we need to define the
control points. Once we have these control points, we need to decide where we want them to be
mapped. In this particular situation, all we need are three points in the source image, and three
points in the output image. To get the transformation matrix, we have a function called
getAffineTransform in OpenCV. Once we have the affine transformation matrix, we use the
warpAffine function to apply this matrix to the input image. This transformation leads to change
in the point of view of the image.
To obtain the transformation matrix we need three points from the source image and three points
of the destination image to define the planes of transformation. Then using the function
cv2.getAffineTransform we get a 2×3 matrix which we pass into the cv2.warpAffine function.
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Example:
img = cv2.imread('drawing.png')
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
Perspective Transform:
Affine transformations are nice, but they impose certain restrictions. A projective transformation,
on the other hand, gives us more freedom. It is also referred to as homography. In order to
understand projective transformations, we need to understand how projective geometry works.
We basically describe what happens to an image when the point of view is changed. For
example, if you are standing right in front of a sheet of paper with a square drawn on it, it will
look like a square. Now, if you start tilting that sheet of paper, the square will start looking more
and more like a trapezoid. Projective transformations allow us to capture this dynamic in a nice
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
mathematical way. These transformations preserve neither sizes nor angles, but they do preserve
incidence and cross-ratio.
Now that we know what projective transformations are, let's see if we can extract more
information here. We can say that any two images on a given plane are related by a homography.
As long as they are in the same plane, we can transform anything into anything else. This has
many practical applications such as augmented reality, image rectification, image registration, or
the computation of camera motion between two images. Once the camera rotation and translation
have been extracted from an estimated homography matrix, this information may be used for
navigation, or to insert models of 3D objects into an image or video. This way, they are rendered
with the correct perspective and it will look like they were part of the original scene.
Examples:
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('red_panda.jpg')
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
plt.show()
cv2.imshow('image',dst)
cv2.waitKey(0)
cv2.imwrite('result.png',dst)
Digital Image Processing Lab Instructor:- Sheharyar Khan
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
cv2.destroyAllWindows()
Lab Tasks:-
1. Capture your own photo and calculate the mirror image of the image
using affine transformation.
2. Read the image named “Task2.jpg” and transform the image into an
upright position using perspective transformation.
3. Write a python script to insert a watermark into an image by using
image blending.
4. Perform image masking to hide all other details in the image other than
the face of subject.
5. Read images named as rectangle.png and circle.png and perform all type
of logical operation on them and explain the results.
Digital Image Processing Lab Instructor:- Sheharyar Khan