A high-performance general-purpose compute library
* Copyright (c) 2014, ArrayFire
* All rights reserved.
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
#include <arrayfire.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace af;
array changeContrast(const array &in, const float contrast) {
float scale = tan((contrast + 1) * Pi / 4);
return (((in / 255.0f - 0.5f) * scale + 0.5f) * 255.0f);
array changeBrightness(const array &in, const float brightness,
const float channelMax = 255.0f) {
float factor = brightness * channelMax;
return (in + factor);
array clamp(const array &in, float min = 0.0f, float max = 255.0f) {
return ((in < min) * 0.0f + (in > max) * 255.0f +
(in >= min && in <= max) * in);
array usm(const array &in, float radius, float amount) {
int gKernelLen = 2 * radius + 1;
array blurKernel = gaussianKernel(gKernelLen, gKernelLen);
array blur = convolve(in, blurKernel);
return (in + amount * (in - blur));
array digZoom(const array &in, int x, int y, int width, int height) {
array cropped = in(seq(x, width - 1), seq(y, height - 1), span);
return resize(cropped, (unsigned)in.dims(0), (unsigned)in.dims(1));
array alphaBlend(const array &a, const array &b, const array &mask) {
array tiledMask;
if (mask.dims(2) != a.dims(2)) tiledMask = tile(mask, 1, 1, a.dims(2));
return a * tiledMask + (1.0f - tiledMask) * b;
void normalizeImage(array &in) {
float min = af::min<float>(in);
float max = af::max<float>(in);
in = 255.0f * ((in - min) / (max - min));
array boundary(const array &in, const array &mask) {
array ret_val = in - erode(in, mask);
return ret_val;
int main(int argc, char **argv) {
try {
int device = argc > 1 ? atoi(argv[1]) : 0;
array man = loadImage(ASSETS_DIR "/examples/images/man.jpg", true);
array fight = loadImage(ASSETS_DIR "/examples/images/fight.jpg", true);
array nature =
resize(loadImage(ASSETS_DIR "/examples/images/nature.jpg", true),
fight.dims(0), fight.dims(1));
array intensity = colorSpace(fight, AF_GRAY, AF_RGB);
array mask = clamp(intensity, 10.0f, 255.0f) > 0.0f;
array blend = alphaBlend(fight, nature, mask);
array highcon = changeContrast(man, 0.3);
array highbright = changeBrightness(man, 0.2);
array translated = translate(man, 100, 100, 200, 126);
array sharp = usm(man, 3, 1.2);
array zoom = digZoom(man, 28, 10, 192, 192);
array morph_mask = constant(1, 3, 3);
array bdry = boundary(man, morph_mask);
af::Window wnd("Image Editing Operations");
printf("Press ESC while the window is in focus to exit\n");
while (!wnd.close()) {
wnd.grid(2, 5);
wnd(0, 0).image(man / 255, "Input");
wnd(1, 0).image(highcon / 255, "High Contrast");
wnd(0, 1).image(highbright / 255, "High Brightness");
wnd(1, 1).image(translated / 255, "Translation");
wnd(0, 2).image(sharp / 255, "Unsharp Masking");
wnd(1, 2).image(zoom / 255, "Digital Zoom");
wnd(0, 3).image(nature / 255, "Background for blend");
wnd(1, 3).image(fight / 255, "Foreground for blend");
wnd(0, 4).image(blend / 255, "Alpha blend");
wnd(1, 4).image(bdry / 255, "Boundary extraction");
} catch (af::exception &e) {
fprintf(stderr, "%s\n", e.what());
return 0;
Window object to render af::arrays.
Definition: graphics.h:37
A multi dimensional data container.
Definition: array.h:37
dim4 dims() const
Get dimensions of the array.
An ArrayFire exception class.
Definition: exception.h:22
virtual const char * what() const
Returns an error message for the exception in a string format.
Definition: exception.h:46
seq is used to create sequences for indexing af::array
Definition: seq.h:46
Definition: defines.h:340
3-channel RGB
Definition: defines.h:341
AFAPI array clamp(const array &in, const array &lo, const array &hi)
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
AFAPI void info()
AFAPI void setDevice(const int device)
Sets the current device.
AFAPI array colorSpace(const array &image, const CSpace to, const CSpace from)
C++ Interface wrapper for colorspace conversion.
AFAPI array erode(const array &in, const array &mask)
C++ Interface for image erosion (min filter)
AFAPI array gaussianKernel(const int rows, const int cols, const double sig_r=0, const double sig_c=0)
C++ Interface for generating gausian kernels.
AFAPI array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
AFAPI array tile(const array &in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1)
C++ Interface to generate a tiled array.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface to return the maximum along a given dimension.
AFAPI array min(const array &in, const int dim=-1)
C++ Interface to return the minimum along a given dimension.
AFAPI array convolve(const array &signal, const array &filter, const convMode mode=AF_CONV_DEFAULT, const convDomain domain=AF_CONV_AUTO)
C++ Interface for convolution any(one through three) dimensional signals.
AFAPI array resize(const array &in, const dim_t odim0, const dim_t odim1, const interpType method=AF_INTERP_NEAREST)
C++ Interface for resizing an image to specified dimensions.
AFAPI array translate(const array &in, const float trans0, const float trans1, const dim_t odim0=0, const dim_t odim1=0, const interpType method=AF_INTERP_NEAREST)
C++ Interface for translating an image.
Definition: algorithm.h:15