DIP Lab Manual No 04
DIP Lab Manual No 04
DIP Lab Manual No 04
Lab Manual No 04
Learning Outcomes:-
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:
wt1: Weight of the first input image elements to be applied to the final image
wt2: Weight of the second input image elements to be applied to the final image
Example:
# 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')
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
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.
Syntax:
cv2.bitwise_and(source1, source2, mask)
Parameters:
Example:
# cv2.bitwise_and is applied over the image inputs with applied parameters
Syntax:
cv2.bitwise_or(source1, source2)
Parameters:
Example:
# cv2.bitwise_or is applied over the image inputs with applied parameters
Syntax:
cv2.bitwise_xor(source1, source2)
Parameters:
Example:
# cv2.bitwise_xor is applied over the image inputs with applied parameters
dest_xor = cv2.bitwise_xor(img1, img2)
Syntax:
cv2.bitwise_not(source)
Parameters:
Example:
# cv2.bitwise_not is applied over the image input with applied parameters
dest_not1 = cv2.bitwise_not(img1)
Syntax:
cv2.warpAffine(src, M, dsize)
Parameters:
M – 2 x 3 transformation matrix.
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')
#OR
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:
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.
where,
Parameters:
center: center of the image (the point about which rotation has to happen)
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")
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.
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
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
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)
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.