Image Processing with OpenCV
Visual information is the most important type of information perceived, processed and interpreted by the human brain. Image processing is a method to perform some operations on an image, in order to extract some useful information from it. An image is nothing more than a two dimensional matrix (3-D in case of coloured images) which is defined by the mathematical function f(x,y) where x and y are the two co-ordinates horizontally and vertically. The value of f(x,y) at any point is gives the pixel value at that point of an image, the pixel value describes how bright that pixel is, and/or what color it should be.
For grayscale images the pixel value is a single number that represents the brightness of that pixel, the most common pixel format is the byte image, which is stored as an 8-bit integer giving a range of possible values from 0 to 255. As a convention is taken to be black, and 255 is taken to be white the values in between make up the different shades of gray.
To represent color images, separate red, green and blue components must be specified for each pixel (assuming a RGB color model), and so the pixel `value’ becomes a vector of three numbers. Often the three different components are stored as three separate `grayscale’ images known as color planes (one for each of red, green and blue), which have to be recombined when displaying or processing.

Now allow me to introduce the color models formally as follows, (citing wiki) a color model is an abstract mathematical model describing the way colors can be represented as tuples of numbers, typically as three or four values or color components. When this model is associated with a precise description of how the components are to be interpreted (viewing conditions, etc.), the resulting set of colors is called color space.
Once known how the images could be represented, let’s focus on the image processing side and specifically with OpenCV and python.
Sub-tasks in image processing could be categorized as follows :
- Image acquisition, storage, transmission: digitization/quantization, compression, encoding/decoding.
- Image Enhancement and Restoration: for improvement of pictorial information.
- Information Extraction: for further computer analysis .
Image Acquisition
OpenCV gives the flexibility to capture image directly from a pre-recorded video stream, camera input feed, or a directory path.
#Taking input from a directory path
img = cv2.imread('C:\Users\USER\Desktop\image.jpg',0)#Capturing input from a video stream
cap = cv2.VideoCapture(0)
Image Enhancement and Restoration
Depending on the use case there are various methods which could be applied, some very common ones are as follows :
Histogram Equalization :
Representation of intensity distribution vs no. of pixels of an image is termed as the histogram.
Equalization stretches out the intensity range in order to suit contrast levels appropriately. More resources here and here.
#Taking input from a directory path
img = cv2.imread('wiki.jpg',0)#Applying Histogram Equalization
equ = cv2.equalizeHist(img)#Save the image
cv2.imwrite('res.png',equ)

Erosion and Dilation
Erosion and Dilation belong to the group of morphological transformations and widely used together for the treatment of noise or detection of intensity bumps.
Here’s an extensive resource on the same from the documentation.
#Taking input from a directory path
img = cv2.imread('wiki.jpg',0)#Applying Erosion
img_erosion = cv2.erode(img, kernel, iterations=1)#Applying Dilation
img_dilation = cv2.dilate(img, kernel, iterations=1)#Display Images
cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)
cv2.waitKey(0)
Image Denoising
Noise has a very peculiar property that its mean is zero, and this is what helps in its removal by averaging it out.
OpenCV provides four variations of this technique.
- cv2.fastNlMeansDenoising() — works with a single grayscale images
- cv2.fastNlMeansDenoisingColored() — works with a color image.
- cv2.fastNlMeansDenoisingMulti() — works with image sequence captured in short period of time (grayscale images)
- cv2.fastNlMeansDenoisingColoredMulti() — same as above, but for color images.
Explicit definitions of each could be found here.

Information Extraction
Once the pre-processed image is ready, information could be extracted from it. This is the part from where the code base begins to get implementation specific and purely inclined towards the end goals.
This part could not be generalized as the prior ones but rather has to be discovered along the way building up the module.
Additional Resources and Installation Guide :
Finally closing in with this articles, I would like to encourage you to read the below mentioned follow up links :
Resources
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html https://web.stanford.edu/class/cs101/image-1-introduction.html
http://www.geeksforgeeks.org/erosion-dilation-images-using-opencv-python/
Installation
http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
