difficulty with bUnwapj transfomations in macros

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

difficulty with bUnwapj transfomations in macros

Jiří Borovec
Hello all,
may I have question about the bUnwarpJ plugin. I already searched over Internet, but not succeed much. I have a case - register two images together, then warp the source image to target image and in the end generate the raw transformation to be able show some important points in the warped image according their positions in source image. (In this moment I don't race much about quality of the registration.) This perfectly works when both image are the same size but in following case the source image is smaller then the target one. Would you have an advice where am I making mistake?

In attachment are two images to be registered together with two list of related points. there is the macro I used for registration, warping image and converting raw transformation and also there is a script that parse the raw transform (as I understood it displacement field for X and Y coordinate) and show the points in image for input source image and warped image.

MACRO
=====================================================
print ("-> images opening...");
im_target="./temp/bUnwarpJ/Rat_Kidney_HE.jpg"
im_source="./temp/bUnwarpJ/Rat_Kidney_PanCytokeratin.jpg"
output="./temp/bUnwarpJ/"
open(im_target);
open(im_source);

print ("-> start registration...");
run("bUnwarpJ",
    "source_image=Rat_Kidney_PanCytokeratin.jpg target_image=Rat_Kidney_HE.jpg "
    + " registration=Fast image_subsample_factor=1 initial_deformation=[Very Coarse] final_deformation=Fine divergence_weight=0.0 curl_weight=0.0 landmark_weight=0.0 image_weight=1 consistency_weight=10 stop_threshold=0.001 verbose save_transformations "
    + " save_direct_transformation="+output+"direct_transform.txt "
    + " save_inverse_transformation="+output+"invers_transform.txt");
print ("-> registration finished");

run("Close All");

// Transform image
call("bunwarpj.bUnwarpJ_.elasticTransformImageMacro",
     im_target, im_source, output+"direct_transform.txt", output+"Rat_Kidney_PanCytokeratin_warped.jpg");
// resave image while bunwarpj macro have an issue
open(output+"Rat_Kidney_PanCytokeratin.jpg");
saveAs("PNG", output+"Rat_Kidney_PanCytokeratin.jpg");

run("Close All");

// Convert transformation
call("bunwarpj.bUnwarpJ_.convertToRawTransformationMacro",
     im_target, im_source, output+"invers_transform.txt", output+"transform_raw.txt");
print ("-> DONE");
=====================================================

SCRIPT PYTHON
=====================================================
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):
    df = pd.DataFrame(landmarks, columns=['X', 'Y'])
    df.to_csv(path_file)

def load_landmarks_csv(path_file):
    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):
    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):
    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()
=====================================================

Thanks
PS: no way how to send the the macro and script as file because it is always automatically rejected
--
Best regards, Jiri Borovec
------------------------------------------------------------------------
Ing. Jiri Borovec, MSc  <[hidden email]>
PhD student at CMP CTU, http://cmp.felk.cvut.cz/~borovji3

--
--
Please avoid top-posting, and please make sure to reply-to-all!
 
Mailing list web interface: http://groups.google.com/group/fiji-devel

---
You received this message because you are subscribed to the Google Groups "Fiji-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...