An Introduction to OpenCV using Google Colab Notebooks- Part 2

Reny Jose
5 min readOct 17, 2020

The first part of this article is available here

In this, we are going to

  1. Colorspaces
  2. Image Functions
  3. bitwise operations

Colorspaces

The color spaces in image processing aim to facilitate the specifications of colors in some standard way. The different color space available are RGB,BGR,Gray,HSV etc. The default colorspace OpenCV is BGR(Blue, Green, Red). the other colorspace is CMY(cyan, magenta, yellow), HSI(hue, saturation, intensity) where Brightness has expressed the intensity or amount of the output. Hue- As we know visible light has an energy that is spread over a band of wavelengths. Hue represents the dominant wavelength in a mixture of all light waves. Saturation-It shows the amount of white light mixed with the hue. Hue and Saturation, together are called Chromaticity. The HSV (Hue, Saturation, Value) color space corresponds better to how people experience color than the RGB color space does.

Fig 1: convert to RGB

To convert between different colorspace openCV provide command called cv2.cvtColor(image,cv2.COLOR_BGRA2RGB) where cv2.COLOR_BGRA2RGB is the tag used to convert the image from BGR to RGB.

Fig2: Convert to other colorspaces

Split the image

Here the variable image contains an RGB image and we are going to split the image into three images as r,g,b. In the same way, we can split the other colorspace images also.

Fig3: split the image

Here the variable ‘b’, the last statement displays the corresponding matrix.

Display multiple images using subplot

To display the three images together, we are using the subplot concept of matplotlib. Hee ‘title’ is a list that contains tilt that needs to display on the image. The variable image store each image as a list. then using a for loop we display the images in a single row.

Fig4: display using subplot

Merge the image: Just the opposite of split the image. Here we combine the split images

Fig 5: merge

Image Functions

  1. Rotations
Fig 6: Rotation

Here we are using a package called imutils for rotation with parameters as image and angle. to run we need to import imutils.

Fig 7: Store the image

This code helps to store the image in the drive. we use a for loop to obtain the angle and rotate the image. then using the str variable we generate the name and then write the image with the name in the str variable.

2. Translate: Translate the image to another location. we are passing the image and the new coordinates.

Fig 8: Translate

3. Flip the image: Flip the image horizontally by Parameter 1, 0 for vertically, and -1 for both.

Fig 9: Horizontally
Fig 10: Flip the image

4. Blur the image: We simple blur an image, the image looks sharper or comes to understand all the objects and their shapes correctly in it.

Fig 11: Blur

Here the kernel provides the filter for blur. we use cv2. blur to blur and pass the image and filter as a tuple. the most commonly used blur method is Gaussian blur by using the Gaussian function which helps to reduce image noise and reduce detail.

Fig12 Gaussian Blur

5. Edge Detection: we use the canny function to detect the edge of an image. The canny function uses a multi-stage algorithm called the Canny Edge Detection algorithm which was developed by John F. Canny in 1986.

First, we need to convert the image to grayscale and we pass the gray image as one parameter and the thresholds. we need two threshold values, minVal, and maxVal. Any edges with an intensity gradient more than maxVal are accepted and those below minVal are discarded.

Fig 13: Canny

6. Dilation and Erosion: Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. The number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image. we apply dilation on output image of edge detection and erosion on output image of dilation.

Fig 14: Dilation and erosion

Bitwise Operations on image

Bitwise operations are also known as arithmetic operations are used in image manipulation and used for extracting essential parts in the image. Bitwise operations help in image masking, adding watermarks to the image, enhancing the properties of the input images and it is possible to create a new image.

Input images should have the same dimensions to apply the Bitwise operations. First, we create two images circle and rectangle.

Fig 15: image creation

The different Bitwise operations used are :

  1. AND: In this, only common pixels in image 1 and image 2 are considered and the remaining pixels are removed from the output image. That is, the intersected regions in both images are displayed.
Fig 16: Bitwise_AND

2. OR: In this, we perform an element-wise product of the array. It merges both images as output.

Fig 17: Bitwise_OR

3. XOR: We inverts the pixels which are intersected in image 1 & image 2 and the rest of the pixels remains the same.

Fig 18: Bitwise_XOR

4. NOT: Only one image is used. we invert every bit of an array. It replaces the white pixels with black pixels and vice versa.

Fig 19: Bitwise_NOT

References

  1. https://analyticsindiamag.com/how-to-implement-bitwise-operations-on-images-using-opencv/
  2. https://docs.opencv.org/master/df/d9d/tutorial_py_colorspaces.html
  3. Learning OpenCv2 Computer vision with Python by Joe Minichino and Joseph Howse
  4. OpenCV with python by examples by Prateek Joshi

--

--

Reny Jose

Research Scholar, Interested in Artificial Intelligence , Machine Learning ,Deep Learning, Computer Vision