Skip to content

Commit 5bac516

Browse files
CensusTransform: override for every census function
BOverrideCensusTransform.census receives each call after the output is reshaped. If it throws a RuntimeException, CensusTransform uses its own code.
1 parent f918d81 commit 5bac516

3 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2026, Peter Abeles. All Rights Reserved.
3+
*
4+
* This file is part of BoofCV (http://boofcv.org).
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package boofcv.alg.transform.census;
20+
21+
import boofcv.override.BOverrideClass;
22+
import boofcv.override.BOverrideManager;
23+
import boofcv.struct.border.ImageBorder;
24+
import boofcv.struct.image.GrayS32;
25+
import boofcv.struct.image.GrayS64;
26+
import boofcv.struct.image.GrayU8;
27+
import boofcv.struct.image.ImageGray;
28+
import boofcv.struct.image.InterleavedU16;
29+
import georegression.struct.point.Point2D_I32;
30+
import org.ddogleg.struct.FastAccess;
31+
import org.jetbrains.annotations.Nullable;
32+
33+
/// Override for [CensusTransform]. An override declines an input by throwing a RuntimeException, and
34+
/// [CensusTransform] then uses its own code
35+
public class BOverrideCensusTransform extends BOverrideClass {
36+
37+
static {
38+
BOverrideManager.register(BOverrideCensusTransform.class);
39+
}
40+
41+
public static @Nullable Census census;
42+
43+
/// Same arguments as the functions in [CensusTransform]. The output has already been reshaped
44+
public interface Census<T extends ImageGray<T>> {
45+
void dense3x3( T input, GrayU8 output, @Nullable ImageBorder<T> border );
46+
47+
void dense5x5( T input, GrayS32 output, @Nullable ImageBorder<T> border );
48+
49+
void sample_S64( T input, FastAccess<Point2D_I32> sample, GrayS64 output, @Nullable ImageBorder<T> border );
50+
51+
void sample_IU16( T input, FastAccess<Point2D_I32> sample, InterleavedU16 output,
52+
@Nullable ImageBorder<T> border );
53+
}
54+
55+
/// Returns true if the override processed the image
56+
public static <T extends ImageGray<T>>
57+
boolean invokeNativeDense3x3( T input, GrayU8 output, @Nullable ImageBorder<T> border ) {
58+
if (census == null)
59+
return false;
60+
try {
61+
census.dense3x3(input, output, border);
62+
return true;
63+
} catch (RuntimeException ignore) {
64+
return false;
65+
}
66+
}
67+
68+
/// Returns true if the override processed the image
69+
public static <T extends ImageGray<T>>
70+
boolean invokeNativeDense5x5( T input, GrayS32 output, @Nullable ImageBorder<T> border ) {
71+
if (census == null)
72+
return false;
73+
try {
74+
census.dense5x5(input, output, border);
75+
return true;
76+
} catch (RuntimeException ignore) {
77+
return false;
78+
}
79+
}
80+
81+
/// Returns true if the override processed the image
82+
public static <T extends ImageGray<T>>
83+
boolean invokeNativeSample_S64( T input, FastAccess<Point2D_I32> sample, GrayS64 output,
84+
@Nullable ImageBorder<T> border ) {
85+
if (census == null)
86+
return false;
87+
try {
88+
census.sample_S64(input, sample, output, border);
89+
return true;
90+
} catch (RuntimeException ignore) {
91+
return false;
92+
}
93+
}
94+
95+
/// Returns true if the override processed the image
96+
public static <T extends ImageGray<T>>
97+
boolean invokeNativeSample_IU16( T input, FastAccess<Point2D_I32> sample, InterleavedU16 output,
98+
@Nullable ImageBorder<T> border ) {
99+
if (census == null)
100+
return false;
101+
try {
102+
census.sample_IU16(input, sample, output, border);
103+
return true;
104+
} catch (RuntimeException ignore) {
105+
return false;
106+
}
107+
}
108+
}

‎main/boofcv-ip/src/main/java/boofcv/alg/transform/census/CensusTransform.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public static DogArray<Point2D_I32> createCircleSamples() {
100100
public static void dense3x3( final GrayU8 input, final GrayU8 output, @Nullable ImageBorder_S32<GrayU8> border ) {
101101
InputSanityCheck.checkReshape(input, output);
102102

103+
if (BOverrideCensusTransform.invokeNativeDense3x3(input, output, border))
104+
return;
105+
103106
if (BoofConcurrency.USE_CONCURRENT) {
104107
ImplCensusTransformInner_MT.dense3x3(input, output);
105108
} else {
@@ -120,6 +123,9 @@ public static void dense3x3( final GrayU8 input, final GrayU8 output, @Nullable
120123
public static void dense3x3( final GrayU16 input, final GrayU8 output, @Nullable ImageBorder_S32<GrayU16> border ) {
121124
InputSanityCheck.checkReshape(input, output);
122125

126+
if (BOverrideCensusTransform.invokeNativeDense3x3(input, output, border))
127+
return;
128+
123129
if (BoofConcurrency.USE_CONCURRENT) {
124130
ImplCensusTransformInner_MT.dense3x3(input, output);
125131
} else {
@@ -140,6 +146,9 @@ public static void dense3x3( final GrayU16 input, final GrayU8 output, @Nullable
140146
public static void dense3x3( final GrayF32 input, final GrayU8 output, @Nullable ImageBorder_F32 border ) {
141147
InputSanityCheck.checkReshape(input, output);
142148

149+
if (BOverrideCensusTransform.invokeNativeDense3x3(input, output, border))
150+
return;
151+
143152
if (BoofConcurrency.USE_CONCURRENT) {
144153
ImplCensusTransformInner_MT.dense3x3(input, output);
145154
} else {
@@ -160,6 +169,9 @@ public static void dense3x3( final GrayF32 input, final GrayU8 output, @Nullable
160169
public static void dense5x5( final GrayU8 input, final GrayS32 output, @Nullable ImageBorder_S32<GrayU8> border ) {
161170
InputSanityCheck.checkReshape(input, output);
162171

172+
if (BOverrideCensusTransform.invokeNativeDense5x5(input, output, border))
173+
return;
174+
163175
if (BoofConcurrency.USE_CONCURRENT) {
164176
ImplCensusTransformInner_MT.dense5x5(input, output);
165177
} else {
@@ -180,6 +192,9 @@ public static void dense5x5( final GrayU8 input, final GrayS32 output, @Nullable
180192
public static void dense5x5( final GrayU16 input, final GrayS32 output, @Nullable ImageBorder_S32<GrayU16> border ) {
181193
InputSanityCheck.checkReshape(input, output);
182194

195+
if (BOverrideCensusTransform.invokeNativeDense5x5(input, output, border))
196+
return;
197+
183198
if (BoofConcurrency.USE_CONCURRENT) {
184199
ImplCensusTransformInner_MT.dense5x5(input, output);
185200
} else {
@@ -200,6 +215,9 @@ public static void dense5x5( final GrayU16 input, final GrayS32 output, @Nullabl
200215
public static void dense5x5( final GrayF32 input, final GrayS32 output, @Nullable ImageBorder_F32 border ) {
201216
InputSanityCheck.checkReshape(input, output);
202217

218+
if (BOverrideCensusTransform.invokeNativeDense5x5(input, output, border))
219+
return;
220+
203221
if (BoofConcurrency.USE_CONCURRENT) {
204222
ImplCensusTransformInner_MT.dense5x5(input, output);
205223
} else {
@@ -223,6 +241,9 @@ public static void sample_S64( final GrayU8 input, final FastAccess<Point2D_I32>
223241
@Nullable DogArray_I32 workSpace ) {
224242
output.reshape(input.width, input.height);
225243

244+
if (BOverrideCensusTransform.invokeNativeSample_S64(input, sample, output, border))
245+
return;
246+
226247
// Precompute the offset in array indexes for the sample points
227248
if (workSpace == null)
228249
workSpace = new DogArray_I32();
@@ -252,6 +273,9 @@ public static void sample_S64( final GrayU16 input, final FastAccess<Point2D_I32
252273
@Nullable DogArray_I32 workSpace ) {
253274
output.reshape(input.width, input.height);
254275

276+
if (BOverrideCensusTransform.invokeNativeSample_S64(input, sample, output, border))
277+
return;
278+
255279
// Precompute the offset in array indexes for the sample points
256280
if (workSpace == null)
257281
workSpace = new DogArray_I32();
@@ -281,6 +305,9 @@ public static void sample_S64( final GrayF32 input, final FastAccess<Point2D_I32
281305
@Nullable DogArray_I32 workSpace ) {
282306
output.reshape(input.width, input.height);
283307

308+
if (BOverrideCensusTransform.invokeNativeSample_S64(input, sample, output, border))
309+
return;
310+
284311
// Precompute the offset in array indexes for the sample points
285312
if (workSpace == null)
286313
workSpace = new DogArray_I32();
@@ -312,6 +339,9 @@ public static void sample_IU16( final GrayU8 input, final FastAccess<Point2D_I32
312339
int numBlocks = BoofMiscOps.bitsToWords(sample.size, 16);
313340
output.reshape(input.width, input.height, numBlocks);
314341

342+
if (BOverrideCensusTransform.invokeNativeSample_IU16(input, sample, output, border))
343+
return;
344+
315345
// Precompute the offset in array indexes for the sample points
316346
if (workSpace == null)
317347
workSpace = new DogArray_I32();
@@ -343,6 +373,9 @@ public static void sample_IU16( final GrayU16 input, final FastAccess<Point2D_I3
343373
int numBlocks = BoofMiscOps.bitsToWords(sample.size, 16);
344374
output.reshape(input.width, input.height, numBlocks);
345375

376+
if (BOverrideCensusTransform.invokeNativeSample_IU16(input, sample, output, border))
377+
return;
378+
346379
// Precompute the offset in array indexes for the sample points
347380
if (workSpace == null)
348381
workSpace = new DogArray_I32();
@@ -374,6 +407,9 @@ public static void sample_IU16( final GrayF32 input, final FastAccess<Point2D_I3
374407
int numBlocks = BoofMiscOps.bitsToWords(sample.size, 16);
375408
output.reshape(input.width, input.height, numBlocks);
376409

410+
if (BOverrideCensusTransform.invokeNativeSample_IU16(input, sample, output, border))
411+
return;
412+
377413
// Precompute the offset in array indexes for the sample points
378414
if (workSpace == null)
379415
workSpace = new DogArray_I32();

‎main/boofcv-ip/src/test/java/boofcv/alg/transform/census/TestCensusTransform.java‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,101 @@
1818

1919
package boofcv.alg.transform.census;
2020

21+
import boofcv.BoofTesting;
2122
import boofcv.alg.misc.GImageMiscOps;
2223
import boofcv.core.image.GeneralizedImageOps;
2324
import boofcv.core.image.border.FactoryImageBorder;
25+
import boofcv.override.BOverrideManager;
2426
import boofcv.struct.border.BorderType;
27+
import boofcv.struct.border.ImageBorder;
2528
import boofcv.struct.image.GrayF32;
29+
import boofcv.struct.image.GrayS32;
2630
import boofcv.struct.image.GrayS64;
2731
import boofcv.struct.image.GrayU16;
2832
import boofcv.struct.image.GrayU8;
33+
import boofcv.struct.image.ImageBase;
2934
import boofcv.struct.image.ImageGray;
35+
import boofcv.struct.image.InterleavedU16;
3036
import boofcv.testing.BoofStandardJUnit;
3137
import georegression.struct.point.Point2D_I32;
3238
import org.ddogleg.struct.DogArray;
3339
import org.ejml.UtilEjml;
40+
import org.junit.jupiter.api.AfterEach;
3441
import org.junit.jupiter.api.Test;
3542

43+
import java.lang.reflect.Proxy;
44+
import java.util.ArrayList;
45+
import java.util.List;
46+
3647
import static org.junit.jupiter.api.Assertions.*;
3748

3849
/// Tests sampling functions and which bit each sample sets. The transforms are compared against a brute force
3950
/// implementation in GCensusTransform
4051
class TestCensusTransform extends BoofStandardJUnit {
52+
@AfterEach void clearOverrides() {
53+
BOverrideManager.clear();
54+
}
55+
56+
/// Every function gives the reshaped output to a registered override, and uses its own code if the override
57+
/// throws a RuntimeException
58+
@Test void override() {
59+
for (Class type : new Class[]{GrayU8.class, GrayU16.class, GrayF32.class}) {
60+
ImageGray input = GeneralizedImageOps.createSingleBand(type, 20, 15);
61+
GImageMiscOps.fillUniform(input, rand, 0, 100);
62+
List<ImageBase> expected = transformAll(input);
63+
64+
// The override fills the output with 3. The output is the second to last argument
65+
List<String> called = new ArrayList<>();
66+
BOverrideCensusTransform.census = override(( method, args ) -> {
67+
called.add(method);
68+
GImageMiscOps.fill((ImageBase)args[args.length - 2], 3);
69+
});
70+
List<ImageBase> found = transformAll(input);
71+
assertEquals(List.of("dense3x3", "dense5x5", "sample_S64", "sample_IU16"), called);
72+
for (int i = 0; i < found.size(); i++) {
73+
ImageBase threes = expected.get(i).createSameShape();
74+
GImageMiscOps.fill(threes, 3);
75+
BoofTesting.assertEquals(threes, found.get(i), 0);
76+
}
77+
78+
BOverrideCensusTransform.census = override(( method, args ) -> {
79+
throw new IllegalArgumentException("Unsupported");
80+
});
81+
found = transformAll(input);
82+
for (int i = 0; i < found.size(); i++) {
83+
BoofTesting.assertEquals(expected.get(i), found.get(i), 0);
84+
}
85+
}
86+
}
87+
88+
interface OverrideCall {
89+
void call( String method, Object[] args );
90+
}
91+
92+
private static BOverrideCensusTransform.Census override( OverrideCall call ) {
93+
return (BOverrideCensusTransform.Census)Proxy.newProxyInstance(
94+
BOverrideCensusTransform.Census.class.getClassLoader(),
95+
new Class<?>[]{BOverrideCensusTransform.Census.class}, ( proxy, method, args ) -> {
96+
call.call(method.getName(), args);
97+
return null;
98+
});
99+
}
100+
101+
/// Output of every census function. Outputs start as 1x1 images, so they must be reshaped
102+
private static List<ImageBase> transformAll( ImageGray input ) {
103+
ImageBorder border = FactoryImageBorder.wrap(BorderType.REFLECT, input);
104+
DogArray<Point2D_I32> samples = CensusTransform.createBlockSamples(3);
105+
var dense3x3 = new GrayU8(1, 1);
106+
var dense5x5 = new GrayS32(1, 1);
107+
var sampleS64 = new GrayS64(1, 1);
108+
var sampleIU16 = new InterleavedU16(1, 1, 1);
109+
GCensusTransform.dense3x3(input, dense3x3, border);
110+
GCensusTransform.dense5x5(input, dense5x5, border);
111+
GCensusTransform.sample_S64(input, samples, sampleS64, border, null);
112+
GCensusTransform.sample_IU16(input, samples, sampleIU16, border, null);
113+
return List.of(dense3x3, dense5x5, sampleS64, sampleIU16);
114+
}
115+
41116
@Test
42117
void createBlockSamples_1() {
43118
DogArray<Point2D_I32> samples = CensusTransform.createBlockSamples(2);

0 commit comments

Comments
 (0)