Getting Started With Opencv For Image Processing 1

Omolade Ekpeni
5 min readJan 12, 2021

OpenCV (Open Source Computer Vision Library) is a library of programming functions with which you can develop various real-time computer vision projects. It is supported by multiple operating systems including Windows, Linux, Mac OS, iOS and Android. It also has interfaces for Python, Java, C++ and C and is free for academic and commercial use.

OpenCV is an independent platform that allows applications to be developed for use on different operating systems with a diverse array of languages.

Getting Started with OpenCV

Installing OpenCV

In this article, we will be using OpenCV in python so it is expected that you already have python3 installed. Execute one of the following commands to install the OpenCV library depending on what operating system you use.

Windows:

$ pip install opencv-python

Mac OS:

$ brew install opencv3 --with-contrib --with-python3

Linux:

$ sudo apt-get install libopencv-dev python-opencv

Once OpenCV has been installed, we’re ready to play with my image😜.

Reading Images with OpenCV

The first step is to import the OpenCV module, cv2.

Like in the image above, We use the cv2.imread() function to read an image. The image should be within the working directory or the image path should be specified. The first argument in the imread() function is the filename and the second argument is the flag. The flag controls how the image is being read. As shown in the snippet above, 1 means the image is read as a colored image and the transparency(alpha channels) of the image will be neglected, 0 means the image is read as a black and white image and -1 means the image is read with alpha channels. For more explanation of alpha channels, check out this link.

Instead of having the flags passed as integers like in the snippet above, the image flag can be specified this way as well.

Note that the flag argument is optional and when you don’t input it, the default flag is cv2.IMREAD_COLOR, which is also 1 in the form of integer. A colored image will be read from the code below.

img = cv2.imread('lade.png')
print(img)
#None

As shown above, when print(img) gives ‘none’ as output. The image filename/path is either inaccurate or the image format is not supported by OpenCV. In the case above, the file I saved in the working directory is ‘lade.jpg’ not ‘lade.png’

Displaying Images with OpenCV

In displaying the image, we can use the cv2.imshow() function. The function helps in displaying the image in a window. It has two arguments, the first argument is the name of the image window which is a string and the second argument is the image we read using the cv2.imread() function. Note that you can create as many windows you want but ensure the window names are different.

The cv2.waitKey() function’s argument is time in milliseconds. The parameter takes in the number of milliseconds the window will be static for before it closes. If a user presses a key while the window is displayed(during the specified milliseconds period), the window will close. In a case where 0 is passed as the parameter like in the image above, the window will wait indefinitely until a key is pressed.

The cv2.destroyAllWindows() function destroys all the image windows created. In a case where you want to destroy a particular window, the cv2.destroyWindow() function is the function to use. For this function, you pass the window name as argument.

Possible Errors and Solutions

Attribute Error

The attribute error displayed above is as a result of typing cv2.waitkey instead of cv2.waitKey. Note the capital letter K.

Assertion Error

The error above is as a result of the cv2.imshow function not seeing the image passed as the second argument. It can be caused by the factors that cause the ‘none’ output explained previously.

The Output

Image Properties

Image Shape

The shape of an image consists of its width, height and number of color channels. This information can be gotten as a tuple from the shape attribute. The height, width and color channels of an image can be gotten from the shape attribute. As a colored image is a 3d array, the .shape attribute yields a tuple of the number of height(rows), width(columns) and color channels. A grayscale image is a 2d array of shape and column and the .shape attribute produces a tuple of the number of height(rows) and width(columns)without a number of color channels since it will be 1.

import cv2
# colored image
img = cv2.imread('lade.jpg')
print(img.shape)
#(4032, 3024, 3)

4032 is the number of rows, 3024 is the number of columns and 3 is the number of color channels.

import cv2
# grayscale image
img = cv2.imread('lade.jpg',cv2.IMREAD_GRAYSCALE)
print(img.shape)
#(4032, 3024)

Image Size

The size of an image is the number of pixels embedded in the image.

import cv2
img = cv2.imread('lade.jpg')
print(img.size)
#36578304

Image Data Type

The data type of the image can be accessed with .dtype

import cv2
img = cv2.imread('lade.jpg')
print(img.dtype)
#uint8

Conclusion

In this article, we learned what OpenCV is, how to install OpenCV, how to read images with OpenCV, how to display images, and ways of accessing image properties.

Here are a few of things to look out for in my next article:

  • Image Resize
  • Image Rotation
  • Image Cropping
  • Pixel Modification
  • Image Addition and
  • Image Blending

This is my first article and I hope you find this article helpful. I’ll appreciate your comments and if you have any questions, don’t hesitate to hit me up on Twitter: @OmoladeEkpeni. Thanks.

--

--

Omolade Ekpeni

DevOps Engineer | QA | Development | Technical Writing | Project Management