Digital Image Processing Lab Manual# 2
Digital Image Processing Lab Manual# 2
Objective:
The objective of this lab is to introduce the student with OpenCV especially with respect to
image processing.
Theory:
Open Source Computer Vision Library (OpenCV) is an open source software library written
in C++ for machine learning and computer vision applications.
Why OpenCV?
OpenCV is comprehensive collection of more than 2500 machine learning and computer
vision algorithms that can be used from something as simple detecting faces in images to
project augmented reality overlaid with scenery.
Another area in which OpenCV excels is its superior support for multiple interfaces and all
the major operating systems as compared to other solutions. OpenCV supports Windows,
Linux, OS X and Android and provides interfaces for C, C++, Python, Java and MATLAB.
OpenCV is used extensively from tech giants such as Google, Microsoft, Intel, IBM etc. to
start-ups like Applied Minds, VideoSurf and Zeitera.
Some of the applications that can be accomplished easily with OpenCV are: identifying
objects, tracking camera movements, stitching images together, finding similar images in a
database using an image, face detection and tracking moving objects in a video feed etc.
OpenCV is used for all sorts of image and video analysis, like facial recognition and
detection, license plate reading, photo editing, advanced robotic vision, optical character
recognition, and a whole lot more.
How to install:
Open cmd.
Go to the directory where python is installed
Array concatenation
#F = numpy.concatenate ( (a,b) ,axis=0);
q = numpy.concatenate ( (c,b) ,axis=0);
print(q)
[[0 0 0]
[0 0 0]
[0 0 0]
[1 1 1]
[1 1 1]
[1 1 1]]
(449, 516, 3)
[ 16 168 189]
[ 1 163 205]
[ 5 170 225]
# Region of interest
roi = img[100 : 200, 200 : 400]
cv2.imshow('Machine Learning',roi)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Resizing an image
resize = cv2.resize(img, (200, 180))
cv2.imshow('Machine Learning',resize)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image
Top-left corner co-ordinates
Bottom-right corner co-ordinates
Color (in BGR format)
Line width
output = img.copy()
rectangle = cv2.rectangle(output, (260, 10),(460, 200), (0, 255, 0), 5)
cv2.imshow('Machine Learning',rectangle)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image
Text to be displayed
Bottom-left corner co-ordinates, from where the text should start
Font
Font size
Color (BGR format)
Line width
output = img.copy()
text = cv2.putText(output, 'Hafsa Tanveer', (120, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow('Machine Learning',text)
cv2.waitKey(0)
cv2.destroyAllWindows()
Lab Tasks:
Task#1
Read any image that you want using the right command and display it in gray scale and RGB
scale and perform following:
Task#2
Write python code to create a border around any image as shown below. The length
of top and bottom border must be 10% of the original height of the image and length of right
and left borders must be 10% of the original horizontal length of the image. Save the image
For example:
Task#3
Write a function to create a white image of 500x500 (or any other size entered
by the user) and then create 4 boxes of Red, Green, Blue and Black respectively on each
corner of the image as shown below. The size of the colored boxes should be 1/8th the size
of the image. (HINT: the arrays of ones and zeros can be in more than 2 dimensions)