import os import numpy as np import pandas as pd import matplotlib.pylab as plt PATH_IMAGE_REF = 'Rat_Kidney_HE.jpg' PATH_IMAGE_MOVE = 'Rat_Kidney_PanCytokeratin.jpg' PATH_IMAGE_WARP = 'Rat_Kidney_PanCytokeratin_warped.jpg' PATH_POINTS_MOVE = 'Rat_Kidney_PanCytokeratin.csv' PATH_POINTS_WARP = 'Rat_Kidney_PanCytokeratin_warp.csv' PATH_TRANSFORM = 'transform_raw.txt' def save_landmarks_csv(path_file, landmarks): """ save landmarks into a csv file :param str path_file: path to the output file :param landmarks: np.array """ df = pd.DataFrame(landmarks, columns=['X', 'Y']) df.to_csv(path_file) def load_landmarks_csv(path_file): """ load file with landmarks in cdv format :param str path_file: path to the input file :return: np.array """ assert os.path.exists(path_file) df = pd.DataFrame.from_csv(path_file) points = df[['X', 'Y']].values return points def load_parse_bunwarpj_displacement_axis(fp, size, points): """ given pointer in the file aiming to the beginning of displacement parse all lines and if in the particular line is a point from list get its new position :param fp: file pointer :param (int, int) size: width, height of the image :param points: np.array :return list: list of new positions on given axis (x/y) for related points """ width, height = size points = np.round(points) selected_lines = points[:, 1].tolist() pos_new = [0] * len(points) # walk thor all lined of this displacement field for i in range(height): line = fp.readline() # if the any point is listed in this line if i in selected_lines : pos = line.rstrip().split() # pos = [float(e) for e in pos if len(e)>0] assert len(pos) == width # find all points in this line for j, point in enumerate(points): if point[1] == i: pos_new[j] = float(pos[point[0]]) return pos_new def load_parse_bunwarpj(path_file, points): """ load and parse displacement field for both X and Y coordinated and return new position of selected points :param str path_file: :param points: np.array :return: np.array """ fp = open(path_file, "r") width = int(fp.readline().split('=')[1]) height = int(fp.readline().split('=')[1]) size = (width, height) assert all(np.max(points, axis=0) <= size) fp.readline(), fp.readline() # read X Trans points_x = load_parse_bunwarpj_displacement_axis(fp, size, points) fp.readline(), fp.readline() # read Y Trans points_y = load_parse_bunwarpj_displacement_axis (fp, size, points) fp.close() points_new = np.array(zip(points_x, points_y)) return points_new if __name__ == '__main__': img_ref = plt.imread(PATH_IMAGE_REF) img_move = plt.imread(PATH_IMAGE_MOVE) img_warp = plt.imread(PATH_IMAGE_WARP) points_move = load_landmarks_csv(PATH_POINTS_MOVE) points_warp = load_parse_bunwarpj(PATH_TRANSFORM, points_move) save_landmarks_csv(PATH_POINTS_WARP, points_warp) plt.figure() plt.imshow(img_ref, alpha=0.5) plt.imshow(img_warp, alpha=0.5) plt.figure() plt.imshow(img_move) plt.plot(points_move[:, 0], points_move[:, 1], 'go') plt.figure() plt.imshow(img_warp) plt.plot(points_warp[:, 0], points_warp[:, 1], 'go') plt.show() -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html