Do more. Code less. Free software for GPU computing.
<scroll to top>

examples/image/image_demo.cpp

#include <stdio.h>
#include <arrayfire.h>

using namespace af;

#include "ppm_utils.h"

static void rgb_split(array& rgb, array& outr, array& outg, array& outb);

static void img_test_demo(bool console)
{
    // conv kernels
    const float h_avg_kernel[] = { 1.0 / 12.0, 2.0 / 12.0, 1.0 / 12.0,
                                   2.0 / 12.0, 0.0       , 2.0 / 12.0,
                                   1.0 / 12.0, 2.0 / 12.0, 1.1 / 12.0 };
    const float h_sobel_kernel[] = { -2.0, -1.0,  0.0,
                                     -1.0,  0.0,  1.0,
                                     0.0,  1.0,  2.0 };
    array avg_k   = array(3, 3, h_avg_kernel);
    array sobel_k = array(3, 3, h_sobel_kernel);

    // load images
    array I1 = 255 * load_gray_ppm("pattern.ppm") ;  // grayscale  [0-255]
    array I3 = 255 * load_rgb_ppm("pattern.ppm");    // RGB packed (3xMxN)
    array I2 = rgbtogray(I3);                        // grayscale  [0-1]
    array Ib = I1 > 190;                             // binary     [0 1]

    // image histogram equalization
    array ihist = histogram(I1, 256);
    array inorm = histequal(I1, ihist);

    // rgb channels
    array rr, gg, bb;
    rgb_split(I3, rr, gg, bb);

    // binary image morphology
    array bw = morph(Ib, AF_BW_Open);
    // connected component labeling
    array L = regions(bw, 8);

    // labeled region statistics
    array areas = moments(L, AF_RP_Area);
    array max_ar, max_id;
    max(max_ar, max_id, areas);
    print(max_id);            // index of largest region
    print(max_ar);            // area  of largest region

    if (!console) {
        // print window
        figure(0, 0, 1366, 768);
        palette("gray");

        // image operations
        subfigure(4, 3, 1); imgplot(I1);                         title("source image");
        subfigure(4, 3, 2); imgplot(I2.T());                     title("transpose");
        subfigure(4, 3, 3); imgplot(resize(I1, 0.33));           title("resize to 33%");
        subfigure(4, 3, 5); imgplot(erode(I1,  avg_k));          title("erode");
        subfigure(4, 3, 6); imgplot(dilate(I1, avg_k));          title("dilate");
        subfigure(4, 3, 7); imgplot(abs(filter(I1, sobel_k))); title("edge detection");
        subfigure(4, 3, 4); imgplot(rotate(I1, 1.5708, false));  title("rotate");
        subfigure(4, 3, 9); imgplot(inorm); title("image Histogram Equalization");
        subfigure(4, 3, 10); imgplot(gg); title("gray channel");
        subfigure(4, 3, 11); imgplot(bb); title("blue channel");
        subfigure(4, 3, 8);  imgplot(morph(Ib, AF_BW_Open)); title("bwmorph-open");
        subfigure(4, 3, 12); imgplot(L);    title("connected components");
        draw();
    }
}

static void rgb_split(array& rgb, array& outr, array& outg, array& outb)
{
    dim4 ss = rgb.dims(); // should be 3xMxN
    array ii = newdims(rgb, ss[2] * ss[1], 3);
    outr = newdims(ii.col(0), ss[1], ss[2]).T();
    outg = newdims(ii.col(1), ss[1], ss[2]).T();
    outb = newdims(ii.col(2), ss[1], ss[2]).T();
}

int main(int argc, char **argv) {

    bool console = false;
    if (argc > 2 || (argc == 2 && argv[1][0] != '-')) {
        printf("usage: fdtd [-]\n");
        return -1;
    } else if (argc == 2 && argv[1][0] == '-') {
        console = true;
    }

    try {
        printf("** ArrayFire C++ SDK Array Img Test Demo **\n\n");
        img_test_demo(console);

    } catch (af::exception& e) {
        fprintf(stderr, "%s\n", e.what());
    }

    #ifdef WIN32 // pause in Windows
    if (!console) {
        printf("hit [enter]...");
        getchar();
    }
    #endif
    return 0;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines