image_processing/image_editing.cpp
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* https://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);
normalizeImage(ret_val);
return ret_val;
}
int main(int argc, char **argv) {
try {
int device = argc > 1 ? atoi(argv[1]) : 0;
af::setDevice(device);
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");
wnd.show();
}
} catch (af::exception &e) {
fprintf(stderr, "%s\n", e.what());
throw;
}
return 0;
}
af::seq
seq is used to create sequences for indexing af::array
Definition: seq.h:46
af::Pi
AFAPI const double Pi
af::info
AFAPI void info()
af::constant
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
af::scale
AFAPI array scale(const array &in, const float scale0, const float scale1, const dim_t odim0=0, const dim_t odim1=0, const interpType method=AF_INTERP_NEAREST)
C++ Interface for scaling an image.
af::setDevice
AFAPI void setDevice(const int device)
Sets the current device.
af::translate
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.
af::array
A multi dimensional data container.
Definition: array.h:35
af
Definition: algorithm.h:15
af::gaussianKernel
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.
af::max
AFAPI array max(const array &in, const int dim=-1)
C++ Interface for maximum values in an array.
af::clamp
AFAPI array clamp(const array &in, const array &lo, const array &hi)
af::exception
An ArrayFire exception class.
Definition: exception.h:29
af::erode
AFAPI array erode(const array &in, const array &mask)
C++ Interface for image erosion (min filter)
af::tile
AFAPI array tile(const array &in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1)
af::span
AFAPI seq span
A special value representing the entire axis of an af::array.
AF_RGB
@ AF_RGB
3-channel RGB
Definition: defines.h:341
af::array::dims
dim4 dims() const
Get dimensions of the array.
af::colorSpace
AFAPI array colorSpace(const array &image, const CSpace to, const CSpace from)
C++ Interface wrapper for colorspace conversion.
af::loadImage
AFAPI array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
arrayfire.h
AF_GRAY
@ AF_GRAY
Grayscale.
Definition: defines.h:340
af::tan
AFAPI array tan(const array &in)
C++ Interface for tan.
af::exception::what
virtual const char * what() const
Returns an error message for the exception in a string format.
Definition: exception.h:60
af::convolve
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.
af::Window
Window object to render af::arrays.
Definition: graphics.h:37
af::resize
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.
af::min
AFAPI array min(const array &in, const int dim=-1)
C++ Interface for minimum values in an array.