Esempio n. 1
0
def templateMatchingDemo(console):

    root_path = os.path.dirname(os.path.abspath(__file__))
    file_path = root_path
    if console:
        file_path += "/../../assets/examples/images/square.png"
    else:
        file_path += "/../../assets/examples/images/man.jpg"
    img_color = af.load_image(file_path, True)

    # Convert the image from RGB to gray-scale
    img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
    iDims = img.dims()
    print("Input image dimensions: ", iDims)

    # Extract a patch from the input image
    patch_size = 100
    tmp_img = img[100:100 + patch_size, 100:100 + patch_size]

    result = af.match_template(img, tmp_img)  # Default disparity metric is
    # Sum of Absolute differences (SAD)
    # Currently supported metrics are
    # AF_SAD, AF_ZSAD, AF_LSAD, AF_SSD,
    # AF_ZSSD, AF_LSSD

    disp_img = img / 255.0
    disp_tmp = tmp_img / 255.0
    disp_res = normalize(result)

    minval, minloc = af.imin(disp_res)
    print("Location(linear index) of minimum disparity value = {}".format(
        minloc))

    if not console:
        marked_res = af.tile(disp_img, 1, 1, 3)
        marked_res = draw_rectangle(marked_res, minloc%iDims[0], minloc/iDims[0],\
                                    patch_size, patch_size)

        print(
            "Note: Based on the disparity metric option provided to matchTemplate function"
        )
        print(
            "either minimum or maximum disparity location is the starting corner"
        )
        print(
            "of our best matching patch to template image in the search image")

        wnd = af.Window(512, 512, "Template Matching Demo")

        while not wnd.close():
            wnd.set_colormap(af.COLORMAP.DEFAULT)
            wnd.grid(2, 2)
            wnd[0, 0].image(disp_img, "Search Image")
            wnd[0, 1].image(disp_tmp, "Template Patch")
            wnd[1, 0].image(marked_res, "Best Match")
            wnd.set_colormap(af.COLORMAP.HEAT)
            wnd[1, 1].image(disp_res, "Disparity Values")
            wnd.show()
Esempio n. 2
0
def susan_demo(console):

    root_path = os.path.dirname(os.path.abspath(__file__))
    file_path = root_path
    if console:
        file_path += "/../../assets/examples/images/square.png"
    else:
        file_path += "/../../assets/examples/images/man.jpg"
    img_color = af.load_image(file_path, True)

    img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
    img_color /= 255.0

    features = af.susan(img)

    xs = features.get_xpos().to_list()
    ys = features.get_ypos().to_list()

    draw_len = 3
    num_features = features.num_features().value
    for f in range(num_features):
        print(f)
        x = xs[f]
        y = ys[f]

        # TODO fix coord order to x,y after upstream fix
        img_color = draw_corners(img_color, y, x, draw_len)

    print("Features found: {}".format(num_features))
    if not console:
        # Previews color image with green crosshairs
        wnd = af.Window(512, 512, "SUSAN Feature Detector")

        while not wnd.close():
            wnd.image(img_color)
    else:
        print(xs)
        print(ys)
Esempio n. 3
0
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import arrayfire as af
import sys
import os

if __name__ == "__main__":

    if (len(sys.argv) == 1):
        raise RuntimeError("Expected to the image as the first argument")

    if not os.path.isfile(sys.argv[1]):
        raise RuntimeError("File %s not found" % sys.argv[1])

    if (len(sys.argv) >  2):
        af.set_device(int(sys.argv[2]))

    af.info()

    hist_win = af.Window(512, 512, "3D Plot example using ArrayFire")
    img_win  = af.Window(480, 640, "Input Image")

    img = (af.load_image(sys.argv[1])).(af.Dtype.u8)
    hist = af.histogram(img, 256, 0, 255)

    while (not hist_win.close()) and (not img_win.close()):
        hist_win.hist(hist, 0, 255)
        img_win.image(img)
Esempio n. 4
0
#!/usr/bin/python

#######################################################
# Copyright (c) 2015, 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
########################################################

import arrayfire as af

af.info()

POINTS = 30
N = 2 * POINTS

x = (af.iota(d0=N, d1=1, tile_dims=(1, N)) - POINTS) / POINTS
y = (af.iota(d0=1, d1=N, tile_dims=(N, 1)) - POINTS) / POINTS

win = af.Window(800, 800, "3D Surface example using ArrayFire")

t = 0
while not win.close():
    t = t + 0.07
    z = 10 * x * -af.abs(y) * af.cos(x * x * (y + t)) + af.sin(y *
                                                               (x + t)) - 1.5
    win.surface(x, y, z)
Esempio n. 5
0
# Copyright (c) 2015, 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
########################################################

import arrayfire as af

af.info()

ITERATIONS = 200
POINTS = int(10.0 * ITERATIONS)

Z = 1 + af.range(POINTS) / ITERATIONS

win = af.Window(800, 800, "3D Plot example using ArrayFire")

t = 0.1
while not win.close():
    X = af.cos(Z * t + t) / Z
    Y = af.sin(Z * t + t) / Z

    X = af.maxof(af.minof(X, 1), -1)
    Y = af.maxof(af.minof(Y, 1), -1)

    Pts = af.join(1, X, Y, Z)
    win.plot3(Pts)
    t = t + 0.01
Esempio n. 6
0
# 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
########################################################

import arrayfire as af
import math

POINTS = 10000
PRECISION = 1.0 / float(POINTS)

val = -math.pi
X = math.pi * (2 * (af.range(POINTS) / POINTS) - 1)

win = af.Window(512, 512, "2D Plot example using ArrayFire")
sign = 1.0

while not win.close():
    Y = af.sin(X)
    win.plot(X, Y)

    X += PRECISION * sign
    val += PRECISION * sign

    if (val > math.pi):
        sign = -1.0
    elif (val < -math.pi):
        sign = 1.0
Esempio n. 7
0
def harris_demo(console):

    root_path = os.path.dirname(os.path.abspath(__file__))
    file_path = root_path
    if console:
        file_path += "/../../assets/examples/images/square.png"
    else:
        file_path += "/../../assets/examples/images/man.jpg"
    img_color = af.load_image(file_path, True)

    img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
    img_color /= 255.0

    ix, iy = af.gradient(img)
    ixx = ix * ix
    ixy = ix * iy
    iyy = iy * iy

    # Compute a Gaussian kernel with standard deviation of 1.0 and length of 5 pixels
    # These values can be changed to use a smaller or larger window
    gauss_filt = af.gaussian_kernel(5, 5, 1.0, 1.0)

    # Filter second order derivatives
    ixx = af.convolve(ixx, gauss_filt)
    ixy = af.convolve(ixy, gauss_filt)
    iyy = af.convolve(iyy, gauss_filt)

    # Calculate trace
    itr = ixx + iyy

    # Calculate determinant
    idet = ixx * iyy - ixy * ixy

    # Calculate Harris response
    response = idet - 0.04 * (itr * itr)

    # Get maximum response for each 3x3 neighborhood
    mask = af.constant(1, 3, 3)
    max_resp = af.dilate(response, mask)

    # Discard responses that are not greater than threshold
    corners = response > 1e5
    corners = corners * response

    # Discard responses that are not equal to maximum neighborhood response,
    # scale them to original value
    corners = (corners == max_resp) * corners

    # Copy device array to python list on host
    corners_list = corners.to_list()

    draw_len = 3
    good_corners = 0
    for x in range(img_color.dims()[1]):
        for y in range(img_color.dims()[0]):
            if corners_list[x][y] > 1e5:
                img_color = draw_corners(img_color, x, y, draw_len)
                good_corners += 1

    print("Corners found: {}".format(good_corners))
    if not console:
        # Previews color image with green crosshairs
        wnd = af.Window(512, 512, "Harris Feature Detector")

        while not wnd.close():
            wnd.image(img_color)
    else:
        idx = af.where(corners)

        corners_x = idx / float(corners.dims()[0])
        corners_y = idx % float(corners.dims()[0])

        print(corners_x)
        print(corners_y)
Esempio n. 8
0
h_kernel = array.array('f', (1, 1, 1, 1, 0, 1, 1, 1, 1))
reset = 500
game_w = 128
game_h = 128
fps = 30

print("Example demonstrating conway's game of life using arrayfire")
print("The conway_pretty example visualizes all the states in Conway")
print("Red   : Cells that have died due to under population"    )
print("Yellow: Cells that continue to live from previous state" )
print("Green : Cells that are new as a result of reproduction"  )
print("Blue  : Cells that have died due to over population"     )
print("This examples is throttled to 30 FPS so as to be a better visualization")

simple_win = af.Window(512, 512, "Conway's Game of Life - Current State")
pretty_win = af.Window(512, 512, "Conway's Game of Life - Current State with visualization")

simple_win.set_pos(25, 15)
pretty_win.set_pos(600, 25)
frame_count = 0

# Copy kernel that specifies neighborhood conditions
kernel = af.Array(h_kernel, dims=(3,3))

# Generate the initial state with 0s and 1s
state = (af.randu(game_h, game_w) > 0.4).as_type(af.Dtype.f32)

# tile 3 times to display color
display  = af.tile(state, 1, 1, 3, 1)
Esempio n. 9
0
def normalize(a):
    mx = af.max(a)
    mn = af.min(a)
    return (a - mn) / (mx - mn)


if __name__ == "__main__":
    if (len(sys.argv) > 1):
        af.set_device(int(sys.argv[1]))

    af.info()

    print("ArrayFire Fractal Demo\n")

    win = af.Window(width, height, "Fractal Demo")
    win.set_colormap(af.COLORMAP.SPECTRUM)

    center = (-0.75, 0.1)

    for i in range(10, 400):
        zoom = i * i
        if not (i % 10):
            print("Iteration: %d zoom: %d" % (i, zoom))

        c = complex_grid(width, height, zoom, center)
        it = sqrt(2 * sqrt(abs(1 - sqrt(5 * zoom)))) * 100

        if (win.close()): break
        mag = mandelbrot(c, int(it), 1000)