You are on page 1of 8

OpenCL Image ConvolutionGaussian Seperable Filters

Pi19404
February 22, 2013

Contents

Contents
OpenCL Image Convolution-Seperable Filters Gaussian Filters 3

0.1 Introduction . . . . . . . . . . . . . . . . . 0.2 Gaussian Filters . . . . . . . . . . . . . . . 0.3 Parallel Implementation . . . . . . . . . . 0.4 Comparison with CPU implementations 0.5 Using the code . . . . . . . . . . . . . . . 0.6 Points of Interest . . . . . . . . . . . . . 0.7 License . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

3 3 5 5 6 7 7 8

2 | 8

OpenCL Image Convolution-Seperable Filters Gaussian Filters

OpenCL Image Convolution-Seperable Filters Gaussian Filters


0.1 Introduction
In this artice we will look at parallel implementation of 2D image convolution using Gaussian filter and compared with OpenCV host implementation.

0.2 Gaussian Filters


We will look at discrete approximation of gaussian 2D filters A 2D gaussian function with equal variance in x and y direction and centered about the origin

G(x; y ) =

2

x2 +y2 e 22

(1)

x is the distance from the origin on the horizontal axis,y is the distance from the origint on the vertical axis and  is the standard distribution than quantifies the spread of the gaussian distribution. Convolution is a neighborhood operations where the central pixel is assigned a values which is weighted sum of its neighborhood pixels. The gaussian function never goes to zero.Thus the kernel is infinately large Thus we would need to consider entire image as neighborhood to compute the discrete convolution at a point. Generally pixels at location 3 further from the origin are small and can be considered to be zero . Thus we have a finite kernel in the range of (3; 3 ). This is still a continuous distribution.The discrete approximation of gaussian function is constructed by sampling the values from continuous distribution.

3 | 8

OpenCL Image Convolution-Seperable Filters Gaussian Filters

To perform the discrete approximation we require two parameters the size of kernel n and the standard deviations  If n is the size of the kernel the center or mean of kernel is (n 1)=2 We calculate the values taken by the gaussian kernel at integral location. The number of integral locations to compute depends on the size of kernel . Consider a 1D gaussian kernel N - size of kernel

G(x) =

2

xfloor(N=2)2 2 2 e

values of x at which kernel is evaluated are

floor(N=2) : : : 0 : : : floor(N=2)

Gaussian Kernels are seperable kernels Let G(x) be the row kernel and G(y) be the column kernel G(x) and G(y) can have different standard deviations we use the normalization method to scale the values so that sum of kernel coefficient is 1

G(x; y ) = G(y )H G(x)

G(x; y ) =

G(x;y) min(G(x;y)

For a 3x3 kernel with sigma 3 we obtain 1 2


1:0000 G(x; y ) = 41:6487 1:0000 1:6487 2:7183 1:0000 1:6487 1:6487 1:0000

A discrete approximation for above kernel is 2 3

G(x; y ) = 42
1

2 4 2

1 2 1

5
(2)

Using a similar approach the kernel approximations for kernels with different configurations can be obtained. We can use the kernel directly or a discrete approximation of kernel.

4 | 8

OpenCL Image Convolution-Seperable Filters Gaussian Filters In the present application we will use a discrete approximation to the kernel. After computing the kernel all the kernel values are scaled by normalization factor to make the sum of values equal to 1. For larger kernel sizes the values of kernel at at distances greater than 3 from the center or mean are set to zero if we observe the area under the gaussian distribution curve values at distances greater than 3 are almost zero.

0.3 Parallel Implementation


We will use the separable convolution to perform the convolution You can refer to the following artice for convolution using separable filters The kernel coefficients are computed by the host code as well as normalization factor.The kernel and normalization factor are passed to the kernel during execution. The scale factor is passed by user define in the kernel along with other parameters like mask dimensions ,width,height and widthstep of image.

0.4 Comparison with CPU implementations


The performance of algorithm for different kernel sizes were analyzed. The performance of the parallel algorithm was also compared with OpenCV algoriths for gaussian filter. For kernel size of 3,5,7,9,11,13 the we get performance improvement of 3x or greater compared to OpenCv Gaussian filter function.The performance improvement is greater for larger kernel sizes . In the present algorithm we have not made use of local memory. We will include this in the future and is expected to give additional performance improvements.

5 | 8

OpenCL Image Convolution-Seperable Filters Gaussian Filters

0.5 Using the code


The code consits of two parts the host code and the device code. Host side code uses OpenCv APIs to read the image from video file and demonstrates the calling of the kernel code for Box filter for 2d convolution,seprable filter and host CPU implementation.

oclc ommon:h - This header file defines OCLX class

contains methods for communication with OpenCL APIs.

This class

Some of the important methods defined in class are as follows .

1. Methods for calling OpenCL initialization routines. 2. Methods for writing and reading to OpenCL compliant device memory. 3. Methods for setting the arguments of kernel code and calling the kernel code. main.cpp - This is main program file The input to the program is a video files.This is choosen so that average running time of OpenCL code,OpenCL with various configuration can be compared over a period of time It creater OCLX class objects.Calls the initialization routine. Then create and compiles the kernel passing the build time options which consists of user defines for various static parameters used by the kernel code. The program call the Parallel OpenCL code and OpenCV code and displays the average time required for execution for both the modules on the standard output. The output of image processing of both the routine are also displayed in seperate windows for comparion Code is available in repository https://github.com/pi19404/m19404/ tree/master/OpenCL-Image-Processing/Convolution

6 | 8

OpenCL Image Convolution-Seperable Filters Gaussian Filters

0.6 Points of Interest


The OpenCL programming interface is available for MultiCore CPUs.This enables us to develope parallel progams which can be ported on CPU,GPU and DSPs. This article presents Parallel implementation of Gaussian filter using OpenCL programming APIs.It has been tested on Intel(R) Core(TM) i3 CPU at 2.53GHz on Ubuntu 12.04 OS.The average run time analysis of parallel implemetation was compared with OpenCV algorithms and a performance improvement of 3x-4x was observed. This algorithm has scope to be optimized further which would most likely give increased performance.

0.7 License
The Article and Software is subjected to Apache License, Version 2.0 or higher

7 | 8

Bibliography

Bibliography
[1] [2] [3] [4] [5]

uic.edu/kreda/gpu/image-convolution/. html.
Image Convolution Filter.

A study of OpenCL image convolution optimization.

url: http://www.evl.

url: http : / / lodev . org / cgtutor / filtering .

NVidia CUDA Example. url: http://developer.download.nvidia.com/ compute/cuda/4_2/rel/sdk/website/OpenCL/html/samples.html. OpenCL.

url: http://www.khronos.org/opencl/.

url: http://www.scribd. com/doc/124173561/OpenCL-2D-Convolution-Using-Separable-FiltersBox-Filter.


OpenCL 2D Convolution Using Seperable Filters. OpenCV color conversion.

[6] [7] [8]

html.

url: http://www.shervinemami.info/colorConversion.

Steve on Image Processing Blog at Matlab Central.

url: http : / / blogs . mathworks.com/steve/2006/10/04/separable-convolution/.


The Scientist and Engineer's Guide to Digital Signal Processing.

//www.dspguide.com/ch24/3.htm.

url: http :

8 | 8

You might also like