There are a number of  additions and updates to image based features in the new v3.4 release of ArrayFire. Among the updates are:
<ul>
 	<li>New interpolation methods for several existing functions
<ul>
 	<li><a href="http://arrayfire.org/docs/group__signal__func__approx1.htm">approx1</a>, <a href="http://arrayfire.org/docs/group__signal__func__approx2.htm">approx2</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__transform.htm">transform</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__resize.htm">resize</a></li>
</ul>
</li>
 	<li>Functions for image <a href="http://arrayfire.org/docs/group__image__func__moments.htm">moments</a></li>
</ul>
This blog post will display some typical use cases for these new features.

<span style="line-height: 1.5;">[custom_headline type="left" level="h3" looks_like="h3"]Interpolation methods[/custom_headline]</span>

ArrayFire v3.4 implements several new <a href="http://arrayfire.org/docs/defines_8h.htm#ac0b7c1cfd1fb146314d96f17bbfffa68"> interpolation methods </a>for 1-d and 2-d domains. The new interpolation methods for 1-d functions are:
<ul>
 	<li><strong>AF_INTERP_LINEAR_COSINE</strong></li>
 	<li><strong>AF_INTERP_CUBIC</strong></li>
</ul>
and for 2-d functions are:
<ul>
 	<li><strong>AF_INTERP_BILINEAR_COSINE</strong></li>
 	<li><strong>AF_INTERP_BICUBIC</strong></li>
</ul>
The behavior of the <a href="http://arrayfire.org/docs/defines_8h.htm#ac0b7c1cfd1fb146314d96f17bbfffa68">interpolation methods</a> can be seen in the following pictures.

&nbsp;

[caption id="attachment_9338" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_0.png"><img class="size-medium wp-image-9338" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_0-300x271.png" alt="Original data points" width="300" height="271" /></a> Original data points[/caption]

[caption id="attachment_9339" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_1.png"><img class="size-medium wp-image-9339" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_1-300x267.png" alt="Linear interpolation" width="300" height="267" /></a> Linear interpolation[/caption]

[caption id="attachment_9340" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_2.png"><img class="size-medium wp-image-9340" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_2-300x252.png" alt="Linear cosine interpolation" width="300" height="252" /></a> Linear cosine interpolation[/caption]

[caption id="attachment_9337" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_3.png"><img class="size-medium wp-image-9337" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/ap1_3-300x255.png" alt="Cubic interpolation" width="300" height="255" /></a> Cubic interpolation[/caption]

A common use for interpolation is image filtering. Given a coarse image, we can resample it to be smoother.
<pre class="lang:c++ decode:true" title="Interpolating images with approx2()">af::array img = af::randu(7,7); //create a random image

//define sample points for interpolation
af::array Xs = af::seq(0, 6, 0.1f);
af::array Ys = af::seq(0, 6, 0.1f);
Xs = af::tile(Xs, 1, Ys.dims(0));
Ys = af::tile(Ys.T(), Xs.dims(0));

//interpolate based on specific method
af::array img_bilinear = af::approx2(img, Xs, Ys, AF_INTERP_BILINEAR);
af::array img_bilinearcos = af::approx2(img, Xs, Ys, AF_INTERP_BILINEAR_COSINE);
af::array img_bicubic = af::approx2(img, Xs, Ys, AF_INTERP_BICUBIC_SPLINE);</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/Capture.png"><img class="aligncenter size-full wp-image-9245" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/Capture.png" alt="Capture" width="1025" height="243" /></a>

The new interpolation methods further apply to several similar image transformation functions:
<ul>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__resize.htm">Resize</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__rotate.htm">Rotate</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__scale.htm">Scale</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__skew.htm">Skew</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__transform.htm">Transform</a></li>
 	<li><a href="http://arrayfire.org/docs/group__transform__func__translate.htm">Translate</a></li>
</ul>
Different interpolation methods greatly affect the final result of image processing operations. In general, bilinear and bilinear-cosine produce a good result when upscaling an image, however they often result in a blurry look. The bicubic method tends to sharpen the image resulting in slightly stronger borders and a better overall look. Below are some examples of this behavior using image rotation and image scaling.

[caption id="attachment_9331" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/s_0.png"><img class="size-medium wp-image-9331" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/s_0-300x84.png" alt="3x image scaling using nearest-neighbor sampling" width="300" height="84" /></a> 3x image scaling using nearest-neighbor sampling[/caption]

[caption id="attachment_9332" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/s_1.png"><img class="size-medium wp-image-9332" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/s_1-300x85.png" alt="3x image scaling using bilinear-cosine sampling" width="300" height="85" /></a> 3x image scaling using bilinear-cosine sampling[/caption]

[caption id="attachment_9334" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/s_2.png"><img class="size-medium wp-image-9334" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/s_2-300x85.png" alt="3x image scaling using bicubic sampling" width="300" height="85" /></a> 3x image scaling using bicubic sampling[/caption]

[block_grid type="four-up"] [block_grid_item]

[caption id="attachment_9335" align="aligncenter" width="150"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_0.png"><img class="wp-image-9335 size-thumbnail" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_0-150x150.png" alt="rot_0" width="150" height="150" /></a> Image rotation with nearest neighbor sampling.[/caption]

[/block_grid_item][block_grid_item]

[caption id="attachment_9336" align="aligncenter" width="150"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_1.png"><img class="wp-image-9336 size-thumbnail" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_1-150x150.png" alt="rot_1" width="150" height="150" /></a> Image rotation with bilinear sampling.[/caption]

[/block_grid_item][block_grid_item]

[caption id="attachment_9329" align="aligncenter" width="150"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_2.png"><img class="wp-image-9329 size-thumbnail" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_2-150x150.png" alt="rot_2" width="150" height="150" /></a> Image rotation with bilinear-cosine sampling.[/caption]

[/block_grid_item][block_grid_item]

[caption id="attachment_9330" align="aligncenter" width="150"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_3.png"><img class="wp-image-9330 size-thumbnail" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_3-150x150.png" alt="rot_3" width="150" height="150" /></a> Image rotation with bicubic sampling.[/caption]

[/block_grid_item][block_grid_item]

[/block_grid]

<span style="line-height: 1.5;">[custom_headline type="left" level="h3" looks_like="h3"]Image moments[/custom_headline]</span>

Image <a href="http://arrayfire.org/docs/group__image__func__moments.htm">moments </a>are another new addition to ArrayFire v3.4. Image moments are weighted averages of pixels in an image which provide useful properties of an image. A common use of image moments is to find the center of mass or area (or gray level sum) of an image.

Currently, ArrayFire calculates all<a href="http://arrayfire.org/docs/defines_8h.htm#ad3ee8e32709131cf4126dfa26210aec0"> first order moments</a>. Each moment can be returned individually or all first-order moments can be calculated at once. All moments can be found as follows:
<pre class="lang:c++ decode:true">af::array moments = af::moments(input_image, AF_MOMENT_FIRST_ORDER);</pre>
<div class="fragment">
<div class="line">Here is an example of how the shorthand versions might be used to find the area and center of mass of an image:</div>
</div>
<pre class="lang:default decode:true" title="Calculating center of mass of grayscale image">double m00, m01, m10;
af::moments(&amp;m00, aimg, AF_MOMENT_M00);
af::moments(&amp;m01, aimg, AF_MOMENT_M01);
af::moments(&amp;m10, aimg, AF_MOMENT_M10);

double area = m00;
double x_center = m10 / m00;
double y_center = m01 / m00;
</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/com.png"><img class="aligncenter wp-image-9246 size-medium" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/com-300x300.png" alt="com" width="300" height="300" /></a>

[custom_headline type="left" level="h3" looks_like="h3"]Download[/custom_headline]

ArrayFire v3.4 can be downloaded from these locations:
<ul>
 	<li><a href="http://sandbox.arrayfire.com/download/">Binaries</a></li>
 	<li><a href="https://github.com/arrayfire">Source code</a></li>
</ul>
[custom_headline type="left" level="h3" looks_like="h3"]Community[/custom_headline]

ArrayFire is continually improving through the addition of new image processing and computer vision functions. We welcome your feedback:
<ul>
 	<li>General discussion forums on the <a href="https://groups.google.com/forum/#!forum/arrayfire-users">ArrayFire Google Group</a></li>
 	<li>Live discussion chat on the <a href="https://gitter.im/arrayfire/arrayfire?utm_source=badge&amp;utm_medium=badge&amp;utm_campaign=pr-badge&amp;utm_content=badge">ArrayFire Gitter</a></li>
 	<li>Issue reports on the <a href="https://github.com/arrayfire/arrayfire/issues">ArrayFire GitHub</a></li>
</ul>
Finally, as you find success with ArrayFire, we invite you to contribute a post to this blog to share with the broader community. Email <a href="mailto:scott@arrayfire.com">scott@arrayfire.com</a> to contribute to this blog.