def find_image_pos(self, xs: int, ys: int, t: int, local=False) -> (List[image], List[coord]): """ For coordinates (xs, yx) in the full size stiched image, returns a list of original images (before stiching) that contain the coordinate. And for each found image, returns the coordinates of the point of interest in the image. """ date = self.dates[t] directory_name = get_dirname(date, self.plate) path_snap = self.directory + directory_name path_tile = path_snap + "/Img/TileConfiguration.txt.registered" skel = read_mat(path_snap + "/Analysis/skeleton_pruned_realigned.mat") Rot = skel["R"] trans = skel["t"] rottrans = np.dot(np.linalg.inv(Rot), np.array([xs, ys] - trans)) ys, xs = round(rottrans[0]), round(rottrans[1]) tileconfig = pd.read_table( path_tile, sep=";", skiprows=4, header=None, converters={2: ast.literal_eval}, skipinitialspace=True, ) xs_yss = list(tileconfig[2]) xes = [xs_ys[0] for xs_ys in xs_yss] yes = [xs_ys[1] for xs_ys in xs_yss] cmin = np.min(xes) cmax = np.max(xes) rmin = np.min(yes) rmax = np.max(yes) ximg = xs yimg = ys def find(xsub, ysub, x, y): indexes = [] for i in range(len(xsub)): if (x >= xsub[i] - cmin and x < xsub[i] - cmin + 4096 and y >= ysub[i] - rmin and y < ysub[i] - rmin + 3000): indexes.append(i) return indexes indsImg = find(xes, yes, ximg, yimg) possImg = [ ximg - np.array(xes)[indsImg] + cmin + 1, yimg - np.array(yes)[indsImg] + rmin + 1, ] paths = [] for index in indsImg: name = tileconfig[0][index] imname = "/Img/" + name.split("/")[-1] directory_name = get_dirname(date, self.plate) path = self.directory + directory_name + imname paths.append(path) ims = [imageio.imread(path) for path in paths] return (ims, possImg)
def get_pos_baits_aligned(exp, t): pos_bait = get_pos_baits(exp, t) date = exp.dates[t] directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name path_tile = path_snap + "/Img/TileConfiguration.txt.registered" skel = read_mat(path_snap + "/Analysis/skeleton_pruned_realigned.mat") Rot = skel["R"] trans = skel["t"] print(Rot, trans) real_pos = [] for x, y, r in pos_bait: compression = 5 xs, ys = x * compression, y * compression rottrans = np.dot(Rot, np.array([ys, xs])) + trans # rottrans = np.array([xs, ys]) xs, ys = round(rottrans[0]), round(rottrans[1]) real_pos.append((xs, ys)) pos_real = {} if real_pos[0][1] >= real_pos[1][1]: pos_real["right"] = real_pos[0] pos_real["left"] = real_pos[1] else: pos_real["right"] = real_pos[1] pos_real["left"] = real_pos[0] return pos_real
def load_graphs(exp, indexes=None): nx_graph_poss = [] labeled = exp.labeled if indexes == None: indexes = range(exp.ts) for index, date in enumerate(exp.dates): directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name if labeled: suffix = "/Analysis/nx_graph_pruned_labeled2.p" else: suffix = "/Analysis/nx_graph_pruned.p" path_save = path_snap + suffix (g, pos) = pickle.load(open(path_save, "rb")) if index in indexes: nx_graph_poss.append((g, pos)) else: edge_empty = {edge: None for edge in g.edges} nx.set_edge_attributes(g, edge_empty, "pixel_list") nx_graph_poss.append((g, pos)) nx_graphs = [nx_graph_pos[0] for nx_graph_pos in nx_graph_poss] poss = [nx_graph_pos[1] for nx_graph_pos in nx_graph_poss] # nx_graph_clean=[] # for graph in nx_graphs: # S = [graph.subgraph(c).copy() for c in nx.connected_components(graph)] # len_connected=[len(nx_graph.nodes) for nx_graph in S] # nx_graph_clean.append(S[np.argmax(len_connected)]) exp.positions = poss exp.nx_graph = nx_graphs
def plot_raw(self, t, figsize=(10, 9)): """Plot the full stiched image compressed (otherwise too heavy)""" date = self.dates[t] directory_name = get_dirname(date, self.plate) path_snap = self.directory + directory_name im = read_mat(path_snap + "/Analysis/raw_image.mat")["raw"] fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) ax.imshow(im)
def get_study_zone(exp, dist, radius, i=0): date = exp.dates[i] directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name im = read_mat(path_snap + "/Analysis/raw_image.mat")["raw"] shape_compressed = im.shape[1] // 5, im.shape[0] // 5 im_comp = cv2.resize(im, shape_compressed) dr_orth, dr_center = place_study_zone(im_comp, dist, radius) return (dr_orth, dr_center)
def check_state(plate, begin, end, file, directory): not_exist = [] dates_datetime = get_dates_datetime(directory, plate) dates_datetime_chosen = dates_datetime[begin : end + 1] dates = dates_datetime_chosen for i, date in enumerate(dates): directory_name = get_dirname(date, plate) path_snap = directory + directory_name stage = os.path.exists(path_snap + file) if not stage: not_exist.append((date, i + begin)) return not_exist
def save_graphs(exp): nx_graph_poss = [] for i, date in enumerate(exp.dates): directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name labeled = exp.labeled print(date, labeled) if labeled: suffix = "/Analysis/nx_graph_pruned_labeled2.p" path_save = path_snap + suffix # print(path_save) g = exp.nx_graph[i] pos = exp.positions[i] pickle.dump((g, pos), open(path_save, "wb"))
def save_study_zone(dr_orth, dr_center, exp, i=0): rect_orth, rect_center = dr_orth.rect, dr_center.rect center = rect_center.xy orthog = np.array(rect_orth.xy) - np.array(center) orthog = orthog / np.linalg.norm(orthog) compress = 25 date = exp.dates[i] directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name skel = read_mat(path_snap + "/Analysis/skeleton_pruned_realigned.mat") Rot = skel["R"] trans = skel["t"] skelet = skel["skeleton"] xc, yc = center[0], center[1] rottrans = np.dot(Rot, np.array([yc, xc])) + trans // compress pos_center = round(rottrans[1]) * compress, round(rottrans[0]) * compress orthog = np.flip(np.dot(Rot, np.flip(orthog))) exp.center, exp.orthog = pos_center, orthog
def load(self, folders: pd.DataFrame, labeled=True): """Loads the graphs from the different time points and other useful attributes""" self.folders = folders dates_datetime = [ datetime.strptime(row["date"], "%d.%m.%Y, %H:%M:") for index, row in folders.iterrows() ] self.dates = dates_datetime self.dates.sort() nx_graph_poss = [] for date in self.dates: print(date) directory_name = get_dirname(date, self.plate) path_snap = self.directory + directory_name if labeled: suffix = "/Analysis/nx_graph_pruned_labeled.p" else: suffix = "/Analysis/nx_graph_pruned.p" path_save = path_snap + suffix (g, pos) = pickle.load(open(path_save, "rb")) nx_graph_poss.append((g, pos)) nx_graphs = [nx_graph_pos[0] for nx_graph_pos in nx_graph_poss] poss = [nx_graph_pos[1] for nx_graph_pos in nx_graph_poss] # nx_graph_clean=[] # for graph in nx_graphs: # S = [graph.subgraph(c).copy() for c in nx.connected_components(graph)] # len_connected=[len(nx_graph.nodes) for nx_graph in S] # nx_graph_clean.append(S[np.argmax(len_connected)]) self.positions = poss self.nx_graph = nx_graphs labels = {node for g in self.nx_graph for node in g} self.nodes = [] for label in labels: self.nodes.append(Node(label, self)) xpos = [pos[0] for poss in self.positions for pos in poss.values()] ypos = [pos[1] for poss in self.positions for pos in poss.values()] self.ts = len(self.dates) self.labeled = labeled
def get_skeleton_non_aligned(exp, boundaries, t, directory): i = t plate = exp.plate listdir = os.listdir(directory) dates = exp.dates date = dates[i] directory_name = get_dirname(date, plate) path_snap = directory + directory_name skel = read_mat(path_snap + "/Analysis/skeleton.mat") skelet = skel["skeleton"] skelet = sparse_to_doc(skelet) # Rot= skel['R'] # trans = skel['t'] skel_aligned = transform_skeleton_final_for_show( skelet, np.array([[1, 0], [0, 1]]), np.array([0, 0]) ) output = skel_aligned[ boundaries[2] : boundaries[3], boundaries[0] : boundaries[1] ].todense() kernel = np.ones((5, 5), np.uint8) output = cv2.dilate(output.astype(np.uint8), kernel, iterations=1) return output
def get_raw(exp, t): date = exp.dates[t] directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name im = read_mat(path_snap + "/Analysis/raw_image.mat")["raw"] return im
get_postion_number, ) i = int(sys.argv[-1]) plate = int(sys.argv[1]) low = int(sys.argv[2]) high = int(sys.argv[3]) no = int(sys.argv[4]) si = int(sys.argv[5]) directory = str(sys.argv[6]) dates_datetime = get_dates_datetime(directory, plate) dates_datetime.sort() dates = dates_datetime date = dates[i] directory_name = get_dirname(date, plate) path_snap = directory + directory_name path_tile = path_snap + "/Img/TileConfiguration.txt.registered" try: tileconfig = pd.read_table( path_tile, sep=";", skiprows=4, header=None, converters={2: ast.literal_eval}, skipinitialspace=True, ) except: print("error_name") path_tile = path_snap + "/Img/TileConfiguration.registered.txt" tileconfig = pd.read_table(
def test(): from path import path_code_dir import sys sys.path.insert(0, path_code_dir) from amftrack.util.sys import get_dirname import pandas as pd import ast from scipy import sparse from datetime import datetime from amftrack.pipeline.functions.image_processing.node_id import orient import scipy.io as sio import cv2 import imageio import numpy as np from skimage.filters import frangi from skimage import filters import scipy.sparse import os from time import time from skimage.feature import hessian_matrix_det from amftrack.pipeline.functions.image_processing.extract_graph import ( from_sparse_to_graph, generate_nx_graph, ) from amftrack.pipeline.functions.image_processing.extract_skel import ( extract_skel_tip_ext, ) from amftrack.util.sys import ( get_dates_datetime, get_dirname, get_plate_number, get_postion_number, ) i = 0 plate = 31 low = 30 high = 80 dist = 30 directory = directory_scratch dates_datetime = get_dates_datetime(directory, plate) dates_datetime.sort() dates = dates_datetime date = dates[i] directory_name = get_dirname(date, plate) path_snap = directory + directory_name path_tile = path_snap + "/Img/TileConfiguration.txt.registered" try: tileconfig = pd.read_table( path_tile, sep=";", skiprows=4, header=None, converters={2: ast.literal_eval}, skipinitialspace=True, ) except: print("error_name") path_tile = path_snap + "/Img/TileConfiguration.registered.txt" tileconfig = pd.read_table( path_tile, sep=";", skiprows=4, header=None, converters={2: ast.literal_eval}, skipinitialspace=True, ) dirName = path_snap + "/Analysis" shape = (3000, 4096) try: os.mkdir(path_snap + "/Analysis") print("Directory ", dirName, " Created ") except FileExistsError: print("Directory ", dirName, " already exists") t = time() xs = [c[0] for c in tileconfig[2]] ys = [c[1] for c in tileconfig[2]] dim = (int(np.max(ys) - np.min(ys)) + 4096, int(np.max(xs) - np.min(xs)) + 4096) ims = [] skel = np.zeros(dim, dtype=np.uint8) for index, name in enumerate(tileconfig[0][:10]): imname = "/Img/" + name.split("/")[-1] im = imageio.imread(directory + directory_name + imname) print(index) segmented = extract_skel_tip_ext(im, low, high, dist) # np.save(f'Temp\dilated{tileconfig[0][i]}',dilated) boundaries = int(tileconfig[2][index][0] - np.min(xs)), int(tileconfig[2][index][1] - np.min(ys)) skel[boundaries[1]:boundaries[1] + shape[0], boundaries[0]:boundaries[0] + shape[1], ] += segmented print("number to reduce : ", np.sum(skel > 0), np.sum(skel <= 0)) skeletonized = cv2.ximgproc.thinning( np.array(255 * (skel > 0), dtype=np.uint8)) skel_sparse = sparse.lil_matrix(skel) sio.savemat(path_snap + "/Analysis/dilated.mat", {"dilated": skel_sparse}) sio.savemat( path_snap + "/Analysis/skeleton.mat", {"skeleton": scipy.sparse.csc_matrix(skeletonized)}, ) compressed = cv2.resize(skeletonized, (dim[1] // 5, dim[0] // 5)) sio.savemat(path_snap + "/Analysis/skeleton_compressed.mat", {"skeleton": compressed}) print("time=", time() - t)
def plot_raw_plus( exp, t0, node_list, shift=(0, 0), n=0, compress=5, center=None, radius_imp=None, fig=None, ax=None, ): """ Plots a group of node on the compressed full stiched image of the skeleton. """ date = exp.dates[t0] directory_name = get_dirname(date, exp.plate) path_snap = exp.directory + directory_name skel = read_mat(path_snap + "/Analysis/skeleton_pruned_realigned.mat") Rot = skel["R"] trans = skel["t"] im = read_mat(path_snap + "/Analysis/raw_image.mat")["raw"] size = 5 if fig is None: fig = plt.figure() ax = fig.add_subplot(111) alpha = 0.3 cmap = "jet" else: alpha = 0.3 cmap = "gray" grey = 1 - 1 / (n + 2) if center is None: center = np.mean( [ exp.positions[t0][node] for node in node_list if node in exp.positions[t0].keys() ], axis=0, ) radius = np.max([ np.linalg.norm(exp.positions[t0][node] - center) for node in node_list if node in exp.positions[t0].keys() ]) radius = np.max((radius, radius_imp)) else: radius = radius_imp boundaries = ( np.max( np.transpose( ((0, 0), (center - 2 * radius).astype(int) // compress)), axis=1, ), (center + 2 * radius).astype(int) // compress, ) # boundaries = np.flip(boundaries,axis = 1) height, width = im.shape[:2] trans_mat = np.concatenate( (np.linalg.inv(Rot), np.flip(trans.reshape(-1, 1) // compress)), axis=1) im = cv2.warpAffine(src=im, M=trans_mat, dsize=(width, height)) # print(radius) im = im[boundaries[0][0]:boundaries[1][0], boundaries[0][1]:boundaries[1][1]] _ = ax.imshow(im, alpha=alpha, cmap=cmap) bbox = dict(boxstyle="circle", fc=colors.rgb2hex((grey, grey, grey))) # ax.text(right, top, time, # horizontalalignment='right', # verticalalignment='bottom', # transform=ax.transAxes,color='white') shift = (max(0, boundaries[0][0] * compress), max(0, boundaries[0][1] * compress)) for node in node_list: # print(self.positions[ts[i]]) if node in exp.positions[t0].keys(): xs, ys = exp.positions[t0][node] # rottrans = np.dot(np.linalg.inv(Rot), np.array([xs, ys] - trans)) # rottrans = np.dot(Rot, np.array([xs, ys]))+trans//5 # ys, xs = round(rottrans[0]), round(rottrans[1]) xs, ys = ys, xs t = ax.text( (xs - shift[1]) // compress, (ys - shift[0]) // compress, str(node), ha="center", va="center", size=size, bbox=bbox, ) return (fig, ax, center, radius)