ArrayFire: image_processing/image_editing.cpp (original) (raw)

#include

#include

#include

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;

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));

}

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(in);

float max = af::max(in);

in = 255.0f * ((in - min) / (max - min));

}

normalizeImage(ret_val);

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);

resize(loadImage(ASSETS_DIR "/examples/images/nature.jpg", true),

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 sharp = usm(man, 3, 1.2);

array zoom = digZoom(man, 28, 10, 192, 192);

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();

}

fprintf(stderr, "%s\n", e.what());

throw;

}

return 0;

}

Window object to render af::arrays.

A multi dimensional data container.

dim4 dims() const

Get dimensions of the array.

An ArrayFire exception class.

virtual const char * what() const

Returns an error message for the exception in a string format.

seq is used to create sequences for indexing af::array

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 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.