opencl heterogenenous program for image processing - colorspace conversion bgr-hsv,hsv-bgr,bgr-gray

7

Click here to load reader

Upload: pi194043

Post on 07-Aug-2015

1.142 views

Category:

Documents


3 download

DESCRIPTION

OpenCL program for color space conversion and comparison with OpenCv code on CPU

TRANSCRIPT

Page 1: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

HeterogeneousParallel

Programming -Color Space

Conversion ForImage Processing

Using OpenCL

Pi19404

January 20, 2013

Page 2: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

Contents

Contents

OpenCL Parallel Programming for Color Conversion 2

0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

0.2 Color Space Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

0.3 OPENCL Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

0.4 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

OpenCL Parallel Programming for Color

Conversion

0.1 Introduction

Data parallelism is one of ways to achieve parallelism wherein data is distributedacross various computation units. In a multiprocessor system executing a single set ofinstructions (SIMD), data parallelism is achieved when each processor performs thesame task on different pieces of distributed data.

The Color Conversion is a pixel level operation.The task or set of operations to beperformed for color conversion on each pixel is the same.Thus data parallelism can beachieved for color conversion by assigning each pixel to a computation unit and sametask is performed by each computation unit.

OpenCLTM is the first open, royalty-free standard for cross-platform, parallel program-ming of modern processors found in personal computers, servers and handheld/em-bedded devices.Open Computing Language (OpenCL) is a framework for writing programs that ex-ecute across heterogeneous platforms consisting of central processing units (CPUs),graphics processing units (GPUs), DSPs and other processors. OpenCL includes alanguage (based on C99) for writing kernels (functions that execute on OpenCL de-vices), plus application programming interfaces (APIs) that are used to define andthen control the platforms. OpenCL provides parallel computing using task-basedand data-based parallelism.

In the present document we describe the details of OpenCL API’s but how OpenCL is

2 | 7

Page 3: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

OpenCL Parallel Programming for Color Conversion

used efficiently to attain the desired task.

We will look at Color Space conversion BGR-HSV,HSV-BGR and BGR-GRAY for Imageprocessing applications.

0.2 Color Space Conversion

A color model is an abstract mathematical model describing the way colors can be rep-resented as tuples of numbers. RGB is a example of color model wherein each color isuniquely represented by a tuple 3 numbers (R, G, B). Another example of color modelis HSV(Hue,Saturation,Value) .

HSV color model is hexacone representation of points in an RGB color model. Let(X,Y,Z) represent the axis of right handed co-ordinate system and axis of heacone lieabout the Z axis.

The angle around the Z axis corresponds to Hue.The radial distance of point on thefrom the axis corresponds to saturation and distance along the Z axis from the origincorresponds to Value.

HSV color model can be obtained by transformation from RGB color space. Eachunique RGB tuple has unique mapping to point in HSV color space and numericallyeach HSV tuple describe a unique color in RGB color space.

HSV exhibits cylindrical geometry with hue, their angular dimension, starting at thered primary at 0, passing through the green primary at 120 and the blue primary at240, and then wrapping back to red at 360. Chroma is relative distance of point fromorigin wrt to maximum distance.

M = max(R, G, B) (1)

m = min(R, G, B) (2)

C = M−m (3)

Hue is the angular distance where the point lies and can be expressed as

H′ =

undefined, if C = 0G−B

C mod 6, if M = RB−R

C + 2, if M = GR−G

C + 4, if M = B

(4)

H = 60◦ × H′ (5)

Value is the largest component of color

V = M

3 | 7

Page 4: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

OpenCL Parallel Programming for Color Conversion

Saturation is essential same as chroma scaled for values from 0 to 1. It is obtained bydividing chroma by maximum chroma for that value.

SHSV =

{0, if C = 0CV , otherwise

(6)

(7)

The Transformation of HSV to BGR can be expressed as Given a color with hue H ∈[0, 360), saturation S ∈∈ [0, 1], and value V ∈ [0, 1], The chroma is calculated as.

C = V × SHSV

H′ =H

60◦(8)

X = C(1− |H′ mod 2− 1|) (9)

(R1, G1, B1) =

(0, 0, 0) if H is undefined

(C, X, 0) if 0 ≤ H′ < 1

(X, C, 0) if 1 ≤ H′ < 2

(0, C, X) if 2 ≤ H′ < 3

(0, X, C) if 3 ≤ H′ < 4

(X, 0, C) if 4 ≤ H′ < 5

(C, 0, X) if 5 ≤ H′ < 6

(10)

m = V − C (11)

(R, G, B) = (R1 + m, G1 + m, B1 + m) (12)

The BGR to GRAY Conversion can be expressed as

G = 0.30 ∗ R + 0.59 ∗ G + 0.11 ∗ B

All these operations are pixel level operations and can be parallelized. each compu-tation unit(thread) will operate on one pixel and implement the desirable conversion.

0.3 OPENCL Program

The parallel program consists of two parts the host code and kernel code. The hostcode is executed on standard CPU device while kernel code is executed on a parallelcomputing device (SIMD processor). The parallel device may CPU’s,GPU’s,DSP’s etc(processor which supports SIMD architecture)

The job of the host application is to submit work to device. A work item is a ba-sic unit of execution of device usually executed by a thread. The kernel is the code to

4 | 7

Page 5: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

OpenCL Parallel Programming for Color Conversion

be executed by work item/thread.

A work group is a collection of work items and is executed on a compute units. Acompute unit may be the number of cores on the CPU’s and several work groups canreside on single compute units .

The application runs on the host ,it job is to read data to and from device and submitthe kernel to be executed,set the parameters of kernel etc

In the present implementation we work on 320x240 images ,each work group is ofsize 16x16. Each thread of work group execute the kernel function for respective con-version. The execution asynchronous but is pipelined. The parallel algorithm on a4 core CPU give at an average 3X − 4X improvement over standard color conversionroutines provided by OpenCv.It would be even higher improvement on a GPU device.

This is a motivation to cast different type’s of image processing code in a parallelfashion to achieve higher throughput.

0.4 Code

The code consits of two parts the host code and the device code. Host side code usesOpenCv API’s to read the image from video file and demonstrates the calling of thekernel code for BGR-HSV,HSV-BGR and BGR-GRAY.

Code is available in repository https://code.google.com/p/m19404/source/browse/

OpenCL-Image-Processing/ColorConversion/

5 | 7

Page 6: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

Bibliography

Bibliography

[1] OpenCL. url: http://www.khronos.org/opencl/.

[2] OpenCV color conversion. url: http://www.shervinemami.info/colorConversion.html.

6 | 7

Page 7: OpenCL Heterogenenous program for Image Processing - ColorSpace conversion BGR-HSV,HSV-BGR,BGR-GRAY

Bibliography

Bibliography

[1] OpenCL. url: http://www.khronos.org/opencl/.

[2] OpenCV color conversion. url: http://www.shervinemami.info/colorConversion.html.

7 | 7