opencl heterogeneous parallel program for gaussian filter

8
 OpenCL Image  Convolution-  Gaussian Seperable  Filters  Pi19404  February 22, 2013

Upload: pi194043

Post on 11-Feb-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 1/8

 OpenCL Image Convolution-

 Gaussian Seperable Filters

 Pi19404

 February 22, 2013

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 2/8

 Contents

 Contents

O p e n C L I m a g e C o n v o l u t i o n - S e p e r a b l e F i l t e r s G a u s s i a n F i l t e r s 3  

0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 

0.2 Gaussian Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 

0.3 Parallel Implementation . . . . . . . . . . . . . . . . . . . . . . .5 

0.4 Comparison with CPU implementations . . . . . . . . . . . . .5 

0.5 Using the code . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 

0.6 Points of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . 7 

0.7 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 

 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 

2 | 8 

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 3/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 imageconvolution using Gaussian filter and compared with OpenCV hostimplementation.

0.2 Gaussian FiltersWe will look at discrete approximation of gaussian 2D filters A 2D gaussian function with equal variance in x and y direction andcentered about the origin

G  (  x ; y  ) = 

2   

 

+  y 

2   

( 1 ) 

 x is the distance from the origin on the horizontal axis,y is thedistance 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 pixelis assigned a values which is weighted sum of its neighborhood pix-els. The gaussian function never goes to zero.Thus the kernel is

infinately large Thus we would need to consider entire image asneighborhood to compute the discrete convolution at a point.

 Generally pixels at location3   

further from the origin are small and can be considered to be zero . Thus we have a finite kernelin the range of 

(  

3  ;  3    ) .

 This is still a continuous distribution.The discrete approximation of gaussian function is constructed by sampling the values from con- tinuous distribution.

3 | 8 

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 4/8

 OpenCL Image Convolution-Seperable Filters Gaussian Filters

 To perform the discrete approximation we require two parame- ters the size of kernel

n and the standard deviations

 

If n is the size of the kernel the center or mean of kernelis

(  n  

1 )  =  2 

We calculate the values taken by the gaussian kernel at integral location. The number of integral locations to computedepends on the size of kernel .

 Consider a 1D gaussian kernel

 N - size of kernel

G  (  x  ) = 

2   

 

x    f l o o r  (  N =  2 ) 

2   

values of x at which kernel is evaluated aref l o o r  ( 

 

N =  2 )  : : :  0  : : : f l o o r   (  N =  2 ) 

 Gaussian Kernels are seperable kernelsLet G(x) be the row kernel and G(y)’ be the column kernel G(x) and G(y)’ can have different standard deviations

G  (  x ; y  ) =  G  (  y  ) 

£ 

G  (  x  ) 

we use the normalization method to scale the valuesso that sum of kernel coefficient is 1

G  (  x ; y  ) = 

G  (  x ; y  ) 

m i n  (  G  (  x ; y  ) 

 For a 3x3 kernel with sigma 1 we obtain

G  (  x ; y  ) = 

2  

4  

1  :  0 0 0 0 1  :  6 4 8 7 1  :  0 0 0 0  

1  :  6 4 8 7 2  :  7 1 8 3 1  :  6 4 8 7  

1  :  0 0 0 0 1  :  6 4 8 7 1  :  0 0 0 0  

 A discrete approximation for above kernel is

G  (  x ; y  ) = 

2  

4  

1 2 1 

2 4 2 

1 2 1 

( 2 ) 

Using a similar approach the kernel approximations for kernels withdifferent configurations can be obtained.

We can use the kernel directly or a discrete approximation of  kernel.

4 | 8 

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 5/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 bynormalization 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 distancesgreater 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 alongwith other parameters like mask dimensions ,width,height and width-step of image.

0.4 Comparison with CPU implementations

 The performance of algorithm for different kernel sizes were ana-lyzed. The performance of the parallel algorithm was also comparedwith OpenCV algoriths for gaussian filter.

 For kernel size of 3,5,7,9,11,13 the we get performance improve-ment of 3x or greater compared to OpenCv Gaussian filter func- tion.The performance improvement is greater for larger kernel sizes .

In the present algorithm we have not made use of local mem- ory. We will include this in the future and is expected to give additional performance improvements.

5 | 8 

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 6/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 API’s to read the image fromvideo file and demonstrates the calling of the kernel code for Box filter for 2d convolution,seprable filter and host CPU implementation.

o c l 

o m m o n : h  - This header file defines OCLX class This class

contains methods for communication with OpenCL API’s.

 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 devicememory.

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 configu- ration can be compared over a period of time

It creater OCLX class objects.Calls the initialization routine. Thencreate and compiles the kernel passing the build time options whichconsists of user defines for various static parameters used by the kernel code.

 The program call the Parallel OpenCL code and OpenCV code anddisplays the average time required for execution for both the mod- ules on the standard output. The output of image processing of both the routine are also displayed in seperate windows for com- parion

 Code is available in repositoryh t t p s : / / g i t h u b . c o m / p i 1 9 4 0 4 / m 1 9 4 0 4 /  

t r e e / m a s t e r / O p e n C L - I m a g e - P r o c e s s i n g / C o n v o l u t i o n  

6 | 8 

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 7/8

 OpenCL Image Convolution-Seperable Filters Gaussian Filters

0.6 Points of Interest The OpenCL programming interface is available for MultiCore CPU’s.Thisenables us to develope parallel progams which can be ported on CPU,GPU and DSP’s.

 This article presents Parallel implementation of Gaussian filter using OpenCL programming API’s.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, Version2.0 or higher

7 | 8 

7/23/2019 OpenCL Heterogeneous parallel program for Gaussian Filter

http://slidepdf.com/reader/full/opencl-heterogeneous-parallel-program-for-gaussian-filter 8/8

 Bibliography 

 Bibliography [ 1 ]  A s t u d y o f O p e n C L i m a g e c o n v o l u t i o n o p t i m i z a t i o n  . 

u r l 

:  h t t p : / / w w w . e v l .  

u i c . e d u / k r e d a / g p u / i m a g e - c o n v o l u t i o n /  . 

[ 2 ]  I m a g e C o n v o l u t i o n F i l t e r  . u r l 

:  h t t p : / / l o d e v . o r g / c g t u t o r / f i l t e r i n g .  

h t m l  . 

[ 3 ]  N V i d i a C U D A E x a m p l e  . u r l 

:  h t t p : / / d e v e l o p e r . d o w n l o a d . n v i d i a . c o m /  

c o m p u t e / c u d a / 4 _ 2 / r e l / s d k / w e b s i t e / O p e n C L / h t m l / s a m p l e s . h t m l  . 

[ 4 ]  O p e n C L  . u r l 

:  h t t p : / / w w w . k h r o n o s . o r g / o p e n c l /  . 

[ 5 ]  O p e n C L 2 D C o n v o l u t i o n U s i n g S e p e r a b l e F i l t e r s  . u r l 

:  h t t p : / / w w w . s c r i b d .  

c o m / d o c / 1 2 4 1 7 3 5 6 1 / O p e n C L - 2 D - C o n v o l u t i o n - U s i n g - S e p a r a b l e - F i l t e r s -  

B o x - F i l t e r  . 

[ 6 ]  O p e n C V c o l o r c o n v e r s i o n   . u r l 

:  h t t p : / / w w w . s h e r v i n e m a m i . i n f o / c o l o r C o n v e r s i o n .  

h t m l  . 

[ 7 ]  S t e v e o n I m a g e P r o c e s s i n g B l o g a t M a t l a b C e n t r a l  . u r l 

:  h t t p : / / b l o g s .  

m a t h w o r k s . c o m / s t e v e / 2 0 0 6 / 1 0 / 0 4 / s e p a r a b l e - c o n v o l u t i o n /  . 

[ 8 ]  T h e S c i e n t i s t a n d E n g i n e e r ' s G u i d e t o D i g i t a l S i g n a l P r o c e s s i n g  . u r l 

:  h t t p :  

/ / w w w . d s p g u i d e . c o m / c h 2 4 / 3 . h t m  . 

8 | 8