A comprehensive and practical guide to Image Processing and Computer Vision using Python: Part 1 (Introduction) | by Pranav Natekar | Towards Data Science

Today’s world is the data-driven world and images form a significant part of it.
Over 2.5 quintillion bytes of data are created every single day, and it’s only going to grow from there. By 2020, it’s estimated that 1.7MB of data will be created every second for every person on earth.
— https://www.domo.com/solution/data-never-sleeps-6
Approximately 55–60% of this data is image/video data. However, the data generated is raw data. This data must be pre-processed. According to the needs, these images must be analyzed, processed and then manipulated before that can be put to use. So here comes, Image processing and Computer vision in the picture.
After we’re done with this guide, you will be able to:-
Basic Image handling.
I/O images and videos after processing them.
Perform techniques under the titles of Affine, Morphological, Segmentation, Hough transforms, etc.
Some Face Detection and Object Detection(Yayyyy!! \ (•◡•) / )
And much more...
This article is my humble effort to get people interested in these areas by a practical or hands-on approach. All we need is Python and OpenCV installed(discussed further) on your on-premise desktop/laptop or the VM instance you are running on some cloud!!
If you are a Linux or a Mac user, don’t worry you already have Python on your machine(Hushhhhh(~ ̆▾ ̆)~). But if you are a Windows user, download Python from here. And don’t forget to add Python to your $PATH while it asks during the setup.
This article is directed towards the folks having a good grasp over Python but might be still starting with Image Processing and OpenCV.
OpenCV(Open Source Computer Vision) is a widely used library for Computer vision and Machine Learning tasks. The original implementation of OpenCV is in C++. Python is slower as compared to C/C++, but on the other hand, it can be easily extended with these languages. This feature allowed us to create computationally intensive codes in C++ but wrappers in Python so that they could be used as Python modules. OpenCV-Python is the Python implementation of OpenCV. The library is cross-platformed and can be used under the open-source BSD license. OpenCV has support across multiple programming languages, operating systems and also hardware. There are bindings in Python, Java, and MATLAB/Octave. Since version 3.4, OpenCV.js is the JavaScript binding of the OpenCV lib.
Also, OpenCV needs some prerequisites like NumPy and Matplotlib, install them by using :
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
For Linux users, this is sure to work:
sudo pip3 install opencv-python
More instructions here.
Sorry, I don’t own a Mac(⌣̩̩́_⌣̩̩̀) and hate using Python on Windows()〈)).
If you get the no errors while importing, then you are ready to go. Ready? Let’s dive off the deep end!
Image processing (generally termed as Digital Image Processing) and Computer Vision are the fields in Computer Science with a plethora of algorithms that deal with images or videos for gaining some kind of information. While image processing deals with image-to-image transformations i.e, the input and output of the processing are both images, computer vision is an interdisciplinary field dealing with how a computer can be made to understand or gain high-level insights or information from a digital image or a video. The map shown below gives a visual understanding of what is said above!
An image processing algorithm takes in an image or a video as an input, processes it and the result of the processing is still, an image or a video. But, a computer vision algorithm takes in an image or a video, processes it and constructs explicit and meaningful descriptions from it.
An image can be defined as a two-dimensional function, f(x, y), where x and y are spatial(plane) coordinates and the amplitude of f at any pair of coordinates (x, y) is called the intensity or gray-level of the image at that point. When (x, y), and the amplitude of f are all finite, discrete values, we call the image as the digital image.
— Rafael C. Gonzalez & Richard E. Woods
A basic definition of a video is nothing but images stacked along the temporal axis. A video can be characterized by the aspect ratio, frame rate, interlaced vs progressive, color model, compression method, etc.
Considering the slice of the video M, X[n] is the first frame and X[n+M-1] is the last frame, and Y[n] is the vector stacking all of these to form a slice(part) of the whole video.
OpenCV supports a lot of image and video formats for I/O. First, let’s understand a few paradigms when it comes to video analysis. With the way just about today’s cameras record, recordings(videos) boil down to frames that are displayed 30–60 FPS(Frames Per Second), where frames being images. Thus, image processing and video analysis use identical methods for the most part
For cv2.imread(), following formats are supported:
Windows bitmaps — *.bmp, *.dib
JPEG files — *.jpeg, *.jpg, *.jpe
JPEG 2000 files — *.jp2
Portable Network Graphics — *.png
Portable image format — *.pbm, *.pgm, *.ppm
Sun rasters — *.sr, *.ras
TIFF files — *.tiff, *.tif
For cv2.VideoCapture(), AVI files — *.avi format has full support because it seems that AVI is the only format with decent cross-platform support. See here for more info.
Below are some examples of Image and Video I/O in OpenCV:
As you can see above, the type of image is Numpy’s ‘ndarray’(n-dimensional array), let’s have a look at some of the image manipulations(array manipulations)and some without using OpenCV’s core modules.
import cv2img = cv2.imread('test1.jpeg')print("The properties of the image are:")print("Shape:" + str(img.shape))print("Total no. of pixels:" + str(img.size))print("Data type of image:" + str(img.dtype))
Access an individual pixel for its gray-level/intensity:
import cv2import numpy as npm =  cv2.imread("test1.jpeg")height,width,depth = np.shape(m)y = 1 # y coordinate(across height)x = 1 # x coordinate(across width)print("Value at (1, 1, 0) = " + str(m[y][x][0])) # This will print the pixel value at given coordinates at depth zero(blue)print("Value at (1, 1, 1) = " + str(m[y][x][1])) # This will print the pixel value at given coordinates at depth one(green)print("Value at (1, 1, 2) = " + str(m[y][x][2])) # This will print the pixel value at given coordinates at depth two(red)
To simply iterate over all pixels in the image we can use:
import cv2import numpy as npm =  cv2.imread("test1.jpeg")height, width, depth= np.shape(m)# iterate over the entire image.for y in range(0, height):    for x in range(0, width):        print(m[y][x])
Behold, this will print a lot of numbers in your terminal!!
To modify the pixel value:
import cv2import numpy as npm =  cv2.imread("test1.jpeg")height, width ,depth = np.shape(m)for py in range(0, height):    for px in range(0, width):        m[py][px][0] = 0cv2.imshow('matrix', m)cv2.imwrite('output2.png', m)cv2.waitKey(0)cv2.destroyAllWindows()
Generally, an RGB image has a 24-bit ‘color-depth’ data i.e, three 8-bit channels of RGB data. These channels are nothing but the colors blue, green and red with intensity level ranging from 0 to 255. It’s possible to split the image in OpenCV using cv2.split() but this method is computationally costly and therefore we opt to go with Numpy indexing as it’s much more efficient and should be used if possible.
Splitting using the cv2.split() method is as easy as:
b,g,r = cv2.split(img)
In OpenCV, the order of channels is BGR and not RGB.
Splitting using indexing:
import cv2import numpy as npm =  cv2.imread("test1.jpeg")blue = m[:, :, 0]green = m[:, :, 1]red = m[:, :, 2]
To understand indexing and splicing more, refer to this.
In the next part of this article, we’ll cover the image processing techniques with the respective codes associated with it in OpenCV.
All the code used in this tutorial can be found in the repository below.