|
| 1 | +####################################################### |
| 2 | +# Copyright (c) 2014, ArrayFire |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This file is distributed under 3-clause BSD license. |
| 6 | +# The complete license agreement can be obtained at: |
| 7 | +# http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | +######################################################## |
| 9 | + |
| 10 | +from .library import * |
| 11 | +from .array import * |
| 12 | +from .data import constant |
| 13 | + |
| 14 | +def gradient(image): |
| 15 | + dx = array() |
| 16 | + dy = array() |
| 17 | + safe_call(clib.af_gradient(pointer(dx.arr), pointer(dy.arr), image.arr)) |
| 18 | + return dx, dy |
| 19 | + |
| 20 | +def load_image(file_name, is_color=False): |
| 21 | + assert(isinstance(file_name, str)) |
| 22 | + image = array() |
| 23 | + safe_call(clib.af_load_image(pointer(image.arr), c_char_p(file_name.encode('ascii')), is_color)) |
| 24 | + return image |
| 25 | + |
| 26 | +def save_image(image, file_name): |
| 27 | + assert(isinstance(file_name, str)) |
| 28 | + safe_call(clib.af_load_image(c_char_p(file_name.encode('ascii')), image.arr)) |
| 29 | + return image |
| 30 | + |
| 31 | +def resize(image, scale=None, odim0=None, odim1=None, method=AF_INTERP_NEAREST): |
| 32 | + |
| 33 | + if (scale is None): |
| 34 | + assert(odim0 is not None) |
| 35 | + assert(odim1 is not None) |
| 36 | + else: |
| 37 | + idims = image.dims() |
| 38 | + odim0 = int(scale * idims[0]) |
| 39 | + odim1 = int(scale * idims[1]) |
| 40 | + |
| 41 | + output = array() |
| 42 | + safe_call(clib.af_resize(pointer(output.arr),\ |
| 43 | + image.arr, c_longlong(odim0), c_longlong(odim1), method)) |
| 44 | + |
| 45 | + return output |
| 46 | + |
| 47 | +def transform(image, transform, odim0 = 0, odim1 = 0, method=AF_INTERP_NEAREST, is_inverse=True): |
| 48 | + output = array() |
| 49 | + safe_call(clib.af_transform(pointer(output.arr),\ |
| 50 | + image.arr, transform.arr,\ |
| 51 | + c_longlong(odim0), c_longlong(odim1), method, is_inverse)) |
| 52 | + return output |
| 53 | + |
| 54 | +def rotate(image, theta, is_crop = True, method = AF_INTERP_NEAREST): |
| 55 | + output = array() |
| 56 | + safe_call(clib.af_rotate(pointer(output.arr), image.arr, c_double(theta), is_crop, method)) |
| 57 | + return output |
| 58 | + |
| 59 | +def translate(image, trans0, trans1, odim0 = 0, odim1 = 0, method = AF_INTERP_NEAREST): |
| 60 | + output = array() |
| 61 | + safe_call(clib.af_translate(pointer(output.arr), \ |
| 62 | + image.arr, trans0, trans1, c_longlong(odim0), c_longlong(odim1), method)) |
| 63 | + return output |
| 64 | + |
| 65 | +def scale(image, scale0, scale1, odim0 = 0, odim1 = 0, method = AF_INTERP_NEAREST): |
| 66 | + output = array() |
| 67 | + safe_call(clib.af_scale(pointer(output.arr),\ |
| 68 | + image.arr, c_double(scale0), c_double(scale1),\ |
| 69 | + c_longlong(odim0), c_longlong(odim1), method)) |
| 70 | + return output |
| 71 | + |
| 72 | +def skew(image, skew0, skew1, odim0 = 0, odim1 = 0, method = AF_INTERP_NEAREST, is_inverse=True): |
| 73 | + output = array() |
| 74 | + safe_call(clib.af_skew(pointer(output.arr),\ |
| 75 | + image.arr, c_double(skew0), c_double(skew1), \ |
| 76 | + c_longlong(odim0), c_longlong(odim1), method, is_inverse)) |
| 77 | + |
| 78 | + return output |
| 79 | + |
| 80 | +def histogram(image, nbins, min_val = None, max_val = None): |
| 81 | + from .algorithm import min as af_min |
| 82 | + from .algorithm import max as af_max |
| 83 | + |
| 84 | + if min_val is None: |
| 85 | + min_val = af_min(image) |
| 86 | + |
| 87 | + if max_val is None: |
| 88 | + max_val = af_max(image) |
| 89 | + |
| 90 | + output = array() |
| 91 | + safe_call(clib.af_histogram(pointer(output.arr),\ |
| 92 | + image.arr, c_uint(nbins), c_double(min_val), c_double(max_val))) |
| 93 | + return output |
| 94 | + |
| 95 | +def hist_equal(image, hist): |
| 96 | + output = array() |
| 97 | + safe_call(clib.af_hist_equal(pointer(output.arr), image.arr, hist.arr)) |
| 98 | + return output |
| 99 | + |
| 100 | +def dilate(image, mask = None): |
| 101 | + |
| 102 | + if mask is None: |
| 103 | + mask = constant(1, 3, 3, dtype=f32) |
| 104 | + |
| 105 | + output = array() |
| 106 | + safe_call(clib.af_dilate(pointer(output.arr), image.arr, mask.arr)) |
| 107 | + |
| 108 | + return output |
| 109 | + |
| 110 | +def dilate3(image, mask = None): |
| 111 | + |
| 112 | + if mask is None: |
| 113 | + mask = constant(1, 3, 3, 3, dtype=f32) |
| 114 | + |
| 115 | + output = array() |
| 116 | + safe_call(clib.af_dilate3(pointer(output.arr), image.arr, mask.arr)) |
| 117 | + |
| 118 | + return output |
| 119 | + |
| 120 | +def erode(image, mask = None): |
| 121 | + |
| 122 | + if mask is None: |
| 123 | + mask = constant(1, 3, 3, dtype=f32) |
| 124 | + |
| 125 | + output = array() |
| 126 | + safe_call(clib.af_erode(pointer(output.arr), image.arr, mask.arr)) |
| 127 | + |
| 128 | + return output |
| 129 | + |
| 130 | +def erode3(image, mask = None): |
| 131 | + |
| 132 | + if mask is None: |
| 133 | + mask = constant(1, 3, 3, 3, dtype=f32) |
| 134 | + |
| 135 | + output = array() |
| 136 | + safe_call(clib.af_erode3(pointer(output.arr), image.arr, mask.arr)) |
| 137 | + |
| 138 | + return output |
| 139 | + |
| 140 | +def bilateral(image, s_sigma, c_sigma, is_color = False): |
| 141 | + output = array() |
| 142 | + safe_call(clib.af_bilateral(pointer(output.arr),\ |
| 143 | + image.arr, c_double(s_sigma), c_double(c_sigma), is_color)) |
| 144 | + return output |
| 145 | + |
| 146 | +def mean_shift(image, s_sigma, c_sigma, n_iter, is_color = False): |
| 147 | + output = array() |
| 148 | + safe_call(clib.af_mean_shift(pointer(output.arr),\ |
| 149 | + image.arr, c_double(s_sigma), c_double(c_sigma),\ |
| 150 | + c_uint(n_iter), is_color)) |
| 151 | + return output |
| 152 | + |
| 153 | +def medfilt(image, w_len = 3, w_wid = 3, edge_pad = AF_PAD_ZERO): |
| 154 | + output = array() |
| 155 | + safe_call(clib.af_medfilt(pointer(output.arr), \ |
| 156 | + image.arr, c_longlong(w_len), c_longlong(w_wid), edge_pad)) |
| 157 | + return output |
| 158 | + |
| 159 | +def minfilt(image, w_len = 3, w_wid = 3, edge_pad = AF_PAD_ZERO): |
| 160 | + output = array() |
| 161 | + safe_call(clib.af_minfilt(pointer(output.arr), \ |
| 162 | + image.arr, c_longlong(w_len), c_longlong(w_wid), edge_pad)) |
| 163 | + return output |
| 164 | + |
| 165 | +def maxfilt(image, w_len = 3, w_wid = 3, edge_pad = AF_PAD_ZERO): |
| 166 | + output = array() |
| 167 | + safe_call(clib.af_maxfilt(pointer(output.arr), \ |
| 168 | + image.arr, c_longlong(w_len), c_longlong(w_wid), edge_pad)) |
| 169 | + return output |
| 170 | + |
| 171 | +def regions(image, connectivity = AF_CONNECTIVITY_4, out_type = f32): |
| 172 | + output = array() |
| 173 | + safe_call(clib.af_regions(pointer(output.arr), image.arr, connectivity, out_type)) |
| 174 | + return output |
| 175 | + |
| 176 | +def sobel_derivatives(image, w_len=3): |
| 177 | + dx = array() |
| 178 | + dy = array() |
| 179 | + safe_call(clib.af_sobel_operator(pointer(dx.arr), pointer(dy.arr),\ |
| 180 | + image.arr, c_uint(w_len))) |
| 181 | + return dx,dy |
| 182 | + |
| 183 | +def sobel_filter(image, w_len = 3, is_fast = False): |
| 184 | + from .arith import abs as af_abs |
| 185 | + from .arith import hypot as af_hypot |
| 186 | + |
| 187 | + dx,dy = sobel_derivatives(image, w_len) |
| 188 | + if (is_fast): |
| 189 | + return af_abs(dx) + af_abs(dy) |
| 190 | + else: |
| 191 | + return af_hypot(dx, dy) |
| 192 | + |
| 193 | +def rgb2gray(image, r_factor = 0.2126, g_factor = 0.7152, b_factor = 0.0722): |
| 194 | + output=array() |
| 195 | + safe_call(clib.af_rgb2gray(pointer(output.arr), \ |
| 196 | + image.arr, c_float(r_factor), c_float(g_factor), c_float(b_factor))) |
| 197 | + return output |
| 198 | + |
| 199 | +def gray2rgb(image, r_factor = 1.0, g_factor = 1.0, b_factor = 1.0): |
| 200 | + output=array() |
| 201 | + safe_call(clib.af_gray2rgb(pointer(output.arr), \ |
| 202 | + image.arr, c_float(r_factor), c_float(g_factor), c_float(b_factor))) |
| 203 | + return output |
| 204 | + |
| 205 | +def hsv2rgb(image): |
| 206 | + output = array() |
| 207 | + safe_call(clib.af_hsv2rgb(pointer(output.arr), image.arr)) |
| 208 | + return output |
| 209 | + |
| 210 | +def rgb2hsv(image): |
| 211 | + output = array() |
| 212 | + safe_call(clib.af_rgb2hsv(pointer(output.arr), image.arr)) |
| 213 | + return output |
| 214 | + |
| 215 | +def color_space(image, to_type, from_type): |
| 216 | + output = array() |
| 217 | + safe_call(clib.af_color_space(pointer(output.arr), image.arr, to_type, from_type)) |
| 218 | + return output |
0 commit comments