-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvader.py
48 lines (37 loc) · 1.66 KB
/
invader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import print_function
import os.path
import json
import numpy as np
import scipy as sp
import matplotlib
matplotlib.use('qt5agg')
import matplotlib.pyplot as plt
import skimage
from skimage import (morphology, segmentation, exposure, feature, filters,
measure, transform, util, io, color)
from toolz.curried import *
import click
import imgz, spotzplot
#-------------------------------------------------------------------------------
def estimate_transform_from_regions(regions1, regions2):
in1 = set(r.label for r in regions1)
in2 = set(r.label for r in regions2)
in1and2 = sorted(list(in1 & in2))
centroids1 = np.array([r.centroid for r in regions1 if r.label in in1and2])
centroids2 = np.array([r.centroid for r in regions2 if r.label in in1and2])
tform = transform.estimate_transform("similarity", np.fliplr(centroids1),
np.fliplr(centroids2))
return tform
def apply_transform(limg, geomtransform, shape):
wlimg = transform.warp(limg, geomtransform.inverse, output_shape=shape,
preserve_range=True, order=0)
wlimg = np.round(wlimg).astype(np.int64)
regions = sort_regions_by_label(measure.regionprops(wlimg))
return wlimg, regions
def align_regions(labeled_img1, labeled_img2, regions1, regions2):
tform = estimate_transform_from_regions(regions1, regions2)
mapped_labels, mapped_regions = apply_transform(labeled_img1,
tform,
labeled_img2.shape)
mapped_labeled_img = mapped_labels
mapped_regions = mapped_regions