-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLocTransform.m
141 lines (129 loc) · 5.78 KB
/
LocTransform.m
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
classdef LocTransform<handle
properties
transform %forward .inversie
tinfo
transformz;
cam_pixnm;
end
methods
function findTransform(obj,xreference,yreference,xtarget,ytarget,transform)
%XXX make sure reference and target are not exchanged
%matrix becomes singular for too large coordinates. Use um
%instead of nm
xreference=xreference/1000;yreference=yreference/1000;
xtarget=xtarget/1000;ytarget=ytarget/1000;
switch transform.type
case {'lwm','polynomial'}
obj.transform.inverse = fitgeotrans(double([xtarget ytarget]), [double(xreference) double(yreference)],transform.type,transform.parameter);
obj.transform.forward = fitgeotrans( [double(xreference) double(yreference)],double([xtarget ytarget]),transform.type,transform.parameter);
otherwise
obj.transform.inverse= fitgeotrans(double([xtarget ytarget]),[double(xreference) double(yreference)],transform.type);
obj.transform.forward= fitgeotrans([double(xreference) double(yreference)],double([xtarget ytarget]),transform.type);
end
end
function findTransformZ(obj,xreference,yreference,zreference,xtargeti,ytargeti,ztarget,transform)
%do 2D transform first;
% obj.findTransform(xreference,yreference,xtargeti,ytargeti,transform);
% transform x,y target
[xtarget,ytarget]=transformCoordinatesInv(obj,xtargeti,ytargeti);
%XXX make sure reference and target are not exchanged
%matrix becomes singular for too large coordinates. Use um
%instead of nm
xreference=xreference/1000;yreference=yreference/1000;
xtarget=xtarget/1000;ytarget=ytarget/1000;
zreference=zreference/1000;
ztarget=ztarget/1000;
%only affine3d possible
obj.transformz.inverse= findAffineTransformZ(double([xtarget ytarget ztarget]),double([zreference]));
obj.transformz.forward= findAffineTransformZ(double([xreference yreference zreference]),double([ztarget]));
end
function [xo,yo,zo]=transformCoordinatesFwd(obj,x,y,z)
%transforms reference onto target
% [xo,yo]=transformPointsForward(obj.transform.forward,x,y);
x=x/1000;y=y/1000;
[xo,yo]=transformPointsInverse(obj.transform.inverse,x,y); %inverse of inverse is forward
if nargin>3&&~isempty(z) %z coordinates present
z=z/1000;
X=transformPointsInverse(obj.transformz.inverse,[xo,yo,z]);
xo=X(:,1);yo=X(:,2);zo=X(:,3);
zo=zo*1000;
else
zo=[];
end
xo=xo*1000;yo=yo*1000;
end
function [xo,yo,zo]=transformCoordinatesInv(obj,x,y,z)
%transforms target onto reference
% [xo,yo]=transformPointsForward(obj.transform.inverse,x,y);
x=x/1000;y=y/1000;
[xo,yo]=transformPointsInverse(obj.transform.forward,x,y);
if nargin>3 &&~isempty(z)%z coordinates present
z=z/1000;
X=transformPointsInverse(obj.transformz.forward,[xo,yo,z]);
xo=X(:,1);yo=X(:,2);zo=X(:,3);
zo=zo*1000;
else
zo=[];
end
xo=xo*1000;yo=yo*1000;
end
function imout=transformImageFwd(obj,image,cam_pixnm,roi)
imout=transformImage(obj.transform.forward,image,cam_pixnm,roi);
end
function imout=transformImageInv(obj,image,cam_pixnm,roi)
imout=transformImage(obj.transform.inverse,image,cam_pixnm,roi);
end
% function srim=getSrimage(obj)
% end
function ind=getRef(obj,x,y) %returns indices for reference.
separator=obj.tinfo.separator;
switch obj.tinfo.targetpos
case 'top'
ind=y>separator(2);
case 'bottom'
ind=y<separator(2);
case 'left'
ind=x>separator(1);
case 'right'
ind=x<separator(1);
case 'center'
ind=true(size(x));
end
end
function makeAffine2d(obj,A)
tform=affine2d(A);
obj.transform.forward=tform;
obj.transform.inverse=invert(tform);
mirror=struct('midmirror',17664,'targetmirror','no mirror');
obj.tinfo=struct('targetpos','all','separator',[70656 35328],'mirror',mirror);
end
function co=transformToTarget(obj,channel,ci,unit) %compatibilty to new version
if channel ~=2
disp('channel needs to be 2 for compatibility')
end
x=ci(:,1);y=ci(:,2);
[xo,yo]=transformCoordinatesFwd(obj,x,y);
co=horzcat(xo,yo);
end
function co=transformToReference(obj,channel,ci,unit) %compatibilty to new version
if channel ~=2
disp('channel needs to be 2 for compatibility')
end
x=ci(:,1);y=ci(:,2);
[xo,yo]=transformCoordinatesInv(obj,x,y);
co=horzcat(xo,yo);
end
function ind=getPart(obj,channel,ci,unit)
x=ci(:,1);y=ci(:,2);
indref=obj.getRef(x,y);
switch channel
case 1
ind=indref;
case 2
ind=~indref;
otherwise
disp('channel for getPart needs to be 1 or 2 for compatibility');
end
end
end
end