Basic Python OpenCV
Basic Python OpenCV
What is OpenCV
OpenCV ( Open Source Computer Vision Library) is an open source software library for computer vision and machine
learning. OpenCV was created to provide a shared infrastructure for applications for computer vision and to speed up the
use of machine perception in consumer products. OpenCV, as a BSD-licensed software, makes it simple for companies to
use and change the code. There are some predefined packages and libraries that make our life simple and OpenCV is
one of them.
Gary Bradsky invented OpenCV in 1999 and soon the first release came in 2000. This library is based on optimised C /
C++ and supports Java and Python along with C++ through an interface. The library has more than 2500 optimised
algorithms, including an extensive collection of computer vision and machine learning algorithms, both classic and state-
of-the-art.Using OpenCV it becomes easy to do complex tasks such as identify and recognise faces, identify objects,
classify human actions in videos, track camera movements, track moving objects, extract 3D object models, generate 3D
point clouds from stereo cameras, stitch images together to generate an entire scene with a high resolution image and
many more.
Python is a user friendly language and easy to work with but this advantage comes with a cost of speed, as Python is
slower to languages such as C or C++.So we extend Python with C/C++, which allows us to write computationally
intensive code in C/C++ and create Python wrappers that can be used as Python modules. Doing this, the code is fast, as
it is written in original C/C++ code (since it is the actual C++ code working in the background) and also, it is easier to code
in Python than C/C++. OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation.
cv2.imread(path, flag)
path - string representing the path of the image to be read. The file should be in the working directory or we must
give the full path to the image
flag - integer value (1, 0 & -1) specify how our image should be read
cv2.IMREAD_COLOR: It specifies to convert the image to the 3 channel BGR colour image. Any transparency of
image will be neglected. It is the default flag. Alternatively, we can passinteger value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to convert an image to thesingle channel grayscale image. Alternatively,
we can pass integer value 0 for this flag.
cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha channel.Alternatively, we can
pass integer value -1 for this flag.
Usually the method imread() returns an image that is loaded from the specified file but in case the image cannot be read
because of unsupported file format, missing file, unsupported or invalid format, it just returns a matrix. Here is a example
in which we read an image from my storage.
Example
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 1/20
1/14/25, 10:54 AM Basic OpenCV
#importing the opencv module
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg',1)
#This is using for display the image
cv2.imshow('image',img)
cv2.waitKey(0) # This is necessary to be required so that the image doesn't close immediate
ly
#It will run continuously until the key press
cv2.destroyAllWindows()
cv2.imwrite(filename, image)
filename - string representing the image file name. The filename must include image format
image - It is the image object that is to be saved
Example
import cv2
# read image in grayscale
img_gray = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 0)
# save image
status = cv2.imwrite('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog_Gray.png',img_gray)
print(f"Image written sucess? : {status}")
Out:
If the file is successfully written then this function returns True and thus it is important to store the outcome of this function.
In the example above, we have done the same and used the ‘status’ variable to know if the file is written successfully.
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 2/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
img = cv2.imread('img\OpenCV_Dog.jpg')
px = img[100,100]
print(px)
Out:
The result of pixel at position (100, 100) ( img[100, 100] ) shows 3 values [204 214 232]. As we know OpenCV stores the
color image as BGR color image, so the first value in the list is the value of the blue channel of this particular pixel, and
the rest are values for green and red channels.
We can also access only one of the channels as shown below.
Example
# accessing only blue pixel
blue = img[100, 100, 0]
green = img[100, 100, 1]
red = img[100, 100, 2]
print(f"blue - {blue}, green - {green}, red - {red}")
Out:
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 3/20
1/14/25, 10:54 AM Basic OpenCV
To modify the values, we just need to access the pixel and then overwrite it with a value.
Example
img[100, 100] = [255, 255, 255]
print(img[100,100])
This method to access and modify the pixel values is slow so you should make use of NumPy library as it is optimized for
fast array calculations. For accessing individual pixel values, the Numpy array methods, array.item() and array.itemset()
are considered better as they always return a scalar. However, if you want to access all the B,G,R values, you will need to
call array.item() separately for each value as shown below.
Example
# accessing RED value
img.item(100, 100,2)
Out:
232
100
Example
# Colour Image
print(img.shape) # Result: (row, column, channels) e.g. (183, 275, 3) - ROW: 183, COLUMN:
275, CHANNELS: 3
print(img.size) # Result: Total number of pixels
# Grayscale Image
print(img_gray.shape) # Result: (row, column) e.g. (183, 275) - ROW: 183, COLUMN: 275
print(img.size)
Out:
(183, 275, 3)
150975
(183, 275)
150975
img.shape returns three numbers in tuple, these are number of rows, number of columns and number of channels
respectively. In case an image is grayscale, the tuple returned contains only the number of rows and columns.
Often a large number of errors in OpenCV-Python code are caused by invalid datatype so img.dtype which returns the
image datatype is very important while debugging.
Example
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 4/20
1/14/25, 10:54 AM Basic OpenCV
print(img.dtype)
print(img_gray.dtype)
Out:
uint8
uint8
Example
b,g,r = cv.split(img)
img = cv.merge((b,g,r))
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
Now suppose you want to just set all the values in the red channel to zero.
Example
#sets all values in red channel as zero
img[:,:,2] = 0
cv2.resize(s, size,fx,fy,interpolation)
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 5/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
print(f"Image Original Size - {img.shape}")
Example
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
print(img.shape)
img_rotate = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 6/20
1/14/25, 10:54 AM Basic OpenCV
Now what if we want to rotate the image by a certain angle. We can use another method for that. First calculate the affine
matrix that does the affine transformation (linear mapping of pixels) by using the getRotationMatrix2D method, next we
warp the input image with the affine matrix using warpAffine method.
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 - scales the image by the value provided, 1.0 means the shape is preserved
H - height of image
W - width of the image
M - affine matrix returned by cv2.getRotationMatrix2D
Img - image to be rotated
Example
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 7/20
1/14/25, 10:54 AM Basic OpenCV
import cv2
import numpy as np
scale = 1.0
# 110 degrees
M = cv2.getRotationMatrix2D(center,110, scale)
rotated110 = cv2.warpAffine(img_resize, M, (w, h))
# 150 degrees
M = cv2.getRotationMatrix2D(center, 150, scale)
rotated150 = cv2.warpAffine(img_resize, M, (h, w))
Drawing circle:
We use the method to circle to draw a circle in an image.
Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
cv2.circle(img,(80,80), 55, (255,0,0), -1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
Drawing Rectangle
In a similar we can draw a rectangle.
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequenc… 9/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
cv2.rectangle(img,(15,25),(200,150),(0,255,255),15)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
Drawing Lines
Here is the syntax of the line method using which we can make lines on an image.
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 10/20
1/14/25, 10:54 AM Basic OpenCV
Example
import numpy as np
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
# Start coordinate, here (0, 0). Represents the top left corner of image
start_point = (0, 0)
# End coordinate, here (250, 250). Represents the bottom right corner of image
end_point = (250, 250)
# Line thickness of 9 px
thickness = 4
Drawing Polylines
We can draw the polylines using the polylines() method on the image. And these can be used to draw polygonal curves on
the image.
Example
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 11/20
1/14/25, 10:54 AM Basic OpenCV
import numpy as np
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
#defining points for polylines
pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)
cv2.polylines(img, [pts], True, (0,255,255), 3)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
Example
import cv2
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg', 1)
cv2.putText(img,'Dog',(10, 35), font, 1,(255,255,255),2)
# Display the image
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 12/20
1/14/25, 10:54 AM Basic OpenCV
As we can see we have two threshold values, minVal and maxVal. Any edges with intensity gradient more than maxVal
are sure to be edges, also those edges with intensity gradient less than minVal are sure to be non-edges and are
discarded. The edges which lie between these two thresholds are classified edges or non-edges based on their
connectivity with the ‘sure edges’. If they are connected to “sure-edge” pixels, they are considered to be part of edges.
Otherwise, they are also discarded as non-edges.
Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
edges = cv2.Canny(img,200,300,True)
cv2.imshow("Edge Detected Image", edges)
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image
Out:
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 13/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 14/20
1/14/25, 10:54 AM Basic OpenCV
Here are a few of the methods that we are going to use for smoothing an image:
OpenCV averaging
OpenCV median Blur
OpenCV Gaussian Blur
OpenCV Bilateral Filter
OpenCV averaging
In this technique, we normalize the image with a box filter. It calculates the average of all the pixels which are under the
kernel area(box filter) and replaces the value of the pixel at the center of the box filter with the calculated average.
OpenCV provides the cv2.blur() to perform this operation.The syntax of cv2.blur() function is as follows.
Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
img_blur = cv2.blur(img, (10,10))
cv2.imshow('cv2.blur output', img_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 15/20
1/14/25, 10:54 AM Basic OpenCV
cv2.medianBlur(src, ksize)
Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
img_median_blur = cv2.medianBlur(img, 11)
cv2.imshow('cv2.medianBlur output', img_median_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
Example
import cv2
img = cv2.imread('/content/enforced/31788-ME2631_NEW/img/OpenCV_Dog.jpg')
img_gaussian_blur = cv2.GaussianBlur(img, (7, 7), cv2.BORDER_DEFAULT)
cv2.imshow('cv2.GaussianBlur output', img_gaussian_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 16/20
1/14/25, 10:54 AM Basic OpenCV
Out:
OpenCV Contours
What are contours? A Contour is a curve joining all the continuous points having the same color or intensity (along the
boundary). Counters are useful especially when we want to find out a shape of some object or incase of object detection
and recognition. Finding contours is like finding white object from black background, so remember, the object to be found
should be white and background should be black. Thus for better accuracy, we should use binary images and before
finding contours, apply thresholding as we discussed in the last section.
In OpenCV,we use two functions,one to find contours and other to draw contours.We use findContours() function to find
contours and drawCounter() to draw one.
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 17/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.imshow('Original', image)
cv2.imshow('Canny Edges After Contouring', edged)
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Out:
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 18/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
capture = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 19/20
1/14/25, 10:54 AM Basic OpenCV
Example
import cv2
cap = cv2.VideoCapture('vtest.avi')
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/545684?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequen… 20/20