This is the fourth in a series of posts looking at our current ArrayFire <a href="http://www.accelereyes.com/arrayfire/c/examples.htm" target="_blank">examples</a>. The code can be compiled and run from <code>arrayfire/examples/</code> when you <a href="http://www.accelereyes.com/download_arrayfire">download</a> and install the ArrayFire library. Today we will discuss the examples found in the <code>image_processing/</code> directory.

In these examples, my machine has the following configuration:
<pre class="lang:default highlight:0 decode:true">ArrayFire v1.9 (build XXXXXXX) by AccelerEyes (64-bit Windows)
License: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CUDA toolkit 5.0, driver 306.94
GPU0 GeForce GT 650M, 2048 MB, Compute 3.0 (single,double)
Display Device: GPU0 GeForce GT 650M
Memory Usage: 1981 MB free (2048 MB total)...</pre>
<a href="http://www.accelereyes.com/arrayfire/c/examples_2image_processing_2image_demo_8cpp-example.htm"><strong>Image Demo</strong></a>

The purpose of this example is to show how to do some common image manipulations. The method <code>channel_split</code> shows how easily multi-dimensional arrays can be subdivided:
<pre class="lang:default decode:true">// Split a MxNx3 image into 3 separate channel matrices.
static void channel_split(array&amp; rgb, array&amp; outr, array&amp; outg, array&amp; outb) {
    outr = rgb(span, span, 0);
    outg = rgb(span, span, 1);
    outb = rgb(span, span, 2);
}</pre>
The <a href="http://www.accelereyes.com/arrayfire/c/group__colorconv__mat.htm#gaf0073a3ad8cbb75c7f13f707b51c6a81">colorspace</a> function is useful for moving images between representation spaces. Images can easily be loaded as color images, or converted to grayscale images. Note that when loading images, scaler division is performed to normalize the values between 0 and 1:
<pre class="lang:default decode:true">array img_rgb  = loadimage("logo.jpg", true) / 255.f; // 3 channel RGB       [0-1]</pre>
Other useful functions in this example include computing the <a href="http://www.accelereyes.com/arrayfire/c/group__hist__mat.htm#ga8e823d2440808b58870c0d9846700616">histogram</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__hist__mat.htm#gab6860c2f3992d9616896c2220fe54574">equalization</a>, and calculating thresholds:
<pre class="lang:default decode:true">    // connected component labeling
    array B = img_gray &lt; 190; // binary [0 1]</pre>
This is just a small sample of what our library can do, check out all of our image processing functions <a href="http://www.accelereyes.com/arrayfire/c/group__image__mat.htm">here</a>.

Running the example produces the following output:

<a href="http://blog.accelereyes.com/blog/wp-content/uploads/2013/03/image.png"><img class="alignnone  wp-image-3857" alt="image" src="http://blog.accelereyes.com/blog/wp-content/uploads/2013/03/image-1024x576.png" width="717" height="403" /></a>

<a href="http://www.accelereyes.com/arrayfire/c/examples_2image_processing_2optical_flow_8cpp-example.htm"><strong>Optical Flow</strong></a>

Optical Flow is the apparent motion of an object due to the difference in motion between the object and its observer. <a href="http://en.wikipedia.org/wiki/Horn%E2%80%93Schunck_method">Horn-Schunck</a> is a mathematical method for calculating optical flow using integration techniques. As usual for these examples, it isn't so much about understanding the math, as it is seeing the results of ArrayFire applied to the math. What this example does essentially is convolve images repeatedly for a period of time. Being able to convolve these images on the GPU greatly increases the speed with which we can simulate the integration. Writing the code is simple with ArrayFire, we simply encode the mathematics in a way that is simple to follow:
<pre class="lang:default decode:true">        array u_ = convolve(u, mean_kernel);
        array v_ = convolve(v, mean_kernel);

        const float alphasq = 0.1f;
        array num = Ix * u_ + Iy * v_ + It;
        array den = alphasq + Ix * Ix + Iy * Iy;

        array tmp = 0.01 * num;
        u = u_ - (Ix * tmp) / den;
        v = v_ - (Iy * tmp) / den;</pre>
This example is best run on your own computer so you can see the results in action, here is what the final output looks like:

<a href="http://blog.accelereyes.com/blog/wp-content/uploads/2013/03/image1.png"><img class="alignnone  wp-image-3861" alt="image" src="http://blog.accelereyes.com/blog/wp-content/uploads/2013/03/image1-1024x576.png" width="717" height="403" /></a>

Being able to repeat image calculations for a period of time is a technique for solving a number of important image processing problems. The GPU is perfect for these kinds of algorithms, because each operation can be performed to an entire image at once (like a convolution). This allows us to get results that are much more real time.

What other image processing applications can you see for GPU processing and ArrayFire? <a href="http://www.accelereyes.com/download_arrayfire">Download</a> the ArrayFire library and find out today!

---

<strong>Posts in this series:</strong>
<ol>
	<li><a href="http://blog.accelereyes.com/blog/2013/03/04/arrayfire-examples-part-1-of-8-getting-started/">ArrayFire Examples (Part 1 of 8) – Getting Started</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/03/06/arrayfire-examples-part-2-of-8-benchmarks/">ArrayFire Examples (Part 2 of 8) – Benchmarks</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/03/25/arrayfire-examples-part-3-of-8-financial/">ArrayFire Examples (Part 3 of 8) – Financial</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/04/16/arrayfire-examples-part-4-of-8-image-processing">ArrayFire Examples (Part 4 of 8) - Image Processing</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/06/05/arrayfire-examples-part-5-of-8-machine-learning/">ArrayFire Examples (Part 5 of 8) - Machine Learning</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/06/12/arrayfire-examples-part-6-of-8-multiple-gpus/">ArrayFire Examples (Part 6 of 8) - Multiple GPU</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/07/03/arrayfire-examples-part-7-of-8-pde/">ArrayFire Examples (Part 7 of 8) - PDE</a></li>
</ol>