-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automatic_SGLE2_channelselect.ijm
210 lines (183 loc) · 7.16 KB
/
Automatic_SGLE2_channelselect.ijm
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//Import all image series from the treatment,
//concatenate in correct sequence and then put in the drift occurance, frame pivots and direction of rotation below;
Dialog.create("How many driftings occur here?");
Dialog.addNumber("The number of drifing occurance", 0);
Dialog.show();
driftnumber = Dialog.getNumber();
endnumber=driftnumber+1;
driftstart=newArray(endnumber);
label=newArray(endnumber);
rotation="direction";
channel="color";
camera="SGLE";
Dialog.create("Put in the values");
for(i=1; i<=driftnumber; i++){
label[i-1]="Drifting"+i+" occurs at frame:";
Dialog.addNumber(label[i-1], 0);
}
label[driftnumber]="Total frame number:";
Dialog.addNumber(label[driftnumber], 0);
Dialog.addString("Which camera:",camera);
Dialog.addString("Rotate left or right L/R:",rotation);
Dialog.addString("The channel used for alignment Green/Red:",channel);
Dialog.show();
Message="The fragments are: ";
for(i=1; i<=driftnumber; i++){
driftstart[i-1] = Dialog.getNumber();
Message=Message+"Drift"+i+" is frame "+driftstart[i-1]+"; ";
}
driftstart[driftnumber] = Dialog.getNumber()+1;
camera=Dialog.getString();
rotation=Dialog.getString();
channel=Dialog.getString();
Message=Message+"Stack ends at frame "+(driftstart[driftnumber]-1)+"; "+channel+" images are used for alignment; Will rotate "+rotation+" to correct direction";
showMessage(Message);
//Channel names swtiched in SGLE1
color1="Green";
color2="Red";
if(camera=="SGLE1"){
color1="Red";
color2="Green";
}
//Crop and split the concatenated stacks, star out this section if doing manual alighment after the image split
selectWindow("Concatenated Stacks");
run("Remove Outliers...", "radius=2 threshold=50 which=Bright stack");
makeRectangle(0, 0, 266, 516);
run("Duplicate...", "duplicate");
selectWindow("Concatenated Stacks-1");
rename(color1);
selectWindow("Concatenated Stacks");
makeRectangle(266, 0, 266, 516);
run("Crop");
rename(color2);
//Close display updating
setBatchMode(true);
//Alignment cycles
for(i=1; i<=driftnumber; i++){
//Prepare frame points needed for drift and ref images
//48-frame average or 60-frame average
refavrstart=driftstart[i-1]-100;
refavrstop=driftstart[i-1]-1;
driftavrstart=driftstart[i-1];
driftavrstop=driftstart[i-1]+100;
//Make a pair of average images for feature extraction and alignment
//Use either Green or Red as references
selectWindow(channel);
run("Z Project...", "start="+refavrstart+" stop="+refavrstop+" projection=[Average Intensity]");
rename("Ref");
selectWindow(channel);
run("Z Project...", "start="+driftavrstart+" stop="+driftavrstop+" projection=[Average Intensity]");
rename("Drift");
//Adjust B/C before feature extraction, 32bit images are not handled properly by the
//plugin and need to manually set B/C for a successful extraction
//If feature extractions all failed, pick features by hand will be the only way,add to ROI and start below.
selectWindow("Drift");
run("Set Measurements...", "modal min redirect=None decimal=3");
List.setMeasurements();
min=List.getValue("Mode");
maximum=List.getValue("Max");
max=min+0.8*(maximum-min);
run("Brightness/Contrast...");
setMinAndMax(min, max);
selectWindow("Ref");
run("Set Measurements...", "modal min redirect=None decimal=3");
List.setMeasurements();
min=List.getValue("Mode");
maximum=List.getValue("Max");
max=min+0.8*(maximum-min);
run("Brightness/Contrast...");
setMinAndMax(min, max);
run("Extract SIFT Correspondences", "source_image=Drift target_image=Ref initial_gaussian_blur=1.60 steps_per_scale_octave=3 minimum_image_size=64 maximum_image_size=1024 feature_descriptor_size=4 feature_descriptor_orientation_bins=8 closest/next_closest_ratio=0.92 filter maximal_alignment_error=25 minimal_inlier_ratio=0.05 minimal_number_of_inliers=7 expected_transformation=Rigid");
//If the first SIFT function didn't find features, or the image is very weak for discrete features using the block matching method for a more coarse extraction or hand picked features using multiple-point tool
//MOPS works badly and output some wrong coordinates sometimes, neglect this and use next step if SIFT doesn't work// run("Extract MOPS Correspondences", "source_image=Drift target_image=Ref initial_gaussian_blur=1.60 steps_per_scale_octave=3 minimum_image_size=64 maximum_image_size=1024 feature_descriptor_size=16 closest/next_closest_ratio=0.92 maximal_alignment_error=25 inlier_ratio=0.05 expected_transformation=Rigid");
//Better to switch to manual pick here// run("Extract Block Matching Correspondences", "source_image=Drift target_image=Ref layer_scale=1 search_radius=50 block_radius=50 resolution=24 minimal_pmcc_r=0.10 maximal_curvature_ratio=1000 maximal_second_best_r/best_r=1 use_local_smoothness_filter approximate_local_transformation=Rigid local_region_sigma=65 maximal_local_displacement=12 maximal_local_displacement=3 export");
//Start the transformation cycle according to the feature extaction
roiManager("Reset");
selectWindow("Drift");
roiManager("Add");
selectWindow("Ref");
roiManager("Add");
for(j=driftstart[i-1]; j<=(driftstart[i]-1); j++){
selectWindow("Ref");
roiManager("Select", 1);
selectWindow("Red");
setSlice(j);
run("Copy");
newImage("Red_temp", "32-bit black", 266, 512, 1);
selectWindow("Red_temp");
run("Paste");
roiManager("Select", 0);
run("Landmark Correspondences", "source_image=Red_temp template_image=Ref transformation_method=[Least Squares] alpha=1 mesh_resolution=32 transformation_class=Rigid interpolate show_matrix");
selectWindow("Red_temp");
close();
selectWindow("TransformedRed_temp");
run("Copy");
close();
selectWindow("Red");
setSlice(j);
run("Paste");
selectWindow("Green");
setSlice(j);
run("Copy");
newImage("Green_temp", "32-bit black", 266, 512, 1);
selectWindow("Green_temp");
run("Paste");
roiManager("Select", 0);
run("Landmark Correspondences", "source_image=Green_temp template_image=Ref transformation_method=[Least Squares] alpha=1 mesh_resolution=32 transformation_class=Rigid interpolate show_matrix");
selectWindow("Green_temp");
close();
selectWindow("TransformedGreen_temp");
run("Copy");
close();
selectWindow("Green");
setSlice(j);
run("Paste");
}
//close temperary windows
selectWindow("Ref");
run("Close");
selectWindow("Drift");
run("Close");
}
//Open display updating.
setBatchMode(false);
//Make 96frame running average, fill 95frames to the end of RunAv
selectWindow("Green");
run("Running ZProjector", "running=96 projection=[Average Intensity]");
setSlice(nSlices);
run("Copy");
for(k=1; k<=95; k++){
run("Add Slice");
run("Paste");
}
selectWindow("Red");
run("Running ZProjector", "running=96 projection=[Average Intensity]");
setSlice(nSlices);
run("Copy");
for(k=1; k<=95; k++){
run("Add Slice");
run("Paste");
}
//Rotate
if(rotation=="R"){
selectWindow("Red-RunAv(96)");
run("Rotate 90 Degrees Right");
selectWindow("Red");
run("Rotate 90 Degrees Right");
selectWindow("Green-RunAv(96)");
run("Rotate 90 Degrees Right");
selectWindow("Green");
run("Rotate 90 Degrees Right");
}
if(rotation=="L"){
selectWindow("Red-RunAv(96)");
run("Rotate 90 Degrees Left");
selectWindow("Red");
run("Rotate 90 Degrees Left");
selectWindow("Green-RunAv(96)");
run("Rotate 90 Degrees Left");
selectWindow("Green");
run("Rotate 90 Degrees Left");
}
//Merge
run("Merge Channels...", "c1=Red-RunAv(96) c2=Red c3=Green-RunAv(96) c4=Green create");