How to get started with OpenCV for Image Processing.

Source: Deep Learning on Medium

How to get started with OpenCV for Image Processing.

Why using OpenCv for Image Processing?

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms.These algorithms can be used to recognize objects in cameras and videos,detect moving objects,combine images and lots of image manipulation.This makes OpenCv a very handy and comprehensive tool for Image Processing and Analysis.The library is originally written in C++.A very good Python support is available to make the task easier.

Getting Started

For Image Manipulation we will be requiring the following libraries to be installed.

Before using OpenCv , first lets see how we can load and create images and visualize them with Numpy,PIL and Matplotlib.

Images are numpy matrices with values ranging from 0 to 255 denoting the pixel value for the color.For images we have the dimensions in the format (height*width*number of channels). For grayscale images number of channels is 1,for colored images number of channels is 3(Red,Green,Blue).

Creating Images with Numpy

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
img = np.zeros((512,512,3))

Lets visualize the image using matplotlib.

plt.imshow(img)
Visualize image with matplotlib

We can also use Pillow library to load the images.

img_lion = Image.open(“Images\lion.jpg”)
plt.imshow(img_lion)
Lion
print(img_lion_arr.shape)

Shape of the above image is (400,590,3).It has three channels Red,Green and Blue.

Visualizing the individual channels

# RED CHANNEL
plt.imshow(img_lion_arr[:,:,0],cmap=’gray’)
Red channel with cmap=’Gray’
# GREEN CHANNEL
plt.imshow(img_lion_arr[:,:,1],cmap=’gray’)
Green channel with cmap=’Gray’
# BLUE CHANNEL
plt.imshow(img_lion_arr[:,:,2],cmap=’gray’)
Blue Channel with cmpa=’Gray’

Using OpenCV

Loading the image

import cv2
cv_img = cv2.imread(“Images\lion.jpg”)

By default OpenCv loads the images in BGR(Blue,Green,Red) order.

plt.imshow(cv_img) # B G R Channel
BGR Opencv

To visualize the image in RGB order we have to convert the image into (R,G,B) using opencv’s cv2.cvtColor.

fixed_cv_img = cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB)
plt.imshow(fixed_cv_img)
RGB

Resizing the image to new pixel dimensions.

new_img = cv2.resize(fixed_cv_img,(200,200))
plt.imshow(new_img)
Resizing with OpenCv

We can even use width and height ratios.

w_ratio = 0.5
h_ratio = 0.5
new_ratio_img = cv2.resize(fixed_cv_img,(0,0),fixed_cv_img,w_ratio,h_ratio)

Inverting the Image

inv_imge = cv2.flip(new_ratio_img,0)
plt.imshow(inv_imge)
Inverted Image

Saving the inverted image on disk

We will use cv2.imwrite to save the image

cv2.imwrite(“inverted_image.jpg”,cv2.cvtColor(inv_imge, cv2.COLOR_RGB2BGR))

Drawing on Images

import numpy as np
import matplotlib.pyplot as plt
import cv2
draw_img = np.zeros((512,512,3),dtype=np.int16)
cv2.rectangle(draw_img,pt1=(384,10),pt2=(500,150),color=(0,255,0),thickness=8)

cv2.rect draws a rectangle with given size and parameters on the image.On plotting with matplotlib the image will look like

plt.imshow(draw_img)
Drawing a rectangle

Drawing other shapes

cv2.circle(img=draw_img,center=(100,100),radius=50,color=(255,0,0),thickness=8)
plt.imshow(draw_img)
Shapes

In order to draw filled shapes we can set the thickness parameter to -1 as in this case.

cv2.circle(img=draw_img,center=(400,400),radius=50,color=(255,0,0),thickness=-1)
plt.imshow(draw_img)
Filled circle

Line and text

cv2.line(draw_img,pt1=(0,0),pt2=(512,512),color=(102,255,255),thickness=5)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(draw_img,text=”Mithilesh”,org=(10,500),fontFace=font,fontScale=4,color=(255,255,255),thickness=3,lineType=cv2.LINE_AA)
plt.imshow(draw_img)
Line and Text

Drawing a polygon

cv2.polylines(poly_img,[pts],isClosed=True,color=(255,0,0),thickness=5)
plt.imshow(poly_img)
Polygon

So folks,that’s all in this article.This is just scratching the surface of OpenCV. OpenCV is a far more advanced library for Image manipulation.This article will get you started with OpenCv and give you the basics.Do check out my upcoming advance Image Processing blogs on OpenCV.

Thank you readers.Your feedbacks and support are highly welcomed and appreciated.