def gradient(im, dx=1, dy=1): if not dx % 2 or not dy % 2: raise Exception("dx and dy must be odd") theta = ffi.new('ccv_dense_matrix_t*[1]') magnitude = ffi.new('ccv_dense_matrix_t*[1]') lib.ccv_gradient(im, theta, 0, magnitude, 0, dx, dy) if theta[0] == ffi.NULL or magnitude[0] == ffi.NULL: raise Exception("NULL output") return _matrix_ref(theta[0]), _matrix_ref(magnitude[0])
def visualize(mat, outtype=0): """ Convert an input matrix into a matrix within visual range, so that one can output it into PNG or similar. """ _check_datatype(outtype) output = ffi.new('ccv_matrix_t*[1]') lib.ccv_visualize(mat, output, outtype) if output[0] == ffi.NULL: raise Exception("NULL output") return _matrix_ref(output[0])
def ccv_slice(inp, x, y, rows, cols, ttype=0): """ Slice an input matrix given offsets x and y, and number of rows and columns. A new matrix is returned. """ _check_datatype(ttype) output = ffi.new('ccv_matrix_t*[1]') lib.ccv_slice(inp, output, ttype, y, x, rows, cols) if output[0] == ffi.NULL: raise Exception("NULL output") return _matrix_ref(output[0])
def sobel(im, mtype, dx=1, dy=1): if (dx and not dx % 2) or (dy and not dy % 2): raise Exception("dx and dy must be odd if specified") if not dx and not dy: raise Exception("both dx and dy are missing") _check_datatype(mtype) output = ffi.new('ccv_dense_matrix_t*[1]') lib.ccv_sobel(im, output, mtype, dx, dy) if output[0] == ffi.NULL: raise Exception("NULL output") return _matrix_ref(output[0])
def ccv_read(inp, ttype=None, rows=0, cols=0, scanline=0): """ Read an image from a filename specified by inp and a matching ttype. Returns an internal ccv_dense_matrix_t* """ if ttype is None: ttype = lib.CCV_IO_RGB_COLOR | lib.CCV_IO_ANY_FILE image = ffi.new('ccv_dense_matrix_t*[1]') res = lib.ccv_read_impl(inp, image, ttype, rows, cols, scanline) if res != lib.CCV_IO_FINAL: raise Exception("Failed to read %s with ttype %d: %d" % (inp, ttype, res)) if image[0] == ffi.NULL: raise Exception("NULL image") return _matrix_ref(image[0])
def prepare_bbf_cascade(inp): casc = ffi.new('ccv_bbf_classifier_cascade_t*[1]') casc[0] = lib.ccv_bbf_read_classifier_cascade(inp) if casc[0] == ffi.NULL: raise Exception("Failed to read BBF cascade from %s" % inp) return ffi.gc(casc, lambda x: lib.ccv_bbf_classifier_cascade_free(x[0]))