Introduction to
OpenCV
PRESENTED BY: NOOR-E-HIRA
DATED: OCTOBER 02, 2019
Topics covered
Installation of
◦ Anaconda
◦ OpenCv2
Python codes, understanding and practice including
◦ loading, displaying and saving images
◦ Understanding pixels, image coordinates
◦ Drawing lines and shapes using cv2 functions
Installation of
Anaconda
Installation of Anaconda
Download the suitable package for your
machine from this link (recommended to install
for python3)
Follow the setup installation wizard
During the installation process when asked to
add the path to environment, check the box if
your system don’t have any preinstalled python
environment otherwise keep it unchecked.
(leave unchecked is recommended)
Continue the installation till end
Installation of Anaconda
Open Anaconda Prompt and type the below commands
◦ where conda
◦ Where python
Setting the path
Open Environment Variables window
Click edit button
Add the paths displayed on anaconda prompt .
Be careful to add semi-colon between two different paths.
Installation of
OpenCV2
Installation of OpenCV2
Create virtual environment using the command
◦ Conda create --name opencv-env python=3.7
Activate virtual environment using the command
◦ activate opencv-env
Installation of OpenCV2
Install the required packages using the
following commands:
◦ Pip install numpy scipy matplotlib scikit-learn
jupyter spyder
◦ Pip install opencv-contrib-python
Test your installations
◦ Import cv2
◦ cv.__version__
Open anaconda navigator from start
menu
◦ Move to the environment you created from
list against applications on
◦ Launch spyder
Installation of
OpenCV2
Loading, Displaying & saving image
import cv2
image =cv2.imread("C:/Users/Dell/Pictures/image.jpg")
print ("width: {} pixels".format(image.shape[1]))
print("channels: {}".format(image.shape[2]))
cv2.imshow("image", image)
cv2.imwrite("C:/Users/Dell/Pictures/image_copy.png", image)
cv2.waitKey(0)
Working with pixels:
import cv2
image =cv2.imread("C:/Users/Dell/Pictures/image.jpg")
(b,g,r)=image[0,0]
print ("pixel at (0,0) = Red: {}, Green: {}, Blue: {}".format(r,g,b))
image[0,0]=(255,255,255)
(b,g,r)=image[0,0]
print ("pixel at (0,0) = Red: {}, Green: {}, Blue: {}".format(r,g,b))
corner =image [0:100, 0:100]
cv2.imshow("Corner",corner)
image[0:100,0:100]=(255,255,255)
cv2.imshow("updated", image)
cv2.waitKey(0)
Drawing
import numpy as np
import cv2
canvas=np.zeros((300,300,3), dtype="uint8")
#300 rows and colums, 3 channels
cv2.imshow("canvas",canvas)
cv2.waitKey(0)
Drawing a line
green =(0,255,0)
cv2.line(canvas, (0,0), (300,300), green)
cv2.imshow("canvas",canvas)
cv2.waitKey(0)
Drawing a rectangle
red =(0,0,255)
cv2.rectangle(canvas, (0,150), (150,0), red, 5)
cv2.imshow("canvas",canvas)
cv2.waitKey(0)
Solid color filled Rectangle
blue =(255,0,0)
cv2.rectangle(canvas, (50,150), (150,50), blue, -1)
cv2.imshow("canvas",canvas)
cv2.waitKey(0)
Drawing Circles
canvas =np.zeros((300,300,3), dtype="uint8")
(centerX, centerY)=(canvas.shape[1]//2, canvas.shape[0]//2)
#print(canvas.shape[0])
white=(255,255,255)
for r in range(0, 175, 25):
cv2.circle(canvas, (centerX, centerY), r, white)
Drawing solid filled circles at different
points with different radii
for i in range(0,20):
radius=np.random.randint(5, high =200)
color=np.random.randint(0,high=256, size=(3,)).tolist()
pt=np.random.randint(0, high=300,size=(2,))
cv2.circle(canvas, tuple(pt), radius, color, -1)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
Flipping Image
import cv2
image =cv2.imread("C:/Users/Dell/Pictures/image.jpg")
cv2.imshow("Original Image", image)
cv2.waitKey(0)
flippedH=cv2.flip(image,1)
cv2.imshow("flipped horizontally", flippedH)
cv2.waitKey(0)
flippedV=cv2.flip(image,0)
cv2.imshow("flipped vertically", flippedV)
cv2.waitKey(0)
flippedHV=cv2.flip(image,-1)
cv2.imshow("flipped horizontally & vertically", flippedHV)
cv2.waitKey(0)
Image Arithmetic
import numpy as np
import cv2
image =cv2.imread("C:/Users/Dell/Pictures/image.jpg")
M=np.ones(image.shape, dtype="uint8") *100
added= cv2.add(image,M)
subtracted= cv2.subtract(image,M)
cv2.imshow("added", added)
cv2.waitKey(0)
cv2.imshow("subtracted", subtracted)
cv2.waitKey(0)
Thankyou!