我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用scipy.ndimage()。
def permissiveMask( self, volumeThres, gaussSigma = 5.0, gaussRethres = 0.07, smoothSigma=1.5 ): """ Given a (tight) volumeThres(hold) measured in Chimera or IMS, this function generates a Gaussian dilated mask that is then smoothed. Everything is done with Gaussian operations so the Fourier space representation of the mask should be relatively smooth as well, and hence ring less. Excepts self.mrc to be loaded. Populates self.mask. """ thres = self.mrc > volumeThres; thres = thres.astype('float32') gaussThres = scipy.ndimage.gaussian_filter( thres, gaussSigma ) rethres = gaussThres > gaussRethres; rethres = rethres.astype('float32') self.mask = scipy.ndimage.gaussian_filter( rethres, smoothSigma ) print( "permissive mask complete, use ioMRC.writeMRC(self.mrc, 'maskname.mrc') to save" ) pass
def common_mask(ma_list, apply=False): if type(ma_list) is not list: print("Input must be list of masked arrays") return None #Note: a.mask will return single False if all elements are False #np.ma.getmaskarray(a) will return full array of False #ma_list = [np.ma.array(a, mask=np.ma.getmaskarray(a), shrink=False) for a in ma_list] a = np.ma.array(ma_list, shrink=False) #Check array dimensions #Check dtype = bool #Masked values are listed as true, so want to return any() #a+b+c - OR (any) mask = np.ma.getmaskarray(a).any(axis=0) #a*b*c - AND (all) #return a.all(axis=0) if apply: return [np.ma.array(b, mask=mask) for b in ma_list] else: return mask #This will attempt to remove islands #Not sure about what happens if the dilation hits the edge of the array #Should look into binary_closing #Also, ndimage now has greyscale support for closing holes #http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.morphology.grey_closing.html
def maskfill_edgeinclude(a, iterations=1, erode=False): import scipy.ndimage as ndimage a = checkma(a) if erode: a = mask_islands(a, iterations=1) #This is the dilation version #newmask = ~np.ma.getmaskarray(a) #newmask = ndimage.morphology.binary_dilation(newmask, iterations=iterations) #newmask = ndimage.morphology.binary_dilation(~newmask, iterations=iterations) #And the erosion version newmask = np.ma.getmaskarray(a) newmask = ndimage.morphology.binary_erosion(newmask, iterations=iterations) newmask = ndimage.morphology.binary_dilation(newmask, iterations=iterations) return newmask #This is an alternative to the ma.notmasked_edges #Note: probably faster/simpler to contour the mask
def nanfill(a, f_a, *args, **kwargs): """Fill masked areas with np.nan Wrapper for functions that can't handle ma (e.g. scipy.ndimage) This will force filters to ignore nan, but causes adjacent pixels to be set to nan as well: http://projects.scipy.org/scipy/ticket/1155 """ a = checkma(a) ndv = a.fill_value #Note: The following fails for arrays that are not float (np.nan is float) b = f_a(a.filled(np.nan), *args, **kwargs) #the fix_invalid fill_value parameter doesn't seem to work out = np.ma.fix_invalid(b, copy=False) out.set_fill_value(ndv) return out #======================= #Masked array stats #=======================
def calDepthMap(I, r): #????? hsvI = cv2.cvtColor(I, cv2.COLOR_BGR2HSV) s = hsvI[:,:,1] / 255.0 v = hsvI[:,:,2] / 255.0 #cv2.imshow("hsvI",hsvI) #cv2.waitKey() sigma = 0.041337 sigmaMat = np.random.normal(0, sigma, (I.shape[0], I.shape[1])) output = 0.121779 + 0.959710 * v - 0.780245 * s + sigmaMat outputPixel = output output = scipy.ndimage.filters.minimum_filter(output,(r,r)) outputRegion = output cv2.imwrite("data/vsFeature.jpg", outputRegion*255 ) #cv2.imshow("outputRegion",outputRegion) #cv2.waitKey() return outputRegion, outputPixel
def _random_preprocessing(self, image, size): # rotate image rand_degree = np.random.randint(0, 90) rand_flip = np.random.randint(0, 2) if rand_flip == 1: image = np.flip(image, 1) image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255) # Select cropping range between (target_size/2 ~ original_size) original_h, original_w = image.shape #crop_width = np.random.randint(self.target_size/3, min(self.target_size, original_w)) #crop_height = np.random.randint(self.target_size/3, min(self.target_size, original_h)) crop_width = self.target_size crop_height = self.target_size topleft_x = np.random.randint(0, original_w - crop_width) topleft_y = np.random.randint(0, original_h - crop_height) cropped_img = image[topleft_y:topleft_y+crop_height, topleft_x:topleft_x+crop_width] #output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size]) output = cropped_img output = (output - 128.0) / 128.0 return output
def _random_preprocessing(self, image, size): # rotate image rand_degree = np.random.randint(0, 180) rand_flip = np.random.randint(0, 2) image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255) if rand_flip == 1: image = np.flip(image, 1) # Select cropping range between (target_size/2 ~ original_size) original_h, original_w = image.shape crop_width = np.random.randint(self.target_size/2, min(self.target_size*2, original_w)) crop_height = np.random.randint(self.target_size/2, min(self.target_size*2, original_h)) topleft_x = np.random.randint(0, original_w - crop_width) topleft_y = np.random.randint(0, original_h - crop_height) cropped_img = image[topleft_y:topleft_y+crop_height, topleft_x:topleft_x+crop_width] output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size]) # threshold output_thres = np.where(output < 150, -1.0, 1.0) return output_thres
def resample(image, spacing, new_spacing=[1, 1, 1]): # Determine current pixel spacing spacing = np.array(spacing) resize_factor = spacing / new_spacing new_real_shape = image.shape * resize_factor new_shape = np.round(new_real_shape) real_resize_factor = new_shape / image.shape new_spacing = spacing / real_resize_factor image = scipy.ndimage.interpolation.zoom(image, real_resize_factor) return image, new_spacing
def apply_transform(x, transform_matrix, channel_axis=0, fill_mode='nearest', cval=0.): """Apply the image transformation specified by a matrix. Arguments: x: 2D numpy array, single image. transform_matrix: Numpy array specifying the geometric transformation. channel_axis: Index of axis for channels in the input tensor. fill_mode: Points outside the boundaries of the input are filled according to the given mode (one of `{'constant', 'nearest', 'reflect', 'wrap'}`). cval: Value used for points outside the boundaries of the input if `mode='constant'`. Returns: The transformed version of the input. """ x = np.rollaxis(x, channel_axis, 0) final_affine_matrix = transform_matrix[:2, :2] final_offset = transform_matrix[:2, 2] channel_images = [ ndi.interpolation.affine_transform( x_channel, final_affine_matrix, final_offset, order=0, mode=fill_mode, cval=cval) for x_channel in x ] x = np.stack(channel_images, axis=0) x = np.rollaxis(x, 0, channel_axis + 1) return x
def load_images_torcs_4(): import glob filepath = 'datasets/torcs_4/training_set/*.png' filenames = glob.glob(filepath) sample = scipy.ndimage.imread(filenames[0]) num_images = len(filenames) images = np.zeros((num_images, sample.shape[0], sample.shape[1]), dtype=np.uint8) for i in range(num_images): images[i] = scipy.ndimage.imread(filenames[i]) images = images.reshape(len(images), 1, 64, 64) return images
def load_scipy_imgs(img_fns, img_sz=(256, 256), use_bgr=True): nb_channels = 3 if not use_bgr: nb_channels = 1 imgs = [ ] #np.ndarray((len(img_fns), img_sz[0], img_sz[1], nb_channels), np.float32) for i in range(len(img_fns)): try: #im = cv2.imread(img_fns[i]) import scipy.ndimage as sni im = sni.imread(img_fns[i]) if im is None: continue if img_sz is not None: im = cv2.resize(im, img_sz) if use_bgr: imgs.append(im) else: # keep same dim curimg = np.ndarray((im.shape[0], im.shape[1], 1), np.uint8) curimg[:, :, 0] = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) imgs.append(curimg) except cv2.error as e: print 'img error: {}, {}'.format(img_fns[i], e.message) #print 'loaded {} cv images'.format(len(imgs)) return np.asarray(imgs) # load images into a numpy array
def _convert_weight(weight, nw_size): w_size = weight.shape assert len(w_size) == len(nw_size) w_ratio = [_nw / _w for _nw, _w in zip(nw_size, w_size)] new_weight = scipy.ndimage.zoom(weight, w_ratio) return new_weight
def mask_islands(a, iterations=3): import scipy.ndimage as ndimage a = checkma(a) #newmask = a.mask newmask = np.ma.getmaskarray(a) newmask = ndimage.morphology.binary_dilation(newmask, iterations=iterations) newmask = ndimage.morphology.binary_erosion(newmask, iterations=iterations) return np.ma.array(a, mask=newmask)
def mask_dilate(a, iterations=1, erode=False): import scipy.ndimage as ndimage a = checkma(a) if erode: a = mask_islands(a, iterations) newmask = ~(np.ma.getmaskarray(a)) newmask = ndimage.morphology.binary_dilation(newmask, iterations=iterations) return ~newmask #This has not been tested thoroughly
def mask_erode(a, iterations=1, erode=False): import scipy.ndimage as ndimage a = checkma(a) if erode: a = mask_islands(a, iterations) newmask = (np.ma.getmaskarray(a)) newmask = ndimage.morphology.binary_dilation(newmask, iterations=iterations) return newmask #This will fill internal holes in the mask #This should be used to mask outer edges after inpainting or gdal_fillnodata
def derive(self, samples): samples = samples[0] uuids = [s['uuid'] for s in samples] image_paths = [os.path.join(self.path, 'images', '{}.png'.format(u)) for u in uuids] derived = numpy.array([scipy.ndimage.imread(p).transpose() for p in image_paths]) derived = derived.astype(numpy.float32) derived /= 255 return derived
def rotation(x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0.): """Rotate an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). rg : int or float Degree to rotate, usually 0 ~ 180. is_random : boolean, default False If True, randomly rotate. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’ - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0 - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ Examples --------- >>> x --> [row, col, 1] greyscale >>> x = rotation(x, rg=40, is_random=False) >>> tl.visualize.frame(x[:,:,0], second=0.01, saveable=True, name='temp',cmap='gray') """ if is_random: theta = np.pi / 180 * np.random.uniform(-rg, rg) else: theta = np.pi /180 * rg rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w) x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval) return x
def shift(x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0.): """Shift an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). wrg : float Percentage of shift in axis x, usually -0.25 ~ 0.25. hrg : float Percentage of shift in axis y, usually -0.25 ~ 0.25. is_random : boolean, default False If True, randomly shift. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ """ h, w = x.shape[row_index], x.shape[col_index] if is_random: tx = np.random.uniform(-hrg, hrg) * h ty = np.random.uniform(-wrg, wrg) * w else: tx, ty = hrg * h, wrg * w translation_matrix = np.array([[1, 0, tx], [0, 1, ty], [0, 0, 1]]) transform_matrix = translation_matrix # no need to do offset x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval) return x
def shear(x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0.): """Shear an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). intensity : float Percentage of shear, usually -0.5 ~ 0.5 (is_random==True), 0 ~ 0.5 (is_random==False), you can have a quick try by shear(X, 1). is_random : boolean, default False If True, randomly shear. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ """ if is_random: shear = np.random.uniform(-intensity, intensity) else: shear = intensity shear_matrix = np.array([[1, -np.sin(shear), 0], [0, np.cos(shear), 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] transform_matrix = transform_matrix_offset_center(shear_matrix, h, w) x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval) return x
def run(self, para=None): frame = ShellFrame(IPy.curapp, locals=cmds) frame.shell.run('# numpy(np) and scipy.ndimage(ndimg) has been imported!\n') frame.shell.run('# plgs.run_name() to call a ImagePy plugin.\n') frame.shell.run('# IPy is avalible here, and curips() to get the current ImagePlus, update() to redraw.\n') frame.Show(True)
def shift(x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): """Shift an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). wrg : float Percentage of shift in axis x, usually -0.25 ~ 0.25. hrg : float Percentage of shift in axis y, usually -0.25 ~ 0.25. is_random : boolean, default False If True, randomly shift. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0. order : int, optional The order of interpolation. The order has to be in the range 0-5. See ``apply_transform``. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ """ h, w = x.shape[row_index], x.shape[col_index] if is_random: tx = np.random.uniform(-hrg, hrg) * h ty = np.random.uniform(-wrg, wrg) * w else: tx, ty = hrg * h, wrg * w translation_matrix = np.array([[1, 0, tx], [0, 1, ty], [0, 0, 1]]) transform_matrix = translation_matrix # no need to do offset x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval, order) return x
def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', cval=0., order=1): """Return transformed images by given transform_matrix from ``transform_matrix_offset_center``. Parameters ---------- x : numpy array An image with dimension of [row, col, channel] (default). transform_matrix : numpy array Transform matrix (offset center), can be generated by ``transform_matrix_offset_center`` channel_index : int Index of channel, default 2. fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’ - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0 order : int, optional The order of interpolation. The order has to be in the range 0-5: - 0 Nearest-neighbor - 1 Bi-linear (default) - 2 Bi-quadratic - 3 Bi-cubic - 4 Bi-quartic - 5 Bi-quintic - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ Examples -------- - See ``rotation``, ``shift``, ``shear``, ``zoom``. """ x = np.rollaxis(x, channel_index, 0) final_affine_matrix = transform_matrix[:2, :2] final_offset = transform_matrix[:2, 2] channel_images = [ndi.interpolation.affine_transform(x_channel, final_affine_matrix, final_offset, order=order, mode=fill_mode, cval=cval) for x_channel in x] x = np.stack(channel_images, axis=0) x = np.rollaxis(x, 0, channel_index+1) return x
def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', cval=0.): """Return transformed images by given transform_matrix from ``transform_matrix_offset_center``. Parameters ---------- x : numpy array Batch of images with dimension of 3, [batch_size, row, col, channel]. transform_matrix : numpy array Transform matrix (offset center), can be generated by ``transform_matrix_offset_center`` channel_index : int Index of channel, default 2. fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’ - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0 - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ Examples -------- - See ``rotation``, ``shift``, ``shear``, ``zoom``. """ x = np.rollaxis(x, channel_index, 0) final_affine_matrix = transform_matrix[:2, :2] final_offset = transform_matrix[:2, 2] channel_images = [ndi.interpolation.affine_transform(x_channel, final_affine_matrix, final_offset, order=0, mode=fill_mode, cval=cval) for x_channel in x] x = np.stack(channel_images, axis=0) x = np.rollaxis(x, 0, channel_index+1) return x
def get_batch(self, subset, batch_size, use_target_distribution=False): # Select a subset if subset == 'train': X = self.X_train y = self.y_train elif subset == 'valid': X = self.X_val y = self.y_val elif subset == 'test': X = self.X_test y = self.y_test # Random choice of samples idx = np.random.choice(X.shape[0], batch_size) batch = X[idx, 0, :].reshape((batch_size, 28, 28)) # Resize from 28x28 to 64x64 batch_resized = [] factor = self.image_size / 28.0 for i in range(batch.shape[0]): # resize to 64x64 pixels batch_resized.append(scipy.ndimage.zoom(batch[i, :, :], factor, order=1)) batch = np.stack(batch_resized) batch = batch.reshape((batch_size, 1, self.image_size, self.image_size)) # Convert to RGB batch = np.concatenate([batch, batch, batch], axis=1) # Modify images if target distribution requested if use_target_distribution: # Binarize images batch[batch >= 0.5] = 1 batch[batch < 0.5] = 0 # For each image in the mini batch for i in range(batch_size): # Take a random crop of the Lena image (background) x_c = np.random.randint(0, self.lena.size[0] - self.image_size) y_c = np.random.randint(0, self.lena.size[1] - self.image_size) image = self.lena.crop((x_c, y_c, x_c + self.image_size, y_c + self.image_size)) image = np.asarray(image).transpose((2, 0, 1)) / 255.0 # Randomly alter the color distribution of the crop for j in range(3): image[j, :, :] = (image[j, :, :] + np.random.uniform(0, 1)) / 2.0 # Invert the color of pixels where there is a number image[batch[i, :, :, :] == 1] = 1 - image[batch[i, :, :, :] == 1] batch[i, :, :, :] = image # Rescale to range [-1, +1] # batch = batch * 2 - 1 # Image label labels = y[idx] return batch.astype('float32'), labels.astype('int32')
def get_edgemask(a, edge_env=False, convex=False, dilate=False): a = checkma(a) #Need to deal with RGB images here #Need to be careful, probably want to take minimum value from masks if a.ndim == 3: #Assume that the same mask applies to each band #Only create edges for one band b = a[:,:,0] #Could convert to HSL and do edges for L channel #b = a[:,:,0].mask + a[:,:,1].mask + a[:,:,2].mask else: b = a #Get pixel locations of edges edges0, edges1, edges = get_edges(b, convex) #Compute min/max indices #minrow, maxrow, mincol, maxcol #edge_bbox = [edges0[0][0].min(), edges0[1][0].max() + 1, edges0[0][1].min(), edges0[1][1].max() + 1] edge_bbox = [edges[0].min(), edges[0].max() + 1, edges[1].min(), edges[1].max() + 1] #Initialize new mask arrays #Need both to deal with undercuts colmask = np.empty_like(b.mask) colmask[:] = True rowmask = np.empty_like(b.mask) rowmask[:] = True #Loop through each item in the edge list #i is index, col is the column number listed at index i #unmask pixels between specified row numbers at index i for i,col in enumerate(edges0[0][1]): colmask[edges0[0][0][i]:edges0[1][0][i], col] = False for j,row in enumerate(edges1[0][0]): rowmask[row, edges1[0][1][j]:edges1[1][1][j]] = False #Combine the two masks with or operator newmask = np.logical_or(colmask, rowmask) if dilate: print("Dilating edgemask") import scipy.ndimage as ndimage n = 3 #Note: this results in unmasked elements near image corners #This will erode True values, which correspond to masked elements #Effect is to expand the unmasked area #newmask = ndimage.morphology.binary_erosion(newmask, iterations=n) #Now dilate to return to original size #newmask = ndimage.morphology.binary_dilation(newmask, iterations=n) #This is a more logical approach, dilating unmasked areas newmask = ~ndimage.morphology.binary_dilation(~newmask, iterations=n) newmask = ~ndimage.morphology.binary_erosion(~newmask, iterations=n) if edge_env: return newmask, edge_bbox else: return newmask #This will update the mask to remove interior holes from unmasked region
def imblur(Y, sig=5, siz=11, nDimBlur=None, kernel=None): """Spatial filtering with a Gaussian or user defined kernel The parameters are specified in GreedyROI """ from scipy.ndimage.filters import correlate X = np.zeros(np.shape(Y)) if kernel is None: if nDimBlur is None: nDimBlur = Y.ndim - 1 else: nDimBlur = np.min((Y.ndim, nDimBlur)) if np.isscalar(sig): sig = sig * np.ones(nDimBlur) if np.isscalar(siz): siz = siz * np.ones(nDimBlur) # xx = np.arange(-np.floor(siz[0] / 2), np.floor(siz[0] / 2) + 1) # yy = np.arange(-np.floor(siz[1] / 2), np.floor(siz[1] / 2) + 1) # hx = np.exp(-xx**2 / (2 * sig[0]**2)) # hx /= np.sqrt(np.sum(hx**2)) # hy = np.exp(-yy**2 / (2 * sig[1]**2)) # hy /= np.sqrt(np.sum(hy**2)) # temp = correlate(Y, hx[:, np.newaxis, np.newaxis], mode='constant') # X = correlate(temp, hy[np.newaxis, :, np.newaxis], mode='constant') # the for loop helps with memory # for t in range(np.shape(Y)[-1]): # temp = correlate(Y[:,:,t],hx[:,np.newaxis])#,mode='constant', cval=0.0) # X[:,:,t] = correlate(temp,hy[np.newaxis,:])#,mode='constant', cval=0.0) X = Y.copy() for i in range(nDimBlur): h = np.exp(-np.arange(-np.floor(siz[i] / 2), np.floor(siz[i] / 2) + 1)**2 / (2 * sig[i]**2)) h /= np.sqrt(h.dot(h)) shape = [1] * len(Y.shape) shape[i] = -1 X = correlate(X, h.reshape(shape), mode='constant') else: X = correlate(Y, kernel[..., np.newaxis], mode='constant') # for t in range(np.shape(Y)[-1]): # X[:,:,t] = correlate(Y[:,:,t],kernel,mode='constant', cval=0.0) return X #%%
def elastic_transform(x, alpha, sigma, mode="constant", cval=0, is_random=False): """Elastic deformation of images as described in `[Simard2003] <http://deeplearning.cs.cmu.edu/pdfs/Simard.pdf>`_ . Parameters ----------- x : numpy array, a greyscale image. alpha : scalar factor. sigma : scalar or sequence of scalars, the smaller the sigma, the more transformation. Standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes. mode : default constant, see `scipy.ndimage.filters.gaussian_filter <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.gaussian_filter.html>`_. cval : float, optional. Used in conjunction with mode ‘constant’, the value outside the image boundaries. is_random : boolean, default False Examples --------- >>> x = elastic_transform(x, alpha = x.shape[1] * 3, sigma = x.shape[1] * 0.07) References ------------ - `Github <https://gist.github.com/chsasank/4d8f68caf01f041a6453e67fb30f8f5a>`_. - `Kaggle <https://www.kaggle.com/pscion/ultrasound-nerve-segmentation/elastic-transform-for-data-augmentation-0878921a>`_ """ if is_random is False: random_state = np.random.RandomState(None) else: random_state = np.random.RandomState(int(time.time())) # is_3d = False if len(x.shape) == 3 and x.shape[-1] == 1: x = x[:,:,0] is_3d = True elif len(x.shape) == 3 and x.shape[-1] != 1: raise Exception("Only support greyscale image") assert len(x.shape)==2 shape = x.shape dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode=mode, cval=cval) * alpha dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode=mode, cval=cval) * alpha x_, y_ = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij') indices = np.reshape(x_ + dx, (-1, 1)), np.reshape(y_ + dy, (-1, 1)) if is_3d: return map_coordinates(x, indices, order=1).reshape((shape[0], shape[1], 1)) else: return map_coordinates(x, indices, order=1).reshape(shape)
def zoom(x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0.): """Zoom in and out of a single image, randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). zoom_range : list or tuple - If is_random=False, (h, w) are the fixed zoom factor for row and column axies, factor small than one is zoom in. - If is_random=True, (min zoom out, max zoom out) for x and y with different random zoom in/out factor. e.g (0.5, 1) zoom in 1~2 times. is_random : boolean, default False If True, randomly zoom. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ """ if len(zoom_range) != 2: raise Exception('zoom_range should be a tuple or list of two floats. ' 'Received arg: ', zoom_range) if is_random: if zoom_range[0] == 1 and zoom_range[1] == 1: zx, zy = 1, 1 print(" random_zoom : not zoom in/out") else: zx, zy = np.random.uniform(zoom_range[0], zoom_range[1], 2) else: zx, zy = zoom_range # print(zx, zy) zoom_matrix = np.array([[zx, 0, 0], [0, zy, 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w) x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval) return x
def get_batch(self, subset, batch_size, use_target_distribution=False): # Select a subset if subset == 'train': X = self.X_train y = self.y_train elif subset == 'valid': X = self.X_val y = self.y_val elif subset == 'test': X = self.X_test y = self.y_test # Random choice of samples idx = np.random.choice(X.shape[0], batch_size) batch = X[idx, 0, :].reshape((batch_size, 28, 28)) # Resize from 28x28 to 64x64 batch_resized = [] for i in range(batch.shape[0]): # resize to 64x64 pixels batch_resized.append(scipy.ndimage.zoom(batch[i, :, :], 2.3, order=1)) batch = np.stack(batch_resized) batch = batch.reshape((batch_size, 1, 64, 64)) # Convert to RGB batch = np.concatenate([batch, batch, batch], axis=1) # Modify images if target distribution requested if use_target_distribution: # Binarize images batch[batch >= 0.5] = 1 batch[batch < 0.5] = 0 # For each image in the mini batch for i in range(batch_size): # Take a random crop of the Lena image (background) x_c = np.random.randint(0, self.lena.size[0] - 64) y_c = np.random.randint(0, self.lena.size[1] - 64) image = self.lena.crop((x_c, y_c, x_c + 64, y_c + 64)) image = np.asarray(image).transpose((2, 0, 1)) / 255.0 # Randomly alter the color distribution of the crop for j in range(3): image[j, :, :] = (image[j, :, :] + np.random.uniform(0, 1)) / 2.0 # Invert the color of pixels where there is a number image[batch[i, :, :, :] == 1] = 1 - image[batch[i, :, :, :] == 1] batch[i, :, :, :] = image # Rescale to range [-1, +1] batch = batch * 2 - 1 # Image label labels = y[idx] return batch.astype('float32'), labels.astype('int32')
def rotation(x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): """Rotate an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). rg : int or float Degree to rotate, usually 0 ~ 180. is_random : boolean, default False If True, randomly rotate. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’ - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0 order : int, optional The order of interpolation. The order has to be in the range 0-5. See ``apply_transform``. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ Examples --------- >>> x --> [row, col, 1] greyscale >>> x = rotation(x, rg=40, is_random=False) >>> tl.visualize.frame(x[:,:,0], second=0.01, saveable=True, name='temp',cmap='gray') """ if is_random: theta = np.pi / 180 * np.random.uniform(-rg, rg) else: theta = np.pi /180 * rg rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w) x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval, order) return x
def shear(x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): """Shear an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). intensity : float Percentage of shear, usually -0.5 ~ 0.5 (is_random==True), 0 ~ 0.5 (is_random==False), you can have a quick try by shear(X, 1). is_random : boolean, default False If True, randomly shear. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0. order : int, optional The order of interpolation. The order has to be in the range 0-5. See ``apply_transform``. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ References ----------- - `Affine transformation <https://uk.mathworks.com/discovery/affine-transformation.html>`_ """ if is_random: shear = np.random.uniform(-intensity, intensity) else: shear = intensity shear_matrix = np.array([[1, -np.sin(shear), 0], [0, np.cos(shear), 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] transform_matrix = transform_matrix_offset_center(shear_matrix, h, w) x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval, order) return x
def shear2(x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): """Shear an image randomly or non-randomly. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). shear : tuple of two floats Percentage of shear for height and width direction (0, 1). is_random : boolean, default False If True, randomly shear. row_index, col_index, channel_index : int Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0). fill_mode : string Method to fill missing pixel, default ‘nearest’, more options ‘constant’, ‘reflect’ or ‘wrap’. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ cval : scalar, optional Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0. order : int, optional The order of interpolation. The order has to be in the range 0-5. See ``apply_transform``. - `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`_ References ----------- - `Affine transformation <https://uk.mathworks.com/discovery/affine-transformation.html>`_ """ assert len(shear) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear rather than tl.prepro.shear2 ?" if is_random: shear[0] = np.random.uniform(-shear[0], shear[0]) shear[1] = np.random.uniform(-shear[1], shear[1]) shear_matrix = np.array([[1, shear[0], 0], [shear[1], 1, 0], [0, 0, 1]]) h, w = x.shape[row_index], x.shape[col_index] transform_matrix = transform_matrix_offset_center(shear_matrix, h, w) x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval, order) return x
def main(): parse_args() if (opt.input_image == '') and (opt.input_dir == ''): raise Exception('Must give exactly one of -input_image or -input_dir') checkpoint = load_t7checkpoint(opt.model, custom_layers=custom_layers) model = checkpoint.model model.evaluate() gc.collect() preprocess_method = checkpoint.opt.preprocessing or 'vgg' preprocess = methods[preprocess_method] def run_image(in_path, out_path): img = imread(in_path) img = np.array(img, dtype=np.float64) if opt.image_size > 0: img = scipy.misc.imresize(img, np.float(opt.image_size)/np.float(np.max(img.shape))) # #(768, 1153, 3)) # FIXME: IT WORKS ONLY WITH THESE DIMS import pdb; pdb.set_trace() img = img.transpose(2, 0, 1) _, H, W = img.shape img = img.reshape(1, 3, H, W) img_pre = preprocess.preprocess(img) img_out = model.forward(img_pre) img_out = preprocess.deprocess(img_out)[0] img_out = img_out.transpose(1, 2, 0) if opt.median_filter > 0: img_out = scipy.ndimage.filters.median_filter( img_out, opt.median_filter) scipy.misc.imsave(out_path, img_out) print('Writing output image to ' + out_path) outdir = os.path.dirname(out_path) if outdir is not '' and not os.path.exists(outdir): os.makedirs(outdir) scipy.misc.imsave(out_path, img_out) if opt.input_dir != '': if opt.output_dir == '': raise Exception('Must give -output_dir with -input_dir') for fn in os.path.isfile(opt.input_dir): if is_image_file(fn): in_path = os.path.concat(opt.input_dir, fn) out_path = os.path.concat(opt.output_dir, fn) run_image(in_path, out_path) elif opt.input_image != '': if opt.output_image == '': raise Exception('Must give -output_image with -input_image') run_image(opt.input_image, opt.output_image)
def renderView(self, camera_pos, light_sources, blur, blending, spher=True, default_bg_setting=True): self.setCameraPosition(camera_pos[0], math.radians(camera_pos[1]), math.radians(camera_pos[2])) self.activateLightSources(light_sources, spher) base.graphicsEngine.renderFrame() tex = base.win.getScreenshot() im = self.textureToImage(tex) if self.generate_depth is True: depth_im = PNMImage() self.depth_tex.store(depth_im) depth_map = np.zeros([self.resolution, self.resolution], dtype='float') for i in range(0, self.resolution): for j in range(0, self.resolution): depth_val = depth_im.getGray(j, i) depth_map[i, j] = self.far_plane * self.near_plane /\ (self.far_plane - depth_val * (self.far_plane - self.near_plane)) depth_map[i, j] = depth_map[i, j] / self.far_plane dm_uint = np.round(depth_map * self.max_16bit_val).astype('uint16') if self.replace_background is True and default_bg_setting is True: mask = (dm_uint == self.max_16bit_val) temp = np.multiply( mask.astype(dtype=np.float32).reshape( self.resolution, self.resolution, 1), im) im = im - temp blurred_mask = scipy.ndimage.gaussian_filter( mask.astype(dtype=np.float32), blending) inv_mask = (blurred_mask - 1)*(-1) bg_ind = random.randint(0, len(self.backgrounds)-1) im = np.multiply( self.backgrounds[bg_ind], blurred_mask.reshape(self.resolution, self.resolution, 1)) + \ np.multiply(im, inv_mask.reshape(self.resolution, self.resolution, 1)) im = scipy.ndimage.gaussian_filter(im, sigma=blur) im = im.astype(dtype=np.uint8) self.deactivateLightSources() return im, dm_uint