Skip to main content

Fast Pixel Processing with OpenCV and Python

In this post. I will explain how fast pixel manipulation of an image can be done in Python and OpenCV.
Image processing is a CPU intensive task. It involves processing on large arrays. Hence when you are implementing your Image Processing algorithm, you algorithm needs to be highly efficient.
The type of operation that can be applied on an Image can be classified into three categories.

Operation Type Characteristics Complexity per pixel
Point Operations The output pixel value at specific location is dependent only at the input value of pixel at that location.
Constant O(1)
Local Operations The output pixel value at specific location is dependent on the input values in the neighborhood of that coordinate. ( P is the size of neighborhood Kernel )
P x P
Global Operations The output value at specific location is dependent on all the pixels of input Image.
N x N



The complexity for all type of operations is shown in the above table. To apply any type of operation , it is required to iterate over all the pixels and access the pixel value.
Suppose we are applying Point Operation on a gray (single channel) image of resolution 640 x 480. Then total number of 307200 calculations needs to be done.
If you ever tried to loop over an image using python, you know that it is painfully slow operation.

Why accessing individual pixel in python is slow?

In Python, OpenCV images are stored as NumPy arrays.Numpy operations are implemented in C. This allows us to avoid the expensive overhead of python loops and provide performance gain by multiple orders of magnitude as compare to Python List.But this performance gain can only be achieved if we can frame our problem as a vector operations using numpy arrays.
When we are implementing something from scratch,we use python for loop and loose much of performance gain.

Fast Pixel Processing in Python

Let's try to implement basic Gamma Correction to an Image.Gamma Correction is known as Power Law Transform where

Iout = Iin ^ ( 1 / G )

Where Iin is our input image , G is our gamma value and Iout is our output Image.
Gamma value < 1 will make the image darker.
Gamma value > 1 will make the image appear lighter.
Gamma value = 1 will have no effect on input Image.

Original image
Gamma Corrected Image
Let's quickly dive into code.
We import our necessary packages , we import time module to measure and compare the performance of different optimization methods.
Next we load an image and convert it to gray scale.

Now we will apply different technique and measure th time taken and create functions that takes gray image and value of Gamma as input and return Gamma Corrected Image.
The time taken will linearly vary with input image size.
Method 1 : Direct element access.
Time : 2.0738 sec

Method 2 : Using item() and itemset() 

Time : 0.4229 sec

Method 3 : Using Look Up Table

 Time : 0.2109 sec

Method 4 : Using numpy Vector method.
 

Time : 0.0004649 sec 
So its great , a massive improvement of performance.Hence we can increase the speed of our pixel-by-pixel loop access to 4500 times than direct element access method. Hope you enjoyed it, feel free for any discussion and feedback in comments section.

Downloads :

To download the code: Click Here (Link to Github Repository)

Thanks.

Comments

Popular posts from this blog

Transparent Image overlay(Alpha blending) with OpenCV and Python

(a)Final Blended Image                     (b) Background Image                             (c)Foreground Image                               Alpha blending Alpha blending is the process of overlaying a foreground image with transparency over a background Image. The transparent image is generally a PNG image.It consists of four channels (RGBA).The fourth channel is the alpha channel which holds the transparency magnitude. Image (b) is a background image and image (c) is the foreground / overlay image. Image (a) is the final blended image obtained by blending  the overalay image using the alpha mask.  Below is the image(Fig d) of the alpha channel of the overlay  image. (d).Alpha Channel At every pixel of the image, we blend the background and foreground image color(F) and background color (B) using the alpha mask . At every pixel value of alpha lie in range(0,255), a pixel intensity of 0 means black color and pixel instensity of 255 means whit