Practical Computer Vision using OpenCV and Python (Basics)
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture, and analysis including features like face detection and object detection.
Let us discuss some image processing operations in OpenCV.
- Install OpenCV
- Import OpenCV package
- Read an image
- Write an image
- Display an image
- Convert to grayscale
- Convert to HSV
- Eroding and Dilation
- Finding Canny Edges
Installing OpenCV-Python :
We can install the package with the following command in terminal.
$ sudo apt-get install python-Opencv
$ pip install opencv-python
After the installation, you can check the version using the below code in Python terminal.
import cv2 as cvprint(cv.__version__)4.1.0
Reading an image: OpenCV provides a function cv2.imread() for reading an image.
Writing an image: OpenCV provides a function cv2.imwrite() for save an image. The first argument is the file name, the second argument is the image array you want to save.
Displaying an image: OpenCV provides a function cv2.imshow(), we can display the image in a window.
The first argument which we will pass will be the window name which is a string. The second argument will be the image.
Conversion to grayscale:OpenCV provides a function cvtColor() which is used to convert colored images to grayscale.
gray = cv2.cvtColor( image, gray_image, CV_BGR2GRAY )
Conversion to HSV: HSV is another type of color space in which H stands for hue, S stands for Saturation and V stands for Value.
*A Hue represents color which is the angle from 0 degrees to 360 degrees.
*Saturation: It indicates the range of grey in the color space. It ranges from 0 to 100%. The value is calculated from 0 to 1. When the value is ‘0,’ the color is grey and when the value is ‘1,’ the color is a primary color.
- Value is the brightness of the color and varies with color saturation. It ranges from 0 to 100%. When the value is ‘0’ the color space will be black. With the increase in the value, the color space brightness up and shows various colors.
HSV_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
Image thresholding: OpenCV provides a function cv.threshold for apply thresholding on an image. Thresholding is the simplest method of image segmentation. From a grayscale image, thresholding can be used to create binary images.
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
Dilation and erosion on an image: Dilation and erosion are two fundamental morphological operations.
Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries.
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
dilation = cv2.dilate(img,kernel,iterations = 1)
Image after applying erosion:
Image after applying dilation:
Finding edges: The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images.
edged = cv2.Canny(gray, 30, 200)
Finding contours: Contours are defined as the line joining all the points along the boundary of an image that are having the same intensity. Contours come handy in shape analysis, finding the size of the object of interest, and object detection.
OpenCV provides a findcontour() function that helps in extracting the contours from the image.
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
I hope that this article helped understand the basics of OpenCV. All the examples are available on the below link.
At crossML we provide custom AI, Cloud,DevOps and software solutions. Contact us at email@example.com.