def patchify(array, patch_size, interval=1): """Create a list of patches by sampling the given array in row-major order with the given patch size and interval between patches. @param array: given array to patchify @type array: L{numpy.array} @param patch_size: a two dimension list which gives the size of width and height of the patch @type patch_size: list @param interval: interval between patches @type interval: int @return: list of sampled patches @rtype: L{numpy.array} """ if not _valid_patch_size(patch_size): raise SRException(INVALID_PATCH_SIZE_ERR % patch_size) patch_width = patch_size[0] patch_dimension = patch_width * patch_width patch_radius = patch_width / 2 patch_y, patch_x = np.mgrid[-patch_radius:patch_radius+1, -patch_radius:patch_radius+1] array_height, array_width = np.shape(array) pad_height, pad_width = get_pad_size(array_height, array_width, patch_width, interval) pad_array = np.pad(array, ((patch_radius, pad_height), (patch_radius, pad_width)), 'reflect') padded_height, padded_width = np.shape(pad_array) patches_y, patches_x = np.mgrid[patch_radius:padded_height-patch_radius:interval, patch_radius:padded_width-patch_radius:interval] patches_number = len(patches_y.flat) patches_y_vector = np.tile(patch_y.flatten(), (patches_number, 1)) + np.tile(patches_y.flatten(), (patch_dimension, 1)).transpose() patches_x_vector = np.tile(patch_x.flatten(), (patches_number, 1)) + np.tile(patches_x.flatten(), (patch_dimension, 1)).transpose() index = np.ravel_multi_index([patches_y_vector, patches_x_vector], (padded_height, padded_width)) return np.reshape(pad_array.flatten()[index], (patches_number, patch_dimension))
def create_method(cls, method_type): """Create a SR method object. @param type: type of SR method @type method_type: str @return: an instance of SR method @rtype: """ if (method_type == "iccv09"): return ICCV09() else: raise SRException("Unsupported SR Method Type:%s" % method_type)
def unpatchify(patches, output_array_size, kernel, overlap=1): """ Create an array from the given patches by merging them together with the given kernel and overlap size. @param patches: given patches array @type patches: L{numpy.array} @param output_array_size: size of output array @type output_array_size: list @param kernel: kernel to merge the patches into array @type kernel: L{numpy.array} @param overlap: overlap size between patches @type overlap: int @return: merged array from the given patches @rtype: L{numpy.array} """ patches_number, patch_dimension = np.shape(patches) if not _valid_patch_dimension(patch_dimension): raise SRException(INVALID_PATCH_DIMENSION % patch_dimension) if patch_dimension != np.shape(kernel.flatten())[0]: raise "Invalid kernel size, kernel size should be equal to patch size." patch_width = int(patch_dimension**(.5)) patch_radius = patch_width / 2 interval = patch_width - overlap output_array_height, output_array_width = output_array_size pad_size = get_pad_size(output_array_height, output_array_width, patch_width, interval) padded_array_size = [ d + patch_radius + p for d, p in zip(output_array_size, pad_size) ] padded_array_height, padded_array_width = padded_array_size padded_array = np.zeros(padded_array_size) weight = np.zeros(padded_array_size) patches_y, patches_x = np.mgrid[patch_radius:padded_array_height - patch_radius:interval, patch_radius:padded_array_width - patch_radius:interval] h, w = np.shape(patches_x) patch_idx = 0 for i in range(h): for j in range(w): patch_x = patches_x[i, j] patch_y = patches_y[i, j] padded_array[patch_y - patch_radius:patch_y + patch_radius + 1, patch_x - patch_radius:patch_x + patch_radius + 1] += \ np.reshape(patches[patch_idx], (patch_width, patch_width)) * kernel weight[patch_y - patch_radius:patch_y + patch_radius + 1, patch_x - patch_radius:patch_x + patch_radius + 1] += kernel patch_idx += 1 padded_array /= weight output_array_height, output_array_width = output_array_size return padded_array[patch_radius:output_array_height + patch_radius, patch_radius:output_array_width + patch_radius]