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.

  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


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

Image for post
Image for post

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 )
Image for post
Image for post

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 for post
Image for post

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)
Image for post
Image for post

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 for post
Image for post

Image after applying dilation:

Image for post
Image for post

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)
Image for post
Image for post

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)
Image for post
Image for post

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


AI simplified

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store