Practical Computer Vision using OpenCV and Python (Basics)

crossML engineering
crossML Blog
Published in
4 min readJul 25, 2019

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.

  1. Install OpenCV
  2. Import OpenCV package
  3. Read an image
  4. Write an image
  5. Display an image
  6. Convert to grayscale
  7. Convert to HSV
  8. Thresholding
  9. Eroding and Dilation
  10. Finding Canny Edges
  11. Contouring

Installing OpenCV-Python :

We can install the package with the following command in terminal.

$ sudo apt-get install python-Opencv

Or

$ 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.

cv2.imread(‘image.jpg’,0)

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.

cv2.imwrite(‘image.png’,img)

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.

cv2.imshow(‘image’,img)
cv2.waitKey(0)
cv2.destroyAllWindows()

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.

contours,hierarchy=cv2.findContours(edged,cv2.RETR_TREE,cv2.CHAIN_ApPROX_SIMPLE)
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 hello@crossml.com.

--

--