Opencv is an open source library which is very useful for computer vision applications such as video analysis, CCTV footage analysis and image analysis. OpenCV is written by C++ and has more than 2,500 optimized algorithms. When we create applications for computer vision that we don’t want to build from scratch we can use this library to start focusing on real world problems. There are many companies using this library today such as Google, Amazon, Microsoft and Toyota. Many researchers and developers contribute. We can easily install it in any OS like Windows, Ubuntu and MacOS.
Run the command below to install the OpenCV library in the Ubuntu system
sudo apt install python3-opencv
For more information check out the link Here
First, import OpenCV module in Python code, which is very important to use all the functionality of the OpenCV library.
Import cv2
This is Syntax to read the image:
variable= cv2.imread(‘path of the image file’)
Example code
The image file name is called balavenkatesh.jpg
1 2
img = cv2.imread('balavenkatesh.jpg') cv2.imshow('OutputImage', img)
Syntax to write image:
variable= cv2.imwrite(‘path of the image file’)
__Here is the Example code __
1 2
img = cv2.imread('balavenkatesh.jpg') cv2.imshow('Original Image', img)
Show the written image using the code below.
cv2.imwrite('output.jpg',img)
It provides the shape value of an image, like height and width pixel values.
img = cv2.imread('photo.jpg')
Now you can print the image shape using print method:
1
2
print(img.shape)
(320, 240, 3)
1
2
3
4
print("Height pixel values : ", img.shape[0])
Height pixel values: 320
print("Width pixel values : ", img.shape[1])
Width pixel values: 240
We can easily rotate images using OpenCV library.
1
2
3
4
5
6
img = cv2.imread("balavenkatesh.jpg")
height, width = img.shape[: 2]
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), 70, .5)
rotated_image = cv2.warpAffine(img, rotation_matrix, (width, height))
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('Original image', img)
We can use the transpose method to rotate an image directly at 90 degrees by using cv2.transpose()
function.
Example
1 2 3 4
img = cv2.imread('photo.jpg') rotated_image = cv2.transpose(img) cv2.imshow('Rotated Image', rotated_image) cv2.imshow('original image', img)
OpenCV has a function to read video, which is cv2.VideoCapture(). We can access our webcam using pass 0 in the function parameter. If you want to capture CCTV footage then we can pass RTSP url in the function parameter, which is really useful for video analysis.
Syntax:
video = cv2.VideoCapture(0)
Example code
1
2
video = cv2.VideoCapture(0)
check, frame = video.read()
If you want to crop your image, you can easily do so in OpenCV using the code below.
Example
1
2
3
4
5
6
7
img = cv2.imread("photo.jpg")
height, width = img.shape[: 2]
start_row, start_col = int(height * .15), int(width * .15)
end_row, end_col = int(height * .65), int(width * .65)
cropped = img[start_row: end_row, start_col: end_col]
cv2.imshow('original image', img)
cv2.imshow('cropped image', cropped)
If there are times you don’t want to show a vulnerable image on a website, this method can be used to blur the image.
Example
1 2 3 4 5 6
img = cv2.imread("photo.jpg") cv2.imshow('original image', img) cv2.waitKey(0) kernel = np.ones((7, 7), np.float32) / 49 blurred = cv2.filter2D(img, -1, kernel) cv2.imshow('blur image', blurred)
OpenCV provides two useful methods for Pyramid functionality using pyrDown()
and pyrUp()
functions.
Example
1 2 3 4 5 6
img = cv2.imread("download.jpg") smaller = cv2.pyrDown(img) larger = cv2.pyrUp(img) cv2.imshow('original image', img) cv2.imshow('smaller image', smaller) cv2.imshow('larger image', larger)
We can use the canny() method for edge detection.
Example
First we need to read the images
1
2
3
img = cv2.imread("download.jpg", )
cv2.imshow('Original Image', img)
cv2.waitKey(0)
Now we can pass the image in canny function
1
2
canny = cv2.Canny(img, 20, 170)
cv2.imshow('canny image', canny)
I hope you learned some basics of OpenCV library. It’s really helpful for computer vision applications.