There are a number of  additions and updates to image based features in the new 3.4 release of ArrayFire. Among the updates are:
<ul>
 	<li>New interpolation methods for several existing functions
<ul>
 	<li>approx1, approx2</li>
 	<li>transform</li>
 	<li>resize</li>
</ul>
</li>
 	<li>Functions for image moments</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="h2" looks_like="h2"]Interpolation methods[/custom_headline]</span>

ArrayFire v3.4 implements several new  interpolation methods for 1-d and 2-d domains. The new interpolation methods are:
<ul>
 	<li>AF_INTERP_LINEAR_COSINE</li>
 	<li>AF_INTERP_CUBIC</li>
</ul>
for 1-d functions and:
<ul>
 	<li>AF_INTERP_BILINEAR_COSINE</li>
 	<li>AF_INTERP_BICUBIC</li>
</ul>
for 2-d functions. The behavior of the interpolation methods can be seen in the following picture.

[caption id="attachment_9244" align="aligncenter" width="1701"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/approx1.png"><img class="size-full wp-image-9244" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/approx1.png" alt="Starting from raw data, to linear interpolation, then linear cosine interpolation. and finally cubic interpolation." width="1701" height="430" /></a> Starting from raw data, linear interpolation, linear-cosine interpolation, to cubic interpolation.[/caption]

A common use for interpolation is image filtering. Given a coarse image, we can re-sample 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 interpolants further apply to several similar image transformation functions:
<ul>
 	<li>Resize</li>
 	<li>Rotate</li>
 	<li>Scale</li>
 	<li>Skew</li>
 	<li>Transform</li>
 	<li>Translate</li>
</ul>
Different interpolants greatly affect the final result of the image processing operations. In general, the bilinear and bilinear cosine interpolants produce a good result when up-scaling an image, however they end up with a somewhat blurry look. The bicubic method method ends up with slightly stronger borders and ends up pleasantly sharpening the image. Below are some examples with rotation and scaling.

[caption id="attachment_9260" align="aligncenter" width="742"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_quality.png"><img class="wp-image-9260 size-large" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/rot_quality-1024x405.png" alt="Image rotation with different interpolation methods." width="742" height="293" /></a> Image rotation with different interpolation methods.[/caption]

&nbsp;

[caption id="attachment_9261" align="aligncenter" width="300"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/scale.png"><img class="size-medium wp-image-9261" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/scale-300x276.png" alt="3x image scaling with different interpolation methods." width="300" height="276" /></a> 3x image scaling with different interpolation methods.[/caption]

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

Another addition to this release of ArrayFire are image moments. Image moments are weighted averages of pixels in an image which let us find out some useful properties of an image. A common use of image moments is to find the center of mass or area of some image.

Currently, ArrayFire calculates all first order moments. 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(or gray level sum) 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>

ArrayFire is always adding new methods to help you with accelerating your applications. We hope the new functions will be the vital tools that assist and inspire in your high performance applications!