def training_patches(imnames, npatches, psize, maxdim=None, colour=False, verbose=False): """ Extract patches from images for dictionary training Arguments: imnames: A list of image names from which to extract training patches. npatches: The number (int) of patches to extract from the images maxdim: The maximum dimension of the image in pixels. The image is rescaled if it is larger than this. By default there is no scaling. psize: A int of the size of the square patches to extract verbose: bool, print progress bar Returns: An np.array (npatches, psize**2*3) for RGB or (npatches, psize**2) for grey of flattened image patches. NOTE, the actual npatches found may be less than that requested. """ nimg = len(imnames) ppeimg = int(round(float(npatches) / nimg)) plist = [] # Set up progess updates progbar = Progress(nimg, title='Extracting patches', verbose=verbose) for i, ims in enumerate(imnames): img = imread_resize(ims, maxdim) # read in and resize the image spaceing = max(int(round(img.shape[1] * ppeimg**(-0.5))), 1) # Extract patches and map to grayscale if necessary if (colour == False) and (img.ndim == 3): imgg = rgb2gray(img) plist.append(grid_patches(imgg, psize, spaceing)[0]) else: plist.append(grid_patches(img, psize, spaceing)[0]) progbar.update(i) progbar.finished() patches = np.concatenate(plist, axis=0) return np.reshape(patches, (patches.shape[0], np.prod(patches.shape[1:])))
def training_patches (imnames, npatches, psize, maxdim=None, colour=False, verbose=False): """ Extract patches from images for dictionary training Arguments: imnames: A list of image names from which to extract training patches. npatches: The number (int) of patches to extract from the images maxdim: The maximum dimension of the image in pixels. The image is rescaled if it is larger than this. By default there is no scaling. psize: A int of the size of the square patches to extract verbose: bool, print progress bar Returns: An np.array (npatches, psize**2*3) for RGB or (npatches, psize**2) for grey of flattened image patches. NOTE, the actual npatches found may be less than that requested. """ nimg = len(imnames) ppeimg = int(round(float(npatches)/nimg)) plist = [] # Set up progess updates progbar = Progress(nimg, title='Extracting patches', verbose=verbose) for i, ims in enumerate(imnames): img = imread_resize(ims, maxdim) # read in and resize the image spaceing = max(int(round(img.shape[1] * ppeimg**(-0.5))), 1) # Extract patches and map to grayscale if necessary if (colour == False) and (img.ndim == 3): imgg = rgb2gray(img) plist.append(grid_patches(imgg, psize, spaceing)[0]) else: plist.append(grid_patches(img, psize, spaceing)[0]) progbar.update(i) progbar.finished() patches = np.concatenate(plist, axis=0) return np.reshape(patches, (patches.shape[0], np.prod(patches.shape[1:])))