def PreProcess(img_path): img = cv2.imread(img_path) I = img[:, :, 2] I2 = bwdist(I <= 100) I3 = bwdist(I > 100) img[:, :, 0] = np.clip(I2, 0, 255) img[:, :, 1] = np.clip(I3, 0, 255) return img
def combined_potential(obstacles_grid, goal, d0_m_coef, influence_radius=1, attractive_coef=1. / 700, repulsive_coef=200, nrows=500, ncols=500): """ Repulsive potential """ goal = meters2grid(goal) d = bwdist(obstacles_grid == 0) d2 = (d / 100.) + 1 # Rescale and transform distances d0 = influence_radius + 1 nu = repulsive_coef repulsive = nu * ((1. / d2 - 1. / d0)**2) repulsive[d2 > d0] = 0 d0_m = influence_radius * d0_m_coef + 1 repulsive_d0 = nu * ((1. / d2 - 1. / d0_m)**2) repulsive_d0[d2 > d0_m] = 0 """ Attractive potential """ [x, y] = np.meshgrid(np.arange(ncols), np.arange(nrows)) xi = attractive_coef attractive = xi * ((x - goal[0])**2 + (y - goal[1])**2) """ Combine terms """ total = attractive + repulsive return total, attractive, repulsive, d2, repulsive_d0
def drawSkeleton(segment, edge): skeleton = skeletonize(segment) dist = 2.0 * bwdist(1.0 - edge) make_scale = np.vectorize(lambda x, y: 0 if y < 0.5 else x) scale = make_scale(dist, skeleton).astype(np.uint8) return scale
def CombinedPotential(obstacle, goal): """ Repulsive potential """ d = bwdist(obstacle == 0) d2 = (d / 100.) + 1 # Rescale and transform distances d0 = 2 nu = 500 # repulsive strength: 500 is stronger than 100 repulsive = nu * ((1. / d2 - 1 / d0)**2) repulsive[d2 > d0] = 0 """ Attractive potential """ xi = 1 / 700. # attractive strength: 1/100 is stronger than 1/500 attractive = xi * ((x - goal[0])**2 + (y - goal[1])**2) """ Combine terms """ f = attractive + repulsive return f
def __getitem__(self, i): # input and target images inputName = self.listData[0][i] targetName = self.listData[1][i] # process the images transf = transforms.ToTensor() inputImage = transf( Image.open(self.rootDir + inputName).convert('RGB')) itemGround = loadmat(self.rootDir + targetName) edge, skeleton = itemGround['edge'], itemGround['symmetry'] dist = bwdist(1.0 - edge.astype(float)) make_scale = np.vectorize(lambda x, y: 0 if y < 0.5 else x) scale = make_scale(dist, skeleton) scaleTarget = torch.from_numpy(scale).float() return inputImage, scaleTarget
def combined_potential(obstacles_poses, goal, nrows=500, ncols=500): obstacles_map = map(obstacles_poses) goal = meters2grid(goal) d = bwdist(obstacles_map == 0) d2 = (d / 100.) + 1 # Rescale and transform distances d0 = 2 nu = 200 repulsive = nu * ((1. / d2 - 1. / d0)**2) repulsive[d2 > d0] = 0 [x, y] = np.meshgrid(np.arange(ncols), np.arange(nrows)) xi = 1 / 700. attractive = xi * ((x - goal[0])**2 + (y - goal[1])**2) """ Combine terms """ f = attractive + repulsive return f
def sum_overlapping(tmp1,tmp2): """ calculate overlapping area between patches """ bw = np.zeros(tmp1.shape) D = bwdist(bw==0) D = np.exp(-D) w1 = np.nan*np.zeros(tmp1.shape) w2 = np.nan*np.zeros(tmp1.shape) w2[:,0:5] = np.fliplr(D[:,0:5]) w2[0:5,4:] = np.flipud(D[0:5,4:]) w1[:,0:5] = D[:,0:5] w1[0:5,4:] = D[0:5,4:] wsum = w1+w2 w1 = w1/wsum w2 = w2/wsum tmp2[~np.isnan(tmp2)] = 2*tmp2[~np.isnan(tmp2)]*w2[~np.isnan(tmp2)] tmp1[~np.isnan(tmp2)] = 2*tmp1[~np.isnan(tmp2)]*w1[~np.isnan(tmp2)] overlap = np.array([tmp2,tmp1]) overlap = np.nanmean(overlap,axis=0) return overlap
def combined_potential(obstacles_poses, goal, nrows=50, ncols=50, nd=50): global num_random_obstacles obstacles_map = map(obstacles_poses) goal = meters2grid(goal) d = bwdist(obstacles_map == 0) #bwdist 3d applicable? d2 = (d / 2.) + 1 # Rescale and transform distances d0 = 2 nu = 300 repulsive = nu * ((1. / d2 - 1. / d0)**2) repulsive[d2 > d0] = 0 [x, y, z] = np.meshgrid(np.arange(ncols), np.arange(nrows), np.arange(nd)) xi = 1 / 50. attractive = xi * (num_random_obstacles / 20) * ((x - goal[0])**2 + (y - goal[1])**2 + (z - goal[2])**2) """ Combine terms """ f = attractive + repulsive return f
def combined_potential(obstacles_poses, R_obstacles, goal, influence_radius=2, attractive_coef=1. / 700, repulsive_coef=200, nrows=500, ncols=500): """ Repulsive potential """ obstacles_map = grid_map(obstacles_poses, R_obstacles) goal = meters2grid(goal) d = bwdist(obstacles_map == 0) d2 = (d / 100.) + 1 # Rescale and transform distances d0 = influence_radius nu = repulsive_coef repulsive = nu * ((1. / d2 - 1. / d0)**2) repulsive[d2 > d0] = 0 """ Attractive potential """ [x, y] = np.meshgrid(np.arange(ncols), np.arange(nrows)) xi = attractive_coef attractive = xi * ((x - goal[0])**2 + (y - goal[1])**2) """ Combine terms """ f = attractive + repulsive return f
def interpolate_nn(X): """Simple nearest-neighbor based interplolation. Missing values will be replaced with the nearest non-missing value. Here, "missing values" are defined as those < 0. X : a tensor with dimensions: (#slices, #classes, #rows, #cols) """ Xout = np.zeros(X.shape, dtype=X.dtype) Xi = np.zeros((Xout.shape[-2], Xout.shape[-1]), dtype=X.dtype) for z in range(Xout.shape[0]): for c in range(Xout.shape[1]): Xi[:] = X[z, c, ...] # interpolate, if needed if np.any(Xi < 0): #pct = 1.0*np.sum(Xi<0) / Xi.size dist, nn = bwdist(Xi < 0, return_indices=True) Xi[Xi < 0] = Xi[nn[0][Xi < 0], nn[1][Xi < 0]] Xout[z, c, ...] = Xi return Xout
def interpolate_nn(X): """Simple nearest-neighbor based interplolation. Missing values will be replaced with the nearest non-missing value. Here, "missing values" are defined as those < 0. X : a tensor with dimensions: (#slices, #classes, #rows, #cols) """ Xout = np.zeros(X.shape, dtype=X.dtype) Xi = np.zeros((Xout.shape[-2], Xout.shape[-1]), dtype=X.dtype) for z in range(Xout.shape[0]): for c in range(Xout.shape[1]): Xi[:] = X[z,c,...] # interpolate, if needed if np.any(Xi < 0): #pct = 1.0*np.sum(Xi<0) / Xi.size dist, nn = bwdist(Xi<0, return_indices=True) Xi[Xi<0] = Xi[nn[0][Xi<0], nn[1][Xi<0]] Xout[z,c,...] = Xi return Xout
obstacle = np.zeros((nrows, ncols)) [x, y] = np.meshgrid(np.arange(ncols), np.arange(nrows)) # Generate some obstacle obstacle[300:, 100:250] = True obstacle[150:200, 400:500] = True t = ((x - 200)**2 + (y - 50)**2) < 50**2 obstacle[t] = True t = ((x - 400)**2 + (y - 300)**2) < 80**2 obstacle[t] = True plt.imshow(1 - obstacle, 'gray') # Compute distance transform from scipy.ndimage.morphology import distance_transform_edt as bwdist d = bwdist(obstacle == 0) # distance to closest obstacle function d2 = (d / 100.) + 1 # rescale and transform distances d0 = 2 # radius of potential field influence nu = 800 repulsive = nu * ((1. / d2 - 1. / d0)**2) repulsive[d2 > d0] = 0 # Display repulsive potential fig = plt.figure() ax = fig.gca(projection='3d') plt.title('Repulsive Potential') # Plot the surface. surf = ax.plot_surface(x, y, repulsive, cmap=cm.coolwarm,
def img_to_coords(im_cell, im_nuc, major_angle_object="cell"): """ Return unit vector of v Parameters ---------- im_cell zyx binary image of a cell shape im_nuc zyx binary image of a nuclear shape major_angle_object string that specifies from which object the major angle is determined can be 'cell' or 'nuc' Returns ------- im_ratio channels corresponding to ratio image (1 at cell boundary, 0 on nuclear boundary, -1 inside nucleus) im_th spherical coordinate system theta (radians) im_phi spherical coordinate system phi (radians) im_r radial distance from center of nucleus major_angle angle of cell or nuclear shape (radians) """ cell_dist_in = skfmm.distance(np.ma.MaskedArray(im_cell, im_nuc)).data cell_dist_out = skfmm.distance(np.ma.MaskedArray(im_nuc == 0, im_cell == 0)).data nuc_dist = bwdist(im_nuc) nuc_ratio = -nuc_dist / np.max(nuc_dist) nuc_ratio[np.isnan(nuc_ratio)] = 0 cyto_ratio = cell_dist_out / (cell_dist_out + cell_dist_in) cyto_ratio[np.isnan(cyto_ratio)] = 0 im_cyto = (im_cell > 0) & (im_nuc == 0) im_ratio = np.zeros(im_nuc.shape) im_ratio[im_nuc > 0] = nuc_ratio[im_nuc > 0] im_ratio[im_cyto] = cyto_ratio[im_cyto] centroid = regionprops((im_nuc > 0).astype("uint8"))[0]["centroid"] if major_angle_object == "nuc": major_angle = rigidAlignment.get_major_angle( im_nuc, degrees_or_radians="radians")[0][1] elif major_angle_object == "cell": major_angle = rigidAlignment.get_major_angle( im_cell, degrees_or_radians="radians")[0][1] coords = np.where(im_cell > 0) th, phi, r = cart2sph(coords[1] - centroid[1], coords[2] - centroid[2], coords[0] - centroid[0]) th = th - major_angle th = th - 2 * math.pi * np.floor((th + math.pi) / (2 * math.pi)) im_th = np.zeros(im_nuc.shape) im_th[im_cell > 0] = th im_phi = np.zeros(im_nuc.shape) im_phi[im_cell > 0] = phi im_r = np.zeros(im_nuc.shape) im_r[im_cell > 0] = r return im_ratio, im_th, im_phi, im_r, major_angle
import matplotlib.pyplot as plt import math import numpy as np from tqdm import tqdm from scipy.io import loadmat from scipy.ndimage.morphology import distance_transform_edt as bwdist rootDirGt = "data/groundTruth/train/" rootDirImg = "data/images/train/" ouputDir = "data/skeleton/train/" os.makedirs(ouputDir, exist_ok=True) listImages = sorted(os.listdir(rootDirImg)) listData = sorted(os.listdir(rootDirGt)) for i, itemName in enumerate(listData, 0): itemGround = loadmat(rootDirGt + itemName) edge, skeleton = itemGround['edge'], itemGround['symmetry'] dist = bwdist(1.0 - edge.astype(float)) appl = np.vectorize(lambda x, y: y if x > 0.5 else 0) result = appl(skeleton, dist) print(np.max(result)) savePath = listImages[i].split(".jpg")[0] + ".bmp" img = Image.fromarray(result.astype(np.uint8), 'L') img.save(ouputDir + savePath) transf = transforms.ToTensor() print( torch.max(255.0 * transf(Image.open(ouputDir + savePath).convert('L')))) print("----------------")
BIGGER_SIZE = big plt.rc('font', size=SMALL_SIZE) # controls default text sizes plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels plt.rc('legend', fontsize=MEDIUM_SIZE) # legend fontsize plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title obstacles_grid = np.zeros(500) obstacles_grid[:235] = 1 obstacles_grid[265:] = 1 d = bwdist(obstacles_grid == 0) d2 = (d / 100.) + 1 d0 = 0.15 + 1 nu = 200 repulsive = nu * ((1. / d2 - 1. / d0)**2) repulsive[d2 > d0] = 0 repulsive /= np.max(repulsive) x = np.linspace(-2.5, 2.5, len(obstacles_grid)) init_fonts() plt.plot(x, 2 * repulsive, color='red', label='Repulsive potential, $U_y^r$',
for angle in tqdm([0, 90, 180, 270]): # os.makedirs(output_dir + str(scale) + "/o/" + str(angle) + "/f/", exist_ok=True) # os.makedirs(img_dir + str(scale) + "/o/" + str(angle) + "/f/", exist_ok=True) for flip in tqdm([0, 1, 2]): # os.makedirs(output_dir + str(scale) + "/o/" + str(angle) + "/f/" + str(flip), exist_ok=True) # os.makedirs(img_dir + str(scale) + "/o/" + str(angle) + "/f/" + str(flip), exist_ok=True) for i in range(len(listData)): targetName = listData[i] gtpath = output_dir + str(scale) + str(angle) + str(flip) + "_" impath = img_dir + str(scale) + str(angle) + str(flip) + "_" name = targetName.replace(".mat", ".png") imageTrain = cv2.imread(trainDirImg + listimages[i]) itemGround = loadmat(rootDirGt + targetName) edge, skeleton = itemGround['edge'], itemGround['symmetry'] dist = 2*bwdist(1 - edge) appl = np.vectorize(lambda x, y: 0 if y < 0.5 else x) img = appl(dist,skeleton) img = Image.fromarray(img.astype(np.uint8)) img = img.rotate(angle, PIL.Image.BICUBIC, True) imageTrain = Image.fromarray(imageTrain) imageTrain = imageTrain.rotate(angle, PIL.Image.BICUBIC, True) new_size = (int(np.ceil(scale*img.size[0])), int(np.ceil(scale*img.size[1]))) img = img.resize(new_size) new_size_train = (int(np.ceil(scale*imageTrain.size[0])), int(np.ceil(scale*imageTrain.size[1]))) imageTrain = imageTrain.resize(new_size_train) if flip == 1: img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT) imageTrain = imageTrain.transpose(PIL.Image.FLIP_LEFT_RIGHT) elif flip == 2: