You are on page 1of 6

Lab # 2 Introduction to OpenCV

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

Use command “Pip install opencv” or “Pip install opencv-contrib-python”

To create a 2D array of zeros and ones using


NumPy:
my_array = numpy.zeros (( row, columns ),dtype=numpy.uint8)
import numpy
a = numpy.zeros (( 5, 9 ),dtype=numpy.uint8)
print(a)
b = numpy.ones (( 3, 3 ) ,dtype=numpy.uint8)
print(b)
c = numpy.zeros (( 3, 3 ) ,dtype=numpy.uint8)
print(c)
[[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]]
[[1 1 1]
[1 1 1]
[1 1 1]]
[[0 0 0]
[0 0 0]
[0 0 0]]

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

# To assemble an array from nested lists of


blocks
all the input array dimensions except for the concatenation axis must match exactly
img = numpy.block([[numpy.ones((2, 2)), numpy.zeros((2, 3))], [numpy.zeros((2, 3)), numpy.ones((2, 2))]])
print(img)
[[1. 1. 0. 0. 0.]
[1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1.]
[0. 0. 0. 1. 1.]]

# To read coloured image


import cv2
img = cv2.imread('sample.png',1)
cv2.imshow('Machine Learning',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(img.shape)
img[100,100]
#img[:]
B,G,R = img[100,100]
print(B,G,R)
roi = img[100 : 200, 200 : 400]
cv2.imshow('Machine Learning',roi)
cv2.waitKey(0)
cv2.destroyAllWindows()
(449, 516, 3)
16 168 189

# Accessing specific location pixel values(RGB


values)
# BGR (Order)
print(img.shape)
print(img[100,100])
print(img[140,110])
print(img[50,30])

(449, 516, 3)
[ 16 168 189]
[ 1 163 205]
[ 5 170 225]

# For greyscale image


#to read greyscale image
img1 = cv2.imread('sample.png',0)
cv2.imshow('Machine Learning',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(img1.shape)
print(img1[100,100])
print(img1[140,110])
print(img1[50,30])
(449, 516)
166
169
181

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

# Drawing a Rectangle on image


rectangle() function take 5 Arguments

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

# Displaying text on Image in Python using


OpenCV
putText() Function takes in 7 arguments –

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:

1) Find dimension of an image.


2) What are the values of pixel at location (10,10) , (50,100) and (120,201) for both type of
images?
3) Read Image from Height 50-200 Pixel and Width 10-40 Pixel
4) Draw a rectangle on an image of red color and line width 5.
5) Write your name and registration number on your image.

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)

You might also like