-
Notifications
You must be signed in to change notification settings - Fork 4
/
blockmatching.bsh
147 lines (115 loc) · 4.34 KB
/
blockmatching.bsh
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
import mpicbg.ij.blockmatching.*;
import mpicbg.models.*;
import mpicbg.trakem2.align.*;
import ij.gui.*;
scale = 1.0f;
blockRadius = searchRadius = 50;
minR = 0.1f;
rodR = 1.0f;
maxCurvatureR = 1000.0f;
useLocalSmoothnessFilter = true;
modelStrings = new String[]{ "Translation", "Rigid", "Similarity", "Affine" };
localModelIndex = 1;
localRegionSigma = 65.0f;
maxLocalEpsilon = 12.0f;
maxLocalTrust = 3.0f;
meshResolution = 24;
boolean setup()
{
gdBlockMatching = new GenericDialog( "Block Matching parameters" );
gdBlockMatching.addMessage( "Block Matching:" );
gdBlockMatching.addNumericField( "layer_scale :", scale, 2 );
gdBlockMatching.addNumericField( "search_radius :", searchRadius, 0, 6, "px" );
gdBlockMatching.addNumericField( "block_radius :", blockRadius, 0, 6, "px" );
gdBlockMatching.addNumericField( "resolution :", meshResolution, 0 );
gdBlockMatching.addMessage( "Correlation Filters:" );
gdBlockMatching.addNumericField( "minimal_PMCC_r :", minR, 2 );
gdBlockMatching.addNumericField( "maximal_curvature_ratio :", maxCurvatureR, 2 );
gdBlockMatching.addNumericField( "maximal_second_best_r/best_r :", rodR, 2 );
gdBlockMatching.addMessage( "Local Smoothness Filter:" );
gdBlockMatching.addCheckbox( "use_local_smoothness_filter", useLocalSmoothnessFilter );
gdBlockMatching.addChoice( "approximate_local_transformation :", modelStrings, modelStrings[ localModelIndex ] );
gdBlockMatching.addNumericField( "local_region_sigma:", localRegionSigma, 2, 6, "px" );
gdBlockMatching.addNumericField( "maximal_local_displacement (absolute):", maxLocalEpsilon, 2, 6, "px" );
gdBlockMatching.addNumericField( "maximal_local_displacement (relative):", maxLocalTrust, 2 );
gdBlockMatching.showDialog();
if ( gdBlockMatching.wasCanceled() )
return false;
scale = ( float )gdBlockMatching.getNextNumber();
searchRadius = ( int )gdBlockMatching.getNextNumber();
blockRadius = ( int )gdBlockMatching.getNextNumber();
meshResolution = ( int )gdBlockMatching.getNextNumber();
minR = ( float )gdBlockMatching.getNextNumber();
maxCurvatureR = ( float )gdBlockMatching.getNextNumber();
rodR = ( float )gdBlockMatching.getNextNumber();
useLocalSmoothnessFilter = gdBlockMatching.getNextBoolean();
localModelIndex = gdBlockMatching.getNextChoiceIndex();
localRegionSigma = ( float )gdBlockMatching.getNextNumber();
maxLocalEpsilon = ( float )gdBlockMatching.getNextNumber();
maxLocalTrust = ( float )gdBlockMatching.getNextNumber();
return true;
}
FloatProcessor createMask( ImageProcessor source )
{
mask = new FloatProcessor( source.getWidth(), source.getHeight() );
maskColor = 0x0000ff00;
n = source.getWidth() * source.getHeight();
maskPixels = mask.getPixels();
for ( int i = 0; i < n; ++i )
{
sourcePixel = source.get( i ) & 0x00ffffff;
if ( sourcePixel == maskColor )
maskPixels[ i ] = 0;
else
maskPixels[ i ] = 1;
}
return mask;
}
if ( setup() )
{
stack = IJ.getImage().getStack();
mesh = new SpringMesh( meshResolution, stack.getWidth(), stack.getHeight(), 1, 1000, 1 );
pm12 = new ArrayList();
v1 = mesh.getVertices();
ip1 = stack.getProcessor( 1 ).convertToFloat().duplicate();
ip2 = stack.getProcessor( 2 ).convertToFloat().duplicate();
ip1Mask = createMask( stack.getProcessor( 1 ) );
ip2Mask = createMask( stack.getProcessor( 2 ) );
ct = new TranslationModel2D();
BlockMatching.matchByMaximalPMCC(
ip1,
ip2,
ip1Mask,
ip2Mask,
scale,
ct,
blockRadius,
blockRadius,
searchRadius,
searchRadius,
minR,
rodR,
maxCurvatureR,
v1,
pm12,
new ErrorStatistic( 1 ) );
IJ.log( pm12.size() + " blockmatch candidates found." );
if ( useLocalSmoothnessFilter )
{
model = mpicbg.trakem2.align.Util.createModel( localModelIndex );
model.localSmoothnessFilter( pm12, pm12, localRegionSigma, maxLocalEpsilon, maxLocalTrust );
IJ.log( pm12.size() + " blockmatch candidates passed local smoothness filter." );
}
pm12Sources = new ArrayList();
pm12Targets = new ArrayList();
PointMatch.sourcePoints( pm12, pm12Sources );
PointMatch.targetPoints( pm12, pm12Targets );
roi1 = mpicbg.ij.util.Util.pointsToPointRoi( pm12Sources );
roi2 = mpicbg.ij.util.Util.pointsToPointRoi( pm12Targets );
imp1 = new ImagePlus( "1", stack.getProcessor( 1 ) );
imp2 = new ImagePlus( "2", stack.getProcessor( 2 ) );
imp1.setRoi( roi1 );
imp2.setRoi( roi2 );
}
imp1.show();
imp2.show();