If you want to work or learn computer vision with Python, you must know OpenCV. It is an open Source Computer Vision Library that has 2500 plus algorithms. In this OpenCV tutorial, I will show you some basic OpenCV functions which you must know if you are starting python OpenCV.
Install OpenCV: Before you start as you know you need to install OpenCV by typing pip install opencv-python
How to read image python OpenCV
Let’s say you have an image in a local folder and you want to read that image and you also want to show that image using OpenCV.
Read video OpenCV python
The below code is to read video frames in OpenCV and play the video. Now the idea of reading video is to read video frame by frame as image and show those images frame by frame. That is how an entire video will play.
line: 8 => Read each video frame from vcap variable and store into img. If reading is successful, it will declare read_ok variable as true else it will declare read_ok as false.
line: 9 => Show each each video frame as image
line: 12 => Close video window by pressing ‘x‘
Read webcam OpenCV python
If you want to capture your webcam video, you can use same above code. You just need replace video file path with 0.
0 for your primary or default webcam. If you have two cameras, the value will be 1, 2 etc. I am using my default webcam of my laptop.
So I am keeping this value as 0.
Morphological Transformation of image in OpenCV python
Morphological transformation is a commonly used feature extraction technique in image processing. Two basic morphological operators are Dilation and Erosion.
- Dilation: Expand edges with white. convert a pixel element to ‘1’ if at least one pixel under the kernel is ‘1’. It is done by kernel slide (2d convolution)
- Erosion: It is opposite to dilation. It is used for sharping the edges. It erodes away the boundaries of the foreground object. It converts a pixel to 1 only if all the pixels under the kernel are 1, otherwise convert that pixel to 0.
To do those morphological operations you can first convert normal image to grayscale image. In this OpenCV tutorial, I will also show you how you can detect edges of an image using canny edge detection.
Note: There are so many algorithms to detect edge, in this tutorial I will only show you canny edge detection.
import cv2 import numpy as np # LOAD AN IMAGE USING 'IMREAD' img = cv2.imread("data/Cristiano_Ronaldo_256.jpg") # DISPLAY cv2.imshow("Normal image",img) # Convert image to grayscale python opencv img_Gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow("Grayscale image",img_Gray) # gaussianblur opencv python img_Blur = cv2.GaussianBlur(img_Gray,(7,7),0) cv2.imshow("Blur image",img_Blur) # canny edge img_Canny = cv2.Canny(img_Blur,100,100) cv2.imshow("Canny edge image",img_Canny) # Dilation of edges kernal_mat = np.ones((5,5),np.uint8) img_dilate = cv2.dilate(img_Canny,kernal_mat , iterations=1) cv2.imshow("Dilation image",img_dilate) # Erosion of edges img_erode = cv2.erode(img_dilate, kernal_mat, iterations=1) cv2.imshow("Erosion image", img_erode) print(img.shape) cv2.waitKey(0)
Resize image in OpenCV
Before starting resizing of image in opencv, let me explain how opencv see an image.
Normally in mathematics +ve X in the east direction and +ve Y in the north direction (which is upward). But in Opencv +ve X in the same direction (east) and +ve Y in the opposite direction (south), which is downwards.
Now if shape of our image is (256, 256).
Now to resize image, let’s say you have an image of any shape and you want to reshape that image to (256, 256) pixel. Same thing you can do to increase size of your image. But it will not increase quality of the image, it will only increase size of the image.
import cv2 # # Load Normal image using 'IMREAD' img = cv2.imread("data/Cristiano_Ronaldo.jpg") # Defining desired shape fWidth = 256 fHeight = 256 # Resize image in opencv img_resize = cv2.resize(img, (fWidth, fHeight)) print("Normal image shape") print(img.shape) print("Resized image shape") print(img_resize.shape) cv2.imshow("Normal image", img) cv2.imshow("Resized image", img_resize) cv2.waitKey(0)
Crop image in OpenCV
Now to crop image in Python OpenCV you don’t need any function as image itself is a matrix. To crop image in OpenCV you just need to define starting and ending value of X and starting and ending value of Y from that image matrix.
Let’s say you want to crop your image vertically half. That means you have to keep all values of X (width) but half of the value of Y (height) from the image matrix.
# OpenCV imread Python import cv2 # LOAD AN IMAGE USING 'IMREAD' img = cv2.imread("data/Cristiano_Ronaldo_256.jpg") # Shape of the image print(img.shape) # First value of shape is height of the image img_height = img.shape # Half height half_height = int(img_height/2) # Second value of shape is height of the image img_width = img.shape # Cropping image vertically img_cropped = img[0:half_height, 0:img_width] cv2.imshow("Normal image", img) cv2.imshow("Cropped image", img_cropped) cv2.waitKey(0)
In this OpenCV tutorial you learned most useful basic functions which you need to use everyday if you want to learn computer vision with Python.
- How to read image python OpenCV
- Read video OpenCV python
- Read webcam python OpenCV
- Morphological Transformation of image in python OpenCV
- Resize image in Opencv python
- Crop image in OpenCV
If you have any question or suggestion regarding this topic see you in comment section. I will try my best to answer.
Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.