diff --git a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/VideoDetectCorners.java b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/VideoDetectCorners.java index 48c18ef7f8..c1e089ce2c 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/VideoDetectCorners.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/VideoDetectCorners.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,6 +23,7 @@ import boofcv.abst.feature.detect.extract.NonMaxSuppression; import boofcv.abst.feature.detect.intensity.GeneralFeatureIntensity; import boofcv.abst.feature.detect.intensity.WrapperGradientCornerIntensity; +import boofcv.abst.filter.derivative.ImageGradient; import boofcv.alg.feature.detect.interest.GeneralFeatureDetector; import boofcv.alg.feature.detect.selector.FeatureSelectLimitIntensity; import boofcv.alg.feature.detect.selector.FeatureSelectNBest; @@ -32,6 +33,7 @@ import boofcv.core.image.GeneralizedImageOps; import boofcv.factory.feature.detect.extract.FactoryFeatureExtractor; import boofcv.factory.feature.detect.intensity.FactoryIntensityPointAlg; +import boofcv.factory.filter.derivative.FactoryDerivative; import boofcv.gui.image.ImagePanel; import boofcv.gui.image.ProcessImageSequence; import boofcv.gui.image.ShowImages; @@ -62,6 +64,7 @@ public class VideoDetectCorners, D extends ImageGray> D derivYY; D derivXY; + ImageGradient gradient; Class derivType; QueueCorner corners; @@ -69,12 +72,14 @@ public class VideoDetectCorners, D extends ImageGray> ImagePanel panel; public VideoDetectCorners( SimpleImageSequence sequence, - GeneralFeatureDetector detector, - Class derivType ) { + GeneralFeatureDetector detector, + ImageGradient gradient, + Class derivType ) { super(sequence); this.derivType = derivType; this.detector = detector; + this.gradient = gradient; } @Override @@ -86,8 +91,7 @@ public void processFrame( T image ) { derivY = GeneralizedImageOps.createSingleBand(derivType, image.width, image.height); } - // compute the image gradient - GImageDerivativeOps.gradient(DerivativeType.SOBEL, image, derivX, derivY, BoofDefaults.DERIV_BORDER_TYPE); + gradient.process(image, derivX, derivY); } if (detector.getRequiresHessian()) { @@ -135,12 +139,16 @@ void perform( String fileName, Class imageType, Class derivType ) { int maxCorners = 200; int radius = 2; + ImageGradient gradient = FactoryDerivative.sobel(imageType, derivType); + GeneralFeatureIntensity intensity = new WrapperGradientCornerIntensity<>(FactoryIntensityPointAlg.shiTomasi(radius, false, derivType)); // GeneralFeatureIntensity intensity = // new WrapperFastCornerIntensity(FactoryIntensityPointAlg.createFast12(defaultType, 8 , 12)); + float intensityScale = intensity.thresholdScaleByDerivative(gradient.divisor()); + ConfigExtract config = new ConfigExtract(); - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(config); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(config, intensityScale); // FeatureExtractor extractor = new WrapperNonMaximumBlock( new NonMaxExtractorNaive(radius+10,10f)); // FeatureExtractor extractor = new WrapperNonMaxCandidate(new NonMaxCandidateStrict(radius+10, 10f)); extractor.setIgnoreBorder(radius + 10); @@ -151,7 +159,7 @@ void perform( String fileName, Class imageType, Class derivType ) { var detector = new GeneralFeatureDetector<>(intensity, null, extractor, selector); detector.setFeatureLimit(maxCorners); - VideoDetectCorners display = new VideoDetectCorners<>(sequence, detector, derivType); + VideoDetectCorners display = new VideoDetectCorners<>(sequence, detector, gradient, derivType); display.process(); } diff --git a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/extract/CompareFeatureExtractorApp.java b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/extract/CompareFeatureExtractorApp.java index 40efeab3a8..f29992ef46 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/extract/CompareFeatureExtractorApp.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/extract/CompareFeatureExtractorApp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -172,12 +172,12 @@ private synchronized void doProcess() { if (intensityAlg.localMinimums()) { configExtract.detectMinimums = true; configExtract.detectMaximums = false; - extractorMin = FactoryFeatureExtractor.nonmax(configExtract); + extractorMin = FactoryFeatureExtractor.nonmax(configExtract, 1); } if (intensityAlg.localMaximums()) { configExtract.detectMinimums = false; configExtract.detectMaximums = true; - extractorMax = FactoryFeatureExtractor.nonmax(configExtract); + extractorMax = FactoryFeatureExtractor.nonmax(configExtract, 1); } GeneralFeatureDetector detector = new GeneralFeatureDetector<>(intensityAlg, diff --git a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/intensity/IntensityPointFeatureApp.java b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/intensity/IntensityPointFeatureApp.java index 71cc2288e8..4292145ee9 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/intensity/IntensityPointFeatureApp.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/intensity/IntensityPointFeatureApp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -266,18 +266,18 @@ private void handleAlgorithmChanged() { case "Harris": detector = FactoryDetectPoint.createHarris(config, - new ConfigHarrisCorner(controlPanel.weighted, controlPanel.radiusCorner), derivType); + new ConfigHarrisCorner(controlPanel.weighted, controlPanel.radiusCorner), derivType, deriv.getEdgeDivisor()); break; case "Shi Tomasi": detector = FactoryDetectPoint.createShiTomasi(config, - new ConfigShiTomasi(controlPanel.weighted, controlPanel.radiusCorner), derivType); + new ConfigShiTomasi(controlPanel.weighted, controlPanel.radiusCorner), derivType, deriv.getEdgeDivisor()); break; case "FAST": config.detectMinimums = true; detector = FactoryDetectPoint.createFast(config, null, imageType); break; case "KitRos": - detector = FactoryDetectPoint.createKitRos(config, derivType); + detector = FactoryDetectPoint.createKitRos(config, derivType, deriv.getEdgeDivisor()); break; case "Median": detector = FactoryDetectPoint.createMedian(config, imageType); diff --git a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DemoDetectPointFeaturesApp.java b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DemoDetectPointFeaturesApp.java index 114551b729..8aee2e91aa 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DemoDetectPointFeaturesApp.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DemoDetectPointFeaturesApp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,12 +19,15 @@ package boofcv.demonstrations.feature.detect.interest; import boofcv.abst.feature.detect.interest.*; +import boofcv.abst.filter.derivative.ImageGradient; import boofcv.alg.feature.detect.intensity.HessianBlobIntensity; import boofcv.alg.feature.detect.interest.GeneralFeatureDetector; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.filter.derivative.GImageDerivativeOps; import boofcv.demonstrations.shapes.ShapeVisualizePanel; import boofcv.factory.feature.detect.interest.FactoryDetectPoint; import boofcv.factory.feature.detect.interest.FactoryInterestPoint; +import boofcv.factory.filter.derivative.FactoryDerivative; import boofcv.gui.BoofSwingUtil; import boofcv.gui.DemonstrationBase; import boofcv.gui.StandardAlgConfigPanel; @@ -67,6 +70,8 @@ public class DemoDetectPointFeaturesApp> extends Demonstr VisualizePanel imagePanel = new VisualizePanel(); ControlPanel controls = new ControlPanel(); + ImageGradient gradient; + InterestPointDetector detector; boolean detectorChanged = true; @@ -83,6 +88,8 @@ public DemoDetectPointFeaturesApp( List exampleInputs, Class imageClass ) this.imageClass = imageClass; this.derivClass = GImageDerivativeOps.getDerivativeType(imageClass); + gradient = FactoryDerivative.gradientSB(DerivativeType.SOBEL, imageClass, derivClass); + imagePanel.setPreferredSize(new Dimension(800, 800)); add(BorderLayout.WEST, controls); @@ -297,14 +304,14 @@ private void createHarris() { controls.configExtract.detectMinimums = false; controls.adjustControls(true, false); changeDetector(FactoryDetectPoint.createHarris(controls.configExtract, - new ConfigHarrisCorner(controls.weighted, controls.configExtract.radius), derivClass)); + new ConfigHarrisCorner(controls.weighted, controls.configExtract.radius), derivClass, gradient.divisor())); } private void createShiTomasi() { controls.configExtract.detectMinimums = false; controls.adjustControls(true, false); changeDetector(FactoryDetectPoint.createShiTomasi(controls.configExtract, - new ConfigShiTomasi(controls.weighted, controls.configExtract.radius), derivClass)); + new ConfigShiTomasi(controls.weighted, controls.configExtract.radius), derivClass, gradient.divisor())); } private void createFastIntensity() { @@ -326,7 +333,7 @@ private void createFast() { private void createKitRos() { controls.configExtract.detectMinimums = false; controls.adjustControls(false, false); - changeDetector(FactoryDetectPoint.createKitRos(controls.configExtract, derivClass)); + changeDetector(FactoryDetectPoint.createKitRos(controls.configExtract, derivClass, 1)); } private void createMedian() { @@ -350,7 +357,7 @@ private void createLaplace() { } private void changeDetector( GeneralFeatureDetector fd ) { - detector = new GeneralToInterestPoint(fd, -1, imageClass, derivClass); + detector = new GeneralToInterestPoint(fd, gradient, -1, imageClass, derivClass); } public static void main( String[] args ) { diff --git a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DetectPointsWithNoiseApp.java b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DetectPointsWithNoiseApp.java index c2f98f8dab..1176bfcce7 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DetectPointsWithNoiseApp.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/feature/detect/interest/DetectPointsWithNoiseApp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,11 +22,14 @@ import boofcv.abst.feature.detect.interest.ConfigGeneralDetector; import boofcv.abst.feature.detect.interest.ConfigHarrisCorner; import boofcv.abst.feature.detect.interest.ConfigShiTomasi; +import boofcv.abst.filter.derivative.ImageGradient; import boofcv.alg.feature.detect.intensity.HessianBlobIntensity; import boofcv.alg.feature.detect.interest.EasyGeneralFeatureDetector; import boofcv.alg.feature.detect.interest.GeneralFeatureDetector; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.demonstrations.feature.detect.ImageCorruptPanel; import boofcv.factory.feature.detect.interest.FactoryDetectPoint; +import boofcv.factory.filter.derivative.FactoryDerivative; import boofcv.gui.SelectAlgorithmAndInputPanel; import boofcv.gui.feature.FancyInterestPointRender; import boofcv.gui.image.ImagePanel; @@ -81,30 +84,31 @@ public DetectPointsWithNoiseApp( Class imageType, Class derivType ) { GeneralFeatureDetector alg; ConfigGeneralDetector configExtract = new ConfigGeneralDetector(maxFeatures, radius, thresh); + ImageGradient gradient = FactoryDerivative.gradientSB(DerivativeType.SOBEL, imageType, derivType); - alg = FactoryDetectPoint.createHarris(configExtract, null, derivType); - addAlgorithm(0, "Harris", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); - alg = FactoryDetectPoint.createHarris(configExtract, new ConfigHarrisCorner(true, radius), derivType); - addAlgorithm(0, "Harris Weighted", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); - alg = FactoryDetectPoint.createShiTomasi(configExtract, null, derivType); - addAlgorithm(0, "Shi-Tomasi", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); - alg = FactoryDetectPoint.createShiTomasi(configExtract, new ConfigShiTomasi(true, radius), derivType); - addAlgorithm(0, "Shi-Tomasi Weighted", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); + alg = FactoryDetectPoint.createHarris(configExtract, null, derivType, gradient.divisor()); + addAlgorithm(0, "Harris", new EasyGeneralFeatureDetector<>(alg, gradient, imageType, derivType)); + alg = FactoryDetectPoint.createHarris(configExtract, new ConfigHarrisCorner(true, radius), derivType, gradient.divisor()); + addAlgorithm(0, "Harris Weighted", new EasyGeneralFeatureDetector<>(alg, gradient, imageType, derivType)); + alg = FactoryDetectPoint.createShiTomasi(configExtract, null, derivType, gradient.divisor()); + addAlgorithm(0, "Shi-Tomasi", new EasyGeneralFeatureDetector<>(alg, gradient, imageType, derivType)); + alg = FactoryDetectPoint.createShiTomasi(configExtract, new ConfigShiTomasi(true, radius), derivType, gradient.divisor()); + addAlgorithm(0, "Shi-Tomasi Weighted", new EasyGeneralFeatureDetector<>(alg, gradient, imageType, derivType)); configExtract.detectMinimums = true; configExtract.threshold = 10; alg = FactoryDetectPoint.createFast(configExtract, new ConfigFastCorner(10, 9), imageType); configExtract.detectMinimums = false; configExtract.threshold = thresh; - addAlgorithm(0, "Fast", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); - alg = FactoryDetectPoint.createKitRos(configExtract, derivType); - addAlgorithm(0, "KitRos", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); + addAlgorithm(0, "Fast", new EasyGeneralFeatureDetector<>(alg, gradient, imageType, derivType)); + alg = FactoryDetectPoint.createKitRos(configExtract, derivType, gradient.divisor()); + addAlgorithm(0, "KitRos", new EasyGeneralFeatureDetector<>(alg, null, imageType, derivType)); alg = FactoryDetectPoint.createMedian(configExtract, imageType); - addAlgorithm(0, "Median", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); + addAlgorithm(0, "Median", new EasyGeneralFeatureDetector<>(alg, null, imageType, derivType)); alg = FactoryDetectPoint.createHessianDeriv(configExtract, HessianBlobIntensity.Type.DETERMINANT, derivType); - addAlgorithm(0, "Hessian", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); + addAlgorithm(0, "Hessian", new EasyGeneralFeatureDetector<>(alg, null, imageType, derivType)); configExtract.detectMinimums = true; alg = FactoryDetectPoint.createHessianDeriv(configExtract, HessianBlobIntensity.Type.TRACE, derivType); - addAlgorithm(0, "Laplace", new EasyGeneralFeatureDetector<>(alg, imageType, derivType)); + addAlgorithm(0, "Laplace", new EasyGeneralFeatureDetector<>(alg, null, imageType, derivType)); JPanel viewArea = new JPanel(new BorderLayout()); corruptPanel = new ImageCorruptPanel(); diff --git a/demonstrations/src/main/java/boofcv/demonstrations/feature/orientation/ShowFeatureOrientationApp.java b/demonstrations/src/main/java/boofcv/demonstrations/feature/orientation/ShowFeatureOrientationApp.java index 6a7059369e..c9ed939f65 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/feature/orientation/ShowFeatureOrientationApp.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/feature/orientation/ShowFeatureOrientationApp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -126,7 +126,7 @@ public synchronized void setActiveAlgorithm( int indexFamily, String name, Objec int r = 2; GeneralFeatureDetector detector = FactoryDetectPoint. - createHarris(new ConfigGeneralDetector(NUM_FEATURES, r, 1), null, derivType); + createHarris(new ConfigGeneralDetector(NUM_FEATURES, r, 1), null, derivType, 1); D derivX = null, derivY = null, derivXX = null, derivYY = null, derivXY = null; diff --git a/demonstrations/src/main/java/boofcv/demonstrations/sfm/d3/VisualizeMonocularPlaneVisualOdometryApp.java b/demonstrations/src/main/java/boofcv/demonstrations/sfm/d3/VisualizeMonocularPlaneVisualOdometryApp.java index 4118bbdfd9..654718a1a0 100644 --- a/demonstrations/src/main/java/boofcv/demonstrations/sfm/d3/VisualizeMonocularPlaneVisualOdometryApp.java +++ b/demonstrations/src/main/java/boofcv/demonstrations/sfm/d3/VisualizeMonocularPlaneVisualOdometryApp.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,6 +23,7 @@ import boofcv.abst.sfm.AccessPointTracks3D; import boofcv.abst.sfm.d3.MonocularPlaneVisualOdometry; import boofcv.abst.tracker.PointTracker; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.filter.derivative.GImageDerivativeOps; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.factory.sfm.ConfigPlanarTrackPnP; @@ -314,16 +315,16 @@ private MonocularPlaneVisualOdometry createVisualOdometry( int whichAlg ) { return FactoryVisualOdometry.monoPlaneInfinity(config, imageClass); } else if (whichAlg == 1) { - ConfigPKlt configKlt = new ConfigPKlt(); + var configKlt = new ConfigPKlt(); configKlt.pyramidLevels = ConfigDiscreteLevels.levels(4); configKlt.templateRadius = 3; - ConfigPointDetector configDetector = new ConfigPointDetector(); + var configDetector = new ConfigPointDetector(); configDetector.type = PointDetectorTypes.SHI_TOMASI; configDetector.general.maxFeatures = 600; configDetector.general.radius = 3; configDetector.general.threshold = 1; - PointTracker tracker = FactoryPointTracker.klt(configKlt, configDetector, imageClass, derivType); + PointTracker tracker = FactoryPointTracker.klt(configKlt, DerivativeType.SOBEL, configDetector, imageClass, derivType); double cellSize = 0.06; double inlierGroundTol = 1.5; diff --git a/examples/src/main/java/boofcv/examples/features/ExampleCornerFeature.java b/examples/src/main/java/boofcv/examples/features/ExampleCornerFeature.java index 49b24ed01a..0ff3652a4b 100644 --- a/examples/src/main/java/boofcv/examples/features/ExampleCornerFeature.java +++ b/examples/src/main/java/boofcv/examples/features/ExampleCornerFeature.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -54,10 +54,12 @@ public static void main( String[] args ) { configCorner.radius = configNonMax.radius; // in general you should use the same radius here configCorner.weighted = true; // weighted corners will appear at the corners on a chessboard - // set weighted to false and see what happens to the feature's locations. unweighted is much faster - GeneralFeatureDetector detector = FactoryDetectPoint.createShiTomasi(configNonMax, configCorner, GrayS16.class); ImageGradient sobel = FactoryDerivative.sobel(GrayU8.class, GrayS16.class); + // set weighted to false and see what happens to the feature's locations. unweighted is much faster + GeneralFeatureDetector detector = FactoryDetectPoint.createShiTomasi( + configNonMax, configCorner, GrayS16.class, sobel.divisor()); + BufferedImage image = UtilImageIO.loadImageNotNull(UtilIO.pathExample("calibration/mono/Sony_DSC-HX5V_Chess/frame05.jpg")); // Convert the image into a usable format and predeclare memory diff --git a/examples/src/main/java/boofcv/examples/features/ExampleDetectDescribe.java b/examples/src/main/java/boofcv/examples/features/ExampleDetectDescribe.java index 2c4c09e395..14950cc949 100644 --- a/examples/src/main/java/boofcv/examples/features/ExampleDetectDescribe.java +++ b/examples/src/main/java/boofcv/examples/features/ExampleDetectDescribe.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -78,7 +78,7 @@ DetectDescribePoint createFromPremade( Class imageType ) { DetectDescribePoint createFromComponents( Class imageType ) { // create a corner detector Class derivType = GImageDerivativeOps.getDerivativeType(imageType); - GeneralFeatureDetector corner = FactoryDetectPoint.createShiTomasi(new ConfigGeneralDetector(1000, 5, 1), null, derivType); + GeneralFeatureDetector corner = FactoryDetectPoint.createShiTomasi(new ConfigGeneralDetector(1000, 5, 1), null, derivType, 1); InterestPointDetector detector = FactoryInterestPoint.wrapPoint(corner, 1, imageType, derivType); // describe points using BRIEF diff --git a/examples/src/main/java/boofcv/examples/features/ExampleFeatureLimit.java b/examples/src/main/java/boofcv/examples/features/ExampleFeatureLimit.java index cf76f1a7d3..1be1b23899 100644 --- a/examples/src/main/java/boofcv/examples/features/ExampleFeatureLimit.java +++ b/examples/src/main/java/boofcv/examples/features/ExampleFeatureLimit.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -61,7 +61,7 @@ public class ExampleFeatureLimit { public static BufferedImage renderLimit( GrayF32 intensity, SelectLimitTypes type ) { // Configure how it will select features inside the intensity image var limit = new ConfigSelectLimit(type, 0xBEEF); - NonMaxLimiter nonmax = FactoryFeatureExtractor.nonmaxLimiter(new ConfigExtract(NON_MAX_RADIUS, 0), limit, MAX_FEATURES); + NonMaxLimiter nonmax = FactoryFeatureExtractor.nonmaxLimiter(new ConfigExtract(NON_MAX_RADIUS, 0), 1f,limit, MAX_FEATURES); // Detect the features nonmax.process(intensity); diff --git a/examples/src/main/java/boofcv/examples/features/ExampleNonMaximumSupression.java b/examples/src/main/java/boofcv/examples/features/ExampleNonMaximumSupression.java index e6a8754301..07755921cd 100644 --- a/examples/src/main/java/boofcv/examples/features/ExampleNonMaximumSupression.java +++ b/examples/src/main/java/boofcv/examples/features/ExampleNonMaximumSupression.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -52,7 +52,7 @@ public class ExampleNonMaximumSupression { public static BufferedImage renderNonMax( GrayF32 intensity, int radius, float threshold ) { // Create and configure the feature detector - NonMaxSuppression nonmax = FactoryFeatureExtractor.nonmax(new ConfigExtract(radius, threshold)); + NonMaxSuppression nonmax = FactoryFeatureExtractor.nonmax(new ConfigExtract(radius, threshold), 1); // We will only search for the maximums. Other variants will look for minimums or will exclude previous // candidate detections from being detected twice diff --git a/examples/src/main/java/boofcv/examples/tracking/ExamplePointFeatureTracker.java b/examples/src/main/java/boofcv/examples/tracking/ExamplePointFeatureTracker.java index aedd1ffdb0..0bb2b3d3a8 100644 --- a/examples/src/main/java/boofcv/examples/tracking/ExamplePointFeatureTracker.java +++ b/examples/src/main/java/boofcv/examples/tracking/ExamplePointFeatureTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,6 +23,7 @@ import boofcv.abst.feature.detect.interest.PointDetectorTypes; import boofcv.abst.tracker.PointTrack; import boofcv.abst.tracker.PointTracker; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.filter.derivative.GImageDerivativeOps; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.factory.tracker.FactoryPointTracker; @@ -140,7 +141,7 @@ public void createKLT() { configDetector.general.radius = 6; configDetector.general.threshold = 1; - tracker = FactoryPointTracker.klt(configKlt, configDetector, imageType, derivType); + tracker = FactoryPointTracker.klt(configKlt, DerivativeType.SOBEL, configDetector, imageType, derivType); } /** diff --git a/integration/boofcv-WebcamCapture/examples/boofcv/examples/ExampleTrackingKlt.java b/integration/boofcv-WebcamCapture/examples/boofcv/examples/ExampleTrackingKlt.java index 042836dd7a..5412adfbc9 100644 --- a/integration/boofcv-WebcamCapture/examples/boofcv/examples/ExampleTrackingKlt.java +++ b/integration/boofcv-WebcamCapture/examples/boofcv/examples/ExampleTrackingKlt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,6 +22,7 @@ import boofcv.abst.feature.detect.interest.PointDetectorTypes; import boofcv.abst.tracker.PointTrack; import boofcv.abst.tracker.PointTracker; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.factory.tracker.FactoryPointTracker; import boofcv.gui.feature.VisualizeFeatures; @@ -43,38 +44,37 @@ */ public class ExampleTrackingKlt { - public static void main(String[] args) { - + public static void main( String[] args ) { // tune the tracker for the image size and visual appearance - ConfigPointDetector configDetector = new ConfigPointDetector(); + var configDetector = new ConfigPointDetector(); configDetector.type = PointDetectorTypes.SHI_TOMASI; configDetector.general.radius = 8; configDetector.general.threshold = 1; - ConfigPKlt configKlt = new ConfigPKlt(3); + var configKlt = new ConfigPKlt(3); - PointTracker tracker = FactoryPointTracker.klt(configKlt,configDetector,GrayF32.class,null); + PointTracker tracker = FactoryPointTracker.klt(configKlt, DerivativeType.SOBEL, configDetector, GrayF32.class, null); // Open a webcam at a resolution close to 640x480 - Webcam webcam = UtilWebcamCapture.openDefault(640,480); + Webcam webcam = UtilWebcamCapture.openDefault(640, 480); // Create the panel used to display the image and feature tracks ImagePanel gui = new ImagePanel(); gui.setPreferredSize(webcam.getViewSize()); - ShowImages.showWindow(gui,"KLT Tracker",true); + ShowImages.showWindow(gui, "KLT Tracker", true); int minimumTracks = 100; - while( true ) { + while (true) { BufferedImage image = webcam.getImage(); - GrayF32 gray = ConvertBufferedImage.convertFrom(image,(GrayF32)null); + GrayF32 gray = ConvertBufferedImage.convertFrom(image, (GrayF32)null); tracker.process(gray); List tracks = tracker.getActiveTracks(null); // Spawn tracks if there are too few - if( tracks.size() < minimumTracks ) { + if (tracks.size() < minimumTracks) { tracker.spawnTracks(); tracks = tracker.getActiveTracks(null); minimumTracks = tracks.size()/2; @@ -83,8 +83,8 @@ public static void main(String[] args) { // Draw the tracks Graphics2D g2 = image.createGraphics(); - for( PointTrack t : tracks ) { - VisualizeFeatures.drawPoint(g2,(int)t.pixel.x,(int)t.pixel.y,Color.RED); + for (PointTrack t : tracks) { + VisualizeFeatures.drawPoint(g2, (int)t.pixel.x, (int)t.pixel.y, Color.RED); } gui.setImageUI(image); diff --git a/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelDetDescAssocBase.java b/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelDetDescAssocBase.java index 9be00ad6e3..a6f2c59d98 100644 --- a/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelDetDescAssocBase.java +++ b/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelDetDescAssocBase.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -175,7 +175,7 @@ InterestPointDetector createDetector( Class imageType ) { c.scaleSpaceSift = configDetDesc.scaleSpaceSift; c.sift = configDetDesc.detectSift; - return FactoryInterestPoint.generic(c, imageType, null); + return FactoryInterestPoint.generic(c, imageType, null, 1); } public , TD extends TupleDesc> diff --git a/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelPointTrackerKlt.java b/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelPointTrackerKlt.java index 82f3d37c1e..ecd556aaa1 100644 --- a/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelPointTrackerKlt.java +++ b/integration/boofcv-swing/src/main/java/boofcv/gui/controls/ControlPanelPointTrackerKlt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -20,6 +20,7 @@ import boofcv.abst.feature.detect.interest.ConfigPointDetector; import boofcv.abst.tracker.PointTracker; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.factory.tracker.FactoryPointTracker; import boofcv.gui.StandardAlgConfigPanel; @@ -101,7 +102,7 @@ protected void initializeConfiguration() { public > PointTracker createTracker(ImageType imageType ) { - return FactoryPointTracker.klt(configKlt, configDetect, imageType.getImageClass(), null); + return FactoryPointTracker.klt(configKlt, DerivativeType.SOBEL, configDetect, imageType.getImageClass(), null); } @Override diff --git a/integration/boofcv-swing/src/main/java/boofcv/gui/feature/ControlPanelPointDetector.java b/integration/boofcv-swing/src/main/java/boofcv/gui/feature/ControlPanelPointDetector.java index 25ac20baa9..0b58ab99e5 100644 --- a/integration/boofcv-swing/src/main/java/boofcv/gui/feature/ControlPanelPointDetector.java +++ b/integration/boofcv-swing/src/main/java/boofcv/gui/feature/ControlPanelPointDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -73,7 +73,7 @@ public ControlPanelPointDetector( ConfigPointDetector config, Listener listener public , D extends ImageGray> GeneralFeatureDetector create( Class imageType ) { - return FactoryDetectPoint.create(config, imageType, null); + return FactoryDetectPoint.create(config, imageType, null, 1); } void handleTypeChange() { diff --git a/main/boofcv-feature/src/benchmark/java/boofcv/alg/feature/detect/extract/BenchmarkExtractors.java b/main/boofcv-feature/src/benchmark/java/boofcv/alg/feature/detect/extract/BenchmarkExtractors.java index 006db4d2d4..a1467c0bfe 100644 --- a/main/boofcv-feature/src/benchmark/java/boofcv/alg/feature/detect/extract/BenchmarkExtractors.java +++ b/main/boofcv-feature/src/benchmark/java/boofcv/alg/feature/detect/extract/BenchmarkExtractors.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -73,7 +73,7 @@ public class BenchmarkExtractors { config.detectMinimums = false; config.threshold = threshold; config.useStrictRule = true; - blockStrictMax = FactoryFeatureExtractor.nonmax(config); + blockStrictMax = FactoryFeatureExtractor.nonmax(config, 1); } @Benchmark public void blockStrictMax() { @@ -87,7 +87,7 @@ public class BenchmarkExtractors { config.detectMinimums = true; config.threshold = threshold; config.useStrictRule = true; - NonMaxSuppression alg = FactoryFeatureExtractor.nonmax(config); + NonMaxSuppression alg = FactoryFeatureExtractor.nonmax(config, 1); alg.process(intensity, null, null, corners, corners); } @@ -98,7 +98,7 @@ public class BenchmarkExtractors { config.detectMinimums = false; config.threshold = threshold; config.useStrictRule = false; - NonMaxSuppression alg = FactoryFeatureExtractor.nonmax(config); + NonMaxSuppression alg = FactoryFeatureExtractor.nonmax(config, 1); alg.process(intensity, null, null, corners, corners); } @@ -109,7 +109,7 @@ public class BenchmarkExtractors { config.detectMinimums = true; config.threshold = threshold; config.useStrictRule = false; - NonMaxSuppression alg = FactoryFeatureExtractor.nonmax(config); + NonMaxSuppression alg = FactoryFeatureExtractor.nonmax(config, 1); alg.process(intensity, null, null, corners, corners); } diff --git a/main/boofcv-feature/src/benchmark/java/boofcv/alg/tracker/klt/BenchmarkKltTracker.java b/main/boofcv-feature/src/benchmark/java/boofcv/alg/tracker/klt/BenchmarkKltTracker.java new file mode 100644 index 0000000000..cfba1f173e --- /dev/null +++ b/main/boofcv-feature/src/benchmark/java/boofcv/alg/tracker/klt/BenchmarkKltTracker.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.alg.tracker.klt; + +import boofcv.alg.misc.GImageMiscOps; +import boofcv.factory.tracker.FactoryTrackerAlg; +import boofcv.struct.image.GrayF32; +import boofcv.struct.image.GrayU8; +import boofcv.struct.image.ImageGray; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.openjdk.jmh.runner.options.TimeValue; + +import java.util.Random; +import java.util.concurrent.TimeUnit; + +@SuppressWarnings({"unchecked", "rawtypes"}) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 2) +@Measurement(iterations = 5) +@State(Scope.Benchmark) +@Fork(value = 1) +public class BenchmarkKltTracker { + + int width = 300, height = 200; + + @Param({"GrayF32", "GrayU8"}) + String imageType; + + @Param({"1", "30"}) + int radius; + + Random rand = new Random(234234); + + ImageGray image; + ImageGray derivX, derivY; + Class type; + KltTracker klt; + + KltFeature feature; + + @Setup public void setup() { + if (imageType.equals("GrayF32")) { + type = GrayF32.class; + } else { + type = GrayU8.class; + } + + var config = new ConfigKlt(); + config.maxIterations = 30; + config.minPositionDelta = 1e-4f; + klt = FactoryTrackerAlg.klt(config, (Class)type, null); + + image = (ImageGray)klt.interpInput.getImageType().createImage(width, height); + derivX = (ImageGray)klt.interpDeriv.getImageType().createImage(width, height); + derivY = (ImageGray)klt.interpDeriv.getImageType().createImage(width, height); + + GImageMiscOps.addUniform(image, rand, 0, 200); + GImageMiscOps.addUniform(derivX, rand, 0, 200); + GImageMiscOps.addUniform(derivY, rand, 0, 200); + + klt.setImage(image, derivX, derivY, 1); + feature = new KltFeature(radius); + } + + @Benchmark public void setDescription() { + feature.x = width/2; + feature.y = height/2; + klt.setDescription(feature); + } + + @Benchmark public void track() { + feature.x = width/2; + feature.y = height/2; + klt.track(feature); + } + + static void main( String[] args ) throws RunnerException { + Options opt = new OptionsBuilder() + .include(BenchmarkKltTracker.class.getSimpleName()) + .warmupTime(TimeValue.seconds(1)) + .measurementTime(TimeValue.seconds(1)) + .build(); + + new Runner(opt).run(); + } +} diff --git a/main/boofcv-feature/src/benchmark/java/boofcv/alg/tracker/klt/BenchmarkPyramidKltTracker.java b/main/boofcv-feature/src/benchmark/java/boofcv/alg/tracker/klt/BenchmarkPyramidKltTracker.java new file mode 100644 index 0000000000..b5d4f9223a --- /dev/null +++ b/main/boofcv-feature/src/benchmark/java/boofcv/alg/tracker/klt/BenchmarkPyramidKltTracker.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.alg.tracker.klt; + +import boofcv.alg.misc.GImageMiscOps; +import boofcv.alg.transform.pyramid.PyramidDiscreteAverage; +import boofcv.factory.tracker.FactoryTrackerAlg; +import boofcv.struct.image.GrayF32; +import boofcv.struct.image.GrayU8; +import boofcv.struct.image.ImageGray; +import boofcv.struct.image.ImageType; +import boofcv.struct.pyramid.ConfigDiscreteLevels; +import boofcv.struct.pyramid.ImagePyramid; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.openjdk.jmh.runner.options.TimeValue; + +import java.util.Random; +import java.util.concurrent.TimeUnit; + +@SuppressWarnings({"unchecked", "rawtypes"}) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 2) +@Measurement(iterations = 5) +@State(Scope.Benchmark) +@Fork(value = 1) +public class BenchmarkPyramidKltTracker { + + int width = 300, height = 200; + int numLevels = 3; + + @Param({"GrayF32", "GrayU8"}) + String imageType; + + @Param({"1", "30"}) + int radius; + + Random rand = new Random(234234); + + ImagePyramid image; + ImageGray[] derivX, derivY; + Class type; + PyramidKltTracker klt; + + PyramidKltFeature feature; + + @Setup public void setup() { + if (imageType.equals("GrayF32")) { + type = GrayF32.class; + } else { + type = GrayU8.class; + } + + var config = new ConfigKlt(); + config.maxIterations = 30; + config.minPositionDelta = 1e-4f; + klt = FactoryTrackerAlg.kltPyramid(config, type, null); + + var configLevels = new ConfigDiscreteLevels(); + configLevels.numLevelsRequested = numLevels; + + image = new PyramidDiscreteAverage(ImageType.single(type), false, configLevels); + image.initialize(width, height); + + ImageType derivType = klt.tracker.interpDeriv.getImageType(); + + derivX = new ImageGray[numLevels]; + derivY = new ImageGray[numLevels]; + for (int i = 0; i < image.getNumLayers(); i++) { + ImageGray level = (ImageGray)image.getLayer(i); + GImageMiscOps.addUniform(level, rand, 0, 200); + derivX[i] = (ImageGray)derivType.createImage(level.width, level.height); + derivY[i] = (ImageGray)derivType.createImage(level.width, level.height); + GImageMiscOps.addUniform(derivX[i], rand, 0, 200); + GImageMiscOps.addUniform(derivY[i], rand, 0, 200); + } + + klt.setImage(image, derivX, derivY, 1); + feature = new PyramidKltFeature(numLevels, radius); + } + + @Benchmark public void setDescription() { + feature.x = width/2; + feature.y = height/2; + klt.setDescription(feature); + } + + @Benchmark public void track() { + feature.x = width/2; + feature.y = height/2; + klt.track(feature); + } + + static void main( String[] args ) throws RunnerException { + Options opt = new OptionsBuilder() + .include(BenchmarkPyramidKltTracker.class.getSimpleName()) + .warmupTime(TimeValue.seconds(1)) + .measurementTime(TimeValue.seconds(1)) + .build(); + + new Runner(opt).run(); + } +} diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/GeneralFeatureIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/GeneralFeatureIntensity.java index f737d92792..83d752d46f 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/GeneralFeatureIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/GeneralFeatureIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -57,6 +57,10 @@ void process( I image, */ GrayF32 getIntensity(); + /// If the gradient has an integer kernel you need to scale threshold to account for the gradient being scaled + /// or else it will vary by image and kernel type + float thresholdScaleByDerivative( int divisor ); + /** * (Optional) Returns a list of candidate for local minimums. * diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperFastCornerIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperFastCornerIntensity.java index 22b35c1a06..d5a0a0ee20 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperFastCornerIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperFastCornerIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -46,6 +46,7 @@ public void process( I input, } // @formatter:off + @Override public float thresholdScaleByDerivative( int divisor ) { return 1; } @Override public ListIntPoint2D getCandidatesMin() { return alg.getCandidatesLow(); } @Override public ListIntPoint2D getCandidatesMax() { return alg.getCandidatesHigh(); } @Override public boolean getRequiresGradient() { return false; } diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperGradientCornerIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperGradientCornerIntensity.java index 20cb5cd6a1..7b732e4cde 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperGradientCornerIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperGradientCornerIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -40,11 +40,15 @@ public WrapperGradientCornerIntensity( GradientCornerIntensity alg ) { } @Override public void process( I image, @Nullable D derivX, @Nullable D derivY, - @Nullable D derivXX, @Nullable D derivYY, @Nullable D derivXY ) { + @Nullable D derivXX, @Nullable D derivYY, @Nullable D derivXY ) { init(image.width, image.height); alg.process(Objects.requireNonNull(derivX), Objects.requireNonNull(derivY), intensity); } + @Override public float thresholdScaleByDerivative( int divisor ) { + return alg.thresholdScaleByDerivative(divisor); + } + @Override public @Nullable ListIntPoint2D getCandidatesMin() { return null; } diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianDerivBlobIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianDerivBlobIntensity.java index 00cce5f003..5a8819b47e 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianDerivBlobIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianDerivBlobIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,6 +22,7 @@ import boofcv.struct.ListIntPoint2D; import boofcv.struct.image.GrayF32; import boofcv.struct.image.ImageGray; +import boofcv.struct.image.ImageType; import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationTargetException; @@ -39,16 +40,20 @@ public class WrapperHessianDerivBlobIntensity, D extends Method m; boolean minimum; + DerivativeScale opScale; + public WrapperHessianDerivBlobIntensity( HessianBlobIntensity.Type type, Class derivType ) { super(null, derivType); this.type = type; try { switch (type) { case DETERMINANT -> { + opScale = HessianBlobIntensity::determinantDerivativeScale; minimum = true; m = HessianBlobIntensity.class.getMethod("determinant", GrayF32.class, derivType, derivType, derivType); } case TRACE -> { + opScale = HessianBlobIntensity::traceScale; minimum = true; m = HessianBlobIntensity.class.getMethod("trace", GrayF32.class, derivType, derivType); } @@ -57,11 +62,15 @@ public WrapperHessianDerivBlobIntensity( HessianBlobIntensity.Type type, Class 1; + } } @Override public void process( I image, @Nullable D derivX, @Nullable D derivY, - @Nullable D derivXX, @Nullable D derivYY, @Nullable D derivXY ) { + @Nullable D derivXX, @Nullable D derivYY, @Nullable D derivXY ) { init(image.width, image.height); try { @@ -74,6 +83,11 @@ public void process( I image, @Nullable D derivX, @Nullable D derivY, } } + /// TODO return the correct scale + @Override public float thresholdScaleByDerivative( int divisor ) { + return opScale.scale(divisor); + } + @Override public @Nullable ListIntPoint2D getCandidatesMin() { return null; @@ -116,4 +130,8 @@ public boolean localMinimums() { public boolean localMaximums() { return true; } + + @FunctionalInterface interface DerivativeScale { + float scale( int divisor ); + } } diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianThreeImageDetIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianThreeImageDetIntensity.java index 68378d794f..af4f0bd5b1 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianThreeImageDetIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperHessianThreeImageDetIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -56,6 +56,11 @@ public void process( I image, @Nullable D derivX, @Nullable D derivY, } } + /// TODO return the correct scale + @Override public float thresholdScaleByDerivative( int divisor ) { + return 1; + } + @Override public @Nullable ListIntPoint2D getCandidatesMin() { return null; diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperKitRosCornerIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperKitRosCornerIntensity.java index 3c980ad942..6a1c75a496 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperKitRosCornerIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperKitRosCornerIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,6 +22,7 @@ import boofcv.struct.ListIntPoint2D; import boofcv.struct.image.GrayF32; import boofcv.struct.image.ImageGray; +import boofcv.struct.image.ImageType; import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationTargetException; @@ -35,9 +36,11 @@ public class WrapperKitRosCornerIntensity, D extends ImageGray> extends BaseGeneralFeatureIntensity { Method m; + boolean integerValued; public WrapperKitRosCornerIntensity( Class derivType ) { super(null, derivType); + integerValued = ImageType.single(derivType).getDataType().isInteger(); try { m = KitRosCornerIntensity.class.getMethod("process", GrayF32.class, derivType, derivType, derivType, derivType, derivType); } catch (NoSuchMethodException e) { @@ -57,6 +60,10 @@ public void process( I image, @Nullable D derivX, @Nullable D derivY, } } + @Override public float thresholdScaleByDerivative( int divisor ) { + return integerValued ? divisor : 1.0f; + } + @Override public @Nullable ListIntPoint2D getCandidatesMin() { return null; diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperLaplacianBlobIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperLaplacianBlobIntensity.java index e4e1aa949e..cd7c028fa6 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperLaplacianBlobIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperLaplacianBlobIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -54,6 +54,11 @@ public void process( I image, @Nullable D derivX, @Nullable D derivY, } } + /// Takes an image in not the derivative so it shouldn't apply + @Override public float thresholdScaleByDerivative( int divisor ) { + return 1; + } + @Override public @Nullable ListIntPoint2D getCandidatesMin() { return null; diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperMedianCornerIntensity.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperMedianCornerIntensity.java index eac6e6f417..782120f8df 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperMedianCornerIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/intensity/WrapperMedianCornerIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -72,6 +72,10 @@ public void process( I input, @Nullable D derivX, @Nullable D derivY, } } + @Override public float thresholdScaleByDerivative( int divisor ) { + return 1.0f; + } + @Override public @Nullable ListIntPoint2D getCandidatesMin() { return null; diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/interest/GeneralToInterestPoint.java b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/interest/GeneralToInterestPoint.java index 803e90bc85..326e7f378f 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/interest/GeneralToInterestPoint.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/feature/detect/interest/GeneralToInterestPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -49,15 +49,16 @@ public class GeneralToInterestPoint, D extends ImageGray< // list of points it found protected DogArray foundPoints = new DogArray<>(10, Point2D_F64::new); - public GeneralToInterestPoint( GeneralFeatureDetector detector, double radius ) { - super(detector, detector.getImageType(), detector.getDerivType()); + public GeneralToInterestPoint( GeneralFeatureDetector detector, ImageGradient gradient, double radius ) { + super(detector, gradient, detector.getImageType(), detector.getDerivType()); configure(detector, radius); } public GeneralToInterestPoint( GeneralFeatureDetector detector, + @Nullable ImageGradient gradient, double radius, @Nullable Class imageType, @Nullable Class derivType ) { - super(detector, imageType, derivType); + super(detector, gradient, imageType, derivType); configure(detector, radius); } diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/flow/FlowKlt_to_DenseOpticalFlow.java b/main/boofcv-feature/src/main/java/boofcv/abst/flow/FlowKlt_to_DenseOpticalFlow.java index c37291355e..78b3dd70c2 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/flow/FlowKlt_to_DenseOpticalFlow.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/flow/FlowKlt_to_DenseOpticalFlow.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -50,10 +50,10 @@ public class FlowKlt_to_DenseOpticalFlow, D extends Image ImageType imageType; public FlowKlt_to_DenseOpticalFlow( DenseOpticalFlowKlt flowKlt, - ImageGradient gradient, - ImagePyramid pyramidSrc, - ImagePyramid pyramidDst, - Class inputType, Class derivType ) { + ImageGradient gradient, + ImagePyramid pyramidSrc, + ImagePyramid pyramidDst, + Class inputType, Class derivType ) { this.flowKlt = flowKlt; this.gradient = gradient; this.pyramidSrc = pyramidSrc; @@ -87,7 +87,9 @@ public void process( I source, I destination, ImageFlow flow ) { PyramidOps.gradient(pyramidSrc, gradient, srcDerivX, srcDerivY); - flowKlt.process(pyramidSrc, srcDerivX, srcDerivY, pyramidDst, flow); + flowKlt.process(pyramidSrc, + srcDerivX, srcDerivY, gradient.divisor(), + pyramidDst, flow); } @Override diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerHybrid.java b/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerHybrid.java index 48ca30c6a4..c42c86c236 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerHybrid.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerHybrid.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,7 +22,6 @@ import boofcv.alg.tracker.hybrid.HybridTrack; import boofcv.alg.tracker.hybrid.HybridTrackerScalePoint; import boofcv.alg.transform.pyramid.PyramidOps; -import boofcv.factory.filter.derivative.FactoryDerivative; import boofcv.factory.transform.pyramid.FactoryPyramid; import boofcv.struct.ConfigLength; import boofcv.struct.feature.TupleDesc; @@ -78,15 +77,15 @@ public class PointTrackerHybrid, D extends ImageGray, boolean detectCalled; public PointTrackerHybrid( HybridTrackerScalePoint tracker, + ImageGradient gradient, ConfigDiscreteLevels configLevels, Class imageType, Class derivType ) { this.tracker = tracker; + this.gradient = gradient; this.derivType = ImageType.single(derivType); this.inputType = ImageType.single(imageType); pyramid = FactoryPyramid.discreteGaussian(configLevels, -1, 2, true, ImageType.single(imageType)); - gradient = FactoryDerivative.sobel(imageType, derivType); - reset(); } diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid.java b/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid.java index 0b7dbd1fa8..4e0fde81ce 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid.java @@ -213,7 +213,7 @@ protected PyramidKltFeature getUnusedTrack() { @Override public void spawnTracks() { spawned.clear(); - tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, gradient.divisor()); // used to convert it from the scale of the bottom layer into the original image float scaleBottom = (float)currPyr.basePyramid.getScale(0); @@ -331,7 +331,7 @@ protected boolean checkValidSpawn( PointTrack p ) { /// Tracks features in the forward direction protected void trackFeatures( I image ) { - tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, gradient.divisor()); for (int i = active.size() - 1; i >= 0; i--) { PyramidKltFeature t = active.get(i); KltTrackFault ret = tracker.track(t); @@ -369,7 +369,7 @@ protected void pruneCloseTracks() { protected void backwardsTrackValidate() { double tol2 = toleranceFB*toleranceFB; - tracker.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY); + tracker.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY, gradient.divisor()); for (int i = active.size() - 1; i >= 0; i--) { PyramidKltFeature t = active.get(i); PointTrackMod p = t.getCookie(); diff --git a/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid_MT.java b/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid_MT.java index 7bb77e7108..04227b8a24 100644 --- a/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid_MT.java +++ b/main/boofcv-feature/src/main/java/boofcv/abst/tracker/PointTrackerKltPyramid_MT.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -78,7 +78,7 @@ public PointTrackerKltPyramid_MT( ConfigKlt config, double toleranceFB, int temp // do image processing on each new track BoofConcurrency.loopBlocks(0, workTracks.size(), workspace, ( helper, idx0, idx1 ) -> { - helper.tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + helper.tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, gradient.divisor()); for (int detIdx = idx0; detIdx < idx1; detIdx++) { Point2D_I16 pt = found.get(detIdx); PyramidKltFeature t = workTracks.get(detIdx); @@ -127,7 +127,7 @@ public PointTrackerKltPyramid_MT( ConfigKlt config, double toleranceFB, int temp // Perform tracking in parallel BoofConcurrency.loopBlocks(0, active.size(), workspace, ( helper, idx0, idx1 ) -> { - helper.tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + helper.tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, gradient.divisor()); for (int trackIdx = idx0; trackIdx < idx1; trackIdx++) { PyramidKltFeature t = active.get(trackIdx); @@ -182,7 +182,7 @@ private void dropFailed() { // Perform tracking in parallel BoofConcurrency.loopBlocks(0, active.size(), workspace, ( helper, idx0, idx1 ) -> { - helper.tracker.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY); + helper.tracker.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY, gradient.divisor()); for (int trackIdx = idx0; trackIdx < idx1; trackIdx++) { PyramidKltFeature t = active.get(trackIdx); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/chess/DetectChessboardCornersX.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/chess/DetectChessboardCornersX.java index 15da0bfc0c..96c04fee38 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/chess/DetectChessboardCornersX.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/chess/DetectChessboardCornersX.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -173,7 +173,7 @@ public DetectChessboardCornersX() { config.detectMaximums = true; config.detectMinimums = false; config.useStrictRule = true; - nonmax = FactoryFeatureExtractor.nonmax(config); + nonmax = FactoryFeatureExtractor.nonmax(config, 1); } blurFilter = FactoryBlurFilter.gaussian(ImageType.SB_F32, -1, blurRadius); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/GradientCornerIntensity.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/GradientCornerIntensity.java index 8188339762..eb0c9ee060 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/GradientCornerIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/GradientCornerIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -61,6 +61,10 @@ public interface GradientCornerIntensity> extends Feature */ void process( T derivX, T derivY, GrayF32 intensity ); + /// If the gradient has an integer kernel you need to scale threshold to account for the gradient being scaled + /// or else it will vary by image and kernel type + float thresholdScaleByDerivative( int divisor ); + /** * Type of input image. */ diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/HessianBlobIntensity.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/HessianBlobIntensity.java index aa34e982f1..79af3c0ce7 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/HessianBlobIntensity.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/HessianBlobIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -71,6 +71,10 @@ public static void determinant( GrayF32 featureIntensity, GrayF32 hessianXX, Gra ImplHessianBlobIntensity.determinant(featureIntensity, hessianXX, hessianYY, hessianXY); } + public static float determinantDerivativeScale( int divisor ) { + return divisor*divisor; + } + /** * Feature intensity using the trace of the Hessian matrix. This is also known as the Laplacian. * @@ -84,6 +88,10 @@ public static void trace( GrayF32 featureIntensity, GrayF32 hessianXX, GrayF32 h ImplHessianBlobIntensity.trace(featureIntensity, hessianXX, hessianYY); } + public static float traceScale( int divisor ) { + return divisor; + } + /** * Feature intensity using the Hessian matrix's determinant. * diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_F32.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_F32.java index 8139ffe176..ccb5c6182d 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_F32.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,36 +19,22 @@ package boofcv.alg.feature.detect.intensity.impl; import boofcv.alg.feature.detect.intensity.HarrisCornerIntensity; +import lombok.Getter; +import lombok.Setter; -/** - *

- * Implementation of {@link boofcv.alg.feature.detect.intensity.HarrisCornerIntensity}. - *

- * - * @author Peter Abeles - */ +/// Implementation of [boofcv.alg.feature.detect.intensity.HarrisCornerIntensity]. public class HarrisCorner_F32 implements ImplSsdCornerBase.CornerIntensity_F32, HarrisCornerIntensity { - float kappa; + @Getter @Setter float kappa; public HarrisCorner_F32( float kappa ) { this.kappa = kappa; } - @Override - public void setKappa( float kappa ) { - this.kappa = kappa; - } - @Override public float compute( float totalXX, float totalXY, float totalYY ) { // det(A) - kappa*trace(A)^2 float trace = totalXX + totalYY; return (totalXX*totalYY - totalXY*totalXY) - kappa*trace*trace; } - - @Override - public float getKappa() { - return kappa; - } } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_S32.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_S32.java index 46f2a7d8f2..49c4a31b6a 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_S32.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/HarrisCorner_S32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,28 +19,18 @@ package boofcv.alg.feature.detect.intensity.impl; import boofcv.alg.feature.detect.intensity.HarrisCornerIntensity; +import lombok.Getter; +import lombok.Setter; -/** - *

- * Implementation of {@link boofcv.alg.feature.detect.intensity.HarrisCornerIntensity}. - *

- * - * @author Peter Abeles - */ +/// Implementation of [boofcv.alg.feature.detect.intensity.HarrisCornerIntensity]. public class HarrisCorner_S32 implements ImplSsdCornerBase.CornerIntensity_S32, HarrisCornerIntensity { - // tuning parameter - float kappa; + @Getter @Setter float kappa; public HarrisCorner_S32( float kappa ) { this.kappa = kappa; } - @Override - public void setKappa( float kappa ) { - this.kappa = kappa; - } - @Override public float compute( int _totalXX, int _totalXY, int _totalYY ) { // det(A) -+ kappa*trace(A)^2 @@ -50,8 +40,5 @@ public float compute( int _totalXX, int _totalXY, int _totalYY ) { return (totalXX*totalYY - totalXY*totalXY) - kappa*trace*trace; } - @Override - public float getKappa() { - return kappa; - } + @Override public float thresholdScaleByDerivative( int divisor ) {return divisor*divisor*divisor*divisor;} } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerBase.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerBase.java index eb91e10b37..22880452ad 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerBase.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerBase.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -91,6 +91,8 @@ public interface CornerIntensity_S32 { * @return corner intensity. */ float compute( int totalXX, int totalXY, int totalYY ); + + float thresholdScaleByDerivative( int divisor ); } public interface CornerIntensity_F32 { diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerNaive.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerNaive.java index b0f33eba77..8ea4f57bac 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerNaive.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerNaive.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -106,6 +106,10 @@ public void process( T derivX, T derivY, GrayF32 intensity ) { } } + @Override public float thresholdScaleByDerivative( int divisor ) { + return divisor*divisor; + } + @Override public Class getInputType() { throw new RuntimeException("Unknown"); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_F32.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_F32.java index ed66fd0572..08bd121624 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_F32.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -93,6 +93,10 @@ public void process( GrayF32 derivX, GrayF32 derivY, GrayF32 intensity ) { //CONCURRENT_ABOVE }); } + @Override public float thresholdScaleByDerivative( int divisor ) { + return 1.0f; + } + private void blur( GrayF32 image, GrayF32 temp ) { ConvolveImageNormalized.horizontal(kernel, image, temp); ConvolveImageNormalized.vertical(kernel, temp, image); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_S16.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_S16.java index a8efc8abf8..5998fb0338 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_S16.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCornerWeighted_S16.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -97,6 +97,10 @@ public void process( GrayS16 derivX, GrayS16 derivY, GrayF32 intensity ) { //CONCURRENT_ABOVE }); } + @Override public float thresholdScaleByDerivative( int divisor ) { + return this.intensity.thresholdScaleByDerivative(divisor); + } + private void blur( GrayS32 image, GrayS32 temp ) { ConvolveImageNormalized.horizontal(kernel, image, temp); ConvolveImageNormalized.vertical(kernel, temp, image); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_F32.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_F32.java index cf54c08f4e..a746efae2f 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_F32.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -193,6 +193,10 @@ protected void vertical( GrayF32 intensity ) { //CONCURRENT_INLINE }); } + @Override public float thresholdScaleByDerivative( int divisor ) { + return 1.0f; + } + private static class WorkSpace { public final float[] xx; public final float[] yy; diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_S16.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_S16.java index 6a9d721b7c..e22beeca60 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_S16.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ImplSsdCorner_S16.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -195,6 +195,10 @@ protected void vertical( GrayF32 intensity ) { //CONCURRENT_INLINE }); } + @Override public float thresholdScaleByDerivative( int divisor ) { + return divisor*divisor; + } + private static class WorkSpace { public final int[] xx; public final int[] yy; diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ShiTomasiCorner_S32.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ShiTomasiCorner_S32.java index e221db4c4f..dfbbc06bb0 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ShiTomasiCorner_S32.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/intensity/impl/ShiTomasiCorner_S32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -20,16 +20,9 @@ import boofcv.alg.feature.detect.intensity.ShiTomasiCornerIntensity; -/** - *

- * Implementation of {@link boofcv.alg.feature.detect.intensity.ShiTomasiCornerIntensity}. - *

- * - * @author Peter Abeles - */ +/// Implementation of [boofcv.alg.feature.detect.intensity.ShiTomasiCornerIntensity]. public class ShiTomasiCorner_S32 implements ImplSsdCornerBase.CornerIntensity_S32, ShiTomasiCornerIntensity { - @Override - public float compute( int totalXX, int totalXY, int totalYY ) { + @Override public float compute( int totalXX, int totalXY, int totalYY ) { // compute the smallest eigenvalue double left = (totalXX + totalYY)*0.5; double b = (totalXX - totalYY)*0.5; @@ -38,4 +31,8 @@ public float compute( int totalXX, int totalXY, int totalYY ) { // the smallest eigenvalue will be minus the right side return (float)(left - right); } + + @Override public float thresholdScaleByDerivative( int divisor ) { + return divisor*divisor; + } } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/EasyGeneralFeatureDetector.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/EasyGeneralFeatureDetector.java index 093782984e..68067cf75c 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/EasyGeneralFeatureDetector.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/EasyGeneralFeatureDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -26,6 +26,7 @@ import boofcv.struct.QueueCorner; import boofcv.struct.image.ImageGray; import boofcv.struct.image.ImageType; +import lombok.Getter; import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -38,8 +39,8 @@ @SuppressWarnings({"NullAway.Init"}) public class EasyGeneralFeatureDetector, D extends ImageGray> { - // Feature detector - protected GeneralFeatureDetector detector; + /// Feature detector + protected @Getter GeneralFeatureDetector detector; // Computes image gradient protected @Nullable ImageGradient gradient; // computes hessian @@ -60,6 +61,7 @@ public class EasyGeneralFeatureDetector, D extends ImageG * @param derivType If null then the derivative will be selected using the image type. */ public EasyGeneralFeatureDetector( GeneralFeatureDetector detector, + @Nullable ImageGradient gradient, @Nullable Class imageType, @Nullable Class derivType ) { this.detector = detector; @@ -77,11 +79,12 @@ public EasyGeneralFeatureDetector( GeneralFeatureDetector detector, } if (detector.getRequiresGradient() || detector.getRequiresHessian()) { - Class im = Objects.requireNonNull(imageType, "Must specify image type since detector doesn't"); - gradient = FactoryDerivative.sobel(im, derivType); + Objects.requireNonNull(gradient); + this.gradient = gradient; } if (detector.getRequiresHessian()) { - hessian = FactoryDerivative.hessianSobel(derivType); + Objects.requireNonNull(gradient); + hessian = FactoryDerivative.hessian(gradient.getClass(), derivType); } declareDerivativeImages(gradient, hessian, derivType); } @@ -151,10 +154,6 @@ private void initializeDerivatives( T input ) { } } - public GeneralFeatureDetector getDetector() { - return detector; - } - public QueueCorner getMaximums() { return detector.getMaximums(); } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/SiftDetector.java b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/SiftDetector.java index 14e047ee0c..cbae0b6324 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/SiftDetector.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/feature/detect/interest/SiftDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -153,7 +153,7 @@ public SiftDetector( FeatureSelectLimitIntensity selectFeaturesAll, * Define sparse image derivative operators. */ private void createSparseDerivatives() { - Kernel1D_F32 kernelD = new Kernel1D_F32(new float[]{-1, 0, 1}, 3); + Kernel1D_F32 kernelD = new Kernel1D_F32(3, new float[]{-1, 0, 1}); Kernel1D_F32 kernelDD = KernelMath.convolve1D_F32(kernelD, kernelD); Kernel2D_F32 kernelXY = KernelMath.convolve2D(kernelD, kernelD); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/flow/DenseOpticalFlowKlt.java b/main/boofcv-feature/src/main/java/boofcv/alg/flow/DenseOpticalFlowKlt.java index bf01e30468..fa30d065e2 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/flow/DenseOpticalFlowKlt.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/flow/DenseOpticalFlowKlt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -59,7 +59,7 @@ public DenseOpticalFlowKlt( PyramidKltTracker tracker, int radius ) { this.regionRadius = radius; } - public void process( ImagePyramid prev, D[] prevDerivX, D[] prevDerivY, + public void process( ImagePyramid prev, D[] prevDerivX, D[] prevDerivY, int derivDivisor, ImagePyramid curr, ImageFlow output ) { if (feature == null) @@ -81,7 +81,7 @@ public void process( ImagePyramid prev, D[] prevDerivX, D[] prevDerivY, for (int y = 0; y < output.height; y++) { for (int x = 0; x < output.width; x++) { - tracker.setImage(prev, prevDerivX, prevDerivY); + tracker.setImage(prev, prevDerivX, prevDerivY, derivDivisor); feature.setPosition(x, y); if (tracker.setDescription(feature)) { diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/HybridTrackerScalePoint.java b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/HybridTrackerScalePoint.java index 24d12a90ae..a912df9cfe 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/HybridTrackerScalePoint.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/HybridTrackerScalePoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -109,9 +109,9 @@ public class HybridTrackerScalePoint * @param tooCloseRadius If tracks are less than or equal to this distance to each other some will be pruned */ public HybridTrackerScalePoint( PyramidKltForHybrid trackerKlt, - DetectDescribePoint detector, - AssociateDescription2D associate, - int tooCloseRadius ) { + DetectDescribePoint detector, + AssociateDescription2D associate, + int tooCloseRadius ) { if (!associate.uniqueDestination() || !associate.uniqueSource()) throw new IllegalArgumentException("Associations must be unique"); @@ -161,9 +161,7 @@ public void reset() { * @param derivX Derivative pyramid of input x-axis * @param derivY Derivative pyramid of input y-axis */ - public void updateTracks( PyramidDiscrete pyramid, - D[] derivX, - D[] derivY ) { + public void updateTracks( PyramidDiscrete pyramid, D[] derivX, D[] derivY ) { this.imageWidth = pyramid.getInputWidth(); this.imageHeight = pyramid.getInputWidth(); this.tracksDropped.clear(); diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/PyramidKltForHybrid.java b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/PyramidKltForHybrid.java index 82f56283a3..05bd6b5c1c 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/PyramidKltForHybrid.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/hybrid/PyramidKltForHybrid.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -43,11 +43,14 @@ public class PyramidKltForHybrid, D extends ImageGray> // the tracker protected PyramidKltTracker tracker; + int derivDivisor; + public PyramidKltForHybrid( ConfigKlt config, int featureRadius, - Class inputType, Class derivType ) { + Class inputType, Class derivType, int derivDivisor ) { this.config = config; this.featureRadius = featureRadius; + this.derivDivisor = derivDivisor; InterpolateRectangle interpInput = FactoryInterpolation.bilinearRectangle(inputType); InterpolateRectangle interpDeriv = FactoryInterpolation.bilinearRectangle(derivType); @@ -68,7 +71,7 @@ public void setInputs( ImagePyramid image, D[] derivX, D[] derivY ) { this.numLevels = image.getNumLayers(); else if (numLevels != image.getNumLayers()) throw new IllegalArgumentException("Number of levels pyramid changed!"); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, derivDivisor); } /** diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/EasyPyramidKlt.java b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/EasyPyramidKlt.java index d343204562..34aae065de 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/EasyPyramidKlt.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/EasyPyramidKlt.java @@ -211,11 +211,11 @@ public void track() { public void track( int idx0, int idx1 ) { if (isUseConcurrent(idx1 - idx0)) { BoofConcurrency.loopBlocks(idx0, idx1, workspace, ( helper, block0, block1 ) -> { - helper.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + helper.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, currPyr.gradientDivisor()); track(block0, block1, helper); }); } else { - tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, prevPyr.gradientDivisor()); track(idx0, idx1, tracker); } } @@ -244,7 +244,7 @@ public KltTrackFault track( int index ) { if (index < 0 || index >= tracks.size) throw new IndexOutOfBoundsException("index=" + index + " size=" + tracks.size); - tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, currPyr.gradientDivisor()); PyramidKltFeature t = tracks.get(index); TrackMeta meta = metadata.get(index); @@ -269,11 +269,11 @@ public void backwardsValidation() { public void backwardsValidation( int idx0, int idx1 ) { if (isUseConcurrent(idx1 - idx0)) { BoofConcurrency.loopBlocks(idx0, idx1, workspace, ( helper, block0, block1 ) -> { - helper.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY); + helper.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY, prevPyr.gradientDivisor()); backwardsValidation(block0, block1, helper); }); } else { - tracker.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY); + tracker.setImage(prevPyr.basePyramid, prevPyr.derivX, prevPyr.derivY, prevPyr.gradientDivisor()); backwardsValidation(idx0, idx1, tracker); } } @@ -317,11 +317,11 @@ public void describe() { public void describe( int idx0, int idx1 ) { if (isUseConcurrent(idx1 - idx0)) { BoofConcurrency.loopBlocks(idx0, idx1, workspace, ( helper, block0, block1 ) -> { - helper.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + helper.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, currPyr.gradientDivisor()); describe(block0, block1, helper); }); } else { - tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY); + tracker.setImage(currPyr.basePyramid, currPyr.derivX, currPyr.derivY, currPyr.gradientDivisor()); describe(idx0, idx1, tracker); } } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltFeature.java b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltFeature.java index 449c7645c9..7a87bcfca5 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltFeature.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltFeature.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -69,4 +69,10 @@ public void setPosition( float x, float y ) { this.x = x; this.y = y; } + + public KltFeature withPosition( float x, float y ) { + this.x = x; + this.y = y; + return this; + } } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltTracker.java b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltTracker.java index 07e4012851..e096e1e75a 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltTracker.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/KltTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -62,6 +62,8 @@ public class KltTracker, D extends ImageGray> { protected I image; // image gradient protected @Nullable D derivX, derivY; + // Integer gradients will be off by a scale factor. This is the inverse of that scale factor. + protected float derivScale = Float.NaN; // Used to interpolate the image and gradient protected InterpolateRectangle interpInput; @@ -123,7 +125,7 @@ public KltTracker( InterpolateRectangle interpInput, * @param derivX Image derivative along the x-axis * @param derivY Image derivative along the y-axis */ - public void setImage( I image, @Nullable D derivX, @Nullable D derivY ) { + public void setImage( I image, @Nullable D derivX, @Nullable D derivY, int derivDivisor ) { if (derivX != null && derivY != null) InputSanityCheck.checkSameShape(image, derivX, derivY); @@ -132,6 +134,7 @@ public void setImage( I image, @Nullable D derivX, @Nullable D derivY ) { this.derivX = derivX; this.derivY = derivY; + this.derivScale = 1.0f/derivDivisor; } /** @@ -182,6 +185,11 @@ protected boolean internalSetDescription( KltFeature feature, D derivX, D derivY Gxy += dX*dY; } + float ss = derivScale*derivScale; + Gxx *= ss; + Gyy *= ss; + Gxy *= ss; + feature.Gxx = Gxx; feature.Gyy = Gyy; feature.Gxy = Gxy; @@ -230,6 +238,11 @@ protected boolean internalSetDescriptionBorder( KltFeature feature, D derivX, D Gxy += dX*dY; } + float ss = derivScale*derivScale; + Gxx *= ss; + Gyy *= ss; + Gxy *= ss; + // technically don't need to save this... feature.Gxx = Gxx; feature.Gyy = Gyy; @@ -373,6 +386,9 @@ protected void computeE( KltFeature feature, float x, float y ) { Ex += d*feature.derivX.data[i]; Ey += d*feature.derivY.data[i]; } + + Ex *= derivScale; + Ey *= derivScale; } /** @@ -420,6 +436,14 @@ protected int computeGandE_border( KltFeature feature, float cx, float cy ) { Gxy += dX*dY; } + float ss = derivScale*derivScale; + Gxx *= ss; + Gyy *= ss; + Gxy *= ss; + + Ex *= derivScale; + Ey *= derivScale; + return total; } diff --git a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/PyramidKltTracker.java b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/PyramidKltTracker.java index b245ca5b7e..a0e3e55c83 100644 --- a/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/PyramidKltTracker.java +++ b/main/boofcv-feature/src/main/java/boofcv/alg/tracker/klt/PyramidKltTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -45,6 +45,7 @@ public class PyramidKltTracker, Derivat // image pyramid for image gradient protected @Nullable DerivativeImage[] derivX; protected @Nullable DerivativeImage[] derivY; + protected int derivDivisor; public PyramidKltTracker( KltTracker tracker ) { this.tracker = tracker; @@ -58,13 +59,14 @@ public PyramidKltTracker( KltTracker tracker ) { * @param derivY Derivative along y-axis. */ public void setImage( ImagePyramid image, - DerivativeImage[] derivX, DerivativeImage[] derivY ) { + DerivativeImage[] derivX, DerivativeImage[] derivY, int derivDivisor ) { if (image.getNumLayers() != derivX.length || image.getNumLayers() != derivY.length) throw new IllegalArgumentException("Number of layers does not match."); this.image = image; this.derivX = derivX; this.derivY = derivY; + this.derivDivisor = derivDivisor; } /** @@ -82,7 +84,7 @@ public void setImage( ImagePyramid image ) { * Sets the feature's description up. The feature's (x,y) must have already been set * and {@link #setImage} been called. * - * @param feature Feature's whose description is being setup. + * @param feature Feature which is being updated. * @return true if there was sufficient information to create a feature or false if not */ public boolean setDescription( PyramidKltFeature feature ) { @@ -129,7 +131,7 @@ public KltTrackFault track( PyramidKltFeature feature ) { y /= scale; // tracking never needs the derivative - tracker.setImage(image.getLayer(layer), null, null); + tracker.setImage(image.getLayer(layer), null, null, derivDivisor); KltFeature f = feature.desc[layer]; f.setPosition(x, y); @@ -166,9 +168,9 @@ public float getError() { private void setupKltTracker( int layer ) { if (derivX != null && derivY != null) - tracker.setImage(image.getLayer(layer), derivX[layer], derivY[layer]); + tracker.setImage(image.getLayer(layer), derivX[layer], derivY[layer], derivDivisor); else - tracker.setImage(image.getLayer(layer), null, null); + tracker.setImage(image.getLayer(layer), null, null, derivDivisor); } /** diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detdesc/FactoryDetectDescribe.java b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detdesc/FactoryDetectDescribe.java index cac3cbf1c4..6446cfcd3b 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detdesc/FactoryDetectDescribe.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detdesc/FactoryDetectDescribe.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -91,7 +91,7 @@ DetectDescribePoint generic( ConfigDetectDescribe config, Class detector = FactoryInterestPoint.sift(config.scaleSpaceSift, config.detectSift, imageType); case POINT -> { - GeneralFeatureDetector alg = FactoryDetectPoint.create(config.detectPoint, imageType, null); + GeneralFeatureDetector alg = FactoryDetectPoint.create(config.detectPoint, imageType, null, 1); detector = FactoryInterestPoint.wrapPoint(alg, config.detectPoint.scaleRadius, imageType, alg.getDerivType()); } default -> throw new IllegalArgumentException("Unknown detector"); diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/extract/FactoryFeatureExtractor.java b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/extract/FactoryFeatureExtractor.java index a476317585..4ae0168750 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/extract/FactoryFeatureExtractor.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/extract/FactoryFeatureExtractor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -42,7 +42,7 @@ public class FactoryFeatureExtractor { /** * Creates a generalized feature detector/extractor that adds n-best capability to {@link boofcv.abst.feature.detect.extract.NonMaxSuppression} - * and performs other house keeping tasks. Handles calling {@link GeneralFeatureIntensity} itself. + * and performs other housekeeping tasks. Handles calling {@link GeneralFeatureIntensity} itself. * * @param intensity Feature intensity algorithm * @param extractorMin Feature extraction algorithm for local minimums @@ -63,22 +63,27 @@ GeneralFeatureDetector general( GeneralFeatureIntensity intensity, return det; } - /** - * Standard non-max feature extractor. - * - * @param config Configuration for extractor - * @return A feature extractor. - */ - public static NonMaxSuppression nonmax( @Nullable ConfigExtract config ) { - return nonmax(config, QueueCorner::append, QueueCorner::reset); + /// Standard non-max feature extractor. + /// + /// @param config Configuration for extractor + /// @param intensityScale How many times more intense is the intensity image vs one with a correctly scaled one. + /// Intensity images can have their scale thrown off by integer derivative operators. Set to 1 if you're not sure. + /// @return A feature extractor. + public static NonMaxSuppression nonmax( @Nullable ConfigExtract config, float intensityScale ) { + return nonmax(config, intensityScale, QueueCorner::append, QueueCorner::reset); } - public static NonMaxSuppression nonmaxPack16( @Nullable ConfigExtract config ) { - return nonmax(config, PackedArrayPoint2D_I16::append, PackedArrayPoint2D_I16::reset); + public static NonMaxSuppression + nonmaxPack16( @Nullable ConfigExtract config, float intensityScale ) { + return nonmax(config, intensityScale, PackedArrayPoint2D_I16::append, PackedArrayPoint2D_I16::reset); } + /// Factory function for creating a [NonMaxSuppression] + /// + /// @param intensityScale How many times more intense is the intensity image vs one with a correctly scaled one. + /// Intensity images can have their scale thrown off by integer derivative operators. Set to 1 if you're not sure. public static NonMaxSuppression - nonmax( @Nullable ConfigExtract config, + nonmax( @Nullable ConfigExtract config, float intensityScale, NonMaxSuppression.Add opAdd, NonMaxSuppression.Reset opReset ) { if (config == null) @@ -116,8 +121,8 @@ public static NonMaxSuppression nonmaxPack16( @Nullable alg.storageAccess(opAdd, opReset); alg.setSearchRadius(config.radius); - alg.setThresholdMax(config.threshold); - alg.setThresholdMin(-config.threshold); + alg.setThresholdMax(config.threshold/intensityScale); + alg.setThresholdMin(-config.threshold/intensityScale); alg.setBorder(config.ignoreBorder); return new WrapperNonMaximumBlock<>(alg); @@ -169,8 +174,9 @@ public static NonMaxSuppression nonmaxCandidate( @Nullable ConfigEx * @param maxFeatures maximum allowed features * @return The NonMaxLimiter */ - public static NonMaxLimiter nonmaxLimiter( @Nullable ConfigExtract configExtract, ConfigSelectLimit configSelect, int maxFeatures ) { - NonMaxSuppression nonmax = nonmaxPack16(configExtract); + public static NonMaxLimiter nonmaxLimiter( @Nullable ConfigExtract configExtract, float intensityScale, + ConfigSelectLimit configSelect, int maxFeatures ) { + NonMaxSuppression nonmax = nonmaxPack16(configExtract, intensityScale); FeatureSelectLimitIntensity selector = FactorySelectLimit.intensity(configSelect); return new NonMaxLimiter(nonmax, selector, maxFeatures); } diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryDetectPoint.java b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryDetectPoint.java index 52b0cf36ff..eb52bab194 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryDetectPoint.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryDetectPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -27,6 +27,7 @@ import boofcv.alg.feature.detect.intensity.HessianBlobIntensity; import boofcv.alg.feature.detect.interest.GeneralFeatureDetector; import boofcv.alg.feature.detect.selector.FeatureSelectLimitIntensity; +import boofcv.alg.filter.derivative.DerivativeLaplacian; import boofcv.alg.filter.derivative.GImageDerivativeOps; import boofcv.factory.feature.detect.extract.FactoryFeatureExtractor; import boofcv.factory.feature.detect.intensity.FactoryIntensityPoint; @@ -47,7 +48,7 @@ *

* *

- * NOTE: Sometimes the image border is ignored and some times it is not. If feature intensities are not + * NOTE: Sometimes the image border is ignored and sometimes it is not. If feature intensities are not * computed along the image border then it will be full of zeros. In that case the ignore border region * needs to be increased for non-max suppression or else it might generate a false positive. *

@@ -60,7 +61,7 @@ public class FactoryDetectPoint { * Creates a point detector from the generic configuration */ public static , D extends ImageGray> - GeneralFeatureDetector create( ConfigPointDetector config, @Nullable Class imageType, @Nullable Class derivType ) { + GeneralFeatureDetector create( ConfigPointDetector config, @Nullable Class imageType, @Nullable Class derivType, int derivDivisor ) { if (derivType == null) derivType = GImageDerivativeOps.getDerivativeType(Objects.requireNonNull(imageType)); @@ -73,15 +74,22 @@ GeneralFeatureDetector create( ConfigPointDetector config, @Nullable Class } return switch (config.type) { - case HARRIS -> FactoryDetectPoint.createHarris(config.general, config.harris, Objects.requireNonNull(derivType)); - case SHI_TOMASI -> FactoryDetectPoint.createShiTomasi(config.general, config.shiTomasi, derivType); + case HARRIS -> + FactoryDetectPoint.createHarris(config.general, config.harris, Objects.requireNonNull(derivType), derivDivisor); + case SHI_TOMASI -> + FactoryDetectPoint.createShiTomasi(config.general, config.shiTomasi, derivType, derivDivisor); case FAST -> FactoryDetectPoint.createFast(config.general, config.fast, Objects.requireNonNull(imageType)); - case KIT_ROS -> FactoryDetectPoint.createKitRos(config.general, Objects.requireNonNull(derivType)); + case KIT_ROS -> + FactoryDetectPoint.createKitRos(config.general, Objects.requireNonNull(derivType), derivDivisor); case MEDIAN -> FactoryDetectPoint.createMedian(config.general, Objects.requireNonNull(imageType)); - case DETERMINANT -> FactoryDetectPoint.createHessianDirect(HessianBlobIntensity.Type.DETERMINANT, config.general, Objects.requireNonNull(imageType)); - case LAPLACIAN -> FactoryDetectPoint.createHessianDirect(HessianBlobIntensity.Type.TRACE, config.general, Objects.requireNonNull(imageType)); - case DETERMINANT_H -> FactoryDetectPoint.createHessianDeriv(config.general, HessianBlobIntensity.Type.DETERMINANT, Objects.requireNonNull(derivType)); - case LAPLACIAN_H -> FactoryDetectPoint.createHessianDeriv(config.general, HessianBlobIntensity.Type.TRACE, Objects.requireNonNull(derivType)); + case DETERMINANT -> + FactoryDetectPoint.createHessianDirect(HessianBlobIntensity.Type.DETERMINANT, config.general, Objects.requireNonNull(imageType)); + case LAPLACIAN -> + FactoryDetectPoint.createHessianDirect(HessianBlobIntensity.Type.TRACE, config.general, Objects.requireNonNull(imageType)); + case DETERMINANT_H -> + FactoryDetectPoint.createHessianDeriv(config.general, HessianBlobIntensity.Type.DETERMINANT, Objects.requireNonNull(derivType)); + case LAPLACIAN_H -> + FactoryDetectPoint.createHessianDeriv(config.general, HessianBlobIntensity.Type.TRACE, Objects.requireNonNull(derivType)); default -> throw new IllegalArgumentException("Unknown type " + config.type); }; } @@ -96,7 +104,8 @@ GeneralFeatureDetector create( ConfigPointDetector config, @Nullable Class */ public static , D extends ImageGray> GeneralFeatureDetector createHarris( @Nullable ConfigGeneralDetector configDetector, - @Nullable ConfigHarrisCorner configCorner, Class derivType ) { + @Nullable ConfigHarrisCorner configCorner, Class derivType, + int derivDivisor ) { if (configDetector == null) configDetector = new ConfigGeneralDetector(); if (configCorner == null) { @@ -107,7 +116,7 @@ GeneralFeatureDetector createHarris( @Nullable ConfigGeneralDetector confi GradientCornerIntensity cornerIntensity = FactoryIntensityPointAlg.harris( configCorner.radius, (float)configCorner.kappa, configCorner.weighted, derivType); - return createGeneral(cornerIntensity, configDetector); + return createGeneral(cornerIntensity, configDetector, derivDivisor); } /** @@ -120,8 +129,8 @@ GeneralFeatureDetector createHarris( @Nullable ConfigGeneralDetector confi */ public static , D extends ImageGray> GeneralFeatureDetector createShiTomasi( @Nullable ConfigGeneralDetector configDetector, - @Nullable ConfigShiTomasi configCorner, - Class derivType ) { + @Nullable ConfigShiTomasi configCorner, + Class derivType, int derivDivisor ) { if (configDetector == null) configDetector = new ConfigGeneralDetector(); @@ -132,7 +141,7 @@ GeneralFeatureDetector createShiTomasi( @Nullable ConfigGeneralDetector co GradientCornerIntensity cornerIntensity = FactoryIntensityPointAlg.shiTomasi(configCorner.radius, configCorner.weighted, derivType); - return createGeneral(cornerIntensity, configDetector); + return createGeneral(cornerIntensity, configDetector, derivDivisor); } /** @@ -143,12 +152,13 @@ GeneralFeatureDetector createShiTomasi( @Nullable ConfigGeneralDetector co * @see boofcv.alg.feature.detect.intensity.KitRosCornerIntensity */ public static , D extends ImageGray> - GeneralFeatureDetector createKitRos( @Nullable ConfigGeneralDetector configDetector, Class derivType ) { + GeneralFeatureDetector createKitRos( @Nullable ConfigGeneralDetector configDetector, + Class derivType, int derivDivisor ) { if (configDetector == null) configDetector = new ConfigGeneralDetector(); GeneralFeatureIntensity intensity = new WrapperKitRosCornerIntensity<>(derivType); - return createGeneral(intensity, configDetector); + return createGeneral(intensity, configDetector, derivDivisor); } /** @@ -162,14 +172,14 @@ GeneralFeatureDetector createKitRos( @Nullable ConfigGeneralDetector confi */ public static , D extends ImageGray> GeneralFeatureDetector createFast( ConfigGeneralDetector configDetector, @Nullable ConfigFastCorner configFast, - Class imageType ) { + Class imageType ) { if (configFast == null) configFast = new ConfigFastCorner(); configFast.checkValidity(); FastCornerDetector alg = FactoryIntensityPointAlg.fast(configFast.pixelTol, configFast.minContinuous, imageType); GeneralFeatureIntensity intensity = new WrapperFastCornerIntensity<>(alg); - return createGeneral(intensity, configDetector); + return createGeneral(intensity, configDetector, 1); } /** @@ -186,7 +196,7 @@ GeneralFeatureDetector createMedian( @Nullable ConfigGeneralDetector confi BlurStorageFilter medianFilter = FactoryBlurFilter.median(ImageType.single(imageType), configDetector.radius); GeneralFeatureIntensity intensity = new WrapperMedianCornerIntensity<>(medianFilter); - return createGeneral(intensity, configDetector); + return createGeneral(intensity, configDetector, 1); } /** @@ -199,12 +209,13 @@ GeneralFeatureDetector createMedian( @Nullable ConfigGeneralDetector confi */ public static , D extends ImageGray> GeneralFeatureDetector createHessianDeriv( @Nullable ConfigGeneralDetector configDetector, HessianBlobIntensity.Type type, - Class derivType ) { + Class derivType ) { if (configDetector == null) configDetector = new ConfigGeneralDetector(); + // TODO provide the real derivDivisor GeneralFeatureIntensity intensity = FactoryIntensityPoint.hessian(type, derivType); - return createGeneral(intensity, configDetector); + return createGeneral(intensity, configDetector, 1); } /** @@ -216,48 +227,54 @@ GeneralFeatureDetector createHessianDeriv( @Nullable ConfigGeneralDetector */ public static , D extends ImageGray> GeneralFeatureDetector createHessianDirect( HessianBlobIntensity.Type type, - @Nullable ConfigGeneralDetector configDetector, - Class imageType ) { + @Nullable ConfigGeneralDetector configDetector, + Class imageType ) { if (configDetector == null) configDetector = new ConfigGeneralDetector(); + int divisor = 1; + GeneralFeatureIntensity intensity = switch (type) { case DETERMINANT -> FactoryIntensityPoint.hessianDet(imageType); - case TRACE -> (GeneralFeatureIntensity)FactoryIntensityPoint.laplacian(imageType); + case TRACE -> { + if (ImageType.single(imageType).getDataType().isInteger()) + divisor = DerivativeLaplacian.divisor; + yield (GeneralFeatureIntensity)FactoryIntensityPoint.laplacian(imageType); + } default -> throw new IllegalArgumentException("Unknown type"); }; - return createGeneral(intensity, configDetector); + return createGeneral(intensity, configDetector, divisor); } public static , D extends ImageGray> GeneralFeatureDetector createGeneral( GradientCornerIntensity cornerIntensity, - ConfigGeneralDetector config ) { - GeneralFeatureIntensity intensity = new WrapperGradientCornerIntensity<>(cornerIntensity); - return createGeneral(intensity, config); + ConfigGeneralDetector config, int derivDivisor ) { + var intensity = new WrapperGradientCornerIntensity(cornerIntensity); + return createGeneral(intensity, config, derivDivisor); } public static , D extends ImageGray> GeneralFeatureDetector createGeneral( GeneralFeatureIntensity intensity, - ConfigGeneralDetector config ) { - ConfigGeneralDetector foo = new ConfigGeneralDetector(); - foo.setTo(config); - config = foo; + ConfigGeneralDetector config, int derivDivisor ) { + config = new ConfigGeneralDetector().setTo(config); config.ignoreBorder += config.radius; + float intensityScale = intensity.thresholdScaleByDerivative(derivDivisor); + NonMaxSuppression extractorMin = null; NonMaxSuppression extractorMax = null; if (intensity.localMinimums()) { config.detectMinimums = true; config.detectMaximums = false; - extractorMin = FactoryFeatureExtractor.nonmax(config); + extractorMin = FactoryFeatureExtractor.nonmax(config, intensityScale); } if (intensity.localMaximums()) { config.detectMinimums = false; config.detectMaximums = true; - extractorMax = FactoryFeatureExtractor.nonmax(config); + extractorMax = FactoryFeatureExtractor.nonmax(config, intensityScale); } FeatureSelectLimitIntensity selector = FactorySelectLimit.intensity(config.selector, Point2D_I16.class); - GeneralFeatureDetector det = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); + var det = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); det.setFeatureLimit(config.maxFeatures); return det; diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPoint.java b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPoint.java index a503b98ea2..fb278c3782 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPoint.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPoint.java @@ -56,7 +56,7 @@ public class FactoryInterestPoint { public static , D extends ImageGray> InterestPointDetector generic( ConfigDetectInterestPoint config, - Class inputType, @Nullable Class derivType ) { + Class inputType, @Nullable Class derivType, int derivDivisor ) { switch (config.type) { case FAST_HESSIAN -> { return FactoryInterestPoint.fastHessian(config.fastHessian, inputType); @@ -67,7 +67,7 @@ InterestPointDetector generic( ConfigDetectInterestPoint config, case POINT -> { if (derivType == null) derivType = GImageDerivativeOps.getDerivativeType(inputType); - GeneralFeatureDetector alg = FactoryDetectPoint.create(config.point, inputType, derivType); + GeneralFeatureDetector alg = FactoryDetectPoint.create(config.point, inputType, derivType, derivDivisor); return FactoryInterestPoint.wrapPoint(alg, config.point.scaleRadius, inputType, derivType); } default -> throw new IllegalArgumentException("Unknown detector"); @@ -198,7 +198,7 @@ InterestPointDetector sift( @Nullable ConfigSiftScaleSpace configSS, var ss = new SiftScaleSpace(configSS.firstOctave, configSS.lastOctave, configSS.numScales, configSS.sigma0); NonMaxLimiter nonmax = FactoryFeatureExtractor.nonmaxLimiter( - configDet.extract, configDet.selector, configDet.maxFeaturesPerScale); + configDet.extract, 1, configDet.selector, configDet.maxFeaturesPerScale); FeatureSelectLimitIntensity selectorAll = FactorySelectLimit.intensity(configDet.selector); var detector = new SiftDetector(selectorAll, configDet.edgeR, nonmax); detector.maxFeaturesAll = configDet.maxFeaturesAll; diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPointAlgs.java b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPointAlgs.java index 4c477adde5..ad08561fe9 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPointAlgs.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/interest/FactoryInterestPointAlgs.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -63,15 +63,15 @@ public class FactoryInterestPointAlgs { */ public static , D extends ImageGray> FeaturePyramid hessianPyramid( int extractRadius, - float detectThreshold, - int maxFeatures, - Class imageType, - Class derivType ) { + float detectThreshold, + int maxFeatures, + Class imageType, + Class derivType ) { GeneralFeatureIntensity intensity = new WrapperHessianDerivBlobIntensity<>(HessianBlobIntensity.Type.DETERMINANT, derivType); NonMaxSuppression extractorMin = intensity.localMinimums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true), 1) : null; NonMaxSuppression extractorMax = intensity.localMaximums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true), 1) : null; FeatureSelectLimitIntensity selector = new FeatureSelectNBest<>(new SampleIntensityImage.I16()); GeneralFeatureDetector detector = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); detector.setFeatureLimit(maxFeatures); @@ -93,22 +93,25 @@ FeaturePyramid hessianPyramid( int extractRadius, */ public static , D extends ImageGray> FeaturePyramid harrisPyramid( int extractRadius, - float detectThreshold, - int maxFeatures, - Class imageType, - Class derivType ) { + float detectThreshold, + int maxFeatures, + Class imageType, + Class derivType ) { + + AnyImageDerivative deriv = GImageDerivativeOps.derivativeForScaleSpace(imageType, derivType); GradientCornerIntensity harris = FactoryIntensityPointAlg.harris(extractRadius, 0.04f, false, derivType); + + float intensityScale = harris.thresholdScaleByDerivative(deriv.getEdgeDivisor()); + GeneralFeatureIntensity intensity = new WrapperGradientCornerIntensity<>(harris); NonMaxSuppression extractorMin = intensity.localMinimums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true), intensityScale) : null; NonMaxSuppression extractorMax = intensity.localMaximums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true), intensityScale) : null; FeatureSelectLimitIntensity selector = new FeatureSelectNBest<>(new SampleIntensityImage.I16()); GeneralFeatureDetector detector = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); detector.setFeatureLimit(maxFeatures); - AnyImageDerivative deriv = GImageDerivativeOps.derivativeForScaleSpace(imageType, derivType); - return new FeaturePyramid<>(detector, deriv, 2); } @@ -124,21 +127,23 @@ FeaturePyramid harrisPyramid( int extractRadius, */ public static , D extends ImageGray> FeatureLaplacePyramid hessianLaplace( int extractRadius, - float detectThreshold, - int maxFeatures, - Class imageType, - Class derivType ) { + float detectThreshold, + int maxFeatures, + Class imageType, + Class derivType ) { + AnyImageDerivative deriv = GImageDerivativeOps.derivativeForScaleSpace(imageType, derivType); GeneralFeatureIntensity intensity = new WrapperHessianDerivBlobIntensity<>(HessianBlobIntensity.Type.DETERMINANT, derivType); + + float intensityScale = intensity.thresholdScaleByDerivative(deriv.getEdgeDivisor()); + NonMaxSuppression extractorMin = intensity.localMinimums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true), intensityScale) : null; NonMaxSuppression extractorMax = intensity.localMaximums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true), intensityScale) : null; FeatureSelectLimitIntensity selector = new FeatureSelectNBest<>(new SampleIntensityImage.I16()); GeneralFeatureDetector detector = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); detector.setFeatureLimit(maxFeatures); - AnyImageDerivative deriv = GImageDerivativeOps.derivativeForScaleSpace(imageType, derivType); - ImageFunctionSparse sparseLaplace = FactoryDerivativeSparse.createLaplacian(imageType, null); return new FeatureLaplacePyramid<>(detector, sparseLaplace, deriv, 2); @@ -156,21 +161,25 @@ FeatureLaplacePyramid hessianLaplace( int extractRadius, */ public static , D extends ImageGray> FeatureLaplacePyramid harrisLaplace( int extractRadius, - float detectThreshold, - int maxFeatures, - Class imageType, - Class derivType ) { + float detectThreshold, + int maxFeatures, + Class imageType, + Class derivType ) { + AnyImageDerivative deriv = GImageDerivativeOps.derivativeForScaleSpace(imageType, derivType); GradientCornerIntensity harris = FactoryIntensityPointAlg.harris(extractRadius, 0.04f, false, derivType); GeneralFeatureIntensity intensity = new WrapperGradientCornerIntensity<>(harris); + + float intensityScale = intensity.thresholdScaleByDerivative(deriv.getEdgeDivisor()); + NonMaxSuppression extractorMin = intensity.localMinimums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.min(extractRadius, detectThreshold, extractRadius, true), intensityScale) : null; NonMaxSuppression extractorMax = intensity.localMaximums() ? FactoryFeatureExtractor.nonmax( - ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true)) : null; + ConfigExtract.max(extractRadius, detectThreshold, extractRadius, true), intensityScale) : null; FeatureSelectLimitIntensity selector = new FeatureSelectNBest<>(new SampleIntensityImage.I16()); GeneralFeatureDetector detector = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); detector.setFeatureLimit(maxFeatures); - AnyImageDerivative deriv = GImageDerivativeOps.derivativeForScaleSpace(imageType, derivType); + ImageFunctionSparse sparseLaplace = FactoryDerivativeSparse.createLaplacian(imageType, null); return new FeatureLaplacePyramid<>(detector, sparseLaplace, deriv, 2); @@ -191,7 +200,7 @@ FastHessianFeatureDetector fastHessian( @Nullable ConfigFastHessian config ) config.checkValidity(); // ignore border is overwritten by Fast Hessian at detection time - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(config.extract); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(config.extract, 1); FeatureSelectLimitIntensity limitLevels = FactorySelectLimit.intensity(config.selector); FeatureSelectLimitIntensity limitAll = FactorySelectLimit.intensity(config.selector); @@ -213,7 +222,7 @@ public static SiftDetector sift( @Nullable ConfigSiftDetector configDetector ) { configDetector = new ConfigSiftDetector(); NonMaxLimiter nonmax = FactoryFeatureExtractor.nonmaxLimiter( - configDetector.extract, configDetector.selector, configDetector.maxFeaturesPerScale); + configDetector.extract, 1, configDetector.selector, configDetector.maxFeaturesPerScale); FeatureSelectLimitIntensity selectorAll = FactorySelectLimit.intensity(configDetector.selector); final var alg = new SiftDetector(selectorAll, configDetector.edgeR, nonmax); alg.maxFeaturesAll = configDetector.maxFeaturesAll; diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/line/FactoryDetectLineAlgs.java b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/line/FactoryDetectLineAlgs.java index 31eca0cc1b..9213621a83 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/line/FactoryDetectLineAlgs.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/feature/detect/line/FactoryDetectLineAlgs.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -55,8 +55,8 @@ public class FactoryDetectLineAlgs { */ public static , D extends ImageGray> DetectLineSegmentsGridRansac lineRansac( @Nullable ConfigLineRansac config, - Class imageType, - Class derivType ) { + Class imageType, + Class derivType ) { if (config == null) config = new ConfigLineRansac(); @@ -100,7 +100,7 @@ DetectLineSegmentsGridRansac lineRansac( @Nullable ConfigLineRansac config */ public static > DetectLineHoughFootSubimage houghFootSub( @Nullable ConfigHoughFootSubimage config, - Class derivType ) { + Class derivType ) { if (config == null) config = new ConfigHoughFootSubimage(); @@ -113,10 +113,10 @@ DetectLineHoughFootSubimage houghFootSub( @Nullable ConfigHoughFootSubimage c public static > HoughTransformGradient houghLineFoot( ConfigHoughGradient configHough, ConfigParamFoot configParam, - Class derivType ) { + Class derivType ) { HoughParametersFootOfNorm param = new HoughParametersFootOfNorm(configParam.minDistanceFromOrigin); NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax( - new ConfigExtract(configHough.localMaxRadius, configHough.minCounts, 0, true)); + new ConfigExtract(configHough.localMaxRadius, configHough.minCounts, 0, true), 1); HoughTransformGradient hough; if (BoofConcurrency.USE_CONCURRENT) { @@ -135,10 +135,10 @@ HoughTransformGradient houghLineFoot( ConfigHoughGradient configHough, Config public static > HoughTransformGradient houghLinePolar( ConfigHoughGradient configHough, ConfigParamPolar configParam, - Class derivType ) { + Class derivType ) { HoughParametersPolar param = new HoughParametersPolar(configParam.resolutionRange, configParam.numBinsAngle); NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax( - new ConfigExtract(configHough.localMaxRadius, configHough.minCounts, 0, true)); + new ConfigExtract(configHough.localMaxRadius, configHough.minCounts, 0, true), 1); HoughTransformGradient hough; if (BoofConcurrency.USE_CONCURRENT) { @@ -158,7 +158,7 @@ HoughTransformGradient houghLinePolar( ConfigHoughGradient configHough, Confi public static HoughTransformBinary houghLinePolar( ConfigHoughBinary configHough, ConfigParamPolar configParam ) { HoughParametersPolar param = new HoughParametersPolar(configParam.resolutionRange, configParam.numBinsAngle); NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax( - new ConfigExtract(configHough.localMaxRadius, 0, 0, false)); + new ConfigExtract(configHough.localMaxRadius, 0, 0, false), 1); HoughTransformBinary hough; if (BoofConcurrency.USE_CONCURRENT) { diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryPointTracker.java b/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryPointTracker.java index f6fb80225c..699e68fb74 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryPointTracker.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryPointTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -40,6 +40,7 @@ import boofcv.alg.feature.detect.intensity.GradientCornerIntensity; import boofcv.alg.feature.detect.intensity.ShiTomasiCornerIntensity; import boofcv.alg.feature.detect.interest.GeneralFeatureDetector; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.filter.derivative.GImageDerivativeOps; import boofcv.alg.interpolate.InterpolateRectangle; import boofcv.alg.tracker.dda.DetectDescribeAssociateTracker; @@ -94,8 +95,11 @@ public class FactoryPointTracker { */ public static , D extends ImageGray> PointTracker tracker( ConfigPointTracker config, Class imageType, @Nullable Class derivType ) { + DerivativeType derivKernel = DerivativeType.SOBEL; + ImageGradient gradient = FactoryDerivative.gradientSB(derivKernel, imageType, derivType); + if (config.typeTracker == ConfigPointTracker.TrackerType.KLT) { - return klt(config.klt, config.detDesc.detectPoint, imageType, derivType); + return klt(config.klt, derivKernel, config.detDesc.detectPoint, imageType, derivType); } DetectDescribePoint detDesc = FactoryDetectDescribe.generic(config.detDesc, imageType); @@ -104,7 +108,7 @@ PointTracker tracker( ConfigPointTracker config, Class imageType, @Nullabl return switch (config.typeTracker) { case DDA -> FactoryPointTracker.dda(detDesc, associate, config.dda); case HYBRID -> FactoryPointTracker.hybrid( - detDesc, associate, config.detDesc.findNonMaxRadius(), config.klt, config.hybrid, imageType); + detDesc, associate, gradient, config.detDesc.findNonMaxRadius(), config.klt, config.hybrid, imageType); default -> throw new RuntimeException("BUG! KLT all trackers should have been handled already"); }; } @@ -122,13 +126,15 @@ PointTracker tracker( ConfigPointTracker config, Class imageType, @Nullabl */ @Deprecated public static , D extends ImageGray> - PointTracker klt( int numLevels, @Nullable ConfigPointDetector configDetect, int featureRadius, - Class imageType, Class derivType ) { - ConfigPKlt config = new ConfigPKlt(); + PointTracker klt( int numLevels, + @Nullable ConfigPointDetector configDetect, + int featureRadius, + Class imageType, Class derivType ) { + var config = new ConfigPKlt(); config.pyramidLevels = ConfigDiscreteLevels.levels(numLevels); config.templateRadius = featureRadius; - return klt(config, configDetect, imageType, derivType); + return klt(config, DerivativeType.SOBEL, configDetect, imageType, derivType); } /** @@ -140,8 +146,10 @@ PointTracker klt( int numLevels, @Nullable ConfigPointDetector configDetect, * @see boofcv.alg.tracker.klt.PyramidKltTracker */ public static , D extends ImageGray> - PointTrackerKltPyramid klt( @Nullable ConfigPKlt config, @Nullable ConfigPointDetector configDetect, - Class imageType, @Nullable Class derivType ) { + PointTrackerKltPyramid klt( @Nullable ConfigPKlt config, + @Nullable DerivativeType derivativeType, + @Nullable ConfigPointDetector configDetect, + Class imageType, @Nullable Class derivType ) { if (derivType == null) derivType = GImageDerivativeOps.getDerivativeType(imageType); @@ -149,6 +157,9 @@ PointTrackerKltPyramid klt( @Nullable ConfigPKlt config, @Nullable ConfigP if (config == null) { config = new ConfigPKlt(); } + if (derivativeType == null) { + derivativeType = DerivativeType.SOBEL; + } config.checkValidity(); if (configDetect == null) { @@ -157,12 +168,12 @@ PointTrackerKltPyramid klt( @Nullable ConfigPKlt config, @Nullable ConfigP } configDetect.checkValidity(); - GeneralFeatureDetector detector = FactoryDetectPoint.create(configDetect, imageType, derivType); - InterpolateRectangle interpInput = FactoryInterpolation.bilinearRectangle(imageType); InterpolateRectangle interpDeriv = FactoryInterpolation.bilinearRectangle(derivType); - ImageGradient gradient = FactoryDerivative.sobel(imageType, derivType); + ImageGradient gradient = FactoryDerivative.gradientSB(derivativeType, imageType, derivType); + + GeneralFeatureDetector detector = FactoryDetectPoint.create(configDetect, imageType, derivType, gradient.divisor()); PyramidDiscrete pyramid = FactoryPyramid.discreteGaussian(config.pyramidLevels, -1, 2, true, ImageType.single(imageType)); @@ -195,9 +206,9 @@ PointTrackerKltPyramid klt( @Nullable ConfigPKlt config, @Nullable ConfigP @Deprecated public static > PointTracker dda_FH_SURF_Fast( ConfigFastHessian configDetector, - ConfigSurfDescribe.Fast configDescribe, - ConfigAverageIntegral configOrientation, - Class imageType ) { + ConfigSurfDescribe.Fast configDescribe, + ConfigAverageIntegral configOrientation, + Class imageType ) { ScoreAssociation score = FactoryAssociation.scoreEuclidean(TupleDesc_F64.class, true); AssociateDescription assoc = FactoryAssociation.greedy(new ConfigAssociateGreedy(true, 5), score); @@ -224,9 +235,9 @@ PointTracker dda_FH_SURF_Fast( ConfigFastHessian configDetector, @Deprecated public static > PointTracker dda_FH_SURF_Stable( ConfigFastHessian configDetector, - ConfigSurfDescribe.Stability configDescribe, - ConfigSlidingIntegral configOrientation, - Class imageType ) { + ConfigSurfDescribe.Stability configDescribe, + ConfigSlidingIntegral configOrientation, + Class imageType ) { ScoreAssociation score = FactoryAssociation.scoreEuclidean(TupleDesc_F64.class, true); AssociateDescription assoc = FactoryAssociation.greedy(new ConfigAssociateGreedy(true, 5), score); @@ -251,8 +262,9 @@ PointTracker dda_FH_SURF_Stable( ConfigFastHessian configDetector, @Deprecated public static , D extends ImageGray> PointTracker dda_ST_BRIEF( int maxAssociationError, - ConfigGeneralDetector configExtract, - Class imageType, Class derivType ) { + DerivativeType derivativeType, + ConfigGeneralDetector configExtract, + Class imageType, Class derivType ) { if (derivType == null) derivType = GImageDerivativeOps.getDerivativeType(imageType); @@ -261,7 +273,8 @@ PointTracker dda_ST_BRIEF( int maxAssociationError, FactoryBlurFilter.gaussian(ImageType.single(imageType), 0, 4)); DescribePointRadiusAngle describeBrief = new DescribeBrief_RadiusAngle<>(brief, imageType); - GeneralFeatureDetector detectPoint = createShiTomasi(configExtract, derivType); + ImageGradient gradient = FactoryDerivative.gradientSB(derivativeType, imageType, derivType); + GeneralFeatureDetector detectPoint = createShiTomasi(configExtract, derivType, gradient.divisor()); ScoreAssociateHamming_B score = new ScoreAssociateHamming_B(); @@ -290,9 +303,9 @@ PointTracker dda_ST_BRIEF( int maxAssociationError, @Deprecated public static , D extends ImageGray> PointTracker dda_FAST_BRIEF( ConfigFastCorner configFast, - ConfigGeneralDetector configExtract, - int maxAssociationError, - Class imageType ) { + ConfigGeneralDetector configExtract, + int maxAssociationError, + Class imageType ) { DescribePointBrief brief = FactoryDescribeAlgs. brief(FactoryBriefDefinition.gaussian2(new Random(123), 16, 512), FactoryBlurFilter.gaussian(ImageType.single(imageType), 0, 4)); @@ -324,8 +337,9 @@ PointTracker dda_FAST_BRIEF( ConfigFastCorner configFast, */ @Deprecated public static , D extends ImageGray> - PointTracker dda_ST_NCC( ConfigGeneralDetector configExtract, int describeRadius, - Class imageType, @Nullable Class derivType ) { + PointTracker dda_ST_NCC( ConfigGeneralDetector configExtract, DerivativeType derivativeType, + int describeRadius, + Class imageType, @Nullable Class derivType ) { if (derivType == null) derivType = GImageDerivativeOps.getDerivativeType(imageType); @@ -335,7 +349,9 @@ PointTracker dda_ST_NCC( ConfigGeneralDetector configExtract, int describeRad DescribePointPixelRegionNCC ncc = FactoryDescribeAlgs.pixelRegionNCC(w, w, imageType); DescribePointRadiusAngle describeNCC = new DescribeNCC_RadiusAngle(ncc, imageType); - GeneralFeatureDetector corner = createShiTomasi(configExtract, derivType); + ImageGradient gradient = FactoryDerivative.gradientSB(derivativeType, imageType, derivType); + + GeneralFeatureDetector corner = createShiTomasi(configExtract, derivType, gradient.divisor()); ScoreAssociateNccFeature score = new ScoreAssociateNccFeature(); @@ -363,10 +379,10 @@ PointTracker dda_ST_NCC( ConfigGeneralDetector configExtract, int describeRad */ public static , Desc extends TupleDesc> DetectDescribeAssociateTracker dda( InterestPointDetector detector, - OrientationImage orientation, - DescribePointRadiusAngle describe, - AssociateDescription2D associate, - ConfigTrackerDda config ) { + OrientationImage orientation, + DescribePointRadiusAngle describe, + AssociateDescription2D associate, + ConfigTrackerDda config ) { DetectDescribeFusion fused = new DetectDescribeFusion<>(detector, orientation, describe); return new DetectDescribeAssociateTracker<>(fused, associate, config); @@ -374,8 +390,8 @@ DetectDescribeAssociateTracker dda( InterestPointDetector detector, public static , Desc extends TupleDesc> PointTrackerDda dda( DetectDescribePoint detDesc, - AssociateDescription2D associate, - ConfigTrackerDda config ) { + AssociateDescription2D associate, + ConfigTrackerDda config ) { return new PointTrackerDda<>(new DetectDescribeAssociateTracker<>(detDesc, associate, config)); } @@ -394,11 +410,15 @@ PointTrackerDda dda( DetectDescribePoint detDesc, @Deprecated public static > PointTracker combined_FH_SURF_KLT( ConfigPKlt kltConfig, - ConfigTrackerHybrid configHybrid, - ConfigFastHessian configDetector, - ConfigSurfDescribe.Stability configDescribe, - ConfigSlidingIntegral configOrientation, - Class imageType ) { + DerivativeType derivativeType, + ConfigTrackerHybrid configHybrid, + ConfigFastHessian configDetector, + ConfigSurfDescribe.Stability configDescribe, + ConfigSlidingIntegral configOrientation, + Class imageType ) { + + + ImageGradient gradient = FactoryDerivative.gradientSB(derivativeType, imageType, null); ScoreAssociation score = FactoryAssociation.defaultScore(TupleDesc_F64.class); AssociateDescription assoc = FactoryAssociation. @@ -408,7 +428,7 @@ PointTracker combined_FH_SURF_KLT( ConfigPKlt kltConfig, DetectDescribePoint fused = FactoryDetectDescribe.surfStable(configDetector, configDescribe, configOrientation, imageType); - return hybrid(fused, associate2D, configDetector.extract.radius, kltConfig, configHybrid, imageType); + return hybrid(fused, associate2D, gradient, configDetector.extract.radius, kltConfig, configHybrid, imageType); } /** @@ -427,17 +447,20 @@ PointTracker combined_FH_SURF_KLT( ConfigPKlt kltConfig, @Deprecated public static , D extends ImageGray> PointTracker combined_ST_SURF_KLT( ConfigGeneralDetector configExtract, - ConfigPKlt kltConfig, - ConfigTrackerHybrid configHybrid, - ConfigSurfDescribe.Stability configDescribe, - ConfigSlidingIntegral configOrientation, - Class imageType, - @Nullable Class derivType ) { + DerivativeType derivativeType, + ConfigPKlt kltConfig, + ConfigTrackerHybrid configHybrid, + ConfigSurfDescribe.Stability configDescribe, + ConfigSlidingIntegral configOrientation, + Class imageType, + @Nullable Class derivType ) { if (derivType == null) derivType = GImageDerivativeOps.getDerivativeType(imageType); - GeneralFeatureDetector corner = createShiTomasi(configExtract, derivType); + ImageGradient gradient = FactoryDerivative.gradientSB(derivativeType, imageType, derivType); + + GeneralFeatureDetector corner = createShiTomasi(configExtract, derivType, gradient.divisor()); InterestPointDetector detector = FactoryInterestPoint.wrapPoint(corner, 1, imageType, derivType); DescribePointRadiusAngle regionDesc @@ -456,7 +479,7 @@ PointTracker combined_ST_SURF_KLT( ConfigGeneralDetector configExtract, orientation = FactoryOrientation.convertImage(orientationII, imageType); } - return hybrid(detector, orientation, regionDesc, associate2D, configExtract.radius, + return hybrid(detector, gradient, orientation, regionDesc, associate2D, configExtract.radius, kltConfig, configHybrid, imageType); } @@ -471,18 +494,19 @@ PointTracker combined_ST_SURF_KLT( ConfigGeneralDetector configExtract, * @param imageType Input image type. @return Feature tracker * @see HybridTrackerScalePoint */ - public static , Desc extends TupleDesc> + public static , D extends ImageGray, Desc extends TupleDesc> PointTracker hybrid( InterestPointDetector detector, - @Nullable OrientationImage orientation, - DescribePointRadiusAngle describe, - AssociateDescription2D associate, - int tooCloseRadius, - ConfigPKlt kltConfig, - ConfigTrackerHybrid configHybrid, - Class imageType ) { + ImageGradient gradient, + @Nullable OrientationImage orientation, + DescribePointRadiusAngle describe, + AssociateDescription2D associate, + int tooCloseRadius, + ConfigPKlt kltConfig, + ConfigTrackerHybrid configHybrid, + Class imageType ) { DetectDescribeFusion fused = new DetectDescribeFusion<>(detector, orientation, describe); - return hybrid(fused, associate, tooCloseRadius, kltConfig, configHybrid, imageType); + return hybrid(fused, associate, gradient, tooCloseRadius, kltConfig, configHybrid, imageType); } /** @@ -496,11 +520,12 @@ PointTracker hybrid( InterestPointDetector detector, */ public static , D extends ImageGray, Desc extends TupleDesc> PointTracker hybrid( DetectDescribePoint detector, - AssociateDescription2D associate, - int tooCloseRadius, - ConfigPKlt kltConfig, - ConfigTrackerHybrid configHybrid, - Class imageType ) { + AssociateDescription2D associate, + ImageGradient gradient, + int tooCloseRadius, + ConfigPKlt kltConfig, + ConfigTrackerHybrid configHybrid, + Class imageType ) { Class derivType = GImageDerivativeOps.getDerivativeType(imageType); if (kltConfig == null) { @@ -513,20 +538,20 @@ PointTracker hybrid( DetectDescribePoint detector, } HybridTrackerScalePoint tracker = FactoryTrackerAlg. - hybrid(detector, associate, tooCloseRadius, kltConfig, configHybrid, imageType, derivType); + hybrid(detector, associate, tooCloseRadius, kltConfig, configHybrid, imageType, derivType, gradient.divisor()); tracker.rand = new Random(configHybrid.seed); - var pointHybrid = new PointTrackerHybrid<>(tracker, kltConfig.pyramidLevels, imageType, derivType); + var pointHybrid = new PointTrackerHybrid<>(tracker, gradient, kltConfig.pyramidLevels, imageType, derivType); pointHybrid.thresholdRespawn.setTo(configHybrid.thresholdRespawn); return pointHybrid; } public static , D extends ImageGray, Desc extends TupleDesc> PointTracker dda( GeneralFeatureDetector detector, - DescribePointRadiusAngle describe, - AssociateDescription2D associate, - double scale, - Class imageType ) { + DescribePointRadiusAngle describe, + AssociateDescription2D associate, + double scale, + Class imageType ) { InterestPointDetector detectInterest = FactoryInterestPoint.wrapPoint(detector, scale, imageType, null); DetectDescribePoint fused = FactoryDetectDescribe.fuseTogether(detectInterest, null, describe); @@ -541,9 +566,10 @@ PointTracker dda( GeneralFeatureDetector detector, @Deprecated public static , D extends ImageGray> GeneralFeatureDetector createShiTomasi( ConfigGeneralDetector config, - Class derivType ) { + Class derivType, + int derivDivisor ) { GradientCornerIntensity cornerIntensity = FactoryIntensityPointAlg.shiTomasi(1, false, derivType); - return FactoryDetectPoint.createGeneral(cornerIntensity, config); + return FactoryDetectPoint.createGeneral(cornerIntensity, config, derivDivisor); } } diff --git a/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryTrackerAlg.java b/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryTrackerAlg.java index 0d57c48072..803362deb3 100644 --- a/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryTrackerAlg.java +++ b/main/boofcv-feature/src/main/java/boofcv/factory/tracker/FactoryTrackerAlg.java @@ -56,7 +56,7 @@ public class FactoryTrackerAlg { * @return Tracker */ public static , D extends ImageGray> - KltTracker klt( @Nullable ConfigKlt config, Class imageType, Class derivType ) { + KltTracker klt( @Nullable ConfigKlt config, Class imageType, @Nullable Class derivType ) { if (config == null) config = new ConfigKlt(); if (derivType == null) @@ -83,7 +83,7 @@ KltTracker klt( @Nullable ConfigKlt config, Class imageType, Class d public static , D extends ImageGray> PyramidKltTracker kltPyramid( @Nullable ConfigKlt config, Class imageType, - Class derivType ) { + @Nullable Class derivType ) { if (config == null) config = new ConfigKlt(); if (derivType == null) @@ -99,7 +99,7 @@ PyramidKltTracker kltPyramid( @Nullable ConfigKlt config, public static , D extends ImageGray> EasyPyramidKlt kltEasy( @Nullable ConfigEasyKlt config, Class imageType, - Class derivType ) { + @Nullable Class derivType ) { if (config == null) config = new ConfigEasyKlt(); if (derivType == null) @@ -132,7 +132,7 @@ HybridTrackerScalePoint hybrid( DetectDescribePoint detecto @Nullable ConfigPKlt kltConfig, @Nullable ConfigTrackerHybrid configHybrid, Class imageType, - @Nullable Class derivType ) { + @Nullable Class derivType, int derivDivisor ) { if (configHybrid == null) configHybrid = new ConfigTrackerHybrid(); if (kltConfig == null) @@ -141,7 +141,7 @@ HybridTrackerScalePoint hybrid( DetectDescribePoint detecto derivType = GImageDerivativeOps.getDerivativeType(imageType); PyramidKltForHybrid klt = new PyramidKltForHybrid<>(kltConfig.config, - kltConfig.templateRadius, imageType, derivType); + kltConfig.templateRadius, imageType, derivType, derivDivisor); var tracker = new HybridTrackerScalePoint<>(klt, detector, associate, tooCloseRadius); tracker.maxInactiveTracks = configHybrid.maxInactiveTracks; diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/extract/TestNonMaxLimiter.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/extract/TestNonMaxLimiter.java index d0768b5595..20fa888856 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/extract/TestNonMaxLimiter.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/extract/TestNonMaxLimiter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,7 +22,6 @@ import boofcv.factory.feature.detect.extract.FactoryFeatureExtractor; import boofcv.factory.feature.detect.selector.ConfigSelectLimit; import boofcv.factory.feature.detect.selector.FactorySelectLimit; -import boofcv.struct.QueueCorner; import boofcv.struct.image.GrayF32; import boofcv.testing.BoofStandardJUnit; import org.ddogleg.struct.FastAccess; @@ -39,7 +38,7 @@ public class TestNonMaxLimiter extends BoofStandardJUnit { intensity.set(1, 15, -20); intensity.set(1, 20, -25); - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0, 0, true, true, true)); + var extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0, 0, true, true, true), 1); FeatureSelectLimitIntensity selector = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); var limiter = new NonMaxLimiter(extractor, selector, 20); @@ -67,7 +66,7 @@ public class TestNonMaxLimiter extends BoofStandardJUnit { intensity.set(1, 15, -20); intensity.set(1, 20, -25); - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0, 0, true, true, true)); + var extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0, 0, true, true, true), 1); FeatureSelectLimitIntensity selector = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); NonMaxLimiter limiter = new NonMaxLimiter(extractor, selector, 2); diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/ChecksGeneralFeatureIntensity.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/ChecksGeneralFeatureIntensity.java index 05288732b8..b0ab3cf9be 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/ChecksGeneralFeatureIntensity.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/ChecksGeneralFeatureIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,15 +19,18 @@ package boofcv.abst.feature.detect.intensity; import boofcv.alg.misc.GImageMiscOps; +import boofcv.alg.misc.GPixelMath; import boofcv.core.image.GeneralizedImageOps; import boofcv.struct.image.GrayF32; import boofcv.struct.image.ImageGray; +import boofcv.struct.image.ImageType; import boofcv.testing.BoofStandardJUnit; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -36,23 +39,76 @@ * @author Peter Abeles */ public abstract class ChecksGeneralFeatureIntensity, D extends ImageGray> - extends BoofStandardJUnit -{ + extends BoofStandardJUnit { public List listInputTypes = new ArrayList<>(); public List listDerivTypes = new ArrayList<>(); int width = 30; int height = 40; + // Range of values in uniform distribution + int imageMin = 0; + int imageMax = 255; + int derivMin = -100; + int derivMax = 100; + I input; - D derivX,derivY,derivXX,derivYY,derivXY; + D derivX, derivY, derivXX, derivYY, derivXY; + + public void addTypes( Class inputType, Class derivType ) { + listInputTypes.add(inputType); + listDerivTypes.add(derivType); + } - public void addTypes( Class inputType , Class derivType ) { - listInputTypes.add( inputType ); - listDerivTypes.add( derivType ); + public abstract GeneralFeatureIntensity createAlg( Class imageType, Class derivType ); + + /// See if it correctly computes the response to the gradient being scaled + @Test void gradientScale() { + for (int i = 0; i < listInputTypes.size(); i++) { + gradientScale(listInputTypes.get(i), listDerivTypes.get(i)); + } } - public abstract GeneralFeatureIntensity createAlg(Class imageType, Class derivType); + public void gradientScale( Class imageType, Class derivType ) { + // change RNG to avoid overflow when scaled in 8-bit images + imageMax = 50; + derivMin = -25; + derivMax = 25; + + randomInit(imageType, derivType, width, height); + + // Compute intensity before the derivatives are scaled + GeneralFeatureIntensity alg = createAlg(imageType, derivType); + alg.process(input, derivX, derivY, derivXX, derivYY, derivXY); + + GrayF32 before = new GrayF32().setTo(alg.getIntensity()); + + // Scale the gradients + int divisor = 2; + GPixelMath.multiply(derivX, divisor, derivX); + GPixelMath.multiply(derivY, divisor, derivY); + GPixelMath.multiply(derivXX, divisor, derivXX); + GPixelMath.multiply(derivYY, divisor, derivYY); + GPixelMath.multiply(derivXY, divisor, derivXY); + + alg.process(input, derivX, derivY, derivXX, derivYY, derivXY); + + // floating point images will never get their gradients scaled, so skip + if (!ImageType.single(derivType).getDataType().isInteger()) { + assertEquals(1.0, alg.thresholdScaleByDerivative(divisor)); + return; + } + + // If adjustment is 1 for an integer image that means it does not depend on the derivatives + float adjustment = alg.thresholdScaleByDerivative(divisor); + GrayF32 after = alg.getIntensity(); + + for (int y = 0; y < before.height; y++) { + for (int x = 0; x < before.width; x++) { + assertEquals(before.get(x,y)*adjustment, after.get(x,y), 0.1); + } + } + } /** * For features which do not process the image border, the border should have a response of zero. @@ -61,15 +117,16 @@ public void addTypes( Class inputType , Class derivType ) { */ @SuppressWarnings("unchecked") @Test void checkReshapeBorder() { - for( int i = 0; i < listInputTypes.size(); i++ ) { - checkReshapeBorder( listInputTypes.get(i), listDerivTypes.get(i)); + for (int i = 0; i < listInputTypes.size(); i++) { + checkReshapeBorder(listInputTypes.get(i), listDerivTypes.get(i)); } } - public void checkReshapeBorder( Class imageType , Class derivType ) { - randomInit(imageType,derivType,width, height); - GeneralFeatureIntensity alg = createAlg(imageType,derivType); - alg.process(input,derivX,derivY,derivXX,derivYY,derivXY); + public void checkReshapeBorder( Class imageType, Class derivType ) { + randomInit(imageType, derivType, width, height); + + GeneralFeatureIntensity alg = createAlg(imageType, derivType); + alg.process(input, derivX, derivY, derivXX, derivYY, derivXY); GrayF32 intensity = alg.getIntensity(); @@ -77,35 +134,35 @@ public void checkReshapeBorder( Class imageType , Class derivType ) { checkBorderZero(intensity, r); // process again with smaller images - randomInit(imageType,derivType,width-1, height-1); + randomInit(imageType, derivType, width - 1, height - 1); alg.process(input, derivX, derivY, derivXX, derivYY, derivXY); intensity = alg.getIntensity(); checkBorderZero(intensity, r); } - private void randomInit(Class imageType , Class derivType , int width, int height) { + private void randomInit( Class imageType, Class derivType, int width, int height ) { input = GeneralizedImageOps.createSingleBand(imageType, width, height); - derivX = GeneralizedImageOps.createSingleBand(derivType,width,height); - derivY = GeneralizedImageOps.createSingleBand(derivType,width,height); - derivXX = GeneralizedImageOps.createSingleBand(derivType,width,height); - derivYY = GeneralizedImageOps.createSingleBand(derivType,width,height); - derivXY = GeneralizedImageOps.createSingleBand(derivType,width,height); - - GImageMiscOps.fillUniform(input, rand, 0, 255); - GImageMiscOps.fillUniform(derivX, rand, -100, 100); - GImageMiscOps.fillUniform(derivY, rand, -100, 100); - GImageMiscOps.fillUniform(derivXX, rand, -100, 100); - GImageMiscOps.fillUniform(derivYY, rand, -100, 100); - GImageMiscOps.fillUniform(derivXY, rand, -100, 100); + derivX = GeneralizedImageOps.createSingleBand(derivType, width, height); + derivY = GeneralizedImageOps.createSingleBand(derivType, width, height); + derivXX = GeneralizedImageOps.createSingleBand(derivType, width, height); + derivYY = GeneralizedImageOps.createSingleBand(derivType, width, height); + derivXY = GeneralizedImageOps.createSingleBand(derivType, width, height); + + GImageMiscOps.fillUniform(input, rand, imageMin, imageMax); + GImageMiscOps.fillUniform(derivX, rand, derivMin, derivMax); + GImageMiscOps.fillUniform(derivY, rand, derivMin, derivMax); + GImageMiscOps.fillUniform(derivXX, rand, derivMin, derivMax); + GImageMiscOps.fillUniform(derivYY, rand, derivMin, derivMax); + GImageMiscOps.fillUniform(derivXY, rand, derivMin, derivMax); } - private void checkBorderZero(GrayF32 intensity, int r) { - for( int y = 0; y < intensity.height; y++ ) { - if( y >= r && y < intensity.height-r ) + private void checkBorderZero( GrayF32 intensity, int r ) { + for (int y = 0; y < intensity.height; y++) { + if (y >= r && y < intensity.height - r) continue; - for( int x = 0; x < intensity.width; x++ ) { - if( x >= r && x < intensity.width-r ) + for (int x = 0; x < intensity.width; x++) { + if (x >= r && x < intensity.width - r) continue; assertTrue(0 == intensity.get(x, y)); diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/TestWrapperHessianBlobIntensity.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/TestWrapperHessianBlobIntensity.java index 06613df9c2..753c2ada09 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/TestWrapperHessianBlobIntensity.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/intensity/TestWrapperHessianBlobIntensity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,16 +23,33 @@ import boofcv.struct.image.GrayF32; import boofcv.struct.image.GrayS16; import boofcv.struct.image.GrayU8; +import boofcv.testing.BoofStandardJUnit; +import org.junit.jupiter.api.Nested; @SuppressWarnings("unchecked") -public class TestWrapperHessianBlobIntensity extends ChecksGeneralFeatureIntensity{ - public TestWrapperHessianBlobIntensity() { - addTypes(GrayF32.class,GrayF32.class); - addTypes(GrayU8.class, GrayS16.class); +public class TestWrapperHessianBlobIntensity extends BoofStandardJUnit { + + @Nested class DETERMINANT extends ChecksGeneralFeatureIntensity { + public DETERMINANT() { + addTypes(GrayF32.class, GrayF32.class); + addTypes(GrayU8.class, GrayS16.class); + } + + @Override + public GeneralFeatureIntensity createAlg( Class imageType, Class derivType ) { + return FactoryIntensityPoint.hessian(HessianBlobIntensity.Type.DETERMINANT, derivType); + } } - @Override - public GeneralFeatureIntensity createAlg(Class imageType, Class derivType) { - return FactoryIntensityPoint.hessian(HessianBlobIntensity.Type.DETERMINANT, derivType); + @Nested class TRACE extends ChecksGeneralFeatureIntensity { + public TRACE() { + addTypes(GrayF32.class, GrayF32.class); + addTypes(GrayU8.class, GrayS16.class); + } + + @Override + public GeneralFeatureIntensity createAlg( Class imageType, Class derivType ) { + return FactoryIntensityPoint.hessian(HessianBlobIntensity.Type.TRACE, derivType); + } } } diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestGeneralToInterestPoint.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestGeneralToInterestPoint.java index 0ca1e34ffa..05e4fdd778 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestGeneralToInterestPoint.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestGeneralToInterestPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,7 +18,10 @@ package boofcv.abst.feature.detect.interest; +import boofcv.abst.filter.derivative.ImageGradient; import boofcv.alg.feature.detect.interest.GeneralFeatureDetector; +import boofcv.alg.filter.derivative.DerivativeType; +import boofcv.factory.filter.derivative.FactoryDerivative; import boofcv.struct.QueueCorner; import boofcv.struct.image.GrayF32; import boofcv.testing.BoofStandardJUnit; @@ -28,7 +31,9 @@ public class TestGeneralToInterestPoint extends BoofStandardJUnit { - GrayF32 input = new GrayF32(10,20); + GrayF32 input = new GrayF32(10, 20); + + ImageGradient gradient = FactoryDerivative.gradientSB(DerivativeType.SOBEL, GrayF32.class, null); /** * Several basic functionality tests @@ -36,17 +41,16 @@ public class TestGeneralToInterestPoint extends BoofStandardJUnit { @Test void various() { Helper detector = new Helper(); detector.maximum = true; - GeneralToInterestPoint alg = - new GeneralToInterestPoint<>(detector, 2.5); + var alg = new GeneralToInterestPoint<>(detector, gradient, 2.5); alg.detect(input); assertEquals(1, alg.getNumberOfSets()); - assertEquals(6,alg.getNumberOfFeatures()); - for( int i = 0; i < alg.getNumberOfFeatures(); i++) { - assertEquals(2.5, alg.getRadius(i),1e-8); - assertEquals(0, alg.getOrientation(i),1e-8); - assertEquals(0,alg.getSet(i)); + assertEquals(6, alg.getNumberOfFeatures()); + for (int i = 0; i < alg.getNumberOfFeatures(); i++) { + assertEquals(2.5, alg.getRadius(i), 1e-8); + assertEquals(0, alg.getOrientation(i), 1e-8); + assertEquals(0, alg.getSet(i)); } assertEquals(1, detector.calledProcess); @@ -60,76 +64,81 @@ public class TestGeneralToInterestPoint extends BoofStandardJUnit { Helper detector = new Helper(); // both turned off - var alg = new GeneralToInterestPoint<>(detector, 2.5); + var alg = new GeneralToInterestPoint<>(detector, gradient, 2.5); alg.detect(input); assertEquals(0, alg.getNumberOfSets()); assertEquals(0, alg.getNumberOfFeatures()); // just minimums detector.minimum = true; - alg = new GeneralToInterestPoint<>(detector, 2.5); + alg = new GeneralToInterestPoint<>(detector, gradient, 2.5); alg.detect(input); assertEquals(1, alg.getNumberOfSets()); assertEquals(5, alg.getNumberOfFeatures()); for (int i = 0; i < alg.getNumberOfFeatures(); i++) { - assertEquals(0,alg.getSet(i)); + assertEquals(0, alg.getSet(i)); } // both minimums and maximums detector.maximum = true; - alg = new GeneralToInterestPoint<>(detector, 2.5); + alg = new GeneralToInterestPoint<>(detector, gradient, 2.5); alg.detect(input); assertEquals(2, alg.getNumberOfSets()); - assertEquals(11,alg.getNumberOfFeatures()); + assertEquals(11, alg.getNumberOfFeatures()); for (int i = 0; i < 6; i++) { - assertEquals(0,alg.getSet(i)); + assertEquals(0, alg.getSet(i)); } for (int i = 6; i < alg.getNumberOfFeatures(); i++) { - assertEquals(1,alg.getSet(i)); + assertEquals(1, alg.getSet(i)); } // just maximums detector.minimum = false; - alg = new GeneralToInterestPoint<>(detector, 2.5); + alg = new GeneralToInterestPoint<>(detector, gradient, 2.5); alg.detect(input); assertEquals(1, alg.getNumberOfSets()); - assertEquals(6,alg.getNumberOfFeatures()); + assertEquals(6, alg.getNumberOfFeatures()); for (int i = 0; i < alg.getNumberOfFeatures(); i++) { - assertEquals(0,alg.getSet(i)); + assertEquals(0, alg.getSet(i)); } } - public static class Helper extends GeneralFeatureDetector { + public static class Helper extends GeneralFeatureDetector { public int calledProcess = 0; public boolean minimum = false; public boolean maximum = false; - @Override public void process(GrayF32 image, - GrayF32 derivX, GrayF32 derivY, - GrayF32 derivXX, GrayF32 derivYY, GrayF32 derivXY) { + @Override public void process( GrayF32 image, + GrayF32 derivX, GrayF32 derivY, + GrayF32 derivXX, GrayF32 derivYY, GrayF32 derivXY ) { calledProcess++; } @Override public QueueCorner getMinimums() { QueueCorner ret = new QueueCorner(); - for( int i = 0; i < 5; i++ ) - ret.append(1,2); + for (int i = 0; i < 5; i++) + ret.append(1, 2); return ret; } @Override public QueueCorner getMaximums() { QueueCorner ret = new QueueCorner(); - for( int i = 0; i < 6; i++ ) - ret.append(1,2); + for (int i = 0; i < 6; i++) + ret.append(1, 2); return ret; } - @Override public boolean isDetectMinimums() { return minimum; } - @Override public boolean isDetectMaximums() { return maximum; } - @Override public boolean getRequiresGradient() { return false; } - @Override public boolean getRequiresHessian() { return false; } - @Override public Class getImageType() { return GrayF32.class; } - @Override public Class getDerivType() { return GrayF32.class; } + @Override public boolean isDetectMinimums() {return minimum;} + + @Override public boolean isDetectMaximums() {return maximum;} + + @Override public boolean getRequiresGradient() {return false;} + + @Override public boolean getRequiresHessian() {return false;} + + @Override public Class getImageType() {return GrayF32.class;} + + @Override public Class getDerivType() {return GrayF32.class;} } } diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestWrapFHtoInterestPoint.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestWrapFHtoInterestPoint.java index 4f24b7f095..730eb5ebd8 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestWrapFHtoInterestPoint.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/interest/TestWrapFHtoInterestPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,16 +19,12 @@ package boofcv.abst.feature.detect.interest; import boofcv.abst.feature.detect.extract.ConfigExtract; -import boofcv.abst.feature.detect.extract.NonMaxSuppression; import boofcv.alg.feature.detect.interest.FastHessianFeatureDetector; -import boofcv.alg.feature.detect.selector.FeatureSelectLimitIntensity; import boofcv.factory.feature.detect.extract.FactoryFeatureExtractor; import boofcv.factory.feature.detect.selector.ConfigSelectLimit; import boofcv.factory.feature.detect.selector.FactorySelectLimit; -import boofcv.struct.feature.ScalePoint; import boofcv.struct.image.GrayU8; import boofcv.testing.BoofStandardJUnit; -import georegression.struct.point.Point2D_I16; import org.junit.jupiter.api.Nested; @SuppressWarnings("unchecked") @@ -37,9 +33,9 @@ public class TestWrapFHtoInterestPoint extends BoofStandardJUnit { @Nested public class Standard extends GeneralInterestPointDetectorChecks { public Standard() { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(2, 1, 5, true)); - FeatureSelectLimitIntensity limitLevels = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); - FeatureSelectLimitIntensity limitAll = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); + var extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(2, 1, 5, true), 1); + var limitLevels = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); + var limitAll = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); var detector = new FastHessianFeatureDetector(extractor,limitLevels,limitAll, 1,9, 4,4, 6); detector.maxFeaturesPerScale = 150; diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughBinary_to_DetectLine.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughBinary_to_DetectLine.java index e7ef846a16..647caaf9de 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughBinary_to_DetectLine.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughBinary_to_DetectLine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,11 +19,9 @@ package boofcv.abst.feature.detect.line; import boofcv.abst.feature.detect.extract.ConfigExtract; -import boofcv.abst.feature.detect.extract.NonMaxSuppression; import boofcv.abst.filter.binary.InputToBinary; import boofcv.alg.feature.detect.line.HoughParametersPolar; import boofcv.alg.feature.detect.line.HoughTransformBinary; -import boofcv.alg.feature.detect.line.HoughTransformParameters; import boofcv.factory.feature.detect.extract.FactoryFeatureExtractor; import boofcv.factory.filter.binary.FactoryThresholdBinary; import boofcv.struct.image.ImageGray; @@ -31,9 +29,9 @@ class TestHoughBinary_to_DetectLine extends GeneralDetectLineGradientTests { @Override public > DetectLine createAlg(Class imageType) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 2, 0, true)); - HoughTransformParameters polar = new HoughParametersPolar(2,180); - HoughTransformBinary hough = new HoughTransformBinary(extractor,polar); + var extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 2, 0, true), 1); + var polar = new HoughParametersPolar(2,180); + var hough = new HoughTransformBinary(extractor,polar); InputToBinary thresholder = FactoryThresholdBinary.globalOtsu(0,255,1.0,true,imageType); diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughGradient_to_DetectLine.java b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughGradient_to_DetectLine.java index 52900f308c..8a9b1ce626 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughGradient_to_DetectLine.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/feature/detect/line/TestHoughGradient_to_DetectLine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -19,11 +19,9 @@ package boofcv.abst.feature.detect.line; import boofcv.abst.feature.detect.extract.ConfigExtract; -import boofcv.abst.feature.detect.extract.NonMaxSuppression; import boofcv.abst.filter.derivative.ImageGradient; import boofcv.alg.feature.detect.line.HoughParametersPolar; import boofcv.alg.feature.detect.line.HoughTransformGradient; -import boofcv.alg.feature.detect.line.HoughTransformParameters; import boofcv.alg.filter.derivative.DerivativeType; import boofcv.factory.feature.detect.extract.FactoryFeatureExtractor; import boofcv.factory.filter.derivative.FactoryDerivative; @@ -33,9 +31,9 @@ public class TestHoughGradient_to_DetectLine extends GeneralDetectLineGradientTests { @Override public > DetectLine createAlg(Class imageType) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 2, 0, true)); - HoughTransformParameters polar = new HoughParametersPolar(2,180); - HoughTransformGradient hough = new HoughTransformGradient(extractor,polar,imageType); + var extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 2, 0, true), 1); + var polar = new HoughParametersPolar(2,180); + var hough = new HoughTransformGradient(extractor,polar,imageType); ImageGradient gradient = FactoryDerivative.gradient(DerivativeType.SOBEL, ImageType.single(imageType),null); return new HoughGradient_to_DetectLine(hough,gradient,imageType); diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid.java b/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid.java index 69f1122a04..3e2497476b 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,6 +22,7 @@ import boofcv.abst.feature.detect.interest.ConfigPointDetector; import boofcv.abst.feature.detect.interest.PointDetectorTypes; import boofcv.abst.tracker.PointTrackerKltPyramid.PointTrackMod; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.misc.GImageMiscOps; import boofcv.alg.tracker.klt.*; import boofcv.concurrency.BoofConcurrency; @@ -185,7 +186,7 @@ private PointTrackerKltPyramid createKLT( ConfigPKlt config ) // Turn off threading since this should only test the single thread implementation BoofConcurrency.setMaxThreads(0); - return FactoryPointTracker.klt(config, configDetector, GrayF32.class, GrayF32.class); + return FactoryPointTracker.klt(config, DerivativeType.SOBEL, configDetector, GrayF32.class, GrayF32.class); } /** diff --git a/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid_MT.java b/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid_MT.java index 79272791e1..5bfeaf8ed5 100644 --- a/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid_MT.java +++ b/main/boofcv-feature/src/test/java/boofcv/abst/tracker/TestPointTrackerKltPyramid_MT.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -21,6 +21,7 @@ import boofcv.abst.feature.detect.interest.ConfigPointDetector; import boofcv.abst.feature.detect.interest.PointDetectorTypes; import boofcv.abst.tracker.PointTrackerKltPyramid.PointTrackMod; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.misc.ImageMiscOps; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.alg.tracker.klt.PyramidKltFeature; @@ -57,14 +58,14 @@ private PointTrackerKltPyramid createKLT( ConfigPKlt config ) // make sure threads are turned on BoofConcurrency.USE_CONCURRENT = true; - var tracker = FactoryPointTracker.klt(config, configDetector, GrayF32.class, GrayF32.class); + var tracker = FactoryPointTracker.klt(config, DerivativeType.SOBEL, configDetector, GrayF32.class, GrayF32.class); if (tracker instanceof PointTrackerKltPyramid_MT) return tracker; throw new RuntimeException("Did not create concurrent implementation"); } /** - * See if concurrent and single thread version produce identical reuslts + * See if concurrent and single thread version produce identical results */ @Test void compareToSingleThread() { var config = new ConfigPKlt(); @@ -81,9 +82,9 @@ private PointTrackerKltPyramid createKLT( ConfigPKlt config ) // Create the two different versions BoofConcurrency.USE_CONCURRENT = true; - var trackerMulti = FactoryPointTracker.klt(config, configDetector, GrayF32.class, GrayF32.class); + var trackerMulti = FactoryPointTracker.klt(config, DerivativeType.SOBEL, configDetector, GrayF32.class, GrayF32.class); BoofConcurrency.USE_CONCURRENT = false; - var trackerSingle = FactoryPointTracker.klt(config, configDetector, GrayF32.class, GrayF32.class); + var trackerSingle = FactoryPointTracker.klt(config, DerivativeType.SOBEL, configDetector, GrayF32.class, GrayF32.class); trackerMulti.process(image); trackerSingle.process(image); diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detdesc/TestCompleteSift.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detdesc/TestCompleteSift.java index de7b4b88a8..c9a25d0cc6 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detdesc/TestCompleteSift.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detdesc/TestCompleteSift.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -94,7 +94,7 @@ public class TestCompleteSift extends BoofStandardJUnit { private CompleteSift createAlg() { SiftScaleSpace ss = new SiftScaleSpace(-1, 4, 3, 1.6); - NonMaxSuppression nonmax = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0, 1, true, true, true)); + NonMaxSuppression nonmax = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0, 1, true, true, true), 1); NonMaxLimiter limiter = new NonMaxLimiter( nonmax, FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()), 300); OrientationHistogramSift ori = diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_F32.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_F32.java index 55ad8c18b7..c005d52d4d 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_F32.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -29,8 +29,7 @@ class TestHarrisCorner_F32 extends BoofStandardJUnit { - @Test - void checkScore() { + @Test void checkScore() { float kappa = 0.04f; HarrisCorner_F32 alg = new HarrisCorner_F32(kappa); @@ -38,13 +37,13 @@ void checkScore() { float XY = 2.5f; float YY = 3.5f; - float expected = XX*YY-XY*XY - kappa*(float)Math.pow(XX+YY,2.0); - assertEquals(expected,alg.compute(XX,XY,YY), UtilEjml.TEST_F32); + float expected = XX*YY - XY*XY - kappa*(float)Math.pow(XX + YY, 2.0); + assertEquals(expected, alg.compute(XX, XY, YY), UtilEjml.TEST_F32); } @Nested public class SingleThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_F32 detector = new ImplSsdCorner_F32(1,new HarrisCorner_F32(0.04f)); + ImplSsdCorner_F32 detector = new ImplSsdCorner_F32(1, new HarrisCorner_F32(0.04f)); @Test void genericTests() { @@ -53,13 +52,13 @@ void genericTests() { @Override public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_F32,derivY_F32,intensity); + detector.process(derivX_F32, derivY_F32, intensity); } } @Nested public class MultiThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_F32_MT detector = new ImplSsdCorner_F32_MT(1,new HarrisCorner_F32(0.04f)); + ImplSsdCorner_F32_MT detector = new ImplSsdCorner_F32_MT(1, new HarrisCorner_F32(0.04f)); @Test void genericTests() { @@ -68,7 +67,7 @@ void genericTests() { @Override public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_F32,derivY_F32,intensity); + detector.process(derivX_F32, derivY_F32, intensity); } } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_S32.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_S32.java index 6b8f66a3e2..4f0c226ce6 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_S32.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestHarrisCorner_S32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -28,46 +28,49 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class TestHarrisCorner_S32 extends BoofStandardJUnit { - @Test - void checkScore() { + @Test void checkScore() { float kappa = 0.04f; - HarrisCorner_S32 alg = new HarrisCorner_S32(kappa); + var alg = new HarrisCorner_S32(kappa); int XX = 50; int XY = 70; int YY = 80; - float expected = XX*YY-XY*XY - kappa*(float)Math.pow(XX+YY,2.0); - assertEquals(expected,alg.compute(XX,XY,YY), UtilEjml.TEST_F32); + float expected = XX*YY - XY*XY - kappa*(float)Math.pow(XX + YY, 2.0); + assertEquals(expected, alg.compute(XX, XY, YY), UtilEjml.TEST_F32); + + // See if it computed the correct scale correction + int derivDivisor = 2; + XX *= derivDivisor*derivDivisor; + XY *= derivDivisor*derivDivisor; + YY *= derivDivisor*derivDivisor; + float correction = alg.thresholdScaleByDerivative(derivDivisor); + assertEquals(expected*correction, alg.compute(XX, XY, YY), UtilEjml.TEST_F32); } @Nested public class SingleThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_S16 detector = new ImplSsdCorner_S16(1,new HarrisCorner_S32(0.04f)); + ImplSsdCorner_S16 detector = new ImplSsdCorner_S16(1, new HarrisCorner_S32(0.04f)); - @Test - void genericTests() { + @Test void genericTests() { performAllTests(); } - @Override - public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_I16,derivY_I16,intensity); + @Override public void computeIntensity( GrayF32 intensity ) { + detector.process(derivX_I16, derivY_I16, intensity); } } @Nested public class MultiThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_S16_MT detector = new ImplSsdCorner_S16_MT(1,new HarrisCorner_S32(0.04f)); + ImplSsdCorner_S16_MT detector = new ImplSsdCorner_S16_MT(1, new HarrisCorner_S32(0.04f)); - @Test - void genericTests() { + @Test void genericTests() { performAllTests(); } - @Override - public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_I16,derivY_I16,intensity); + @Override public void computeIntensity( GrayF32 intensity ) { + detector.process(derivX_I16, derivY_I16, intensity); } } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCornerWeighted_S16.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCornerWeighted_S16.java index dada5f2c25..285f9d1ab2 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCornerWeighted_S16.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCornerWeighted_S16.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -36,18 +36,18 @@ class TestImplSsdCornerWeighted_S16 extends BoofStandardJUnit { int width = 40; int height = 50; - int radius=4; + int radius = 4; - Kernel1D_S32 weights = FactoryKernelGaussian.gaussian1D(GrayS16.class,-1,radius); + Kernel1D_S32 weights = FactoryKernelGaussian.gaussian1D(GrayS16.class, -1, radius); - GrayU8 input = new GrayU8(width,height); + GrayU8 input = new GrayU8(width, height); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); + GrayS16 derivX = new GrayS16(width, height); + GrayS16 derivY = new GrayS16(width, height); - GrayS32 derivXX = new GrayS32(width,height); - GrayS32 derivXY = new GrayS32(width,height); - GrayS32 derivYY = new GrayS32(width,height); + GrayS32 derivXX = new GrayS32(width, height); + GrayS32 derivXY = new GrayS32(width, height); + GrayS32 derivYY = new GrayS32(width, height); /** * Manually compute intensity values and see if they are the same @@ -64,9 +64,9 @@ void compareToManual() { int x = derivX.get(j, i); int y = derivY.get(j, i); - derivXX.set(j, i, x * x); - derivXY.set(j, i, x * y); - derivYY.set(j, i, y * y); + derivXX.set(j, i, x*x); + derivXY.set(j, i, x*y); + derivYY.set(j, i, y*y); } } @@ -87,19 +87,19 @@ void compareToManual() { } } - public int sum( int x , int y , GrayS32 img ) { + public int sum( int x, int y, GrayS32 img ) { int ret = 0; int totalWeight = 0; - for( int i = -radius; i <= radius; i++ ) { + for (int i = -radius; i <= radius; i++) { int hsum = 0; int wsum = 0; - for( int j = -radius; j <= radius; j++ ) { - int w = weights.get(j+radius); - hsum += img.get(j+x,i+y)*w; + for (int j = -radius; j <= radius; j++) { + int w = weights.get(j + radius); + hsum += img.get(j + x, i + y)*w; wsum += w; } - int w = weights.get(i+radius); + int w = weights.get(i + radius); ret += w*hsum/wsum; totalWeight += w; } @@ -107,11 +107,11 @@ public int sum( int x , int y , GrayS32 img ) { return ret/totalWeight; } - private class MockSum implements ImplSsdCornerBase.CornerIntensity_S32 - { - @Override - public float compute(int totalXX, int totalXY, int totalYY) { + private class MockSum implements ImplSsdCornerBase.CornerIntensity_S32 { + @Override public float compute( int totalXX, int totalXY, int totalYY ) { return totalXX + totalXY + totalYY; } + + @Override public float thresholdScaleByDerivative( int divisor ) {return 1.0f;} } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCorner_S16.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCorner_S16.java index a14751a011..acd25f7aec 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCorner_S16.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestImplSsdCorner_S16.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -103,5 +103,7 @@ private class MockSum implements ImplSsdCornerBase.CornerIntensity_S32 public float compute(int totalXX, int totalXY, int totalYY) { return totalXX + totalXY + totalYY; } + + @Override public float thresholdScaleByDerivative( int divisor ) {return 0;} } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_F32.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_F32.java index be53a5bacd..a8000d5b6a 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_F32.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -30,35 +30,30 @@ import static org.junit.jupiter.api.Assertions.assertEquals; - public class TestShiTomasiCorner_F32 extends BoofStandardJUnit { - - /** - * Compare against the definition - */ - @Test - void checkScore() { + /// Compare against the definition + @Test void checkScore() { ShiTomasiCorner_F32 alg = new ShiTomasiCorner_F32(); int XX = 50; int XY = 70; int YY = 80; - DMatrixRMaj A = new DMatrixRMaj(2,2,true,new double[]{XX,XY,XY,YY}); + DMatrixRMaj A = new DMatrixRMaj(2, 2, true, new double[]{XX, XY, XY, YY}); // find the smallest eigenvalue - EigenDecomposition_F64 evd = DecompositionFactory_DDRM.eig(true,true); + EigenDecomposition_F64 evd = DecompositionFactory_DDRM.eig(true, true); evd.decompose(A); double ev1 = evd.getEigenvalue(0).real; double ev2 = evd.getEigenvalue(1).real; - float expected = (float)Math.min(ev1,ev2); - assertEquals(expected,alg.compute(XX,XY,YY), UtilEjml.TEST_F32); + float expected = (float)Math.min(ev1, ev2); + assertEquals(expected, alg.compute(XX, XY, YY), UtilEjml.TEST_F32); } @Nested public class SingleThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_F32 detector = new ImplSsdCorner_F32(1,new ShiTomasiCorner_F32()); + ImplSsdCorner_F32 detector = new ImplSsdCorner_F32(1, new ShiTomasiCorner_F32()); @Test void genericTests() { @@ -67,13 +62,13 @@ void genericTests() { @Override public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_F32,derivY_F32,intensity); + detector.process(derivX_F32, derivY_F32, intensity); } } @Nested public class MultiThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_F32 detector = new ImplSsdCorner_F32(1,new ShiTomasiCorner_F32()); + ImplSsdCorner_F32 detector = new ImplSsdCorner_F32(1, new ShiTomasiCorner_F32()); @Test void genericTests() { @@ -82,7 +77,7 @@ void genericTests() { @Override public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_F32,derivY_F32,intensity); + detector.process(derivX_F32, derivY_F32, intensity); } } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_S32.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_S32.java index bf399525ca..17287e77c9 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_S32.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/intensity/impl/TestShiTomasiCorner_S32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -31,32 +31,37 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class TestShiTomasiCorner_S32 extends BoofStandardJUnit { - /** - * Compare against the definition - */ - @Test - void checkScore() { - ShiTomasiCorner_S32 alg = new ShiTomasiCorner_S32(); + /// Compare against the definition and with scaled inputs + @Test void checkScore() { + var alg = new ShiTomasiCorner_S32(); int XX = 50; int XY = 70; int YY = 80; - DMatrixRMaj A = new DMatrixRMaj(2,2,true,new double[]{XX,XY,XY,YY}); + DMatrixRMaj A = new DMatrixRMaj(2, 2, true, new double[]{XX, XY, XY, YY}); // find the smallest eigenvalue - EigenDecomposition_F64 evd = DecompositionFactory_DDRM.eig(true,true); + EigenDecomposition_F64 evd = DecompositionFactory_DDRM.eig(true, true); evd.decompose(A); double ev1 = evd.getEigenvalue(0).real; double ev2 = evd.getEigenvalue(1).real; - float expected = (float)Math.min(ev1,ev2); - assertEquals(expected,alg.compute(XX,XY,YY), UtilEjml.TEST_F32); + float expected = (float)Math.min(ev1, ev2); + assertEquals(expected, alg.compute(XX, XY, YY), UtilEjml.TEST_F32); + + // See if it computed the correct scale correction + int derivDivisor = 2; + XX *= derivDivisor*derivDivisor; + XY *= derivDivisor*derivDivisor; + YY *= derivDivisor*derivDivisor; + float correction = alg.thresholdScaleByDerivative(derivDivisor); + assertEquals(expected*correction, alg.compute(XX, XY, YY), UtilEjml.TEST_F32); } @Nested public class SingleThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_S16 detector = new ImplSsdCorner_S16(1,new ShiTomasiCorner_S32()); + ImplSsdCorner_S16 detector = new ImplSsdCorner_S16(1, new ShiTomasiCorner_S32()); @Test void genericTests() { @@ -65,13 +70,13 @@ void genericTests() { @Override public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_I16,derivY_I16,intensity); + detector.process(derivX_I16, derivY_I16, intensity); } } @Nested public class MultiThread extends GenericCornerIntensityGradientTests { - ImplSsdCorner_S16_MT detector = new ImplSsdCorner_S16_MT(1,new ShiTomasiCorner_S32()); + ImplSsdCorner_S16_MT detector = new ImplSsdCorner_S16_MT(1, new ShiTomasiCorner_S32()); @Test void genericTests() { @@ -80,7 +85,7 @@ void genericTests() { @Override public void computeIntensity( GrayF32 intensity ) { - detector.process(derivX_I16,derivY_I16,intensity); + detector.process(derivX_I16, derivY_I16, intensity); } } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestEasyGeneralFeatureDetector.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestEasyGeneralFeatureDetector.java index f8599ae975..aa2a7fd3ac 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestEasyGeneralFeatureDetector.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestEasyGeneralFeatureDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,6 +18,8 @@ package boofcv.alg.feature.detect.interest; +import boofcv.abst.filter.derivative.ImageGradient; +import boofcv.factory.filter.derivative.FactoryDerivative; import boofcv.struct.QueueCorner; import boofcv.struct.image.GrayS16; import boofcv.struct.image.GrayU8; @@ -32,12 +34,12 @@ public class TestEasyGeneralFeatureDetector extends BoofStandardJUnit { int width = 20; int height = 30; - GrayU8 image = new GrayU8(width,height); + GrayU8 image = new GrayU8(width, height); @Test void requiresGradient() { - Helper detector = new Helper<>(true, false); - EasyGeneralFeatureDetector alg = - new EasyGeneralFeatureDetector<>(detector, GrayU8.class, GrayS16.class); + var detector = new Helper(true, false); + ImageGradient gradient = FactoryDerivative.sobel(GrayU8.class, GrayS16.class); + var alg = new EasyGeneralFeatureDetector<>(detector, gradient, GrayU8.class, GrayS16.class); assertNotNull(alg.derivX); assertNotNull(alg.derivY); @@ -45,16 +47,15 @@ public class TestEasyGeneralFeatureDetector extends BoofStandardJUnit { assertNull(alg.derivXY); assertNull(alg.derivYY); - alg.detect(image,null); + alg.detect(image, null); assertTrue(detector.excludeIsNull); - } @Test void requiresHessian() { - Helper detector = new Helper<>(false, true); - EasyGeneralFeatureDetector alg = - new EasyGeneralFeatureDetector<>(detector, GrayU8.class, GrayS16.class); + var detector = new Helper(false, true); + ImageGradient gradient = FactoryDerivative.sobel(GrayU8.class, GrayS16.class); + var alg = new EasyGeneralFeatureDetector<>(detector, gradient, GrayU8.class, GrayS16.class); // It uses the gradient to compute the hessian faster assertNotNull(alg.derivX); @@ -63,45 +64,45 @@ public class TestEasyGeneralFeatureDetector extends BoofStandardJUnit { assertNotNull(alg.derivXY); assertNotNull(alg.derivYY); - alg.detect(image,null); + alg.detect(image, null); assertTrue(detector.excludeIsNull); } @Test void checkExclude() { - Helper detector = new Helper<>(true, true); - EasyGeneralFeatureDetector alg = - new EasyGeneralFeatureDetector<>(detector, GrayU8.class, GrayS16.class); + var detector = new Helper(true, true); + ImageGradient gradient = FactoryDerivative.sobel(GrayU8.class, GrayS16.class); + var alg = new EasyGeneralFeatureDetector<>(detector, gradient, GrayU8.class, GrayS16.class); - alg.detect(image,new QueueCorner(10)); + alg.detect(image, new QueueCorner(10)); assertFalse(detector.excludeIsNull); } private static class Helper, D extends ImageGray> - extends GeneralFeatureDetector { + extends GeneralFeatureDetector { boolean gradient; boolean hessian; boolean excludeIsNull; - private Helper(boolean gradient, boolean hessian) { + private Helper( boolean gradient, boolean hessian ) { this.gradient = gradient; this.hessian = hessian; } @Override - public void setExclude(QueueCorner exclude) { - excludeIsNull = exclude==null; + public void setExclude( QueueCorner exclude ) { + excludeIsNull = exclude == null; } @Override - public void process(I image, D derivX, D derivY, D derivXX, D derivYY, D derivXY) { - if( gradient ) { + public void process( I image, D derivX, D derivY, D derivXX, D derivYY, D derivXY ) { + if (gradient) { assertNotNull(derivX); assertNotNull(derivY); } - if( hessian ) { + if (hessian) { assertNotNull(derivXX); assertNotNull(derivYY); assertNotNull(derivXY); diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestFastHessianFeatureDetector.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestFastHessianFeatureDetector.java index 9129d79b9c..a61937d55e 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestFastHessianFeatureDetector.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestFastHessianFeatureDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -43,7 +43,7 @@ public TestFastHessianFeatureDetector() { @Override protected Object createDetector( int maxFeatures ) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 1, 5, true)); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 1, 5, true), 1); FeatureSelectLimitIntensity limitLevels = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); FeatureSelectLimitIntensity limitAll = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); var alg = new FastHessianFeatureDetector(extractor,limitLevels,limitAll,1, 9,4,4, 6); diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestGeneralFeatureDetector.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestGeneralFeatureDetector.java index 886ae795fb..d55189ac23 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestGeneralFeatureDetector.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestGeneralFeatureDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -61,7 +61,7 @@ class TestGeneralFeatureDetector extends BoofStandardJUnit { // configure it to only detect positive features intensity.minimums = false; - extractorMax = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, false, true)); + extractorMax = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, false, true), 1); GeneralFeatureDetector detector = new GeneralFeatureDetector<>(intensity, null, extractorMax, selector); detector.process(new GrayF32(width, height), null, null, null, null, null); @@ -70,7 +70,7 @@ class TestGeneralFeatureDetector extends BoofStandardJUnit { // try detecting the negative features too intensity.minimums = true; - extractorMin = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, true, false)); + extractorMin = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, true, false), 1); detector = new GeneralFeatureDetector<>(intensity, extractorMin, extractorMax, selector); detector.process(new GrayF32(width, height), null, null, null, null, null); assertEquals(6, detector.getMaximums().size()); @@ -104,8 +104,8 @@ class TestGeneralFeatureDetector extends BoofStandardJUnit { intensity.img.set(9, 4, -10); // use a real extractor - NonMaxSuppression extractorMin = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, true, false)); - NonMaxSuppression extractorMax = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, false, true)); + NonMaxSuppression extractorMin = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, true, false), 1); + NonMaxSuppression extractorMax = FactoryFeatureExtractor.nonmax(new ConfigExtract(1, 0.001f, 1, true, false, true), 1); var detector = new GeneralFeatureDetector(intensity, extractorMin, extractorMax, selector); detector.process(new GrayF32(width, height), null, null, null, null, null); @@ -276,8 +276,8 @@ public HelperExtractor( boolean usesCandidates, boolean acceptsRequests ) { @Override public void storageAccess( Add opAdd, Reset opReset ) {} @Override public void process( GrayF32 intensity, - ListIntPoint2D candidateMin, ListIntPoint2D candidateMax, - QueueCorner foundMin, QueueCorner foundMax ) { + ListIntPoint2D candidateMin, ListIntPoint2D candidateMax, + QueueCorner foundMin, QueueCorner foundMax ) { numTimesProcessed++; if (foundMin != null) { @@ -341,6 +341,8 @@ public void process( GrayF32 image, GrayF32 derivX, GrayF32 derivY, GrayF32 deri @Override public GrayF32 getIntensity() {return img;} + @Override public float thresholdScaleByDerivative( int divisor ) {return 1;} + @Override public ListIntPoint2D getCandidatesMin() { candidatesMinCalled++; return candidates; diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestSiftDetector.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestSiftDetector.java index 2d771d597c..0dbde1f4ad 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestSiftDetector.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/interest/TestSiftDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -207,7 +207,7 @@ public class TestSiftDetector extends BoofStandardJUnit { private SiftDetector createDetector() { NonMaxLimiter nonmax = FactoryFeatureExtractor.nonmaxLimiter( - new ConfigExtract(1, 0, 1, true, true, true), + new ConfigExtract(1, 0, 1, true, true, true), 1, ConfigSelectLimit.selectBestN(), 1000); FeatureSelectLimitIntensity selectorAll = FactorySelectLimit.intensity(ConfigSelectLimit.selectBestN()); return new SiftDetector(selectorAll, 10, nonmax); diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersFootOfNorm.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersFootOfNorm.java index 64c8805f31..daf547420c 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersFootOfNorm.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersFootOfNorm.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -29,8 +29,8 @@ class TestHoughParametersFootOfNorm extends BoofStandardJUnit { class Gradient extends CommonHoughGradientChecks { @Override HoughTransformGradient createAlgorithm( Class derivType ) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true)); - HoughTransformParameters hough = new HoughParametersFootOfNorm(5); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true), 1); + var hough = new HoughParametersFootOfNorm(5); return new HoughTransformGradient(extractor,hough,derivType); } } @@ -39,8 +39,8 @@ HoughTransformGradient createAlgorithm( Class derivType ) { class Gradient_MT extends CommonHoughGradientChecks { @Override HoughTransformGradient createAlgorithm( Class derivType ) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true)); - HoughTransformParameters hough = new HoughParametersFootOfNorm(5); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true), 1); + var hough = new HoughParametersFootOfNorm(5); return new HoughTransformGradient_MT(extractor,hough,derivType); } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersPolar.java b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersPolar.java index a43bbdc33b..10e2ba8ebe 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersPolar.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/feature/detect/line/TestHoughParametersPolar.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -30,8 +30,8 @@ public class TestHoughParametersPolar extends BoofStandardJUnit { class Gradient extends CommonHoughGradientChecks { @Override HoughTransformGradient createAlgorithm( Class derivType ) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true)); - HoughTransformParameters hough = new HoughParametersPolar(0.5,180); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true), 1); + var hough = new HoughParametersPolar(0.5,180); return new HoughTransformGradient(extractor,hough,derivType); } } @@ -40,8 +40,8 @@ HoughTransformGradient createAlgorithm( Class derivType ) { class Gradient_MT extends CommonHoughGradientChecks { @Override HoughTransformGradient createAlgorithm( Class derivType ) { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true)); - HoughTransformParameters hough = new HoughParametersPolar(0.5,180); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, 5, 0, true), 1); + var hough = new HoughParametersPolar(0.5,180); return new HoughTransformGradient_MT(extractor,hough,derivType); } } @@ -51,7 +51,7 @@ class Binary extends CommonHoughBinaryChecks { @Override HoughTransformBinary createAlgorithm() { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, -1, 0, false)); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, -1, 0, false), 1); HoughTransformParameters hough = new HoughParametersPolar(0.5,180); HoughTransformBinary alg = new HoughTransformBinary(extractor,hough); alg.setMaxLines(1); @@ -64,9 +64,9 @@ class Binary_MT extends CommonHoughBinaryChecks { @Override HoughTransformBinary createAlgorithm() { - NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, -1, 0, false)); - HoughTransformParameters hough = new HoughParametersPolar(0.5,180); - HoughTransformBinary alg = new HoughTransformBinary_MT(extractor,hough); + NonMaxSuppression extractor = FactoryFeatureExtractor.nonmax(new ConfigExtract(4, -1, 0, false), 1); + var hough = new HoughParametersPolar(0.5,180); + var alg = new HoughTransformBinary_MT(extractor,hough); alg.setMaxLines(1); return alg; } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/flow/TestDenseOpticalFlowKlt.java b/main/boofcv-feature/src/test/java/boofcv/alg/flow/TestDenseOpticalFlowKlt.java index 3c6c3c1562..85b4b0a5e2 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/flow/TestDenseOpticalFlowKlt.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/flow/TestDenseOpticalFlowKlt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -40,8 +40,8 @@ class TestDenseOpticalFlowKlt extends BoofStandardJUnit { - GrayF32 image0 = new GrayF32(30,40); - GrayF32 image1 = new GrayF32(30,40); + GrayF32 image0 = new GrayF32(30, 40); + GrayF32 image1 = new GrayF32(30, 40); ImagePyramid prev; GrayF32[] prevDerivX; @@ -63,18 +63,18 @@ void setup() { prev.process(image0); curr.process(image0); - prevDerivX = PyramidOps.declareOutput(prev,ImageType.SB_F32); - prevDerivY = PyramidOps.declareOutput(prev,ImageType.SB_F32); + prevDerivX = PyramidOps.declareOutput(prev, ImageType.SB_F32); + prevDerivY = PyramidOps.declareOutput(prev, ImageType.SB_F32); } private void processInputImage() { prev.process(image0); curr.process(image1); - PyramidOps.gradient(prev, gradient, prevDerivX,prevDerivY); + PyramidOps.gradient(prev, gradient, prevDerivX, prevDerivY); } - protected DenseOpticalFlowKlt createAlg() { + protected DenseOpticalFlowKlt createAlg() { PyramidKltTracker tracker = FactoryTrackerAlg.kltPyramid(config.config, GrayF32.class, GrayF32.class); return new DenseOpticalFlowKlt<>(tracker, 3); @@ -83,63 +83,59 @@ protected DenseOpticalFlowKlt createAlg() { /** * Very simple positive case */ - @Test - void positive() { - - ImageMiscOps.fillRectangle(image0,50,10,12,2,2); - ImageMiscOps.fillRectangle(image1,50,11,13,2,2); + @Test void positive() { + ImageMiscOps.fillRectangle(image0, 50, 10, 12, 2, 2); + ImageMiscOps.fillRectangle(image1, 50, 11, 13, 2, 2); processInputImage(); - DenseOpticalFlowKlt alg = createAlg(); + DenseOpticalFlowKlt alg = createAlg(); - ImageFlow flow = new ImageFlow(image0.width,image0.height); + ImageFlow flow = new ImageFlow(image0.width, image0.height); flow.invalidateAll(); - alg.process(prev,prevDerivX,prevDerivY,curr,flow); + alg.process(prev, prevDerivX, prevDerivY, gradient.divisor(), curr, flow); // no texture in the image so KLT can't do anything - check(flow.get(0,0),false,0,0); - check(flow.get(29,39),false,0,0); + check(flow.get(0, 0), false, 0, 0); + check(flow.get(29, 39), false, 0, 0); // there is texture at the target - check(flow.get(10,12),true,1,1); - check(flow.get(11,12),true,1,1); - check(flow.get(10,13),true,1,1); - check(flow.get(11,13),true,1,1); + check(flow.get(10, 12), true, 1, 1); + check(flow.get(11, 12), true, 1, 1); + check(flow.get(10, 13), true, 1, 1); + check(flow.get(11, 13), true, 1, 1); } - private void check( ImageFlow.D flow , boolean valid , float x , float y ) { - assertEquals(valid,flow.isValid()); - if( valid ) { - assertEquals(x,flow.x,0.05f); - assertEquals(y,flow.y,0.05f); + private void check( ImageFlow.D flow, boolean valid, float x, float y ) { + assertEquals(valid, flow.isValid()); + if (valid) { + assertEquals(x, flow.x, 0.05f); + assertEquals(y, flow.y, 0.05f); } } /** * Very simple negative case. The second image is blank so it should fail at tracking */ - @Test - void negative() { + @Test void negative() { ImageMiscOps.fillRectangle(image0, 200, 7, 9, 5, 5); processInputImage(); - DenseOpticalFlowKlt alg = createAlg(); + DenseOpticalFlowKlt alg = createAlg(); - ImageFlow flow = new ImageFlow(image0.width,image0.height); + ImageFlow flow = new ImageFlow(image0.width, image0.height); - alg.process(prev,prevDerivX,prevDerivY,curr,flow); + alg.process(prev, prevDerivX, prevDerivY, gradient.divisor(), curr, flow); int totalFail = 0; for (int i = 0; i < flow.data.length; i++) { - if( !flow.data[i].isValid() ) { + if (!flow.data[i].isValid()) { totalFail++; } } - assertTrue( totalFail/(double)flow.data.length >= 0.90 ); + assertTrue(totalFail/(double)flow.data.length >= 0.90); } - } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestHybridTrackerScalePoint.java b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestHybridTrackerScalePoint.java index 1c209649d0..d7c4892e66 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestHybridTrackerScalePoint.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestHybridTrackerScalePoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -42,14 +42,14 @@ @SuppressWarnings({"unchecked", "rawtypes"}) class TestHybridTrackerScalePoint extends BoofStandardJUnit { - int width=100,height=120; + int width = 100, height = 120; private HybridTrackerScalePoint createAlgorithm() { var trackerKlt = new DummyKlt(); var detector = new DummyDetector(5); var associate = new DummyAssoc(5); - return new HybridTrackerScalePoint(trackerKlt,detector,associate,10); + return new HybridTrackerScalePoint(trackerKlt, detector, associate, 10); } @Test void reset() { @@ -65,13 +65,13 @@ private HybridTrackerScalePoint createAlgorithm alg.reset(); - assertEquals(0,alg.tracksAll.size()); - assertEquals(0,alg.tracksActive.size()); - assertEquals(0,alg.tracksInactive.size()); - assertEquals(0,alg.tracksSpawned.size()); - assertEquals(0,alg.tracksDropped.size()); - assertEquals(-1,alg.frameID); - assertEquals(0,alg.totalTracks); + assertEquals(0, alg.tracksAll.size()); + assertEquals(0, alg.tracksActive.size()); + assertEquals(0, alg.tracksInactive.size()); + assertEquals(0, alg.tracksSpawned.size()); + assertEquals(0, alg.tracksDropped.size()); + assertEquals(-1, alg.frameID); + assertEquals(0, alg.totalTracks); } @Test void dropAllTracks() { @@ -85,11 +85,11 @@ private HybridTrackerScalePoint createAlgorithm alg.dropAllTracks(); - assertEquals(0,alg.tracksAll.size()); - assertEquals(0,alg.tracksActive.size()); - assertEquals(0,alg.tracksInactive.size()); - assertEquals(0,alg.tracksSpawned.size()); - assertEquals(0,alg.tracksDropped.size()); + assertEquals(0, alg.tracksAll.size()); + assertEquals(0, alg.tracksActive.size()); + assertEquals(0, alg.tracksInactive.size()); + assertEquals(0, alg.tracksSpawned.size()); + assertEquals(0, alg.tracksDropped.size()); } @Test void dropTrackByAllIndex() { @@ -97,7 +97,7 @@ private HybridTrackerScalePoint createAlgorithm alg.trackerKlt = new DummyKlt(); - addTracks(alg.tracksAll,8); + addTracks(alg.tracksAll, 8); alg.tracksActive.addAll(alg.tracksAll); HybridTrack track = alg.dropTrackByAllIndex(6); @@ -115,13 +115,13 @@ private HybridTrackerScalePoint createAlgorithm alg.trackerKlt = new DummyKlt(); - addTracks(alg.tracksAll,8); + addTracks(alg.tracksAll, 8); alg.tracksActive.addAll(alg.tracksAll); // this should be cleared alg.tracksSpawned.add(1); - alg.updateTracks(new DummyPyramid(),null,null); + alg.updateTracks(new DummyPyramid(), null, null); // tracks after 5 should be dropped assertEquals(8, alg.tracksAll.size()); @@ -134,59 +134,60 @@ private HybridTrackerScalePoint createAlgorithm @Test void dropExcessiveInactiveTracks() { var alg = createAlgorithm(); alg.maxInactiveTracks = 20; - addTracks(alg.tracksAll,50); + addTracks(alg.tracksAll, 50); for (int i = 0; i < 30; i++) { alg.tracksInactive.add(alg.tracksAll.get(i)); } alg.dropExcessiveInactiveTracks(); - assertEquals(40,alg.tracksAll.size); - assertEquals(20,alg.tracksInactive.size); + assertEquals(40, alg.tracksAll.size); + assertEquals(20, alg.tracksInactive.size); } @Test void pruneActiveTracksWhichAreTooClose() { var alg = createAlgorithm(); alg.imageWidth = width; alg.imageHeight = height; - addTracks(alg.tracksAll,8); + addTracks(alg.tracksAll, 8); // assign all tracks to the same pixel - for( var t : alg.tracksAll.toList() ) { - t.pixel.setTo(5,5); + for (var t : alg.tracksAll.toList()) { + t.pixel.setTo(5, 5); } alg.pruneActiveTracksWhichAreTooClose(); // only one should survive since they have the same location - addTracks(alg.tracksAll,1); + addTracks(alg.tracksAll, 1); } - private void addTracks( DogArray> l , int num ) { - for( int i = 0; i < num; i++ ) { + private void addTracks( DogArray> l, int num ) { + for (int i = 0; i < num; i++) { HybridTrack t = l.grow(); - t.trackKlt = new PyramidKltFeature(2,5); + t.trackKlt = new PyramidKltFeature(2, 5); } } - private void addTracks( List l , int num ) { - for( int i = 0; i < num; i++ ) { - HybridTrack t =new HybridTrack(); - t.trackKlt = new PyramidKltFeature(2,5); - l.add( t ); + private void addTracks( List l, int num ) { + for (int i = 0; i < num; i++) { + HybridTrack t = new HybridTrack(); + t.trackKlt = new PyramidKltFeature(2, 5); + l.add(t); } } - private static class DummyKlt extends PyramidKltForHybrid { + private static class DummyKlt extends PyramidKltForHybrid { int count = 0; - @Override public boolean performTracking( PyramidKltFeature feature ) {return count++ < 5;} - @Override public void setInputs(ImagePyramid image , GrayF32[] derivX , GrayF32[] derivY ) {} - @Override public void setDescription( float x , float y , PyramidKltFeature ret ) {} - } + @Override public boolean performTracking( PyramidKltFeature feature ) {return count++ < 5;} + + @Override public void setInputs( ImagePyramid image, GrayF32[] derivX, GrayF32[] derivY ) {} + @Override public void setDescription( float x, float y, PyramidKltFeature ret ) {} + } private static class DummyAssoc extends AbstractAssociateDescription2D { int N; - public DummyAssoc(int N ) { + public DummyAssoc( int N ) { this.N = N; } @@ -194,38 +195,54 @@ public DummyAssoc(int N ) { public FastAccess getMatches() { DogArray queue = new DogArray<>(N, AssociatedIndex::new); - for( int i = 0; i < N; i++ ) { - queue.grow().setTo(i,i,1); + for (int i = 0; i < N; i++) { + queue.grow().setTo(i, i, 1); } return queue; } @Override public boolean uniqueSource() {return true;} + @Override public boolean uniqueDestination() {return true;} - @Override public Class getDescriptionType() {return TupleDesc_F64.class;}; + + @Override public Class getDescriptionType() {return TupleDesc_F64.class;} + + ; } - private static class DummyDetector extends DetectDescribePointAbstract { + private static class DummyDetector extends DetectDescribePointAbstract { int N; - private DummyDetector(int n) {N = n;} + + private DummyDetector( int n ) {N = n;} + @Override public int getNumberOfFeatures() {return N;} - @Override public Point2D_F64 getLocation(int featureIndex) {return new Point2D_F64(2,2);} + + @Override public Point2D_F64 getLocation( int featureIndex ) {return new Point2D_F64(2, 2);} + @Override public int getNumberOfSets() {return 1;} - @Override public int getSet(int index) {return 0;} + + @Override public int getSet( int index ) {return 0;} + @Override public TupleDesc_F64 createDescription() {return new TupleDesc_F64(1);} - @Override public Class getDescriptionType() {return TupleDesc_F64.class;}; + + @Override public Class getDescriptionType() {return TupleDesc_F64.class;} + + ; } private class DummyPyramid extends PyramidDiscrete { public DummyPyramid() { - super(ImageType.SB_F32, true,ConfigDiscreteLevels.levels(2)); - initialize(width,height); + super(ImageType.SB_F32, true, ConfigDiscreteLevels.levels(2)); + initialize(width, height); } - @Override public void process(GrayF32 input) {} - @Override public double getSampleOffset(int layer) {return 0;} - @Override public double getSigma(int layer) {return 0;} + @Override public void process( GrayF32 input ) {} + + @Override public double getSampleOffset( int layer ) {return 0;} + + @Override public double getSigma( int layer ) {return 0;} + @Override public > IP copyStructure() {return null;} } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestPyramidKltForCombined.java b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestPyramidKltForCombined.java index 438f7a9568..419e397f9a 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestPyramidKltForCombined.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/hybrid/TestPyramidKltForCombined.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -34,6 +34,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; class TestPyramidKltForCombined extends BoofStandardJUnit { @@ -47,61 +48,55 @@ class TestPyramidKltForCombined extends BoofStandardJUnit { GrayF32[] derivX; GrayF32[] derivY; - - public PyramidKltForHybrid createAlg() - { + public PyramidKltForHybrid createAlg() { ConfigKlt config = new ConfigKlt(); - return new PyramidKltForHybrid<>(config, 5, GrayF32.class, GrayF32.class); + return new PyramidKltForHybrid<>(config, 5, GrayF32.class, GrayF32.class, 1); } - @BeforeEach - void init() { - - pyramid = FactoryPyramid.discreteGaussian(configLevels,-1,2,false, ImageType.single(GrayF32.class)); + @BeforeEach void init() { + pyramid = FactoryPyramid.discreteGaussian(configLevels, -1, 2, false, ImageType.single(GrayF32.class)); - GrayF32 input = new GrayF32(width,height); - ImageMiscOps.fillUniform(input,rand,0,100); + GrayF32 input = new GrayF32(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 100); // do a real update so that it can track a feature - ImageGradient gradient = + ImageGradient gradient = FactoryDerivative.sobel(GrayF32.class, GrayF32.class); pyramid.process(input); - derivX = PyramidOps.declareOutput(pyramid,ImageType.SB_F32); - derivY = PyramidOps.declareOutput(pyramid,ImageType.SB_F32); + derivX = PyramidOps.declareOutput(pyramid, ImageType.SB_F32); + derivY = PyramidOps.declareOutput(pyramid, ImageType.SB_F32); PyramidOps.gradient(pyramid, gradient, derivX, derivY); } - @Test - void setDescription() { - PyramidKltForHybrid alg = createAlg(); + @Test void setDescription() { + PyramidKltForHybrid alg = createAlg(); - alg.setInputs(pyramid,derivX,derivY); + alg.setInputs(pyramid, derivX, derivY); PyramidKltFeature t = alg.createNewTrack(); - alg.setDescription(30.1f,25,t); + alg.setDescription(30.1f, 25, t); - assertTrue(30.1f == t.x); - assertTrue(25f == t.y); + assertEquals(30.1f, t.x); + assertEquals(25f, t.y); - for( int i = 0; i < t.desc.length; i++ ) { + for (int i = 0; i < t.desc.length; i++) { double v = ImageStatistics.sum(t.desc[i].desc); double dx = ImageStatistics.sum(t.desc[i].derivX); double dy = ImageStatistics.sum(t.desc[i].derivY); - assertTrue(v!=0); - assertTrue(dx!=0); - assertTrue(dy!=0); + assertTrue(v != 0); + assertTrue(dx != 0); + assertTrue(dy != 0); } } - @Test - void performTracking() { - PyramidKltForHybrid alg = createAlg(); + @Test void performTracking() { + PyramidKltForHybrid alg = createAlg(); - alg.setInputs(pyramid,derivX,derivY); + alg.setInputs(pyramid, derivX, derivY); PyramidKltFeature t = alg.createNewTrack(); @@ -114,7 +109,7 @@ void performTracking() { // see if it moves it back close to the original pose assertTrue(alg.performTracking(t)); - assertTrue(Math.abs(t.x-30.1)<0.1); - assertTrue(Math.abs(t.y-25)<0.1); + assertTrue(Math.abs(t.x - 30.1) < 0.1); + assertTrue(Math.abs(t.y - 25) < 0.1); } } diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestKltTracker.java b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestKltTracker.java index f4fb7eab40..844c96c951 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestKltTracker.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestKltTracker.java @@ -22,17 +22,18 @@ import boofcv.alg.filter.derivative.GradientSobel; import boofcv.alg.interpolate.InterpolateRectangle; import boofcv.alg.misc.ImageMiscOps; +import boofcv.alg.misc.PixelMath; import boofcv.core.image.border.BorderIndex1D_Extend; import boofcv.factory.interpolate.FactoryInterpolation; import boofcv.struct.border.ImageBorder1D_F32; import boofcv.struct.image.GrayF32; import boofcv.testing.BoofStandardJUnit; +import org.ejml.UtilEjml; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class TestKltTracker extends BoofStandardJUnit { - int imageWidth = 40; int imageHeight = 50; @@ -42,18 +43,18 @@ public class TestKltTracker extends BoofStandardJUnit { /// Process the same features in two different sets of image. only difference is that one is a sub image /// results should be identical - @Test void testSubImages() { + @Test void subImages() { ImageMiscOps.fillUniform(image, rand, 0, 100); GradientSobel.process(image, derivX, derivY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); KltTracker trackerA = createDefaultTracker(); - trackerA.setImage(image, derivX, derivY); + trackerA.setImage(image, derivX, derivY, 1); KltTracker trackerB = createDefaultTracker(); GrayF32 image = BoofTesting.createSubImageOf(this.image); GrayF32 derivX = BoofTesting.createSubImageOf(this.derivX); GrayF32 derivY = BoofTesting.createSubImageOf(this.derivY); - trackerB.setImage(image, derivX, derivY); + trackerB.setImage(image, derivX, derivY, 1); for (int y = 0; y < imageHeight; y += 4) { for (int x = 0; x < imageWidth; x += 4) { @@ -87,13 +88,13 @@ public class TestKltTracker extends BoofStandardJUnit { /// Create a description of a feature next to the border then place the feature just outside of the image /// and see if it can track to its original position. - @Test void testTracking_border1() { + @Test void tracking_border1() { ImageMiscOps.fillUniform(image, rand, 0, 100); GradientSobel.process(image, derivX, derivY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); int r = 4; var feature = new KltFeature(r); @@ -121,12 +122,12 @@ public class TestKltTracker extends BoofStandardJUnit { } /// Place a feature on the border then put it inside the image. See if it moves towards the border - @Test void testTracking_border2() { + @Test void tracking_border2() { ImageMiscOps.fillUniform(image, rand, 1, 100); GradientSobel.process(image, derivX, derivY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); int r = 3; var feature = new KltFeature(r); @@ -157,7 +158,7 @@ public class TestKltTracker extends BoofStandardJUnit { /// Set description should fail if a feature is entirely outside the image @Test void setDescription_outsideFail() { KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); var feature = new KltFeature(3); feature.setPosition(-100, 200); @@ -171,7 +172,7 @@ public class TestKltTracker extends BoofStandardJUnit { GradientSobel.process(image, derivX, derivY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); var featureA = new KltFeature(3); var featureB = new KltFeature(3); @@ -193,7 +194,7 @@ public class TestKltTracker extends BoofStandardJUnit { /// When placed outside the image pixels should be NaN @Test void setDescription_borderNaN() { KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); var feature = new KltFeature(3); feature.setPosition(2, 1); @@ -212,7 +213,7 @@ public class TestKltTracker extends BoofStandardJUnit { /// Pass in a feature with a small determinant and see if it returns a fault. @Test void detectBadFeature() { KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); var feature = new KltFeature(2); // put a feature right on the corner @@ -220,7 +221,7 @@ public class TestKltTracker extends BoofStandardJUnit { // Gxx, Gyy, and Gxy will all be zero, which is bad // update the feature's position - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); assertNotSame(tracker.track(feature), KltTrackFault.SUCCESS); } @@ -229,7 +230,7 @@ public class TestKltTracker extends BoofStandardJUnit { GradientSobel.process(image, derivX, derivY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); KltTracker tracker = createDefaultTracker(); - tracker.setImage(image, derivX, derivY); + tracker.setImage(image, derivX, derivY, 1); var feature = new KltFeature(2); feature.setPosition(20, 22); @@ -318,6 +319,80 @@ public class TestKltTracker extends BoofStandardJUnit { assertTrue(tracker.isFullyOutside(imageWidth + r - 1, imageHeight + r - 0.999f)); } + // Test to see if the divisor is applied correctly at every step + @Test void divisorApplication() { + var input = new GrayF32(imageWidth, imageHeight); + var derivX = new GrayF32(imageWidth, imageHeight); + var derivY = new GrayF32(imageWidth, imageHeight); + ImageMiscOps.fillUniform(input, rand, 0, 200); + ImageMiscOps.fillUniform(derivX, rand, 0, 200); + ImageMiscOps.fillUniform(derivY, rand, 0, 200); + + KltTracker normal = createDefaultTracker(); + normal.widthFeature = 3; + normal.lengthFeature = 9; + normal.setImage(input, derivX, derivY, 1); + + // Simulate a kernel scaling the derivative, like you have in integer images + int divisor = 10; + GrayF32 scaledX = derivX.clone(); + GrayF32 scaledY = derivY.clone(); + PixelMath.multiply(derivX, divisor, scaledX); + PixelMath.multiply(derivY, divisor, scaledY); + + // Create a separate tracker to process these scaled image + KltTracker scaled = createDefaultTracker(); + scaled.widthFeature = 3; + scaled.lengthFeature = 9; + scaled.setImage(input, scaledX, scaledY, divisor); + + + // Test internalSetDescription. Easier to call setDescribe with a point inside + var expected = new KltFeature(3).withPosition(10.1f, 12.2f); + var found = new KltFeature(3).withPosition(10.1f, 12.2f); + normal.setDescription(expected); + scaled.setDescription(found); + assertEquals(expected.Gxx, found.Gxx, Math.abs(expected.Gxx)*UtilEjml.TEST_F32); + assertEquals(expected.Gyy, found.Gyy, Math.abs(expected.Gyy)*UtilEjml.TEST_F32); + assertEquals(expected.Gxy, found.Gxy, Math.abs(expected.Gxy)*UtilEjml.TEST_F32); + + // Test computeE() which assumes the feature is inside the image. If currDesc and track.desc are the same + // this becomes a trivial test case with 0 + assertTrue(expected.derivX.get(0, 0) != 0); + normal.currDesc.setTo(expected.desc); + ImageMiscOps.fillUniform(expected.desc, rand, 0, 200); + normal.computeE(expected, expected.x, expected.y); + scaled.currDesc.setTo(found.desc); + found.desc.setTo(expected.desc); + scaled.computeE(found, found.x, found.y); + assertNotEquals(0.0f, normal.Ex); + assertEquals(normal.Ex, scaled.Ex, Math.abs(normal.Ex)*UtilEjml.TEST_F32); + assertEquals(normal.Ey, scaled.Ey, Math.abs(normal.Ey)*UtilEjml.TEST_F32); + + // Save for internalSetDescriptionBorder. Place these points so they are partially outside + expected.withPosition(0.9f, 12.2f); + found.withPosition(0.9f, 12.2f); + normal.setDescription(expected); + scaled.setDescription(found); + assertNotEquals(0.0f, expected.Gxx); + assertEquals(expected.Gxx, found.Gxx, Math.abs(expected.Gxx)*UtilEjml.TEST_F32); + assertEquals(expected.Gyy, found.Gyy, Math.abs(expected.Gyy)*UtilEjml.TEST_F32); + assertEquals(expected.Gxy, found.Gxy, Math.abs(expected.Gxy)*UtilEjml.TEST_F32); + + // Avoid trivial case + ImageMiscOps.fillUniform(expected.desc, rand, 0, 200); + found.desc.setTo(expected.desc); + + normal.computeGandE_border(expected, expected.x, expected.y); + scaled.computeGandE_border(found, expected.x, expected.y); + assertNotEquals(0.0f, normal.Ex); + assertEquals(normal.Ex, scaled.Ex, Math.abs(normal.Ex)*UtilEjml.TEST_F32); + assertEquals(normal.Ey, scaled.Ey, Math.abs(normal.Ey)*UtilEjml.TEST_F32); + assertEquals(normal.Gxx, scaled.Gxx, Math.abs(normal.Gxx)*UtilEjml.TEST_F32); + assertEquals(normal.Gyy, scaled.Gyy, Math.abs(normal.Gyy)*UtilEjml.TEST_F32); + assertEquals(normal.Gxy, scaled.Gxy, Math.abs(normal.Gxy)*UtilEjml.TEST_F32); + } + public static KltTracker createDefaultTracker() { var config = new ConfigKlt(); config.maxPerPixelError = 10; diff --git a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestPyramidKltTracker.java b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestPyramidKltTracker.java index 521135a7fe..34a652711a 100644 --- a/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestPyramidKltTracker.java +++ b/main/boofcv-feature/src/test/java/boofcv/alg/tracker/klt/TestPyramidKltTracker.java @@ -50,7 +50,7 @@ private void setTargetLocation( int x, int y ) { // tell it to generate a feature inside directly on a pixel var feature = new PyramidKltFeature(pyramid.getNumLayers(), featureRadius); feature.setPosition(25, 20); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); assertTrue(tracker.setDescription(feature)); // all the layers should have been set @@ -65,7 +65,7 @@ private void setTargetLocation( int x, int y ) { // only the first layer should be set var feature = new PyramidKltFeature(pyramid.getNumLayers(), featureRadius); feature.setPosition(featureRadius - 1, featureRadius - 1); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); assertTrue(tracker.setDescription(feature)); for (int i = 0; i < pyramid.getNumLayers(); i++) { assertTrue(feature.desc[i].x != 0); @@ -80,7 +80,7 @@ private void setTargetLocation( int x, int y ) { // only the first layer should be set var feature = new PyramidKltFeature(pyramid.getNumLayers(), featureRadius); feature.setPosition(-featureRadius - 1, -featureRadius - 1); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); assertFalse(tracker.setDescription(feature)); } @@ -90,7 +90,7 @@ private void setTargetLocation( int x, int y ) { // set the feature right on the corner var feature = new PyramidKltFeature(pyramid.getNumLayers(), featureRadius); feature.setPosition(cornerX, cornerY); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); tracker.setDescription(feature); // now move the corner away from the feature @@ -109,7 +109,7 @@ private void setTargetLocation( int x, int y ) { // set the feature right on the corner var feature = new PyramidKltFeature(pyramid.getNumLayers(), featureRadius); feature.setPosition(cornerX, cornerY); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); tracker.setDescription(feature); // now move the corner away from the feature @@ -130,7 +130,7 @@ private void setTargetLocation( int x, int y ) { // set the feature right on the corner var feature = new PyramidKltFeature(pyramid.getNumLayers(), featureRadius); feature.setPosition(targetX, targetY); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); tracker.setDescription(feature); // start it outside the image, but still near its true position @@ -148,7 +148,7 @@ private void setTargetLocation( int x, int y ) { // set the feature right on the corner var feature = new PyramidKltFeature(pyramid.getNumLayers(), 4); feature.setPosition(21, 22); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); tracker.setDescription(feature); // put the feature out of bounds @@ -164,7 +164,7 @@ private void setTargetLocation( int x, int y ) { // set the feature right on the corner var feature = new PyramidKltFeature(pyramid.getNumLayers(), 4); feature.setPosition(21, 22); - tracker.setImage(pyramid, derivX, derivY); + tracker.setImage(pyramid, derivX, derivY, 1); tracker.setDescription(feature); // mess up the description so that it will produce a large error diff --git a/main/boofcv-feature/src/test/java/boofcv/factory/feature/interest/TestFactoryDetectPoint.java b/main/boofcv-feature/src/test/java/boofcv/factory/feature/interest/TestFactoryDetectPoint.java index a9c791eeba..7e214eb226 100644 --- a/main/boofcv-feature/src/test/java/boofcv/factory/feature/interest/TestFactoryDetectPoint.java +++ b/main/boofcv-feature/src/test/java/boofcv/factory/feature/interest/TestFactoryDetectPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -34,8 +34,8 @@ public class TestFactoryDetectPoint extends BoofStandardJUnit { var config = new ConfigPointDetector(); for (PointDetectorTypes type : PointDetectorTypes.values()) { config.type = type; - FactoryDetectPoint.create(config, GrayU8.class, null); - FactoryDetectPoint.create(config, GrayF32.class, null); + FactoryDetectPoint.create(config, GrayU8.class, null, 1); + FactoryDetectPoint.create(config, GrayF32.class, null, 1); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/AnyImageDerivative.java b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/AnyImageDerivative.java index ffe2e614c1..5e256052a0 100644 --- a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/AnyImageDerivative.java +++ b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/AnyImageDerivative.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -82,6 +82,12 @@ public AnyImageDerivative( Kernel1D deriv, Class inputType, Class derivTyp derivDerivY = FactoryConvolve.convolve(deriv, _derivType, _derivType, borderDeriv, false); } + /// Returns the divisor needed to correct the derivatives scale. Integer kernels are off by a scale factor + /// TODO put in the actual value and not hard coded one + public int getEdgeDivisor() { + return 1; + } + /** * Constructor for 2D kernels. * diff --git a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient.java b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient.java index 82f5557352..32c1f5dda0 100644 --- a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient.java +++ b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2019, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -22,27 +22,23 @@ import boofcv.struct.image.ImageType; -/** - * A generic interface for computing first order image derivative along the x and y axes. - * - * @author Peter Abeles - */ +/// A generic interface for computing first order image derivative along the x and y axes. public interface ImageGradient, Output extends ImageBase> extends ImageDerivative { - /** - * Computes the image gradient from the input image and stores the results into - * 'derivX' and 'derivY' - * - * @param inputImage Original input image. Not modified. - * @param derivX First order image derivative along the x-axis. Modified. - * @param derivY First order image derivative along the y-axis. Modified. - */ + /// Computes the image gradient from the input image and stores the results into + /// 'derivX' and 'derivY' + /// + /// @param inputImage Original input image. Not modified. + /// @param derivX First order image derivative along the x-axis. Modified. + /// @param derivY First order image derivative along the y-axis. Modified. void process( Input inputImage , Output derivX, Output derivY ); - /** - * Image type of input image - */ + /// Scale factor that the computed gradient is off by. This is needed for integer valued images. For + /// floating point images this will always return 1. + int divisor(); + + /// Image type of input image ImageType getInputType(); } diff --git a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradientThenReduce.java b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradientThenReduce.java index 3c2fcd6bb4..64abaebcb3 100644 --- a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradientThenReduce.java +++ b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradientThenReduce.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2019, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -80,6 +80,10 @@ public void process(Input inputImage, Output derivX, Output derivY) { reduce.process(middleX,middleY, derivX,derivY); } + @Override public int divisor() { + return gradient.divisor(); + } + @Override public ImageType getInputType() { return gradient.getInputType(); diff --git a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_Gaussian.java b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_Gaussian.java index 5c57851577..34d43e3db1 100644 --- a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_Gaussian.java +++ b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_Gaussian.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -90,6 +90,10 @@ public void process( I inputImage, D derivX, D derivY ) { GConvolveImageOps.vertical(kernelDeriv, storage, derivY, border); } + @Override public int divisor() { + throw new RuntimeException("Not implemented"); + } + @Override public void setBorderType( BorderType type ) { this.borderType = type; diff --git a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_PL.java b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_PL.java index 3d29c44645..252617a573 100644 --- a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_PL.java +++ b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_PL.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2019, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -48,6 +48,10 @@ public void process(Planar inputImage, Planar derivX, Planar derivY) { } } + @Override public int divisor() { + return bandGradient.divisor(); + } + @Override public ImageType> getInputType() { return ImageType.pl(derivType.numBands,bandGradient.getInputType().getDataType()); diff --git a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_SB.java b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_SB.java index 0ed6140b66..d889ef9828 100644 --- a/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_SB.java +++ b/main/boofcv-ip/src/main/java/boofcv/abst/filter/derivative/ImageGradient_SB.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -39,11 +39,17 @@ public abstract class ImageGradient_SB, Output ex ImageBorder border; Class inputType; Class derivType; + int derivDivisor = 1; - protected ImageGradient_SB( Class inputType, Class derivType ) { + /// @param divisorInt Derivative for integer images. For floating point images, the divisor will be saved as 1. + protected ImageGradient_SB( Class inputType, Class derivType, int divisorInt ) { this.inputType = inputType; this.derivType = derivType; setBorderType(borderType); + + // the divisor will not be 1 only if it's an integer valued image + if (ImageType.single(inputType).getDataType().isInteger()) + derivDivisor = divisorInt; } @Override @@ -75,8 +81,10 @@ public ImageType getDerivativeType() { return ImageType.single(derivType); } + @Override public int divisor() {return derivDivisor;} + public static class Sobel, D extends ImageGray> extends ImageGradient_SB { - public Sobel( Class inputType, Class derivType ) {super(inputType, derivType);} + public Sobel( Class inputType, Class derivType ) {super(inputType, derivType, GradientSobel.divisor);} @Override public void process( T inputImage, D derivX, D derivY ) { @@ -85,7 +93,7 @@ public void process( T inputImage, D derivX, D derivY ) { } public static class Prewitt, D extends ImageGray> extends ImageGradient_SB { - public Prewitt( Class inputType, Class derivType ) {super(inputType, derivType);} + public Prewitt( Class inputType, Class derivType ) {super(inputType, derivType, GradientPrewitt.divisor);} @Override public void process( T inputImage, D derivX, D derivY ) { @@ -94,7 +102,7 @@ public void process( T inputImage, D derivX, D derivY ) { } public static class Scharr, D extends ImageGray> extends ImageGradient_SB { - public Scharr( Class inputType, Class derivType ) {super(inputType, derivType);} + public Scharr( Class inputType, Class derivType ) {super(inputType, derivType, GradientScharr.divisor);} @Override public void process( T inputImage, D derivX, D derivY ) { @@ -103,7 +111,7 @@ public void process( T inputImage, D derivX, D derivY ) { } public static class Three, D extends ImageGray> extends ImageGradient_SB { - public Three( Class inputType, Class derivType ) {super(inputType, derivType);} + public Three( Class inputType, Class derivType ) {super(inputType, derivType, GradientThree.divisor);} @Override public void process( T inputImage, D derivX, D derivY ) { @@ -112,7 +120,7 @@ public void process( T inputImage, D derivX, D derivY ) { } public static class Two0, D extends ImageGray> extends ImageGradient_SB { - public Two0( Class inputType, Class derivType ) {super(inputType, derivType);} + public Two0( Class inputType, Class derivType ) {super(inputType, derivType, GradientTwo0.divisor);} @Override public void process( T inputImage, D derivX, D derivY ) { @@ -121,7 +129,7 @@ public void process( T inputImage, D derivX, D derivY ) { } public static class Two1, D extends ImageGray> extends ImageGradient_SB { - public Two1( Class inputType, Class derivType ) {super(inputType, derivType);} + public Two1( Class inputType, Class derivType ) {super(inputType, derivType, GradientTwo1.divisor);} @Override public void process( T inputImage, D derivX, D derivY ) { diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeLaplacian.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeLaplacian.java index 3cd6f915c0..b608ebb809 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeLaplacian.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeLaplacian.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -31,71 +31,61 @@ import boofcv.struct.image.GrayU8; import org.jetbrains.annotations.Nullable; -/** - *

- * The Laplacian is convolved across an image to find second derivative of the image. - * It is often a faster way to compute the intensity of an edge than first derivative algorithms. - *

- *

- *

- * This implementation of the laplacian has a 3 by 3 kernel.
- *
- *            partial^2 f     partial^2 f
- * f(x,y) =   ~~~~~~~~~~~  +  ~~~~~~~~~~~
- *            partial x^2     partial x^2
- *
- *          [ 0   1   0 ]
- * kernel = [ 1  -4   1 ]
- *          [ 0   1   0 ]
- * 
- *

- *

- * This formulation is derived by using the [-1 1 0] and [0 -1 1] difference kernels for the image derivative. Alternative - * formulations can be found using other kernels. - *

- *

- * DEVELOPER NOTE: This is still a strong candidate for further optimizations due to redundant - * array accesses. - *

- * - * @author Peter Abeles - */ +/// The Laplacian is convolved across an image to find second derivative of the image. +/// It is often a faster way to compute the intensity of an edge than first derivative algorithms. +/// +///
+/// This implementation of the laplacian has a 3 by 3 kernel.
+///
+///            partial^2 f     partial^2 f
+/// f(x,y) =   ~~~~~~~~~~~  +  ~~~~~~~~~~~
+///            partial x^2     partial y^2
+///
+///          [ 0   1   0 ]
+/// kernel = [ 1  -4   1 ]
+///          [ 0   1   0 ]
+/// 
+/// +/// This formulation is derived by using the [-1 1 0] and [0 -1 1] difference kernels for the image derivative. Alternative +/// formulations can be found using other kernels. +/// +/// DEVELOPER NOTE: This is still a strong candidate for further optimizations due to redundant +/// array accesses. public class DerivativeLaplacian { - public static Kernel2D_S32 kernel_I32 = new Kernel2D_S32(3, new int[]{0,1,0,1,-4,1,0,1,0}); - public static Kernel2D_F32 kernel_F32 = new Kernel2D_F32(3, new float[]{0,1,0,1,-4,1,0,1,0}); + /// The divisor is 1 because the formula used to derive this specific Laplacian uses central difference + /// which divides by 1 twice + public static final int divisor = 1; + public static Kernel2D_S32 kernelXY_I32 = new Kernel2D_S32(3, new int[]{0, 1, 0, 1, -4, 1, 0, 1, 0}); + public static Kernel2D_F32 kernelXY_F32 = new Kernel2D_F32(3, new float[]{0, 1, 0, 1, -4, 1, 0, 1, 0}); - /** - * Computes the Laplacian of input image. - * - * @param orig Input image. Not modified. - * @param deriv Where the Laplacian is written to. Modified. - */ - public static void process(GrayU8 orig, GrayS16 deriv, @Nullable ImageBorder_S32 border ) { - deriv.reshape(orig.width,orig.height); + /// Computes the Laplacian of input image. + /// + /// @param orig Input image. Not modified. + /// @param deriv Where the Laplacian is written to. Modified. + public static void process( GrayU8 orig, GrayS16 deriv, @Nullable ImageBorder_S32 border ) { + deriv.reshape(orig.width, orig.height); - if( BoofConcurrency.USE_CONCURRENT ) { - DerivativeLaplacian_Inner_MT.process(orig,deriv); + if (BoofConcurrency.USE_CONCURRENT) { + DerivativeLaplacian_Inner_MT.process(orig, deriv); } else { - DerivativeLaplacian_Inner.process(orig,deriv); + DerivativeLaplacian_Inner.process(orig, deriv); } - if( border != null ) { + if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernel_I32, border,deriv); + ConvolveJustBorder_General_SB.convolve(kernelXY_I32, border, deriv); } } - /** - * Computes Laplacian on an U8 image but outputs derivative in a F32 image. Removes a step when processing - * images for feature detection - */ - public static void process(GrayU8 orig, GrayF32 deriv) { - deriv.reshape(orig.width,orig.height); + /// Computes Laplacian on an U8 image but outputs derivative in a F32 image. Removes a step when processing + /// images for feature detection + public static void process( GrayU8 orig, GrayF32 deriv ) { + deriv.reshape(orig.width, orig.height); - if( BoofConcurrency.USE_CONCURRENT ) { - DerivativeLaplacian_Inner_MT.process(orig,deriv); + if (BoofConcurrency.USE_CONCURRENT) { + DerivativeLaplacian_Inner_MT.process(orig, deriv); } else { - DerivativeLaplacian_Inner.process(orig,deriv); + DerivativeLaplacian_Inner.process(orig, deriv); } // if( border != null ) { @@ -104,24 +94,22 @@ public static void process(GrayU8 orig, GrayF32 deriv) { // } } - /** - * Computes the Laplacian of 'orig'. - * - * @param orig Input image. Not modified. - * @param deriv Where the Laplacian is written to. Modified. - */ - public static void process(GrayF32 orig, GrayF32 deriv, @Nullable ImageBorder_F32 border) { - deriv.reshape(orig.width,orig.height); + /// Computes the Laplacian of 'orig'. + /// + /// @param orig Input image. Not modified. + /// @param deriv Where the Laplacian is written to. Modified. + public static void process( GrayF32 orig, GrayF32 deriv, @Nullable ImageBorder_F32 border ) { + deriv.reshape(orig.width, orig.height); - if( BoofConcurrency.USE_CONCURRENT ) { - DerivativeLaplacian_Inner_MT.process(orig,deriv); + if (BoofConcurrency.USE_CONCURRENT) { + DerivativeLaplacian_Inner_MT.process(orig, deriv); } else { - DerivativeLaplacian_Inner.process(orig,deriv); + DerivativeLaplacian_Inner.process(orig, deriv); } - if( border != null ) { + if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernel_F32, border,deriv); + ConvolveJustBorder_General_SB.convolve(kernelXY_F32, border, deriv); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeType.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeType.java index b6499fa787..b599df600c 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeType.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/DerivativeType.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,22 +18,23 @@ package boofcv.alg.filter.derivative; -/** - * List of standard kernels used to compute the gradient of an image. - * - * @author Peter Abeles - */ +/// List of standard kernels used to compute the gradient of an image. +/// +/// The divisor for integer kernel is derived from the smoothing and differentiating which define is. For +/// Sobel its smoothing kernel is [1 2 1] and differentiated using [-1 0 1]. The smoothing has a sum of 4 and +/// differentiating has a distance of 2, so a divisor of 8. Three [1 0 1] has a smoothing kernel of [1] and +/// it is the differentiating kernel, so 1*2 = 2. public enum DerivativeType { - /** {@link GradientPrewitt} */ + /// [GradientPrewitt] PREWITT, - /** {@link GradientSobel} */ + /// [GradientSobel] SOBEL, - /** {@link GradientScharr} */ + /// [GradientScharr] SCHARR, - /** {@link GradientThree} */ + /// [GradientThree] THREE, - /** {@link GradientTwo0} */ + /// [GradientTwo0] TWO_0, - /** {@link GradientTwo1} */ + /// [GradientTwo1] TWO_1 } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GImageDerivativeOps.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GImageDerivativeOps.java index f8ea468206..5c5b8effb7 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GImageDerivativeOps.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GImageDerivativeOps.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -35,6 +35,19 @@ */ @SuppressWarnings({"unchecked", "rawtypes"}) public class GImageDerivativeOps { + /// From OpenCV and seems to match theory, as they state. Designed to produce the + /// true derivative of a ramp. 2^(ksize*2 - dx - dy - 2). This is what you need to divide the integer + /// kernels by. + /// + /// @param ksize Aperture size (kernel width/height), 1,3,5,7, ..etc + /// @param dx order of derivative in x direction. 0=smoothing only, 1=first derivative, 2=second + /// + public static int kernelDivisor( int ksize, int dx, int dy ) { + int scaleX = 1 << (ksize - dx - 1); + int scaleY = 1 << (ksize - dy - 1); + return scaleX * scaleY; + } + public static , D extends ImageGray> void laplace( I input, D output, BorderType borderType ) { ImageBorder border = BorderType.SKIP == borderType ? null : FactoryImageBorder.wrap(borderType, input); diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientPrewitt.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientPrewitt.java index eaf581c676..7e5e0a509a 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientPrewitt.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientPrewitt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -26,6 +26,7 @@ import boofcv.struct.border.ImageBorder; import boofcv.struct.border.ImageBorder_F32; import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel1D_S32; import boofcv.struct.convolve.Kernel2D; import boofcv.struct.convolve.Kernel2D_F32; import boofcv.struct.convolve.Kernel2D_S32; @@ -35,31 +36,51 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - * Operations for computing Prewitt image gradient. - * - * @author Peter Abeles - */ +/// Prewitt image derivative operator. Contains 2D kernels and specialized efficient functions for computing image +/// gradient. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// y-axis +/// +/// | -1 | -1 | -1 | +/// |----|----|----| +/// | 0 | 0 | 0 | +/// | 1 | 1 | 1 | +/// +/// x-axis +/// +/// | -1 | 0 | 1 | +/// |----|---|---| +/// | -1 | 0 | 1 | +/// | -1 | 0 | 1 | +/// +/// Prewitt operator is equivalent to convolving the image with the following 1D +/// kernels: `conv2([1/3 1/3 1/3], [-1 0 1])` public class GradientPrewitt { - public static Kernel2D_S32 kernelDerivX_I32 = new Kernel2D_S32(3, new int[]{-1, 0, 1, -1, 0, 1, -1, 0, 1}); - public static Kernel2D_S32 kernelDerivY_I32 = new Kernel2D_S32(3, new int[]{-1, -1, -1, 0, 0, 0, 1, 1, 1}); - public static Kernel2D_F32 kernelDerivX_F32 = new Kernel2D_F32( - 3, new float[]{-1f, 0, 1f, -1f, 0, 1f, -1f, 0, 1f}); - public static Kernel2D_F32 kernelDerivY_F32 = new Kernel2D_F32( - 3, new float[]{-1f, -1f, -1f, 0, 0, 0, 1f, 1f, 1f}); + /// Integer divisor to ensure brightness does not change + public static final int divisor = 6; + public static Kernel1D_S32 kernelSmooth = new Kernel1D_S32(3, new int[]{1, 1, 1}); + public static Kernel1D_S32 kernelDiff = new Kernel1D_S32(3, new int[]{-1, 0, 1}); + public static Kernel2D_S32 kernelX_I32 = new Kernel2D_S32(3, new int[]{-1, 0, 1, -1, 0, 1, -1, 0, 1}); + public static Kernel2D_S32 kernelY_I32 = new Kernel2D_S32(3, new int[]{-1, -1, -1, 0, 0, 0, 1, 1, 1}); + public static Kernel2D_F32 kernelX_F32 = new Kernel2D_F32( + 3, new float[]{-1f/6f, 0, 1f/6f, -1f/6f, 0, 1f/6f, -1f/6f, 0, 1f/6f}); + public static Kernel2D_F32 kernelY_F32 = new Kernel2D_F32( + 3, new float[]{-1f/6f, -1f/6f, -1f/6f, 0, 0, 0, 1f/6f, 1f/6f, 1f/6f}); - /** - * Returns the kernel for computing the derivative along the x-axis. - */ + /// Returns the kernel for computing the derivative along the x-axis. public static Kernel2D getKernelX( boolean isInteger ) { if (isInteger) - return kernelDerivX_I32; + return kernelX_I32; else - return kernelDerivX_F32; + return kernelX_F32; } public static , D extends ImageGray> void process( I input, D derivX, D derivY, - @Nullable ImageBorder border ) { + @Nullable ImageBorder border ) { switch (input.getImageType().getDataType()) { case U8 -> process((GrayU8)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); case S16 -> process((GrayS16)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); @@ -68,14 +89,12 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the derivative in the X and Y direction using an integer Prewitt edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Prewitt edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -87,19 +106,17 @@ public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullab if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_I32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_I32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_I32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_I32, border, derivY); } } - /** - * Computes the derivative in the X and Y direction using an integer Prewitt edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Prewitt edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -111,19 +128,17 @@ public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nulla if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_I32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_I32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_I32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_I32, border, derivY); } } - /** - * Computes the derivative in the X and Y direction using a floating point Prewitt edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using a floating point Prewitt edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -135,8 +150,8 @@ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nulla if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_F32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_F32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_F32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_F32, border, derivY); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientScharr.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientScharr.java index e78745cf51..dc5cda4c03 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientScharr.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientScharr.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -26,6 +26,7 @@ import boofcv.struct.border.ImageBorder; import boofcv.struct.border.ImageBorder_F32; import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel1D_S32; import boofcv.struct.convolve.Kernel2D; import boofcv.struct.convolve.Kernel2D_F32; import boofcv.struct.convolve.Kernel2D_S32; @@ -35,34 +36,50 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - * Implementation of the standard 3x3 Scharr operator. (1/32)*[-1;0;1]**[3 10 3] = (1/3)*[-3 10 -3;0 0 0;3 10 3]. - * This is by far the most widely used kernel, but apparently the original paper described more accurate kernels and - * kernels up to 5x5. Unfortunately, the thesis is in German [1] and other related papers require you to work - * through the math, so this implementation is sticking with the standard kernel only. - * - * Scharr is supposed to be more accurate then Sobel when dealing with optical flow. - * - * [1] Scharr, Hanno, 2000, Dissertation (in German), Optimal Operators in Digital Image Processing . - * - * @author Peter Abeles - */ +/// Scharr image derivative operator. Contains 2D kernels and specialized efficient functions for computing image +/// gradient. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// `[-1; 0; 1]` ∗∗ `[3 10 3]` = +/// +/// (1/32) · +/// +/// | -3 | -10 | -3 | +/// |-----|------|-----| +/// | 0 | 0 | 0 | +/// | 3 | 10 | 3 | +/// +/// where `∗∗` denotes 2D convolution and `·` denotes element-wise scalar multiplication. +/// +/// This is by far the most widely used kernel, but apparently the original paper described +/// more accurate kernels and kernels up to 5×5. Unfortunately, the thesis is in German \[1\] +/// and other related papers require you to work through the math, so this implementation is +/// sticking with the standard kernel only. +/// +/// Scharr is supposed to be more accurate than Sobel when dealing with optical flow. +/// +/// \[1\] Scharr, Hanno, 2000, Dissertation (in German), *Optimal Operators in Digital Image Processing*. public class GradientScharr { - public static Kernel2D_S32 kernelDerivX_I32 = new Kernel2D_S32(3, new int[]{-3, 0, 3, -10, 0, 10, -3, 0, 3}); - public static Kernel2D_S32 kernelDerivY_I32 = new Kernel2D_S32(3, new int[]{-3, -10, -3, 0, 0, 0, 3, 10, 3}); - public static Kernel2D_F32 kernelDerivX_F32 = new Kernel2D_F32( + /// Integer divisor to ensure brightness does not change + public static final int divisor = 32; + public static Kernel1D_S32 kernelSmooth = new Kernel1D_S32(3, new int[]{3, 10, 3}); + public static Kernel1D_S32 kernelDiff = new Kernel1D_S32(3, new int[]{-1, 0, 1}); + public static Kernel2D_S32 kernelX_I32 = new Kernel2D_S32(3, new int[]{-3, 0, 3, -10, 0, 10, -3, 0, 3}); + public static Kernel2D_S32 kernelY_I32 = new Kernel2D_S32(3, new int[]{-3, -10, -3, 0, 0, 0, 3, 10, 3}); + public static Kernel2D_F32 kernelX_F32 = new Kernel2D_F32( 3, new float[]{-0.093750f, 0, 0.093750f, -0.31250f, 0, 0.31250f, -0.093750f, 0, 0.093750f}); - public static Kernel2D_F32 kernelDerivY_F32 = new Kernel2D_F32( + public static Kernel2D_F32 kernelY_F32 = new Kernel2D_F32( 3, new float[]{-0.093750f, -0.31250f, -0.093750f, 0, 0, 0, 0.093750f, 0.31250f, 0.093750f}); - /** - * Returns the kernel for computing the derivative along the x-axis. - */ + /// Returns the kernel for computing the derivative along the x-axis. public static Kernel2D getKernelX( boolean isInteger ) { if (isInteger) - return kernelDerivX_I32; + return kernelX_I32; else - return kernelDerivX_F32; + return kernelX_F32; } public static , D extends ImageGray> void process( I input, D derivX, D derivY, @@ -75,14 +92,12 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the derivative in the X and Y direction using an integer Sobel edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Sobel edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -94,19 +109,17 @@ public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullab if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_I32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_I32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_I32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_I32, border, derivY); } } - /** - * Computes the derivative in the X and Y direction using an integer Sobel edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Sobel edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -118,19 +131,17 @@ public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nulla if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_I32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_I32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_I32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_I32, border, derivY); } } - /** - * Computes the derivative in the X and Y direction using an integer Sobel edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Sobel edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -143,8 +154,8 @@ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nulla if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_F32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_F32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_F32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_F32, border, derivY); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientSobel.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientSobel.java index b2b79fa339..9712f5abb3 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientSobel.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientSobel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -28,6 +28,7 @@ import boofcv.struct.border.ImageBorder; import boofcv.struct.border.ImageBorder_F32; import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel1D_S32; import boofcv.struct.convolve.Kernel2D; import boofcv.struct.convolve.Kernel2D_F32; import boofcv.struct.convolve.Kernel2D_S32; @@ -37,53 +38,49 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - *

- * Computes the image's first derivative along the x and y axises using the Sobel operator. - *

- *

- * The Sobel kernel weights the inner most pixels more than ones farther away. This tends to produce better results, - * but not as good as a gaussian kernel with larger kernel. However, it can be optimized so that it is much faster than a Gaussian. - *

- *

- * For integer images, the derivatives in the x and y direction are computed by convolving the following kernels:
- *
- * y-axis
- * - * - * - * - * - *
-0.25 -0.5 -0.25
0 0 0
0.25 0.5 0.25
- * - * - * - * - * - * @author Peter Abeles - */ +/// Sobel first derivative operator. Contains 2D kernels and specialized efficient functions for computing image +/// gradient. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// For integer images, the derivatives in the x and y direction are computed by convolving the following kernels: +/// +/// y-axis +/// +/// | -0.25 | -0.5 | -0.25 | +/// |-------|------|-------| +/// | 0 | 0 | 0 | +/// | 0.25 | 0.5 | 0.25 | +/// +/// x-axis +/// +/// | -1 | 0 | 1 | +/// |----|---|---| +/// | -2 | 0 | 2 | +/// | -1 | 0 | 1 | +/// +/// Sobel operator is equivalent to convolving the image with the following 1D +/// kernels: `conv2([0.25 0.5 0.25], [-1 0 1])` public class GradientSobel { + /// Integer divisor to ensure brightness does not change + public static final int divisor = 8; + public static Kernel1D_S32 kernelSmooth = new Kernel1D_S32(3, new int[]{1, 2, 1}); + public static Kernel1D_S32 kernelDiff = new Kernel1D_S32(3, new int[]{-1, 0, 1}); + public static Kernel2D_S32 kernelX_I32 = new Kernel2D_S32(3, new int[]{-1, 0, 1, -2, 0, 2, -1, 0, 1}); + public static Kernel2D_S32 kernelY_I32 = new Kernel2D_S32(3, new int[]{-1, -2, -1, 0, 0, 0, 1, 2, 1}); + public static Kernel2D_F32 kernelX_F32 = new Kernel2D_F32( + 3, new float[]{-0.125f, 0, 0.125f, -0.25f, 0, 0.25f, -0.125f, 0, 0.125f}); + public static Kernel2D_F32 kernelY_F32 = new Kernel2D_F32( + 3, new float[]{-0.125f, -0.25f, -0.125f, 0, 0, 0, 0.125f, 0.25f, 0.125f}); - public static Kernel2D_S32 kernelDerivX_I32 = new Kernel2D_S32(3, new int[]{-1, 0, 1, -2, 0, 2, -1, 0, 1}); - public static Kernel2D_S32 kernelDerivY_I32 = new Kernel2D_S32(3, new int[]{-1, -2, -1, 0, 0, 0, 1, 2, 1}); - public static Kernel2D_F32 kernelDerivX_F32 = new Kernel2D_F32( - 3, new float[]{-0.25f, 0, 0.25f, -0.5f, 0, 0.5f, -0.25f, 0, 0.25f}); - public static Kernel2D_F32 kernelDerivY_F32 = new Kernel2D_F32( - 3, new float[]{-0.25f, -0.5f, -0.25f, 0, 0, 0, 0.25f, 0.5f, 0.25f}); - - /** - * Returns the kernel for computing the derivative along the x-axis. - */ + /// Returns the kernel for computing the derivative along the x-axis. public static Kernel2D getKernelX( boolean isInteger ) { if (isInteger) - return kernelDerivX_I32; + return kernelX_I32; else - return kernelDerivX_F32; + return kernelX_F32; } public static , D extends ImageGray> void process( I input, D derivX, D derivY, @@ -96,14 +93,12 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the derivative in the X and Y direction using an integer Sobel edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Sobel edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -115,19 +110,17 @@ public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullab if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_I32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_I32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_I32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_I32, border, derivY); } } - /** - * Computes the derivative in the X and Y direction using an integer Sobel edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Sobel edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -139,19 +132,17 @@ public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nulla if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_I32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_I32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_I32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_I32, border, derivY); } } - /** - * Computes the derivative in the X and Y direction using an integer Sobel edge detector. - * - * @param orig Input image. Not modified. - * @param derivX Storage for image derivative along the x-axis. Modified. - * @param derivY Storage for image derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative in the X and Y direction using an integer Sobel edge detector. + /// + /// @param orig Input image. Not modified. + /// @param derivX Storage for image derivative along the x-axis. Modified. + /// @param derivY Storage for image derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); @@ -164,8 +155,8 @@ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nulla if (border != null) { border.setImage(orig); - ConvolveJustBorder_General_SB.convolve(kernelDerivX_F32, border, derivX); - ConvolveJustBorder_General_SB.convolve(kernelDerivY_F32, border, derivY); + ConvolveJustBorder_General_SB.convolve(kernelX_F32, border, derivX); + ConvolveJustBorder_General_SB.convolve(kernelY_F32, border, derivY); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientThree.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientThree.java index 94623b17d9..685fa4787e 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientThree.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientThree.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -31,38 +31,36 @@ import boofcv.struct.image.*; import org.jetbrains.annotations.Nullable; -/** - *

- * Computes the image's first derivative along the x and y axises using [-1 0 1] kernel. - *

- *

- * Th 1-D kernel allows the image's gradient to be computed efficiently but is more sensitive to local noise. - *

- *

- * For example in an integer image:
- * derivX(x,y) = img(x+1,y) - img(x-1,y)
- * derivY(x,y) = img(x,y+1) - img(x,y-1)
- *

- * - * @author Peter Abeles - */ +/// Central-difference first derivative. 3x1 kernel: `[-1 0 1]`. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// For example in an integer image: +/// +/// ``` +/// derivX(x,y) = img(x+1,y) - img(x-1,y) +/// derivY(x,y) = img(x,y+1) - img(x,y-1) +/// ``` public class GradientThree { - - public static Kernel1D_S32 kernelDeriv_I32 = new Kernel1D_S32(new int[]{-1, 0, 1}, 3); - public static Kernel1D_F32 kernelDeriv_F32 = new Kernel1D_F32(new float[]{-0.5f, 0, 0.5f}, 3); - - /** - * Returns the kernel for computing the derivative along the x-axis. - */ + /// Integer divisor to ensure brightness does not change + public static final int divisor = 2; + public static Kernel1D_S32 kernelSmooth = new Kernel1D_S32(1, new int[]{1}); + public static Kernel1D_S32 kernelDiff = new Kernel1D_S32(3, new int[]{-1, 0, 1}); + public static Kernel1D_S32 kernel_I32 = new Kernel1D_S32(3, new int[]{-1, 0, 1}); + public static Kernel1D_F32 kernel_F32 = new Kernel1D_F32(3, new float[]{-0.5f, 0, 0.5f}); + + /// Returns the kernel for computing the derivative along the x-axis. public static Kernel1D getKernelX( boolean isInteger ) { if (isInteger) - return kernelDeriv_I32; + return kernel_I32; else - return kernelDeriv_F32; + return kernel_F32; } - public static , D extends ImageGray> void process( I input, D derivX, D derivY, - @Nullable ImageBorder border ) { + public static , D extends ImageGray> + void process( I input, D derivX, D derivY, @Nullable ImageBorder border ) { switch (input.getImageType().getDataType()) { case U8 -> process((GrayU8)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); case S16 -> process((GrayS16)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); @@ -71,17 +69,13 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the derivative of an {@link GrayU8} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ - public static void process( GrayU8 orig, - GrayS16 derivX, - GrayS16 derivY, @Nullable ImageBorder_S32 border ) { + /// Computes the derivative of an [GrayU8] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. + public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -91,22 +85,18 @@ public static void process( GrayU8 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayU8} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ - public static void process( GrayU8 orig, - GrayS32 derivX, - GrayS32 derivY, @Nullable ImageBorder_S32 border ) { + /// Computes the derivative of an [GrayU8] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. + public static void process( GrayU8 orig, GrayS32 derivX, GrayS32 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -116,22 +106,18 @@ public static void process( GrayU8 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayS16} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ - public static void process( GrayS16 orig, - GrayS16 derivX, - GrayS16 derivY, @Nullable ImageBorder_S32 border ) { + /// Computes the derivative of an [GrayS16] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. + public static void process( GrayS16 orig, GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -141,22 +127,18 @@ public static void process( GrayS16 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayF32} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ - public static void process( GrayF32 orig, - GrayF32 derivX, - GrayF32 derivY, @Nullable ImageBorder_F32 border ) { + /// Computes the derivative of an [GrayF32] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. + public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -165,8 +147,8 @@ public static void process( GrayF32 orig, GradientThree_Standard.process(orig, derivX, derivY); } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_F32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_F32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_F32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_F32, border); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo0.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo0.java index b01a8357a1..890fdf8aad 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo0.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo0.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -34,39 +34,37 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - *

- * Computes the image's first derivative along the x and y axises using [-1 1] kernel, where the "center" of the - * kernel is on the -1. - *

- *

- * The 1-D kernel allows the image's gradient to be computed efficiently but is more sensitive to local noise. - *

- *

- * For example in an integer image:
- * derivX(x,y) = img(x+1,y) - img(x,y)
- * derivY(x,y) = img(x,y+1) - img(x,y)
- *

- * - * @author Peter Abeles - */ +/// Forward difference image derivative. `[-1 1]` kernel, where the "center" of the kernel is on the -1. +/// This kernel causes a +0.5 pixel shift in image gradient, which is undesirable in most application. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// For example in an integer image: +/// +/// ``` +/// derivX(x,y) = img(x+1,y) - img(x,y) +/// derivY(x,y) = img(x,y+1) - img(x,y) +/// ``` public class GradientTwo0 { - - public static Kernel1D_S32 kernelDeriv_I32 = new Kernel1D_S32(new int[]{-1, 1}, 2, 0); - public static Kernel1D_F32 kernelDeriv_F32 = new Kernel1D_F32(new float[]{-1, 1}, 2, 0); - - /** - * Returns the kernel for computing the derivative along the x-axis. - */ + /// Integer divisor to ensure brightness does not change + public static final int divisor = 1; + public static Kernel1D_S32 kernelSmooth = new Kernel1D_S32(1, new int[]{1}); + public static Kernel1D_S32 kernelDiff = new Kernel1D_S32(2, 0, new int[]{-1, 1}); + public static Kernel1D_S32 kernel_I32 = new Kernel1D_S32(2, 0, new int[]{-1, 1}); + public static Kernel1D_F32 kernel_F32 = new Kernel1D_F32(2, 0, new float[]{-1, 1}); + + /// Returns the kernel for computing the derivative along the x-axis. public static Kernel1D getKernelX( boolean isInteger ) { if (isInteger) - return kernelDeriv_I32; + return kernel_I32; else - return kernelDeriv_F32; + return kernel_F32; } public static , D extends ImageGray> void process( I input, D derivX, D derivY, - @Nullable ImageBorder border ) { + @Nullable ImageBorder border ) { switch (input.getImageType().getDataType()) { case U8 -> process((GrayU8)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); case S16 -> process((GrayS16)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); @@ -75,16 +73,14 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the derivative of an {@link GrayU8} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative of an [GrayU8] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, - GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { + GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -94,21 +90,19 @@ public static void process( GrayU8 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayS16} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative of an [GrayS16] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayS16 orig, - GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { + GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -119,21 +113,19 @@ public static void process( GrayS16 orig, if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayF32} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative of an [GrayF32] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, - GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { + GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -143,8 +135,8 @@ public static void process( GrayF32 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_F32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_F32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_F32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_F32, border); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo1.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo1.java index 57e9f18d19..a02c39ced5 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo1.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/GradientTwo1.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -34,39 +34,37 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - *

- * Computes the image's first derivative along the x and y axises using [-1 1] kernel, where the "center" of the - * kernel is on the 1. - *

- *

- * The 1-D kernel allows the image's gradient to be computed efficiently but is more sensitive to local noise. - *

- *

- * For example in an integer image:
- * derivX(x,y) = img(x,y) - img(x-1,y)
- * derivY(x,y) = img(x,y) - img(x,y-1)
- *

- * - * @author Peter Abeles - */ +/// Backwards difference image derivative. `[-1 1]` kernel, where the "center" of the kernel is on the 1. +/// This kernel causes a -0.5 pixel shift in image gradient, which is undesirable in most application. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// For example in an integer image: +/// +/// ``` +/// derivX(x,y) = img(x,y) - img(x-1,y) +/// derivY(x,y) = img(x,y) - img(x,y-1) +/// ``` public class GradientTwo1 { + /// Integer divisor to ensure brightness does not change + public static final int divisor = 1; + public static Kernel1D_S32 kernelSmooth = new Kernel1D_S32(1, new int[]{1}); + public static Kernel1D_S32 kernelDiff = new Kernel1D_S32(2, 1, new int[]{-1, 1}); + public static Kernel1D_S32 kernel_I32 = new Kernel1D_S32(2, 1, new int[]{-1, 1}); + public static Kernel1D_F32 kernel_F32 = new Kernel1D_F32(2, 1, new float[]{-1, 1}); - public static Kernel1D_S32 kernelDeriv_I32 = new Kernel1D_S32(new int[]{-1, 1}, 2, 1); - public static Kernel1D_F32 kernelDeriv_F32 = new Kernel1D_F32(new float[]{-1, 1}, 2, 1); - - /** - * Returns the kernel for computing the derivative along the x-axis. - */ + /// Returns the kernel for computing the derivative along the x-axis. public static Kernel1D getKernelX( boolean isInteger ) { if (isInteger) - return kernelDeriv_I32; + return kernel_I32; else - return kernelDeriv_F32; + return kernel_F32; } public static , D extends ImageGray> void process( I input, D derivX, D derivY, - @Nullable ImageBorder border ) { + @Nullable ImageBorder border ) { switch (input.getImageType().getDataType()) { case U8 -> process((GrayU8)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); case S16 -> process((GrayS16)input, (GrayS16)derivX, (GrayS16)derivY, (ImageBorder_S32)border); @@ -75,16 +73,14 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the derivative of an {@link GrayU8} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative of an [GrayU8] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, - GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { + GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -94,21 +90,19 @@ public static void process( GrayU8 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayS16} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative of an [GrayS16] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayS16 orig, - GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { + GrayS16 derivX, GrayS16 derivY, @Nullable ImageBorder_S32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -118,21 +112,19 @@ public static void process( GrayS16 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_I32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_I32, border); } } - /** - * Computes the derivative of an {@link GrayF32} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivX Derivative along the x-axis. Modified. - * @param derivY Derivative along the y-axis. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the derivative of an [GrayF32] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivX Derivative along the x-axis. Modified. + /// @param derivY Derivative along the y-axis. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, - GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { + GrayF32 derivX, GrayF32 derivY, @Nullable ImageBorder_F32 border ) { InputSanityCheck.reshapeOneIn(orig, derivX, derivY); if (BoofConcurrency.USE_CONCURRENT) { @@ -142,8 +134,8 @@ public static void process( GrayF32 orig, } if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernelDeriv_F32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernelDeriv_F32, border); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivX, kernel_F32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivY, kernel_F32, border); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianFromGradient.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianFromGradient.java index a600d9b48d..c8a83c1d56 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianFromGradient.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianFromGradient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -66,9 +66,9 @@ public static void hessianPrewitt( GrayS16 inputDerivX, GrayS16 inputDerivY, GradientPrewitt.process(inputDerivX, derivXX, derivXY, border); if (border != null) - ConvolveImage.convolve(GradientPrewitt.kernelDerivY_I32, inputDerivY, derivYY, border); + ConvolveImage.convolve(GradientPrewitt.kernelY_I32, inputDerivY, derivYY, border); else - ConvolveImageNoBorder.convolve(GradientPrewitt.kernelDerivY_I32, inputDerivY, derivYY); + ConvolveImageNoBorder.convolve(GradientPrewitt.kernelY_I32, inputDerivY, derivYY); } /** @@ -89,9 +89,9 @@ public static void hessianPrewitt( GrayF32 inputDerivX, GrayF32 inputDerivY, GradientPrewitt.process(inputDerivX, derivXX, derivXY, border); if (border != null) - ConvolveImage.convolve(GradientPrewitt.kernelDerivY_F32, inputDerivY, derivYY, border); + ConvolveImage.convolve(GradientPrewitt.kernelY_F32, inputDerivY, derivYY, border); else - ConvolveImageNoBorder.convolve(GradientPrewitt.kernelDerivY_F32, inputDerivY, derivYY); + ConvolveImageNoBorder.convolve(GradientPrewitt.kernelY_F32, inputDerivY, derivYY); } /** @@ -112,10 +112,10 @@ public static void hessianSobel( GrayS16 inputDerivX, GrayS16 inputDerivY, GradientSobel.process(inputDerivX, derivXX, derivXY, border); if (border != null) - ConvolveImage.convolve(GradientSobel.kernelDerivY_I32, inputDerivY, derivYY, + ConvolveImage.convolve(GradientSobel.kernelY_I32, inputDerivY, derivYY, new ImageBorder1D_S32(BorderIndex1D_Extend::new)); else - ConvolveImageNoBorder.convolve(GradientSobel.kernelDerivY_I32, inputDerivY, derivYY); + ConvolveImageNoBorder.convolve(GradientSobel.kernelY_I32, inputDerivY, derivYY); } /** @@ -136,10 +136,10 @@ public static void hessianSobel( GrayF32 inputDerivX, GrayF32 inputDerivY, GradientSobel.process(inputDerivX, derivXX, derivXY, border); if (border != null) - ConvolveImage.convolve(GradientSobel.kernelDerivY_F32, inputDerivY, derivYY, + ConvolveImage.convolve(GradientSobel.kernelY_F32, inputDerivY, derivYY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); else - ConvolveImageNoBorder.convolve(GradientSobel.kernelDerivY_F32, inputDerivY, derivYY); + ConvolveImageNoBorder.convolve(GradientSobel.kernelY_F32, inputDerivY, derivYY); } /** @@ -160,10 +160,10 @@ public static void hessianThree( GrayS16 inputDerivX, GrayS16 inputDerivY, GradientThree.process(inputDerivX, derivXX, derivXY, border); if (border != null) - ConvolveImage.vertical(GradientThree.kernelDeriv_I32, inputDerivY, derivYY, + ConvolveImage.vertical(GradientThree.kernel_I32, inputDerivY, derivYY, new ImageBorder1D_S32(BorderIndex1D_Extend::new)); else - ConvolveImageNoBorder.vertical(GradientThree.kernelDeriv_I32, inputDerivY, derivYY); + ConvolveImageNoBorder.vertical(GradientThree.kernel_I32, inputDerivY, derivYY); } /** @@ -184,9 +184,9 @@ public static void hessianThree( GrayF32 inputDerivX, GrayF32 inputDerivY, GradientThree.process(inputDerivX, derivXX, derivXY, border); if (border != null) - ConvolveImage.vertical(GradientThree.kernelDeriv_F32, inputDerivY, derivYY, + ConvolveImage.vertical(GradientThree.kernel_F32, inputDerivY, derivYY, new ImageBorder1D_F32(BorderIndex1D_Extend::new)); else - ConvolveImageNoBorder.vertical(GradientThree.kernelDeriv_F32, inputDerivY, derivYY); + ConvolveImageNoBorder.vertical(GradientThree.kernel_F32, inputDerivY, derivYY); } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianSobel.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianSobel.java index 53659950bc..e87fbd35f6 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianSobel.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianSobel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -32,40 +32,37 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - *

- * Computes the second derivative (Hessian) of an image using. This hessian is derived using the {@link GradientSobel} - * gradient function. - *

- * - *

- * WARNING: It is computationally more expensive to compute the Hessian with this operation than applying the Sobel - * gradient operator multiple times. However, this does not require the creation additional storage to save - * intermediate results. - *

- * - *

- * Kernel for ∂ 2f/∂ y2: - *

-1 0 1
-2 0 2
-1 0 1
- * - * - * - * - * - * 2f/∂ x∂y is:
- *
1 4 6 4 1
0 0 0 0 0
-2 -8 -12 -8 -2
0 0 0 0 0
1 4 6 4 1
- * - * - * - * - * - * - * - * @author Peter Abeles - */ +/// Second order (Hessian) Sobel image derivatives. This hessian is derived using the [GradientSobel] +/// gradient function. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// WARNING: It is computationally more expensive to compute the Hessian with this operation than applying the Sobel +/// gradient operator multiple times. However, this does not require the creation additional storage to save +/// intermediate results. +/// +/// Kernel for ∂²f/∂y²: +/// +/// | 1 | 4 | 6 | 4 | 1 | +/// |----|----|-----|----|----| +/// | 0 | 0 | 0 | 0 | 0 | +/// | -2 | -8 | -12 | -8 | -2 | +/// | 0 | 0 | 0 | 0 | 0 | +/// | 1 | 4 | 6 | 4 | 1 | +/// +/// `[1 0 -2 0 1]` and ∂²f/∂x∂y is: +/// +/// | 1 | 2 | 0 | -2 | -1 | +/// |----|----|---|----|----| +/// | 2 | 4 | 0 | -4 | -2 | +/// | 0 | 0 | 0 | 0 | 0 | +/// | -2 | -4 | 0 | 4 | 2 | +/// | -1 | -2 | 0 | 2 | 1 | public class HessianSobel { + /// Integer divisor to ensure brightness does not change + public static final int divisor = 64; public static Kernel2D_S32 kernelYY_I32 = new Kernel2D_S32(5, new int[] {1, 4, 6, 4, 1, @@ -86,23 +83,23 @@ public class HessianSobel { -2, -4, 0, 4, 2, -1, -2, 0, 2, 1}); public static Kernel2D_F32 kernelYY_F32 = new Kernel2D_F32(5, new float[] - {1, 4, 6, 4, 1, + {1.0f/64.0f, 4.0f/64.0f, 6.0f/64.0f, 4.0f/64.0f, 1.0f/64.0f, 0, 0, 0, 0, 0, - -2, -8, -12, -8, -2, + -2.0f/64.0f, -8.0f/64.0f, -12.0f/64.0f, -8.0f/64.0f, -2.0f/64.0f, 0, 0, 0, 0, 0, - 1, 4, 6, 4, 1}); + 1.0f/64.0f, 4.0f/64.0f, 6.0f/64.0f, 4.0f/64.0f, 1.0f/64.0f}); public static Kernel2D_F32 kernelXX_F32 = new Kernel2D_F32(5, new float[] - {1, 0, -2, 0, 1, - 4, 0, -8, 0, 4, - 6, 0, -12, 0, 6, - 4, 0, -8, 0, 4, - 1, 0, -2, 0, 1}); + {1.0f/64.0f, 0, -2.0f/64.0f, 0, 1.0f/64.0f, + 4.0f/64.0f, 0, -8.0f/64.0f, 0, 4.0f/64.0f, + 6.0f/64.0f, 0, -12.0f/64.0f, 0, 6.0f/64.0f, + 4.0f/64.0f, 0, -8.0f/64.0f, 0, 4.0f/64.0f, + 1.0f/64.0f, 0, -2.0f/64.0f, 0, 1.0f/64.0f}); public static Kernel2D_F32 kernelXY_F32 = new Kernel2D_F32(5, new float[] - {1, 2, 0, -2, -1, - 2, 4, 0, -4, -2, + {1f/64f, 2f/64f, 0, -2f/64f, -1f/64f, + 2f/64f, 4f/64f, 0, -4f/64f, -2f/64f, 0, 0, 0, 0, 0, - -2, -4, 0, 4, 2, - -1, -2, 0, 2, 1}); + -2f/64f, -4f/64f, 0, 4f/64f, 2f/64f, + -1f/64f, -2f/64f, 0, 2f/64f, 1f/64f}); public static , D extends ImageGray> void process( I input, D derivXX, D derivYY, D derivXY, @@ -114,15 +111,13 @@ public static , D extends ImageGray> void process( I i } } - /** - * Computes the image's second derivatives. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivXX Second derivative along the x-axis. Modified. - * @param derivYY Second derivative along the y-axis. Modified. - * @param derivXY Second cross derivative. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the image's second derivatives. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivXX Second derivative along the x-axis. Modified. + /// @param derivYY Second derivative along the y-axis. Modified. + /// @param derivXY Second cross derivative. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, GrayS16 derivXX, GrayS16 derivYY, GrayS16 derivXY, @Nullable ImageBorder_S32 border ) { @@ -137,15 +132,13 @@ public static void process( GrayU8 orig, } } - /** - * Computes the image's second derivatives. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivXX Second derivative along the x-axis. Modified. - * @param derivYY Second derivative along the y-axis. Modified. - * @param derivXY Second cross derivative. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the image's second derivatives. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivXX Second derivative along the x-axis. Modified. + /// @param derivYY Second derivative along the y-axis. Modified. + /// @param derivXY Second cross derivative. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, GrayF32 derivXX, GrayF32 derivYY, GrayF32 derivXY, @Nullable ImageBorder_F32 border ) { diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThree.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThree.java index 286003b825..036ed3a179 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThree.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThree.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -34,37 +34,33 @@ import boofcv.struct.image.ImageGray; import org.jetbrains.annotations.Nullable; -/** - *

- * Computes the second derivative (Hessian) of an image using. This hessian is derived by using the same gradient - * function used in {@link GradientThree}, which uses a kernel of [-1 0 1]. - *

- * - *

- * WARNING: It is computationally more expensive to compute the Hessian with this operation than applying the - * gradient operator multiple times. However, this does not require the creation additional storage to save - * intermediate results. - *

- * - *

- * Kernel for ∂ 2f/∂ x2 and ∂2f /∂ y2 is - * [1 0 -2 0 1] and ∂2f/∂ x∂y is:
- *

1 2 0 -2 -1
2 4 0 -4 -2
0 0 0 0 0
-2 -4 0 4 2
-1 -2 0 2 1
- * - * - * - *
1 0 -1
0 0 0
-1 0 1
- *

- * - * @author Peter Abeles - */ +/// Central-difference second derivative (Hessian). This hessian is derived by using the same gradient +/// function used in [GradientThree], which uses a kernel of `[-1 0 1]`. +/// +/// For integer images, divide the result by [#divisor] for proper scaling. Floating +/// point kernels already include this scaling; without it, output magnitudes are +/// inflated by that factor. +/// +/// WARNING: It is computationally more expensive to compute the Hessian with this operation than applying the +/// gradient operator multiple times. However, this does not require the creation additional storage to save +/// intermediate results. +/// +/// Kernel for ∂²f/∂x² and ∂²f/∂y² is `[1 0 -2 0 1]` and ∂²f/∂x∂y is: +/// +/// | | | | +/// |----|----|----| +/// | 1 | 0 | -1 | +/// | 0 | 0 | 0 | +/// | -1 | 0 | 1 | public class HessianThree { + /// Integer divisor to ensure brightness does not change + public static final int divisor = 4; - public static Kernel1D_S32 kernelXXYY_I32 = new Kernel1D_S32(new int[]{1, 0, -2, 0, 1}, 5); - public static Kernel2D_S32 kernelCross_I32 = new Kernel2D_S32(3, new int[]{1, 0, -1, 0, 0, 0, -1, 0, 1}); + public static Kernel1D_S32 kernelXX_I32 = new Kernel1D_S32(5, new int[]{1, 0, -2, 0, 1}); + public static Kernel2D_S32 kernelXY_I32 = new Kernel2D_S32(3, new int[]{1, 0, -1, 0, 0, 0, -1, 0, 1}); - public static Kernel1D_F32 kernelXXYY_F32 = new Kernel1D_F32(new float[]{0.5f, 0, -1, 0, 0.5f}, 5); - public static Kernel2D_F32 kernelCross_F32 = new Kernel2D_F32(3, new float[]{0.5f, 0, -0.5f, 0, 0, 0, -0.5f, 0, 0.5f}); + public static Kernel1D_F32 kernelXX_F32 = new Kernel1D_F32(5, new float[]{0.5f, 0, -1, 0, 0.5f}); + public static Kernel2D_F32 kernelXY_F32 = new Kernel2D_F32(3, new float[]{0.5f, 0, -0.5f, 0, 0, 0, -0.5f, 0, 0.5f}); public static , D extends ImageGray> void process( I input, D derivXX, D derivYY, D derivXY, @@ -76,17 +72,13 @@ public static , D extends ImageGray> void process( I i } } - /** - *

- * Computes the second derivative of an {@link GrayU8} along the x and y axes. - *

- * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivXX Second derivative along the x-axis. Modified. - * @param derivYY Second derivative along the y-axis. Modified. - * @param derivXY Second cross derivative. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the second derivative of an [GrayU8] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivXX Second derivative along the x-axis. Modified. + /// @param derivYY Second derivative along the y-axis. Modified. + /// @param derivXY Second cross derivative. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayU8 orig, GrayS16 derivXX, GrayS16 derivYY, GrayS16 derivXY, @Nullable ImageBorder_S32 border ) { @@ -94,21 +86,19 @@ public static void process( GrayU8 orig, HessianThree_Standard.process(orig, derivXX, derivYY, derivXY); if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivXX, kernelXXYY_I32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivYY, kernelXXYY_I32, border); - ConvolveJustBorder_General_SB.convolve(kernelCross_I32, border, derivXY); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivXX, kernelXX_I32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivYY, kernelXX_I32, border); + ConvolveJustBorder_General_SB.convolve(kernelXY_I32, border, derivXY); } } - /** - * Computes the second derivative of an {@link GrayU8} along the x and y axes. - * - * @param orig Which which is to be differentiated. Not Modified. - * @param derivXX Second derivative along the x-axis. Modified. - * @param derivYY Second derivative along the y-axis. Modified. - * @param derivXY Second cross derivative. Modified. - * @param border Specifies how the image border is handled. If null the border is not processed. - */ + /// Computes the second derivative of an [GrayU8] along the x and y axes. + /// + /// @param orig Input image that is differentiated. Not Modified. + /// @param derivXX Second derivative along the x-axis. Modified. + /// @param derivYY Second derivative along the y-axis. Modified. + /// @param derivXY Second cross derivative. Modified. + /// @param border Specifies how the image border is handled. If null the border is not processed. public static void process( GrayF32 orig, GrayF32 derivXX, GrayF32 derivYY, GrayF32 derivXY, @Nullable ImageBorder_F32 border ) { @@ -116,9 +106,9 @@ public static void process( GrayF32 orig, HessianThree_Standard.process(orig, derivXX, derivYY, derivXY); if (border != null) { - DerivativeHelperFunctions.processBorderHorizontal(orig, derivXX, kernelXXYY_F32, border); - DerivativeHelperFunctions.processBorderVertical(orig, derivYY, kernelXXYY_F32, border); - ConvolveJustBorder_General_SB.convolve(kernelCross_F32, border, derivXY); + DerivativeHelperFunctions.processBorderHorizontal(orig, derivXX, kernelXX_F32, border); + DerivativeHelperFunctions.processBorderVertical(orig, derivYY, kernelXX_F32, border); + ConvolveJustBorder_General_SB.convolve(kernelXY_F32, border, derivXY); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThreeDeterminant.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThreeDeterminant.java index bbabbe2d4e..f9719de578 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThreeDeterminant.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/HessianThreeDeterminant.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -43,6 +43,9 @@ * @author Peter Abeles */ public class HessianThreeDeterminant { + /// Divisor comes from differentiating via (h(x-1) + h(x+1))/2 twice + public static final int divisor = 4; + /** * Computes the Laplacian of input image. * diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientPrewitt_Shared.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientPrewitt_Shared.java index 288bc998f6..d5ce160785 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientPrewitt_Shared.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientPrewitt_Shared.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -112,6 +112,9 @@ public static void process( GrayF32 orig, final int height = orig.getHeight() - 1; final int strideSrc = orig.getStride(); + // multiplication is faster than division but slightly less accurate + final float div = 1f/6f; + //CONCURRENT_BELOW BoofConcurrency.loopFor(1,height,y->{ for (int y = 1; y < height; y++) { int indexSrc = orig.startIndex + orig.stride*y + 1; @@ -127,10 +130,10 @@ public static void process( GrayF32 orig, float v = data[indexSrc + strideSrc - 1] - data[indexSrc - strideSrc + 1]; //a32 + w + v - a12 - imgY[indexY++] = data[indexSrc + strideSrc] + w + v - data[indexSrc - strideSrc]; + imgY[indexY++] = (data[indexSrc + strideSrc] + w + v - data[indexSrc - strideSrc])*div; //a23 + w - v - a21 - imgX[indexX++] = data[indexSrc + 1] + w - v - data[indexSrc - 1]; + imgX[indexX++] = (data[indexSrc + 1] + w - v - data[indexSrc - 1])*div; } } //CONCURRENT_ABOVE }); diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Naive.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Naive.java index 911194a469..20f7ec1184 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Naive.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Naive.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -75,12 +75,12 @@ public static void process( GrayF32 orig, for (int x = 1; x < width - 1; x++) { - float dy = -(orig.get(x - 1, y - 1)*0.25F + orig.get(x, y - 1)*0.5F + orig.get(x + 1, y - 1)*0.25F); - dy += (orig.get(x - 1, y + 1)*0.25F + orig.get(x, y + 1)*0.5F + orig.get(x + 1, y + 1)*0.25F); + float dy = -(orig.get(x - 1, y - 1)*0.125F + orig.get(x, y - 1)*0.25F + orig.get(x + 1, y - 1)*0.125F); + dy += (orig.get(x - 1, y + 1)*0.125F + orig.get(x, y + 1)*0.25F + orig.get(x + 1, y + 1)*0.125F); - float dx = -(orig.get(x - 1, y - 1)*0.25F + orig.get(x - 1, y)*0.5F + orig.get(x - 1, y + 1)*0.25F); - dx += (orig.get(x + 1, y - 1)*0.25F + orig.get(x + 1, y)*0.5F + orig.get(x + 1, y + 1)*0.25F); + float dx = -(orig.get(x - 1, y - 1)*0.125F + orig.get(x - 1, y)*0.25F + orig.get(x - 1, y + 1)*0.125F); + dx += (orig.get(x + 1, y - 1)*0.125F + orig.get(x + 1, y)*0.25F + orig.get(x + 1, y + 1)*0.125F); derivX.set(x, y, dx); derivY.set(x, y, dy); diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Outer.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Outer.java index ef5db7862b..0907662760 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Outer.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_Outer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -151,11 +151,11 @@ public static void process( GrayF32 orig, int endX = width*y + width - 1; for (int index = width*y + 1; index < endX; index++) { - float v = (data[index + width + 1] - data[index - width - 1])*0.25F; - float w = (data[index + width - 1] - data[index - width + 1])*0.25F; + float v = (data[index + width + 1] - data[index - width - 1])*0.125F; + float w = (data[index + width - 1] - data[index - width + 1])*0.125F; - imgY[index] = (data[index + width] - data[index - width])*0.5F + v + w; - imgX[index] = (data[index + 1] - data[index - 1])*0.5F + v - w; + imgY[index] = (data[index + width] - data[index - width])*0.25F + v + w; + imgX[index] = (data[index + 1] - data[index - 1])*0.25F + v - w; } } //CONCURRENT_ABOVE }); diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_UnrolledOuter.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_UnrolledOuter.java index 9d015dd5ae..e0ab3bb23e 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_UnrolledOuter.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSobel_UnrolledOuter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -159,11 +159,11 @@ public static void process_F32(GrayF32 orig, a23 = data[index + 1]; a33 = data[index + width + 1]; - float v = (a33 - a11) * 0.25F; - float w = (a31 - a13) * 0.25F; + float v = (a33 - a11) * 0.125F; + float w = (a31 - a13) * 0.125F; - imgY[index] = (a32 - a12) * 0.5F + v + w; - imgX[index] = (a23 - a21) * 0.5F + v - w; + imgY[index] = (a32 - a12) * 0.25F + v + w; + imgX[index] = (a23 - a21) * 0.25F + v - w; index++; @@ -171,11 +171,11 @@ public static void process_F32(GrayF32 orig, a21 = data[index + 1]; a31 = data[index + width + 1]; - v = (a31 - a12) * 0.25F; - w = (a32 - a11) * 0.25F; + v = (a31 - a12) * 0.125F; + w = (a32 - a11) * 0.125F; - imgY[index] = (a33 - a13) * 0.5F + v + w; - imgX[index] = (a21 - a22) * 0.5F + v - w; + imgY[index] = (a33 - a13) * 0.25F + v + w; + imgX[index] = (a21 - a22) * 0.25F + v - w; index++; @@ -183,22 +183,22 @@ public static void process_F32(GrayF32 orig, a22 = data[index + 1]; a32 = data[index + width + 1]; - v = (a32 - a13) * 0.25F; - w = (a33 - a12) * 0.25F; + v = (a32 - a13) * 0.125F; + w = (a33 - a12) * 0.125F; - imgY[index] = (a31 - a11) * 0.5F + v + w; - imgX[index] = (a22 - a23) * 0.5F + v - w; + imgY[index] = (a31 - a11) * 0.25F + v + w; + imgX[index] = (a22 - a23) * 0.25F + v - w; index++; } // handle the remaining for (; index < endX; index++) { - float v = (data[index + width + 1] - data[index - width - 1]) * 0.25F; - float w = (data[index + width - 1] - data[index - width + 1]) * 0.25F; + float v = (data[index + width + 1] - data[index - width - 1]) * 0.125F; + float w = (data[index + width - 1] - data[index - width + 1]) * 0.125F; - imgY[index] = (data[index + width] - data[index - width]) * 0.5F + v + w; - imgX[index] = (data[index + 1] - data[index - 1]) * 0.5F + v - w; + imgY[index] = (data[index + width] - data[index - width]) * 0.25F + v + w; + imgX[index] = (data[index + 1] - data[index - 1]) * 0.25F + v - w; } } //CONCURRENT_ABOVE }); @@ -248,11 +248,11 @@ public static void process_F32_sub(GrayF32 orig, a23 = data[index + 1]; a33 = data[index + strideSrc + 1]; - float v = (a33 - a11) * 0.25F; - float w = (a31 - a13) * 0.25F; + float v = (a33 - a11) * 0.125F; + float w = (a31 - a13) * 0.125F; - imgY[indexY++] = (a32 - a12) * 0.5F + v + w; - imgX[indexX++] = (a23 - a21) * 0.5F + v - w; + imgY[indexY++] = (a32 - a12) * 0.25F + v + w; + imgX[indexX++] = (a23 - a21) * 0.25F + v - w; index++; @@ -260,11 +260,11 @@ public static void process_F32_sub(GrayF32 orig, a21 = data[index + 1]; a31 = data[index + strideSrc + 1]; - v = (a31 - a12) * 0.25F; - w = (a32 - a11) * 0.25F; + v = (a31 - a12) * 0.125F; + w = (a32 - a11) * 0.125F; - imgY[indexY++] = (a33 - a13) * 0.5F + v + w; - imgX[indexX++] = (a21 - a22) * 0.5F + v - w; + imgY[indexY++] = (a33 - a13) * 0.25F + v + w; + imgX[indexX++] = (a21 - a22) * 0.25F + v - w; index++; @@ -272,11 +272,11 @@ public static void process_F32_sub(GrayF32 orig, a22 = data[index + 1]; a32 = data[index + strideSrc + 1]; - v = (a32 - a13) * 0.25F; - w = (a33 - a12) * 0.25F; + v = (a32 - a13) * 0.125F; + w = (a33 - a12) * 0.125F; - imgY[indexY++] = (a31 - a11) * 0.5F + v + w; - imgX[indexX++] = (a22 - a23) * 0.5F + v - w; + imgY[indexY++] = (a31 - a11) * 0.25F + v + w; + imgX[indexX++] = (a22 - a23) * 0.25F + v - w; index++; @@ -285,11 +285,11 @@ public static void process_F32_sub(GrayF32 orig, // handle the remaining for (; index < endX; index++) { - float v = (data[index + strideSrc + 1] - data[index - strideSrc - 1]) * 0.25F; - float w = (data[index + strideSrc - 1] - data[index - strideSrc + 1]) * 0.25F; + float v = (data[index + strideSrc + 1] - data[index - strideSrc - 1]) * 0.125F; + float w = (data[index + strideSrc - 1] - data[index - strideSrc + 1]) * 0.125F; - imgY[indexY++] = (data[index + strideSrc] - data[index - strideSrc]) * 0.5F + v + w; - imgX[indexX++] = (data[index + 1] - data[index - 1]) * 0.5F + v - w; + imgY[indexY++] = (data[index + strideSrc] - data[index - strideSrc]) * 0.25F + v + w; + imgX[indexX++] = (data[index + 1] - data[index - 1]) * 0.25F + v - w; } } //CONCURRENT_ABOVE }); diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparsePrewitt_F32.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparsePrewitt_F32.java index 560d8e2cbe..a97f64682b 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparsePrewitt_F32.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparsePrewitt_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -71,11 +71,11 @@ public GradientValue_F32 compute(int x, int y) { a21 = border.get(x ,y+1); a22 = border.get(x+1,y+1); } - gradient.y = -(a00 + a01 + a02); - gradient.y += (a20 + a21 + a22); + gradient.y = -(a00 + a01 + a02)/6.0f; + gradient.y += (a20 + a21 + a22)/6.0f; - gradient.x = -(a00 + a10 + a20); - gradient.x += (a02 + a12 + a22); + gradient.x = -(a00 + a10 + a20)/6.0f; + gradient.x += (a02 + a12 + a22)/6.0f; return gradient; } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseSobel_F32.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseSobel_F32.java index d7dc9cbdb5..f682b5b159 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseSobel_F32.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseSobel_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -71,11 +71,11 @@ public GradientValue_F32 compute(int x, int y) { a21 = border.get(x ,y+1); a22 = border.get(x+1,y+1); } - gradient.y = -(a00 + 2.0f*a01 + a02)*0.25F; - gradient.y += (a20 + 2.0f*a21 + a22)*0.25F; + gradient.y = -(a00 + 2.0f*a01 + a02)*0.125F; + gradient.y += (a20 + 2.0f*a21 + a22)*0.125F; - gradient.x = -(a00 + 2.0f*a10 + a20)*0.25F; - gradient.x += (a02 + 2.0f*a12 + a22)*0.25F; + gradient.x = -(a00 + 2.0f*a10 + a20)*0.125F; + gradient.x += (a02 + 2.0f*a12 + a22)*0.125F; return gradient; } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo0_F32.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo0_F32.java index 8a81bf531a..4c1c44c265 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo0_F32.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo0_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,13 +23,9 @@ import boofcv.struct.sparse.GradientValue_F32; import boofcv.struct.sparse.SparseImageGradient; -/** - * Sparse computation of the two-0 gradient operator. - * - * @author Peter Abeles - */ +/// Sparse computation of the two-0 gradient operator. @SuppressWarnings({"NullAway.Init"}) -public class GradientSparseTwo0_F32 implements SparseImageGradient { +public class GradientSparseTwo0_F32 implements SparseImageGradient { // image being processed GrayF32 input; @@ -38,50 +34,45 @@ public class GradientSparseTwo0_F32 implements SparseImageGradient= 0 && y >= 0 && x < input.width - 1 && y < input.height - 1 ) { + @Override public GradientValue_F32 compute( int x, int y ) { + float a00, a10, a01; + if (x >= 0 && y >= 0 && x < input.width - 1 && y < input.height - 1) { int s = input.stride; int tl = input.startIndex + input.stride*y + x; - a00 = input.data[tl ]; - a10 = input.data[tl +s]; - a01 = input.data[tl+1 ]; + a00 = input.data[tl]; + a10 = input.data[tl + s]; + a01 = input.data[tl + 1]; } else { - a00 = border.get(x ,y ); - a10 = border.get(x ,y+1); - a01 = border.get(x+1,y ); + a00 = border.get(x, y); + a10 = border.get(x, y + 1); + a01 = border.get(x + 1, y); } - gradient.y = a10-a00; - gradient.x = a01-a00; + gradient.y = a10 - a00; + gradient.x = a01 - a00; return gradient; } - @Override - public Class getGradientType() { + @Override public Class getGradientType() { return GradientValue_F32.class; } - @Override - public void setImage(GrayF32 input) { + @Override public void setImage( GrayF32 input ) { this.input = input; - if( border != null ) { + if (border != null) { border.setImage(input); } } - @Override - public boolean isInBounds(int x, int y) { + @Override public boolean isInBounds( int x, int y ) { return border != null || (x >= 0 && y >= 0 && x < input.width - 1 && y < input.height - 1); } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo1_F32.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo1_F32.java index 8d5bb3563c..5b1ed92b1b 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo1_F32.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientSparseTwo1_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,13 +23,9 @@ import boofcv.struct.sparse.GradientValue_F32; import boofcv.struct.sparse.SparseImageGradient; -/** - * Sparse computation of the two-0 gradient operator. - * - * @author Peter Abeles - */ +/// Sparse computation of the two-0 gradient operator. @SuppressWarnings({"NullAway.Init"}) -public class GradientSparseTwo1_F32 implements SparseImageGradient { +public class GradientSparseTwo1_F32 implements SparseImageGradient { // image being processed GrayF32 input; @@ -38,31 +34,30 @@ public class GradientSparseTwo1_F32 implements SparseImageGradient 0 && y > 0 && x < input.width && y < input.height ) { + public GradientValue_F32 compute( int x, int y ) { + float a11, a01, a10; + if (x > 0 && y > 0 && x < input.width && y < input.height) { int s = input.stride; int br = input.startIndex + s*y + x; - a11 = input.data[br ]; - a01 = input.data[br -s]; - a10 = input.data[br-1 ]; + a11 = input.data[br]; + a01 = input.data[br - s]; + a10 = input.data[br - 1]; } else { - a11 = border.get(x ,y ); - a01 = border.get(x ,y-1); - a10 = border.get(x-1,y ); + a11 = border.get(x, y); + a01 = border.get(x, y - 1); + a10 = border.get(x - 1, y); } - gradient.y = a11-a01; - gradient.x = a11-a10; + gradient.y = a11 - a01; + gradient.x = a11 - a10; return gradient; } @@ -73,15 +68,15 @@ public Class getGradientType() { } @Override - public void setImage(GrayF32 input) { + public void setImage( GrayF32 input ) { this.input = input; - if( border != null ) { + if (border != null) { border.setImage(input); } } @Override - public boolean isInBounds(int x, int y) { + public boolean isInBounds( int x, int y ) { return border != null || (x > 0 && y > 0 && x < input.width && y < input.height); } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientThree_Standard.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientThree_Standard.java index fe3af5d940..c8439851cb 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientThree_Standard.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientThree_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo0_Standard.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo0_Standard.java index 8bccc901ac..d6661d1df5 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo0_Standard.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo0_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -55,8 +55,8 @@ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY ) { for (; indexSrc < endX; indexSrc++) { float val = data[indexSrc]; - imgX[indexX++] = (data[indexSrc + 1] - val); - imgY[indexY++] = (data[indexSrc + stride] - val); + imgX[indexX++] = data[indexSrc + 1] - val; + imgY[indexY++] = data[indexSrc + stride] - val; } } //CONCURRENT_ABOVE }); diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo1_Standard.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo1_Standard.java index 908b226b7b..500d46eae0 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo1_Standard.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/GradientTwo1_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -25,18 +25,10 @@ import boofcv.struct.image.GrayS16; import boofcv.struct.image.GrayU8; -/** - *

- * Basic implementation of {@link GradientTwo1} with nothing fancy is done to improve its performance. - *

- * - * @author Peter Abeles - */ +/// Basic implementation of [GradientTwo1] with nothing fancy is done to improve its performance. public class GradientTwo1_Standard { - /** - * Computes the derivative along the x and y axes - */ + /// Computes the derivative along the x and y axes public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY ) { final float[] data = orig.data; final float[] imgX = derivX.data; @@ -55,16 +47,14 @@ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY ) { for (; indexSrc < endX; indexSrc++) { float val = data[indexSrc]; - imgX[indexX++] = (val - data[indexSrc - 1]); - imgY[indexY++] = (val - data[indexSrc - stride]); + imgX[indexX++] = val - data[indexSrc - 1]; + imgY[indexY++] = val - data[indexSrc - stride]; } } //CONCURRENT_ABOVE }); } - /** - * Computes the derivative along the x and y axes - */ + /// Computes the derivative along the x and y axes public static void process( GrayU8 orig, GrayS16 derivX, GrayS16 derivY ) { final byte[] data = orig.data; final short[] imgX = derivX.data; diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/HessianSobel_Shared.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/HessianSobel_Shared.java index d470a496ae..4e7df64b7f 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/HessianSobel_Shared.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/derivative/impl/HessianSobel_Shared.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2017, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -98,6 +98,13 @@ public static void process(GrayF32 orig, final int height = orig.getHeight() - 2; final int strideSrc = orig.getStride(); + final float c1 = 1.0f/64.0f; + final float c2 = 2.0f/64.0f; + final float c4 = 4.0f/64.0f; + final float c6 = 6.0f/64.0f; + final float c8 = 8.0f/64.0f; + final float c12 = 12.0f/64.0f; + for (int y = 2; y < height; y++) { int indexSrc = orig.startIndex + orig.stride * y + 2; final int endX = indexSrc + width-2; @@ -133,10 +140,9 @@ public static void process(GrayF32 orig, float a54 = data[indexSrc + 2*strideSrc + 1]; float a55 = data[indexSrc + 2*strideSrc + 2]; - - imgY[indexY++] = (a11+a15+a51+a55+4*(a12+a52+a14+a54)+6*(a13+a53)-2*(a31+a35)-8*(a32+a34)-12*a33); - imgX[indexX++] = (a11+a51+a15+a55+4*(a21+a25+a41+a45)+6*(a31+a35)-2*(a13+a53)-8*(a23+a43)-12*a33); - imgXY[indexXY++] = (a11+a55-a15-a51+2*(a12+a21+a45+a54-a41-a52-a14-a25) + 4*(a22+a44-a42-a24)); + imgY[indexY++] = (c1*(a11 + a15 + a51 + a55) + c4*(a12 + a52 + a14 + a54) + c6*(a13 + a53) - c2*(a31 + a35) - c8*(a32 + a34) - c12*a33); + imgX[indexX++] = (c1*(a11 + a51 + a15 + a55) + c4*(a21 + a25 + a41 + a45) + c6*(a31 + a35) - c2*(a13 + a53) - c8*(a23 + a43) - c12*a33); + imgXY[indexXY++] = (c1*(a11 + a55 - a15 - a51) + c2*(a12 + a21 + a45 + a54 - a41 - a52 - a14 - a25) + c4*(a22 + a44 - a42 - a24)); } } } diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/filter/kernel/KernelMath.java b/main/boofcv-ip/src/main/java/boofcv/alg/filter/kernel/KernelMath.java index 744b9ade86..2c6cd36cdf 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/filter/kernel/KernelMath.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/filter/kernel/KernelMath.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -117,7 +117,7 @@ public static Kernel1D convolve1D( Kernel1D a, Kernel1D b ) { public static Kernel1D_S32 convolve1D_I32( Kernel1D_S32 a, Kernel1D_S32 b ) { int w = a.width + b.width - 1; - Kernel1D_S32 ret = new Kernel1D_S32(w); + Kernel1D_S32 ret = new Kernel1D_S32(w, a.offset + b.offset); int end = w - (b.getWidth() - 1); int N = b.width - 1; @@ -167,7 +167,7 @@ public static Kernel1D_F32 convolve1D_F32( Kernel1D_F32 a, Kernel1D_F32 b ) { } /** - * Applies a smoothing kernel the the array. The kernel must sum to 1. Array borders are handling by + * Applies a smoothing kernel to the array. The kernel must sum to 1. Array borders are handling by * normalizing based on the overlap of the kernel with the image at that point * * @param kernel Smothing kernel that sums to 1 diff --git a/main/boofcv-ip/src/main/java/boofcv/alg/interpolate/kernel/BicubicKernel_F32.java b/main/boofcv-ip/src/main/java/boofcv/alg/interpolate/kernel/BicubicKernel_F32.java index dc651a47b6..0b8ace2350 100644 --- a/main/boofcv-ip/src/main/java/boofcv/alg/interpolate/kernel/BicubicKernel_F32.java +++ b/main/boofcv-ip/src/main/java/boofcv/alg/interpolate/kernel/BicubicKernel_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -44,6 +44,7 @@ public class BicubicKernel_F32 extends KernelContinuous1D_F32 { /** * Values of a =-0.5 and -0.75 are typical + * * @param a A parameter */ public BicubicKernel_F32( float a ) { @@ -62,29 +63,29 @@ public T copy() { } @Override - public float compute(float x) { + public float compute( float x ) { float absX = x < 0 ? -x : x; - if( absX <= 1f ) { + if (absX <= 1f) { float x2 = absX*absX; - return (a+2f)*x2*absX - (a+3f)*x2 + 1f; - } else if( absX < 2 ) { + return (a + 2f)*x2*absX - (a + 3f)*x2 + 1f; + } else if (absX < 2) { float x2 = absX*absX; - return a*x2*absX - 5f*a*x2 + 8f*a*absX -4f*a; + return a*x2*absX - 5f*a*x2 + 8f*a*absX - 4f*a; } else { return 0f; } } @Override - public double getDouble(int index) { + public double getDouble( int index ) { throw new RuntimeException("Well this function really shouldn't be required to be implemented"); } @Override - public void setD(int index, double value) { + public void setD( int index, double value ) { throw new RuntimeException("Well this function really shouldn't be required to be implemented"); } } diff --git a/main/boofcv-ip/src/main/java/boofcv/core/image/PyramidGradient.java b/main/boofcv-ip/src/main/java/boofcv/core/image/PyramidGradient.java index ccd4b031a3..4bfad5546e 100644 --- a/main/boofcv-ip/src/main/java/boofcv/core/image/PyramidGradient.java +++ b/main/boofcv-ip/src/main/java/boofcv/core/image/PyramidGradient.java @@ -53,6 +53,10 @@ public void update( I image ) { PyramidOps.gradient(basePyramid, gradient, derivX, derivY); } + public int gradientDivisor() { + return gradient.divisor(); + } + public ImageType getInputType() { return gradient.getInputType(); } diff --git a/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivative.java b/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivative.java index fde0dce640..b2f59b8261 100644 --- a/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivative.java +++ b/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivative.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -52,8 +52,8 @@ public class FactoryDerivative { */ public static , M extends ImageMultiBand, D extends ImageGray> ImageGradient gradientReduce( ImageGradient gradient, - DerivativeReduceType type, - Class outputType ) { + DerivativeReduceType type, + Class outputType ) { String name; @@ -262,11 +262,15 @@ private static Method findReduce( String name, Class inputType, @Nullable Cla private static Method findHessianFromGradient( Class gradientType, @Nullable Class derivType ) { if (derivType == null) derivType = gradientType; - String name = gradientType.getSimpleName().substring(8); + String className = gradientType.getSimpleName(); + String typeName = className.endsWith("Sobel") ? "Sobel" : + className.endsWith("Prewitt") ? "Prewitt" : + className.endsWith("Three") ? "Three" : "Unknown"; + Method m; try { Class borderType = GeneralizedImageOps.isFloatingPoint(derivType) ? ImageBorder_F32.class : ImageBorder_S32.class; - m = HessianFromGradient.class.getDeclaredMethod("hessian" + name, derivType, derivType, derivType, derivType, derivType, borderType); + m = HessianFromGradient.class.getDeclaredMethod("hessian" + typeName, derivType, derivType, derivType, derivType, derivType, borderType); } catch (NoSuchMethodException e) { throw new RuntimeException("Input and derivative types are probably not compatible", e); } diff --git a/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivativeSparse.java b/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivativeSparse.java index a2fb3df5c8..6a6e7aec70 100644 --- a/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivativeSparse.java +++ b/main/boofcv-ip/src/main/java/boofcv/factory/filter/derivative/FactoryDerivativeSparse.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -62,13 +62,13 @@ ImageFunctionSparse createLaplacian( Class imageType, @Nullable ImageBorde } if (GeneralizedImageOps.isFloatingPoint(imageType)) { - ImageConvolveSparse r = FactoryConvolveSparse.convolve2D(GrayF32.class, DerivativeLaplacian.kernel_F32); + ImageConvolveSparse r = FactoryConvolveSparse.convolve2D(GrayF32.class, DerivativeLaplacian.kernelXY_F32); r.setImageBorder((ImageBorder_F32)border); return (ImageFunctionSparse)r; } else { - ImageConvolveSparse r = FactoryConvolveSparse.convolve2D(GrayI.class, DerivativeLaplacian.kernel_I32); + ImageConvolveSparse r = FactoryConvolveSparse.convolve2D(GrayI.class, DerivativeLaplacian.kernelXY_I32); r.setImageBorder(border); diff --git a/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradientThenReduce.java b/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradientThenReduce.java index 10aa1e2f98..a406a80f6b 100644 --- a/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradientThenReduce.java +++ b/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradientThenReduce.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -81,6 +81,10 @@ public void process(Planar inputImage, Planar derivX, Planar> getInputType() { return ImageType.pl(3,GrayF32.class); diff --git a/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradient_SB.java b/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradient_SB.java index 43a07c7881..46653144bf 100644 --- a/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradient_SB.java +++ b/main/boofcv-ip/src/test/java/boofcv/abst/filter/derivative/TestImageGradient_SB.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,10 +18,16 @@ package boofcv.abst.filter.derivative; +import boofcv.alg.filter.derivative.*; import boofcv.struct.image.GrayF32; +import boofcv.struct.image.GrayS16; +import boofcv.struct.image.GrayU8; import boofcv.testing.BoofStandardJUnit; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class TestImageGradient_SB extends BoofStandardJUnit { int width = 20; @@ -35,8 +41,51 @@ public class TestImageGradient_SB extends BoofStandardJUnit { GrayF32 derivX = new GrayF32(width, height); GrayF32 derivY = new GrayF32(width, height); - ImageGradient_SB alg = new ImageGradient_SB.Sobel<>(GrayF32.class, GrayF32.class); + var alg = new ImageGradient_SB.Sobel<>(GrayF32.class, GrayF32.class); alg.process(input, derivX, derivY); } + + @Nested class Sobel { + // make sure its set to one when floating point image and the actual divisor for others + @Test void gradientDivisor() { + assertEquals(1.0f, new ImageGradient_SB.Sobel<>(GrayF32.class, GrayF32.class).divisor()); + assertEquals(GradientSobel.divisor, new ImageGradient_SB.Sobel<>(GrayU8.class, GrayS16.class).divisor()); + } + } + + @Nested class Prewitt { + @Test void gradientDivisor() { + assertEquals(1.0f, new ImageGradient_SB.Prewitt<>(GrayF32.class, GrayF32.class).divisor()); + assertEquals(GradientPrewitt.divisor, new ImageGradient_SB.Prewitt<>(GrayU8.class, GrayS16.class).divisor()); + } + } + + @Nested class Scharr { + @Test void gradientDivisor() { + assertEquals(1.0f, new ImageGradient_SB.Scharr<>(GrayF32.class, GrayF32.class).divisor()); + assertEquals(GradientScharr.divisor, new ImageGradient_SB.Scharr<>(GrayU8.class, GrayS16.class).divisor()); + } + } + + @Nested class Three { + @Test void gradientDivisor() { + assertEquals(1.0f, new ImageGradient_SB.Three<>(GrayF32.class, GrayF32.class).divisor()); + assertEquals(GradientThree.divisor, new ImageGradient_SB.Three<>(GrayU8.class, GrayS16.class).divisor()); + } + } + + @Nested class Two0 { + @Test void gradientDivisor() { + assertEquals(1.0f, new ImageGradient_SB.Two0<>(GrayF32.class, GrayF32.class).divisor()); + assertEquals(GradientTwo0.divisor, new ImageGradient_SB.Two0<>(GrayU8.class, GrayS16.class).divisor()); + } + } + + @Nested class Two1 { + @Test void gradientDivisor() { + assertEquals(1.0f, new ImageGradient_SB.Two1<>(GrayF32.class, GrayF32.class).divisor()); + assertEquals(GradientTwo1.divisor, new ImageGradient_SB.Two1<>(GrayU8.class, GrayS16.class).divisor()); + } + } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_IL.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_IL.java index ea38cda708..77ccf2da91 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_IL.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_IL.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -40,7 +40,7 @@ public class TestConvolveNormalizedNaive_IL extends BoofStandardJUnit { * Check it against one specific type to see if the core algorithm is correct */ @Test void horizontal() { - Kernel1D_S32 kernel = new Kernel1D_S32(new int[]{1, 2, 3, 4, 5, 6}, 6, 4); + Kernel1D_S32 kernel = new Kernel1D_S32(6, 4, new int[]{1, 2, 3, 4, 5, 6}); InterleavedU8 input = new InterleavedU8(width, height, numBands); ImageMiscOps.fillUniform(input, rand, 0, 50); @@ -80,7 +80,7 @@ private int horizontal( int x, int y, int band, Kernel1D_S32 kernel, Interleaved * Check it against one specific type to see if the core algorithm is correct */ @Test void vertical() { - Kernel1D_S32 kernel = new Kernel1D_S32(new int[]{1, 2, 3, 4, 5, 6}, 6, 4); + Kernel1D_S32 kernel = new Kernel1D_S32(6, 4, new int[]{1, 2, 3, 4, 5, 6}); InterleavedU8 input = new InterleavedU8(width, height, numBands); ImageMiscOps.fillUniform(input, rand, 0, 50); @@ -120,8 +120,8 @@ private int vertical( int x, int y, int band, Kernel1D_S32 kernel, InterleavedU8 * Check it against one specific type to see if the core algorithm is correct */ @Test void vertical2_U16_U8() { - Kernel1D_S32 kernelY = new Kernel1D_S32(new int[]{1, 2, 3, 4, 5, 6}, 6, 4); - Kernel1D_S32 kernelX = new Kernel1D_S32(new int[]{4, 2, 1, 4, 3, 6}, 5, 2); + Kernel1D_S32 kernelY = new Kernel1D_S32(6, 4, new int[]{1, 2, 3, 4, 5, 6}); + Kernel1D_S32 kernelX = new Kernel1D_S32(5, 2, new int[]{4, 2, 1, 4, 3, 6}); InterleavedU16 input = new InterleavedU16(width, height, numBands); ImageMiscOps.fillUniform(input, rand, 0, 80); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_SB.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_SB.java index adc86db2e4..60fc00626a 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_SB.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/convolve/normalized/TestConvolveNormalizedNaive_SB.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -36,7 +36,7 @@ public class TestConvolveNormalizedNaive_SB extends BoofStandardJUnit { * Check it against one specific type to see if the core algorithm is correct */ @Test void horizontal() { - Kernel1D_S32 kernel = new Kernel1D_S32(new int[]{1, 2, 3, 4, 5, 6}, 6, 4); + Kernel1D_S32 kernel = new Kernel1D_S32(6, 4, new int[]{1, 2, 3, 4, 5, 6}); GrayU8 input = new GrayU8(15, 16); ImageMiscOps.fillUniform(input, rand, 0, 50); @@ -74,7 +74,7 @@ private int horizontal( int x, int y, Kernel1D_S32 kernel, GrayU8 image ) { * Check it against one specific type to see if the core algorithm is correct */ @Test void vertical() { - Kernel1D_S32 kernel = new Kernel1D_S32(new int[]{1, 2, 3, 4, 5, 6}, 6, 4); + Kernel1D_S32 kernel = new Kernel1D_S32(6, 4, new int[]{1, 2, 3, 4, 5, 6}); GrayU8 input = new GrayU8(15, 16); ImageMiscOps.fillUniform(input, rand, 0, 50); @@ -112,8 +112,8 @@ private int vertical( int x, int y, Kernel1D_S32 kernel, GrayU8 image ) { * Check it against one specific type to see if the core algorithm is correct */ @Test void vertical2_U16_U8() { - Kernel1D_S32 kernelY = new Kernel1D_S32(new int[]{1, 2, 3, 4, 5, 6}, 6, 4); - Kernel1D_S32 kernelX = new Kernel1D_S32(new int[]{4, 2, 1, 4, 3, 6}, 5, 2); + Kernel1D_S32 kernelY = new Kernel1D_S32(6, 4, new int[]{1, 2, 3, 4, 5, 6}); + Kernel1D_S32 kernelX = new Kernel1D_S32(5, 2, new int[]{4, 2, 1, 4, 3, 6}); GrayU16 input = new GrayU16(15, 16); ImageMiscOps.fillUniform(input, rand, 0, 80); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralGradient1DTests.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralGradient1DTests.java new file mode 100644 index 0000000000..d43b237495 --- /dev/null +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralGradient1DTests.java @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.alg.filter.derivative; + +import boofcv.alg.filter.kernel.KernelMath; +import boofcv.alg.misc.ImageMiscOps; +import boofcv.struct.border.ImageBorder_F32; +import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel1D_F32; +import boofcv.struct.convolve.Kernel1D_S32; +import boofcv.struct.image.GrayF32; +import boofcv.struct.image.GrayS16; +import boofcv.struct.image.GrayU8; +import boofcv.testing.BoofStandardJUnit; +import org.junit.jupiter.api.Test; + +import static boofcv.alg.filter.derivative.GeneralGradient2DTests.divisor; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/// Tests that are designed for 2D gradient kernels. Ensures coding standards are following, floating point +/// kernels are correctly normalized, and that scale has been set correctly. +/// +/// These tests are derived from kernels needing +/// +/// 1) Preserves brightness. Sum g[i] == 1 +/// 2) Must be zero on flat regions Sum a*g[i] == 0 +public abstract class GeneralGradient1DTests extends BoofStandardJUnit { + int width = 20; + int height = 25; + + Class targetClass; + + public GeneralGradient1DTests( Class targetClass ) { + this.targetClass = targetClass; + } + + /// Derive the class being tested from the file name. Preferred approach. + public GeneralGradient1DTests() { + String testClassName = getClass().getSimpleName().substring(4); + try { + targetClass = Class.forName(getClass().getPackageName() + "." + testClassName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Test void kernelInt() throws NoSuchFieldException, IllegalAccessException { + int divisor = (int)targetClass.getDeclaredField("divisor").get(null); + var deriv = (Kernel1D_S32)targetClass.getDeclaredField("kernel_I32").get(null); + + var smooth = (Kernel1D_S32)targetClass.getDeclaredField("kernelSmooth").get(null); + var diff = (Kernel1D_S32)targetClass.getDeclaredField("kernelDiff").get(null); + Kernel1D_S32 expected = kernel1D(smooth, diff); + + assertTrue(expected.isIdentical(deriv)); + + // Check the sign convention + for (int i = 0; i < deriv.width/2; i++) { + assertTrue(deriv.get(i) < 0); + } + + assertEquals(divisor(smooth, diff), divisor); + } + + @Test void kernelFloat() throws NoSuchFieldException, IllegalAccessException { + int divisor = (int)targetClass.getDeclaredField("divisor").get(null); + var derivI = (Kernel1D_S32)targetClass.getDeclaredField("kernel_I32").get(null); + var derivF = (Kernel1D_F32)targetClass.getDeclaredField("kernel_F32").get(null); + + assertEquals(derivI.width, derivF.width); + assertEquals(derivI.offset, derivF.offset); + + for (int i = 0; i < derivI.width; i++) { + assertEquals((float)derivI.get(i)/divisor, derivF.get(i)); + } + } + + @Test void compareToConvolve_I8() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernel = (Kernel1D_S32)targetClass.getDeclaredField("kernel_I32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); + + validator.setKernel(0, kernel, true); + validator.setKernel(1, kernel, false); + + var input = new GrayU8(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 200); + var derivX = new GrayS16(width, height); + var derivY = new GrayS16(width, height); + + validator.compare(input, derivX, derivY); + } + + @Test void compareToConvolve_I16() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernel = (Kernel1D_S32)targetClass.getDeclaredField("kernel_I32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); + + validator.setKernel(0, kernel, true); + validator.setKernel(1, kernel, false); + + var input = new GrayS16(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 1000); + var derivX = new GrayS16(width, height); + var derivY = new GrayS16(width, height); + + validator.compare(input, derivX, derivY); + } + + @Test void compareToConvolve_F32() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernel = (Kernel1D_F32)targetClass.getDeclaredField("kernel_F32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class)); + + validator.setKernel(0, kernel, true); + validator.setKernel(1, kernel, false); + + var input = new GrayF32(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 10); + var derivX = new GrayF32(width, height); + var derivY = new GrayF32(width, height); + + validator.compare(input, derivX, derivY); + } + + public static Kernel1D_S32 kernel1D( Kernel1D_S32 smoothing, Kernel1D_S32 differentiate ) { + + Kernel1D_S32 a = KernelMath.convolve1D_I32(smoothing, differentiate); + return a.trimBorder(a.countZeroBorder()); + } +} diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralGradient2DTests.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralGradient2DTests.java new file mode 100644 index 0000000000..50e5d878b5 --- /dev/null +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralGradient2DTests.java @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.alg.filter.derivative; + +import boofcv.alg.filter.kernel.KernelMath; +import boofcv.alg.misc.ImageMiscOps; +import boofcv.struct.border.ImageBorder_F32; +import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel1D_S32; +import boofcv.struct.convolve.Kernel2D_F32; +import boofcv.struct.convolve.Kernel2D_S32; +import boofcv.struct.image.GrayF32; +import boofcv.struct.image.GrayS16; +import boofcv.struct.image.GrayU8; +import boofcv.testing.BoofStandardJUnit; +import org.ejml.UtilEjml; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/// Tests that are designed for 2D gradient kernels. Ensures coding standards are following, floating point +/// kernels are correctly normalized, and that scale has been set correctly. +/// +/// These tests are derived from kernels needing +/// +/// 1) Preserves brightness. Sum g[i] == 1 +/// 2) Must be zero on flat regions Sum a*g[i] == 0 +public abstract class GeneralGradient2DTests extends BoofStandardJUnit { + int width = 20; + int height = 25; + + Class targetClass; + + public GeneralGradient2DTests( Class targetClass ) { + this.targetClass = targetClass; + } + + /// Derive the class being tested from the file name. Preferred approach. + public GeneralGradient2DTests() { + String testClassName = getClass().getSimpleName().substring(4); + try { + targetClass = Class.forName(getClass().getPackageName() + "." + testClassName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Test void kernelInt() throws NoSuchFieldException, IllegalAccessException { + int divisor = (int)targetClass.getDeclaredField("divisor").get(null); + var derivX = (Kernel2D_S32)targetClass.getDeclaredField("kernelX_I32").get(null); + var derivY = (Kernel2D_S32)targetClass.getDeclaredField("kernelY_I32").get(null); + + var smooth = (Kernel1D_S32)targetClass.getDeclaredField("kernelSmooth").get(null); + var diff = (Kernel1D_S32)targetClass.getDeclaredField("kernelDiff").get(null); + + Kernel2D_S32 expectedX = kernel2D(smooth, diff, true); + Kernel2D_S32 expectedY = kernel2D(smooth, diff, false); + + assertTrue(expectedX.isIdentical(derivX)); + assertTrue(expectedY.isIdentical(derivY)); + + // Check the sign convention + for (int row = 0; row < derivY.width/2; row++) { + for (int col = 0; col < derivY.width; col++) { + assertTrue(derivY.get(col, row) < 0); + } + } + + assertEquals(divisor(smooth, diff), divisor); + } + + @Test void kernelFloat() throws NoSuchFieldException, IllegalAccessException { + var derivX = (Kernel2D_F32)targetClass.getDeclaredField("kernelX_F32").get(null); + var derivY = (Kernel2D_F32)targetClass.getDeclaredField("kernelY_F32").get(null); + + // Middle should be zeros + for (int i = 0; i < derivX.width; i++) { + assertEquals(0.0, derivX.get(1, i)); + assertEquals(0.0, derivY.get(i, 1)); + } + + // Test that they are transpose of each other + for (int row = 0; row < derivX.width; row++) { + for (int col = 0; col < derivX.width; col++) { + assertEquals(derivX.get(row, col), derivY.get(col, row)); + } + } + + // Check the sign convention + for (int row = 0; row < derivY.width/2; row++) { + for (int col = 0; col < derivY.width; col++) { + assertTrue(derivY.get(col, row) < 0); + } + } + + // Test normalization + float sum = 0.0f; + for (int y = 0; y < derivY.width; y++) { + for (int x = 0; x < derivY.width; x++) { + sum += Math.abs(derivY.get(x, y)); + } + } + assertEquals(1.0f, sum, UtilEjml.TEST_F32); + } + + @Test void compareToConvolve_I8() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernelX = (Kernel2D_S32)targetClass.getDeclaredField("kernelX_I32").get(null); + var kernelY = (Kernel2D_S32)targetClass.getDeclaredField("kernelY_I32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); + + validator.setKernel(0, kernelX); + validator.setKernel(1, kernelY); + + var input = new GrayU8(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 200); + var derivX = new GrayS16(width, height); + var derivY = new GrayS16(width, height); + + validator.compare(input, derivX, derivY); + } + + @Test void compareToConvolve_I16() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernelX = (Kernel2D_S32)targetClass.getDeclaredField("kernelX_I32").get(null); + var kernelY = (Kernel2D_S32)targetClass.getDeclaredField("kernelY_I32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); + + validator.setKernel(0, kernelX); + validator.setKernel(1, kernelY); + + var input = new GrayS16(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 1000); + var derivX = new GrayS16(width, height); + var derivY = new GrayS16(width, height); + + validator.compare(input, derivX, derivY); + } + + @Test void compareToConvolve_F32() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernelX = (Kernel2D_F32)targetClass.getDeclaredField("kernelX_F32").get(null); + var kernelY = (Kernel2D_F32)targetClass.getDeclaredField("kernelY_F32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class)); + + validator.setKernel(0, kernelX); + validator.setKernel(1, kernelY); + + var input = new GrayF32(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 10); + var derivX = new GrayF32(width, height); + var derivY = new GrayF32(width, height); + + validator.compare(input, derivX, derivY); + } + + public static Kernel2D_S32 kernel2D( Kernel1D_S32 smoothing, Kernel1D_S32 differentiate, boolean axisX ) { + var ks = new Kernel2D_S32(smoothing.width); + var kd = new Kernel2D_S32(differentiate.width); + + if (axisX) { + for (int i = 0; i < ks.width; i++) { + ks.set(ks.width/2, i, smoothing.get(i)); + } + for (int i = 0; i < kd.width; i++) { + kd.set(i, kd.width/2, differentiate.get(i)); + } + } else { + for (int i = 0; i < ks.width; i++) { + ks.set(i, ks.width/2, smoothing.get(i)); + } + for (int i = 0; i < kd.width; i++) { + kd.set(kd.width/2, i, differentiate.get(i)); + } + } + + Kernel2D_S32 a = KernelMath.convolve2D(ks, kd); + return a.trimBorder(a.countZeroBorder()); + } + + public static int divisor( Kernel1D_S32 smoothing, Kernel1D_S32 differentiate ) { + int sum = smoothing.computeSum(); + int numZeros = 0; + for (int i = differentiate.width/2; i < differentiate.width; i++) { + if (differentiate.get(i) != 0) + break; + numZeros++; + } + return sum*(numZeros + 1); + } +} diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralHessian2DTests.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralHessian2DTests.java new file mode 100644 index 0000000000..c09b163f85 --- /dev/null +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/GeneralHessian2DTests.java @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.alg.filter.derivative; + +import boofcv.alg.misc.ImageMiscOps; +import boofcv.struct.border.ImageBorder_F32; +import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel2D_F32; +import boofcv.struct.convolve.Kernel2D_S32; +import boofcv.struct.image.GrayF32; +import boofcv.struct.image.GrayS16; +import boofcv.struct.image.GrayU8; +import boofcv.testing.BoofStandardJUnit; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/// Tests that are designed for 2D gradient kernels. Ensures coding standards are following, floating point +/// kernels are correctly normalized, and that scale has been set correctly. +/// +/// These tests are derived from kernels needing +/// +/// 1) Preserves brightness. Sum g[i] == 1 +/// 2) Must be zero on flat regions Sum a*g[i] == 0 +public abstract class GeneralHessian2DTests extends BoofStandardJUnit { + int width = 20; + int height = 25; + + Class targetClass; + + public GeneralHessian2DTests( Class targetClass ) { + this.targetClass = targetClass; + } + + /// Derive the class being tested from the file name. Preferred approach. + public GeneralHessian2DTests() { + String testClassName = getClass().getSimpleName().substring(4); + try { + targetClass = Class.forName(getClass().getPackageName() + "." + testClassName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Test void kernelInt() throws NoSuchFieldException, IllegalAccessException { + int divisor = (int)targetClass.getDeclaredField("divisor").get(null); + var kernelXX = (Kernel2D_S32)targetClass.getDeclaredField("kernelXX_I32").get(null); + var kernelXY = (Kernel2D_S32)targetClass.getDeclaredField("kernelXY_I32").get(null); + var kernelYY = (Kernel2D_S32)targetClass.getDeclaredField("kernelYY_I32").get(null); + + // Test that they are transpose of each other + for (int row = 0; row < kernelXX.width; row++) { + for (int col = 0; col < kernelXX.width; col++) { + assertEquals(kernelXX.get(row, col), kernelYY.get(col, row)); + } + } + + int sumXX = 0; + for (int y = 0; y < kernelXX.width; y++) { + for (int x = 0; x < kernelXX.width; x++) { + sumXX += Math.abs(kernelXX.get(x, y)); + } + } + assertEquals(sumXX, divisor); + + // This is a very weak test. In general, it would be better if we tested 2nd order by deriving them + // from the 1st order derivatives. + int sumXY = 0; + for (int y = 0; y < kernelXY.width; y++) { + for (int x = 0; x < kernelXY.width; x++) { + sumXY += Math.abs(kernelXY.get(x, y)); + } + } + assertTrue(sumXY <= sumXX); + } + + @Test void kernelFloat() throws NoSuchFieldException, IllegalAccessException { + var kernelXX = (Kernel2D_F32)targetClass.getDeclaredField("kernelXX_F32").get(null); + var kernelXY = (Kernel2D_F32)targetClass.getDeclaredField("kernelXY_F32").get(null); + var kernelYY = (Kernel2D_F32)targetClass.getDeclaredField("kernelYY_F32").get(null); + + // Test that they are transpose of each other + for (int row = 0; row < kernelXX.width; row++) { + for (int col = 0; col < kernelXX.width; col++) { + assertEquals(kernelXX.get(row, col), kernelYY.get(col, row)); + } + } + + float sumXX = 0.0f; + for (int y = 0; y < kernelXX.width; y++) { + for (int x = 0; x < kernelXX.width; x++) { + sumXX += Math.abs(kernelXX.get(x, y)); + } + } + assertEquals(1.0f, sumXX); + + int divisor = (int)targetClass.getDeclaredField("divisor").get(null); + var kernelIntXY = (Kernel2D_S32)targetClass.getDeclaredField("kernelXY_I32").get(null); + + int sumIntXY = 0; + float sumXY = 0; + for (int y = 0; y < kernelXY.width; y++) { + for (int x = 0; x < kernelXY.width; x++) { + sumXY += Math.abs(kernelXY.get(x, y)); + sumIntXY += Math.abs(kernelIntXY.get(x, y)); + } + } + assertEquals(sumIntXY/(float)divisor, sumXY); + } + + @Test void compareToConvolve_I8() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernelXX = (Kernel2D_S32)targetClass.getDeclaredField("kernelXX_I32").get(null); + var kernelXY = (Kernel2D_S32)targetClass.getDeclaredField("kernelXY_I32").get(null); + var kernelYY = (Kernel2D_S32)targetClass.getDeclaredField("kernelYY_I32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayU8.class, GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); + + validator.setKernel(0, kernelXX); + validator.setKernel(1, kernelYY); + validator.setKernel(2, kernelXY); + + var input = new GrayU8(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 200); + var derivXX = new GrayS16(width, height); + var derivYY = new GrayS16(width, height); + var derivXY = new GrayS16(width, height); + + validator.compare(input, derivXX, derivYY, derivXY); + } + + @Test void compareToConvolve_F32() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { + var kernelXX = (Kernel2D_F32)targetClass.getDeclaredField("kernelXX_F32").get(null); + var kernelXY = (Kernel2D_F32)targetClass.getDeclaredField("kernelXY_F32").get(null); + var kernelYY = (Kernel2D_F32)targetClass.getDeclaredField("kernelYY_F32").get(null); + + var validator = new CompareDerivativeToConvolution(); + validator.setTarget(targetClass.getMethod("process", + GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class)); + + validator.setKernel(0, kernelXX); + validator.setKernel(1, kernelYY); + validator.setKernel(2, kernelXY); + + var input = new GrayF32(width, height); + ImageMiscOps.fillUniform(input, rand, 0, 10); + var derivXX = new GrayF32(width, height); + var derivYY = new GrayF32(width, height); + var derivXY = new GrayF32(width, height); + + validator.compare(input, derivXX, derivYY, derivXY); + } +} diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestDerivativeLaplacian.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestDerivativeLaplacian.java index ef865c35e4..6b2c78dfc3 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestDerivativeLaplacian.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestDerivativeLaplacian.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -25,6 +25,8 @@ import boofcv.struct.border.BorderType; import boofcv.struct.border.ImageBorder_F32; import boofcv.struct.border.ImageBorder_S32; +import boofcv.struct.convolve.Kernel2D_F32; +import boofcv.struct.convolve.Kernel2D_S32; import boofcv.struct.image.GrayF32; import boofcv.struct.image.GrayS16; import boofcv.struct.image.GrayU8; @@ -39,6 +41,18 @@ public class TestDerivativeLaplacian extends BoofStandardJUnit { private final int width = 4; private final int height = 5; + @Test void floatScale() { + int divisor = DerivativeLaplacian.divisor; + Kernel2D_S32 kernel_I32 = DerivativeLaplacian.kernelXY_I32; + Kernel2D_F32 kernel_F32 = DerivativeLaplacian.kernelXY_F32; + for (int y = 0; y < kernel_I32.width; y++) { + for (int x = 0; x < kernel_I32.width; x++) { + float expected = kernel_I32.get(x, y)/(float)divisor; + assertEquals(expected, kernel_F32.get(x, y), UtilEjml.TEST_F32); + } + } + } + @Test void process_U8_S16() { GrayU8 img = new GrayU8(width, height); ImageMiscOps.fillUniform(img, rand, 0, 100); @@ -47,14 +61,14 @@ public class TestDerivativeLaplacian extends BoofStandardJUnit { BoofTesting.checkSubImage(this, "process_U8_S16", true, img, deriv); } - public void process_U8_S16(GrayU8 img, GrayS16 deriv) { + public void process_U8_S16( GrayU8 img, GrayS16 deriv ) { ImageBorder_S32 border = (ImageBorder_S32)FactoryImageBorder.single(BorderType.EXTENDED, GrayU8.class); DerivativeLaplacian.process(img, deriv, border); GrayS16 expected = deriv.createSameShape(); - ConvolveImage.convolve(DerivativeLaplacian.kernel_I32,img,expected,border); + ConvolveImage.convolve(DerivativeLaplacian.kernelXY_I32, img, expected, border); - BoofTesting.assertEquals(expected,deriv,0); + BoofTesting.assertEquals(expected, deriv, 0); } @Test void process_I8_F32() { @@ -65,10 +79,10 @@ public void process_U8_S16(GrayU8 img, GrayS16 deriv) { BoofTesting.checkSubImage(this, "process_U8_F32", true, img, deriv); } - public void process_U8_F32(GrayU8 img, GrayF32 deriv) { + public void process_U8_F32( GrayU8 img, GrayF32 deriv ) { DerivativeLaplacian.process(img, deriv); - int expected = -4 * img.get(1, 1) + img.get(0, 1) + img.get(1, 0) + int expected = -4*img.get(1, 1) + img.get(0, 1) + img.get(1, 0) + img.get(2, 1) + img.get(1, 2); assertEquals(expected, deriv.get(1, 1), 1e-5); @@ -82,13 +96,13 @@ public void process_U8_F32(GrayU8 img, GrayF32 deriv) { BoofTesting.checkSubImage(this, "process_F32", true, img, deriv); } - public void process_F32(GrayF32 img, GrayF32 deriv) { + public void process_F32( GrayF32 img, GrayF32 deriv ) { ImageBorder_F32 border = (ImageBorder_F32)FactoryImageBorder.single(BorderType.EXTENDED, GrayF32.class); DerivativeLaplacian.process(img, deriv, border); GrayF32 expected = deriv.createSameShape(); - ConvolveImage.convolve(DerivativeLaplacian.kernel_F32,img,expected,border); + ConvolveImage.convolve(DerivativeLaplacian.kernelXY_F32, img, expected, border); - BoofTesting.assertEquals(expected,deriv, UtilEjml.TEST_F32); + BoofTesting.assertEquals(expected, deriv, UtilEjml.TEST_F32); } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGImageDerivativeOps.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGImageDerivativeOps.java index 5d60915257..8c6ea9d10b 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGImageDerivativeOps.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGImageDerivativeOps.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -26,19 +26,19 @@ public class TestGImageDerivativeOps extends BoofStandardJUnit { @Test void lookupKernelX() { - assertSame(GradientPrewitt.kernelDerivX_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.PREWITT, true)); - assertSame(GradientPrewitt.kernelDerivX_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.PREWITT, false)); + assertSame(GradientPrewitt.kernelX_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.PREWITT, true)); + assertSame(GradientPrewitt.kernelX_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.PREWITT, false)); - assertSame(GradientSobel.kernelDerivX_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.SOBEL, true)); - assertSame(GradientSobel.kernelDerivX_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.SOBEL, false)); + assertSame(GradientSobel.kernelX_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.SOBEL, true)); + assertSame(GradientSobel.kernelX_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.SOBEL, false)); - assertSame(GradientThree.kernelDeriv_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.THREE, true)); - assertSame(GradientThree.kernelDeriv_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.THREE, false)); + assertSame(GradientThree.kernel_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.THREE, true)); + assertSame(GradientThree.kernel_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.THREE, false)); - assertSame(GradientTwo0.kernelDeriv_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_0, true)); - assertSame(GradientTwo0.kernelDeriv_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_0, false)); + assertSame(GradientTwo0.kernel_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_0, true)); + assertSame(GradientTwo0.kernel_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_0, false)); - assertSame(GradientTwo1.kernelDeriv_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_1, true)); - assertSame(GradientTwo1.kernelDeriv_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_1, false)); + assertSame(GradientTwo1.kernel_I32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_1, true)); + assertSame(GradientTwo1.kernel_F32, GImageDerivativeOps.lookupKernelX(DerivativeType.TWO_1, false)); } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientPrewitt.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientPrewitt.java index 7f95cbf4ee..c52a69cc82 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientPrewitt.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientPrewitt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,71 +18,11 @@ package boofcv.alg.filter.derivative; -import boofcv.alg.misc.ImageMiscOps; -import boofcv.struct.border.ImageBorder_F32; -import boofcv.struct.border.ImageBorder_S32; -import boofcv.struct.image.GrayF32; -import boofcv.struct.image.GrayS16; -import boofcv.struct.image.GrayU8; -import boofcv.testing.BoofStandardJUnit; import org.junit.jupiter.api.Test; -public class TestGradientPrewitt extends BoofStandardJUnit { - - int width = 20; - int height = 25; - - @Test void testStandard() { - StandardGradientChecks standard = new StandardGradientChecks(); +public class TestGradientPrewitt extends GeneralGradient2DTests { + @Test void standard() { + var standard = new StandardGradientChecks(); standard.secondDerivativeTest(GradientPrewitt.class,3); } - - @Test void compareToConvolve_I8() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientPrewitt.class.getMethod("process", - GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0,GradientPrewitt.kernelDerivX_I32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_I32); - - GrayU8 input = new GrayU8(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - - @Test void compareToConvolve_I16() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientPrewitt.class.getMethod("process", - GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0,GradientPrewitt.kernelDerivX_I32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_I32); - - GrayS16 input = new GrayS16(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - -@ Test - public void compareToConvolve_F32() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientPrewitt.class.getMethod("process", - GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class )); - - validator.setKernel(0,GradientPrewitt.kernelDerivX_F32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_F32); - - GrayF32 input = new GrayF32(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayF32 derivX = new GrayF32(width,height); - GrayF32 derivY = new GrayF32(width,height); - - validator.compare(input,derivX,derivY); - } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientScharr.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientScharr.java index f9d9aa8db1..b8326bee39 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientScharr.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientScharr.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,69 +18,11 @@ package boofcv.alg.filter.derivative; -import boofcv.alg.misc.ImageMiscOps; -import boofcv.struct.border.ImageBorder_F32; -import boofcv.struct.border.ImageBorder_S32; -import boofcv.struct.image.GrayF32; -import boofcv.struct.image.GrayS16; -import boofcv.struct.image.GrayU8; -import boofcv.testing.BoofStandardJUnit; import org.junit.jupiter.api.Test; -public class TestGradientScharr extends BoofStandardJUnit { - int width = 20; - int height = 25; - +public class TestGradientScharr extends GeneralGradient2DTests { @Test void testStandard() { - StandardGradientChecks standard = new StandardGradientChecks(); + var standard = new StandardGradientChecks(); standard.secondDerivativeTest(GradientScharr.class, 3); } - - @Test void compareToConvolve_I8() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientScharr.class.getMethod("process", - GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); - - validator.setKernel(0, GradientScharr.kernelDerivX_I32); - validator.setKernel(1, GradientScharr.kernelDerivY_I32); - - GrayU8 input = new GrayU8(width, height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width, height); - GrayS16 derivY = new GrayS16(width, height); - - validator.compare(input, derivX, derivY); - } - - @Test void compareToConvolve_I16() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientScharr.class.getMethod("process", - GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class)); - - validator.setKernel(0, GradientScharr.kernelDerivX_I32); - validator.setKernel(1, GradientScharr.kernelDerivY_I32); - - GrayS16 input = new GrayS16(width, height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width, height); - GrayS16 derivY = new GrayS16(width, height); - - validator.compare(input, derivX, derivY); - } - - @Test void compareToConvolve_F32() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientScharr.class.getMethod("process", - GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class)); - - validator.setKernel(0, GradientScharr.kernelDerivX_F32); - validator.setKernel(1, GradientScharr.kernelDerivY_F32); - - GrayF32 input = new GrayF32(width, height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayF32 derivX = new GrayF32(width, height); - GrayF32 derivY = new GrayF32(width, height); - - validator.compare(input, derivX, derivY); - } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientSobel.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientSobel.java index f36bb8c4da..cfcd655e9e 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientSobel.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientSobel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,70 +18,11 @@ package boofcv.alg.filter.derivative; -import boofcv.alg.misc.ImageMiscOps; -import boofcv.struct.border.ImageBorder_F32; -import boofcv.struct.border.ImageBorder_S32; -import boofcv.struct.image.GrayF32; -import boofcv.struct.image.GrayS16; -import boofcv.struct.image.GrayU8; -import boofcv.testing.BoofStandardJUnit; import org.junit.jupiter.api.Test; -public class TestGradientSobel extends BoofStandardJUnit { - int width = 20; - int height = 25; - +public class TestGradientSobel extends GeneralGradient2DTests { @Test void testStandard() { - StandardGradientChecks standard = new StandardGradientChecks(); + var standard = new StandardGradientChecks(); standard.secondDerivativeTest(GradientSobel.class,3); } - - @Test void compareToConvolve_I8() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientSobel.class.getMethod("process", - GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0,GradientSobel.kernelDerivX_I32); - validator.setKernel(1,GradientSobel.kernelDerivY_I32); - - GrayU8 input = new GrayU8(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - - @Test void compareToConvolve_I16() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientSobel.class.getMethod("process", - GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0,GradientSobel.kernelDerivX_I32); - validator.setKernel(1,GradientSobel.kernelDerivY_I32); - - GrayS16 input = new GrayS16(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - -@ Test - public void compareToConvolve_F32() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientSobel.class.getMethod("process", - GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class)); - - validator.setKernel(0,GradientSobel.kernelDerivX_F32); - validator.setKernel(1,GradientSobel.kernelDerivY_F32); - - GrayF32 input = new GrayF32(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayF32 derivX = new GrayF32(width,height); - GrayF32 derivY = new GrayF32(width,height); - - validator.compare(input,derivX,derivY); - } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientThree.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientThree.java index 8a1b38d72b..69d0bf439e 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientThree.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientThree.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,71 +18,11 @@ package boofcv.alg.filter.derivative; -import boofcv.alg.misc.ImageMiscOps; -import boofcv.struct.border.ImageBorder_F32; -import boofcv.struct.border.ImageBorder_S32; -import boofcv.struct.image.GrayF32; -import boofcv.struct.image.GrayS16; -import boofcv.struct.image.GrayU8; -import boofcv.testing.BoofStandardJUnit; import org.junit.jupiter.api.Test; -public class TestGradientThree extends BoofStandardJUnit { - int width = 5; - int height = 7; - - @Test void testStandard() { - StandardGradientChecks standard = new StandardGradientChecks(); +public class TestGradientThree extends GeneralGradient1DTests { + @Test void standard() { + var standard = new StandardGradientChecks(); standard.secondDerivativeTest(GradientThree.class,3); } - - @Test void compareToConvolve_I8() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientThree.class.getMethod("process", - GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0,GradientThree.kernelDeriv_I32,true); - validator.setKernel(1,GradientThree.kernelDeriv_I32,false); - - GrayU8 input = new GrayU8(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - - @Test void compareToConvolve_I16() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientThree.class.getMethod("process", - GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0,GradientThree.kernelDeriv_I32,true); - validator.setKernel(1,GradientThree.kernelDeriv_I32,false); - - GrayS16 input = new GrayS16(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - -@ Test - public void compareToConvolve_F32() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientThree.class.getMethod("process", - GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class )); - - validator.setKernel(0,GradientThree.kernelDeriv_F32,true); - validator.setKernel(1,GradientThree.kernelDeriv_F32,false); - - GrayF32 input = new GrayF32(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayF32 derivX = new GrayF32(width,height); - GrayF32 derivY = new GrayF32(width,height); - - validator.compare(input,derivX,derivY); - } - } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo0.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo0.java index 3e4b5619d6..e3960bead0 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo0.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo0.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,72 +18,11 @@ package boofcv.alg.filter.derivative; -import boofcv.alg.misc.ImageMiscOps; -import boofcv.struct.border.ImageBorder_F32; -import boofcv.struct.border.ImageBorder_S32; -import boofcv.struct.image.GrayF32; -import boofcv.struct.image.GrayS16; -import boofcv.struct.image.GrayU8; -import boofcv.testing.BoofStandardJUnit; import org.junit.jupiter.api.Test; -public class TestGradientTwo0 extends BoofStandardJUnit { - - int width = 5; - int height = 7; - - @Test void testStandard() { - StandardGradientChecks standard = new StandardGradientChecks(); +public class TestGradientTwo0 extends GeneralGradient1DTests { + @Test void standard() { + var standard = new StandardGradientChecks(); standard.secondDerivativeTest(GradientTwo0.class,3); } - - @Test void compareToConvolve_I8() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientTwo0.class.getMethod("process", - GrayU8.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0, GradientTwo0.kernelDeriv_I32,true); - validator.setKernel(1, GradientTwo0.kernelDeriv_I32,false); - - GrayU8 input = new GrayU8(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - - @Test void compareToConvolve_I16() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientTwo0.class.getMethod("process", - GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - - validator.setKernel(0, GradientTwo0.kernelDeriv_I32,true); - validator.setKernel(1, GradientTwo0.kernelDeriv_I32,false); - - GrayS16 input = new GrayS16(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivX = new GrayS16(width,height); - GrayS16 derivY = new GrayS16(width,height); - - validator.compare(input,derivX,derivY); - } - -@ Test - public void compareToConvolve_F32() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(GradientTwo0.class.getMethod("process", - GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class )); - - validator.setKernel(0, GradientTwo0.kernelDeriv_F32,true); - validator.setKernel(1, GradientTwo0.kernelDeriv_F32,false); - - GrayF32 input = new GrayF32(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayF32 derivX = new GrayF32(width,height); - GrayF32 derivY = new GrayF32(width,height); - - validator.compare(input,derivX,derivY); - } - } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo1.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo1.java new file mode 100644 index 0000000000..66746acf72 --- /dev/null +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestGradientTwo1.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.alg.filter.derivative; + +import org.junit.jupiter.api.Test; + +public class TestGradientTwo1 extends GeneralGradient1DTests { + @Test void standard() { + var standard = new StandardGradientChecks(); + standard.secondDerivativeTest(GradientTwo1.class,3); + } +} diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianFromGradient.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianFromGradient.java index 496a751ea7..3c5619d053 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianFromGradient.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianFromGradient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -36,8 +36,8 @@ public class TestHessianFromGradient extends BoofStandardJUnit { validator.setTarget(HessianFromGradient.class.getMethod("hessianPrewitt", GrayS16.class,GrayS16.class, GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - validator.setKernel(0,GradientPrewitt.kernelDerivX_I32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_I32); + validator.setKernel(0,GradientPrewitt.kernelX_I32); + validator.setKernel(1,GradientPrewitt.kernelY_I32); GrayS16 derivX = new GrayS16(width,height); GrayS16 derivY = new GrayS16(width,height); @@ -55,8 +55,8 @@ public class TestHessianFromGradient extends BoofStandardJUnit { validator.setTarget(HessianFromGradient.class.getMethod("hessianPrewitt", GrayF32.class,GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class )); - validator.setKernel(0,GradientPrewitt.kernelDerivX_F32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_F32); + validator.setKernel(0,GradientPrewitt.kernelX_F32); + validator.setKernel(1,GradientPrewitt.kernelY_F32); GrayF32 derivX = new GrayF32(width,height); GrayF32 derivY = new GrayF32(width,height); @@ -74,8 +74,8 @@ public class TestHessianFromGradient extends BoofStandardJUnit { validator.setTarget(HessianFromGradient.class.getMethod("hessianSobel", GrayS16.class,GrayS16.class, GrayS16.class, GrayS16.class, GrayS16.class,ImageBorder_S32.class )); - validator.setKernel(0,GradientSobel.kernelDerivX_I32); - validator.setKernel(1,GradientSobel.kernelDerivY_I32); + validator.setKernel(0,GradientSobel.kernelX_I32); + validator.setKernel(1,GradientSobel.kernelY_I32); GrayS16 derivX = new GrayS16(width,height); GrayS16 derivY = new GrayS16(width,height); @@ -93,8 +93,8 @@ public class TestHessianFromGradient extends BoofStandardJUnit { validator.setTarget(HessianFromGradient.class.getMethod("hessianSobel", GrayF32.class,GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class,ImageBorder_F32.class )); - validator.setKernel(0,GradientSobel.kernelDerivX_F32); - validator.setKernel(1,GradientSobel.kernelDerivY_F32); + validator.setKernel(0,GradientSobel.kernelX_F32); + validator.setKernel(1,GradientSobel.kernelY_F32); GrayF32 derivX = new GrayF32(width,height); GrayF32 derivY = new GrayF32(width,height); @@ -112,8 +112,8 @@ public class TestHessianFromGradient extends BoofStandardJUnit { validator.setTarget(HessianFromGradient.class.getMethod("hessianThree", GrayS16.class,GrayS16.class, GrayS16.class, GrayS16.class, GrayS16.class,ImageBorder_S32.class )); - validator.setKernel(0,GradientThree.kernelDeriv_I32,true); - validator.setKernel(1,GradientThree.kernelDeriv_I32,false); + validator.setKernel(0,GradientThree.kernel_I32,true); + validator.setKernel(1,GradientThree.kernel_I32,false); GrayS16 derivX = new GrayS16(width,height); GrayS16 derivY = new GrayS16(width,height); @@ -131,8 +131,8 @@ public class TestHessianFromGradient extends BoofStandardJUnit { validator.setTarget(HessianFromGradient.class.getMethod("hessianThree", GrayF32.class,GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class,ImageBorder_F32.class )); - validator.setKernel(0,GradientThree.kernelDeriv_F32,true); - validator.setKernel(1,GradientThree.kernelDeriv_F32,false); + validator.setKernel(0,GradientThree.kernel_F32,true); + validator.setKernel(1,GradientThree.kernel_F32,false); GrayF32 derivX = new GrayF32(width,height); GrayF32 derivY = new GrayF32(width,height); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianSobel.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianSobel.java index 1d623c174d..49422d73ef 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianSobel.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianSobel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,54 +18,4 @@ package boofcv.alg.filter.derivative; -import boofcv.alg.misc.ImageMiscOps; -import boofcv.struct.border.ImageBorder_F32; -import boofcv.struct.border.ImageBorder_S32; -import boofcv.struct.image.GrayF32; -import boofcv.struct.image.GrayS16; -import boofcv.struct.image.GrayU8; -import boofcv.testing.BoofStandardJUnit; -import org.junit.jupiter.api.Test; - - -public class TestHessianSobel extends BoofStandardJUnit { - - int width = 20; - int height = 25; - - @Test void compareToConvolve_I8() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(HessianSobel.class.getMethod("process", - GrayU8.class, GrayS16.class, GrayS16.class, GrayS16.class , ImageBorder_S32.class )); - - validator.setKernel(0, HessianSobel.kernelXX_I32); - validator.setKernel(1, HessianSobel.kernelYY_I32); - validator.setKernel(2, HessianSobel.kernelXY_I32); - - GrayU8 input = new GrayU8(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayS16 derivXX = new GrayS16(width,height); - GrayS16 derivYY = new GrayS16(width,height); - GrayS16 derivXY = new GrayS16(width,height); - - validator.compare(input,derivXX,derivYY,derivXY); - } - - @Test void compareToConvolve_F32() throws NoSuchMethodException { - CompareDerivativeToConvolution validator = new CompareDerivativeToConvolution(); - validator.setTarget(HessianSobel.class.getMethod("process", - GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class , ImageBorder_F32.class )); - - validator.setKernel(0, HessianSobel.kernelXX_F32); - validator.setKernel(1, HessianSobel.kernelYY_F32); - validator.setKernel(2, HessianSobel.kernelXY_F32); - - GrayF32 input = new GrayF32(width,height); - ImageMiscOps.fillUniform(input, rand, 0, 10); - GrayF32 derivXX = new GrayF32(width,height); - GrayF32 derivYY = new GrayF32(width,height); - GrayF32 derivXY = new GrayF32(width,height); - - validator.compare(input,derivXX,derivYY,derivXY); - } -} +public class TestHessianSobel extends GeneralHessian2DTests {} diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianThree.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianThree.java index ebb61c9a26..28524e64d9 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianThree.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/TestHessianThree.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -42,9 +42,9 @@ public class TestHessianThree extends BoofStandardJUnit { validator.setTarget(HessianThree.class.getMethod("process", GrayU8.class, GrayS16.class, GrayS16.class, GrayS16.class, ImageBorder_S32.class )); - validator.setKernel(0,HessianThree.kernelXXYY_I32,true); - validator.setKernel(1,HessianThree.kernelXXYY_I32,false); - validator.setKernel(2,HessianThree.kernelCross_I32); + validator.setKernel(0,HessianThree.kernelXX_I32,true); + validator.setKernel(1,HessianThree.kernelXX_I32,false); + validator.setKernel(2,HessianThree.kernelXY_I32); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -60,9 +60,9 @@ public class TestHessianThree extends BoofStandardJUnit { validator.setTarget(HessianThree.class.getMethod("process", GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class, ImageBorder_F32.class )); - validator.setKernel(0,HessianThree.kernelXXYY_F32,true); - validator.setKernel(1,HessianThree.kernelXXYY_F32,false); - validator.setKernel(2,HessianThree.kernelCross_F32); + validator.setKernel(0,HessianThree.kernelXX_F32,true); + validator.setKernel(1,HessianThree.kernelXX_F32,false); + validator.setKernel(2,HessianThree.kernelXY_F32); GrayF32 input = new GrayF32(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestDerivativeLaplacian_Inner.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestDerivativeLaplacian_Inner.java index 77c0e87d90..1ba206787d 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestDerivativeLaplacian_Inner.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestDerivativeLaplacian_Inner.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -52,7 +52,7 @@ public void process_U8_S16(GrayU8 img, GrayS16 deriv) { ImageBorder_S32 border = (ImageBorder_S32) FactoryImageBorder.single(BorderType.EXTENDED, GrayU8.class); GrayS16 expected = deriv.createSameShape(); - ConvolveImage.convolve(DerivativeLaplacian.kernel_I32,img,expected,border); + ConvolveImage.convolve(DerivativeLaplacian.kernelXY_I32,img,expected,border); BoofTesting.assertEqualsInner(expected,deriv,0,1,1,false); } @@ -87,7 +87,7 @@ public void process_F32(GrayF32 img, GrayF32 deriv) { ImageBorder_F32 border = (ImageBorder_F32)FactoryImageBorder.single(BorderType.EXTENDED, GrayF32.class); GrayF32 expected = deriv.createSameShape(); - ConvolveImage.convolve(DerivativeLaplacian.kernel_F32,img,expected,border); + ConvolveImage.convolve(DerivativeLaplacian.kernelXY_F32,img,expected,border); BoofTesting.assertEqualsInner(expected,deriv, UtilEjml.TEST_F32,1,1,false); } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientPrewitt_Shared.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientPrewitt_Shared.java index fe7b034c08..640fdaa4a9 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientPrewitt_Shared.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientPrewitt_Shared.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -37,8 +37,8 @@ public class TestGradientPrewitt_Shared extends BoofStandardJUnit { validator.setTarget(GradientPrewitt_Shared.class.getMethod("process", GrayU8.class, GrayS16.class, GrayS16.class )); - validator.setKernel(0, GradientPrewitt.kernelDerivX_I32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_I32); + validator.setKernel(0, GradientPrewitt.kernelX_I32); + validator.setKernel(1,GradientPrewitt.kernelY_I32); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -53,8 +53,8 @@ public class TestGradientPrewitt_Shared extends BoofStandardJUnit { validator.setTarget(GradientPrewitt_Shared.class.getMethod("process", GrayS16.class, GrayS16.class, GrayS16.class )); - validator.setKernel(0, GradientPrewitt.kernelDerivX_I32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_I32); + validator.setKernel(0, GradientPrewitt.kernelX_I32); + validator.setKernel(1,GradientPrewitt.kernelY_I32); GrayS16 input = new GrayS16(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -69,8 +69,8 @@ public class TestGradientPrewitt_Shared extends BoofStandardJUnit { validator.setTarget(GradientPrewitt_Shared.class.getMethod("process", GrayF32.class, GrayF32.class, GrayF32.class )); - validator.setKernel(0, GradientPrewitt.kernelDerivX_F32); - validator.setKernel(1,GradientPrewitt.kernelDerivY_F32); + validator.setKernel(0, GradientPrewitt.kernelX_F32); + validator.setKernel(1,GradientPrewitt.kernelY_F32); GrayF32 input = new GrayF32(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientSobel_Naive.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientSobel_Naive.java index dc776e6124..21bc870769 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientSobel_Naive.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientSobel_Naive.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -34,9 +34,7 @@ public class TestGradientSobel_Naive extends BoofStandardJUnit { private final int width = 4; private final int height = 5; - /** - * Compare the results to a hand computed value - */ + /// Compare the results to a hand computed value @Test void compareToKnown_I8() { GrayU8 img = new GrayU8(width, height); ImageMiscOps.fillUniform(img, rand, 0, 100); @@ -47,22 +45,20 @@ public class TestGradientSobel_Naive extends BoofStandardJUnit { BoofTesting.checkSubImage(this, "compareToKnown_I8", true, img, derivX, derivY); } - public void compareToKnown_I8(GrayU8 img, GrayS16 derivX, GrayS16 derivY) { + public void compareToKnown_I8( GrayU8 img, GrayS16 derivX, GrayS16 derivY ) { GradientSobel_Naive.process(img, derivX, derivY); - int dX = -((img.get(0, 2) + img.get(0, 0)) + img.get(0, 1) * 2); - dX += (img.get(2, 2) + img.get(2, 0)) + img.get(2, 1) * 2; + int dX = -((img.get(0, 2) + img.get(0, 0)) + img.get(0, 1)*2); + dX += (img.get(2, 2) + img.get(2, 0)) + img.get(2, 1)*2; - int dY = -((img.get(2, 0) + img.get(0, 0)) + img.get(1, 0) * 2); - dY += (img.get(2, 2) + img.get(0, 2)) + img.get(1, 2) * 2; + int dY = -((img.get(2, 0) + img.get(0, 0)) + img.get(1, 0)*2); + dY += (img.get(2, 2) + img.get(0, 2)) + img.get(1, 2)*2; assertEquals(dX, derivX.get(1, 1), 1e-6); assertEquals(dY, derivY.get(1, 1), 1e-6); } - /** - * Compare the results to a hand computed value - */ + /// Compare the results to a hand computed value @Test void compareToKnown_F32() { GrayF32 img = new GrayF32(width, height); ImageMiscOps.fillUniform(img, rand, 0, 255); @@ -71,17 +67,16 @@ public void compareToKnown_I8(GrayU8 img, GrayS16 derivX, GrayS16 derivY) { GrayF32 derivY = new GrayF32(width, height); BoofTesting.checkSubImage(this, "compareToKnown_F32", true, img, derivX, derivY); - } - public void compareToKnown_F32(GrayF32 img, GrayF32 derivX, GrayF32 derivY) { + public void compareToKnown_F32( GrayF32 img, GrayF32 derivX, GrayF32 derivY ) { GradientSobel_Naive.process(img, derivX, derivY); - float dX = -((img.get(0, 2) + img.get(0, 0)) * 0.25f + img.get(0, 1) * 0.5f); - dX += (img.get(2, 2) + img.get(2, 0)) * 0.25f + img.get(2, 1) * 0.5f; + float dX = -((img.get(0, 2) + img.get(0, 0))*0.125F + img.get(0, 1)*0.25F); + dX += (img.get(2, 2) + img.get(2, 0))*0.125F + img.get(2, 1)*0.25F; - float dY = -((img.get(2, 0) + img.get(0, 0)) * 0.25f + img.get(1, 0) * 0.5f); - dY += (img.get(2, 2) + img.get(0, 2)) * 0.25f + img.get(1, 2) * 0.5f; + float dY = -((img.get(2, 0) + img.get(0, 0))*0.125F + img.get(1, 0)*0.25F); + dY += (img.get(2, 2) + img.get(0, 2))*0.125F + img.get(1, 2)*0.25F; assertEquals(dX, derivX.get(1, 1), UtilEjml.TEST_F32); assertEquals(dY, derivY.get(1, 1), UtilEjml.TEST_F32); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientThree_Standard.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientThree_Standard.java index a6954d2f02..9a02c1d73c 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientThree_Standard.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientThree_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -38,8 +38,8 @@ public class TestGradientThree_Standard extends BoofStandardJUnit { validator.setTarget(GradientThree_Standard.class.getMethod("process", GrayU8.class, GrayS16.class, GrayS16.class )); - validator.setKernel(0, GradientThree.kernelDeriv_I32,true); - validator.setKernel(1, GradientThree.kernelDeriv_I32,false); + validator.setKernel(0, GradientThree.kernel_I32,true); + validator.setKernel(1, GradientThree.kernel_I32,false); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -54,8 +54,8 @@ public class TestGradientThree_Standard extends BoofStandardJUnit { validator.setTarget(GradientThree_Standard.class.getMethod("process", GrayU8.class, GrayS32.class, GrayS32.class )); - validator.setKernel(0, GradientThree.kernelDeriv_I32,true); - validator.setKernel(1, GradientThree.kernelDeriv_I32,false); + validator.setKernel(0, GradientThree.kernel_I32,true); + validator.setKernel(1, GradientThree.kernel_I32,false); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -70,8 +70,8 @@ public class TestGradientThree_Standard extends BoofStandardJUnit { validator.setTarget(GradientThree_Standard.class.getMethod("process", GrayF32.class, GrayF32.class, GrayF32.class )); - validator.setKernel(0, GradientThree.kernelDeriv_F32,true); - validator.setKernel(1, GradientThree.kernelDeriv_F32,false); + validator.setKernel(0, GradientThree.kernel_F32,true); + validator.setKernel(1, GradientThree.kernel_F32,false); GrayF32 input = new GrayF32(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo0_Standard.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo0_Standard.java index 9850294d6f..d53da5dfc4 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo0_Standard.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo0_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -37,8 +37,8 @@ public class TestGradientTwo0_Standard extends BoofStandardJUnit { validator.setTarget(GradientTwo0_Standard.class.getMethod("process", GrayU8.class, GrayS16.class, GrayS16.class )); - validator.setKernel(0, GradientTwo0.kernelDeriv_I32,true); - validator.setKernel(1, GradientTwo0.kernelDeriv_I32,false); + validator.setKernel(0, GradientTwo0.kernel_I32,true); + validator.setKernel(1, GradientTwo0.kernel_I32,false); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -53,8 +53,8 @@ public class TestGradientTwo0_Standard extends BoofStandardJUnit { validator.setTarget(GradientTwo0_Standard.class.getMethod("process", GrayF32.class, GrayF32.class, GrayF32.class )); - validator.setKernel(0, GradientTwo0.kernelDeriv_F32,true); - validator.setKernel(1, GradientTwo0.kernelDeriv_F32,false); + validator.setKernel(0, GradientTwo0.kernel_F32,true); + validator.setKernel(1, GradientTwo0.kernel_F32,false); GrayF32 input = new GrayF32(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo1_Standard.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo1_Standard.java index 9ad4dacb13..402b7352ca 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo1_Standard.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestGradientTwo1_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -37,8 +37,8 @@ public class TestGradientTwo1_Standard extends BoofStandardJUnit { validator.setTarget(GradientTwo1_Standard.class.getMethod("process", GrayU8.class, GrayS16.class, GrayS16.class )); - validator.setKernel(0, GradientTwo1.kernelDeriv_I32,true); - validator.setKernel(1, GradientTwo1.kernelDeriv_I32,false); + validator.setKernel(0, GradientTwo1.kernel_I32,true); + validator.setKernel(1, GradientTwo1.kernel_I32,false); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -53,8 +53,8 @@ public class TestGradientTwo1_Standard extends BoofStandardJUnit { validator.setTarget(GradientTwo1_Standard.class.getMethod("process", GrayF32.class, GrayF32.class, GrayF32.class )); - validator.setKernel(0, GradientTwo1.kernelDeriv_F32,true); - validator.setKernel(1, GradientTwo1.kernelDeriv_F32,false); + validator.setKernel(0, GradientTwo1.kernel_F32,true); + validator.setKernel(1, GradientTwo1.kernel_F32,false); GrayF32 input = new GrayF32(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Border.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Border.java index 4a27fb0643..856e70d6f1 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Border.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Border.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -94,9 +94,9 @@ public static GrayS16 hessianDetFromConv_U8_S16(GrayU8 img) { GrayS16 Lyy = new GrayS16(img.width,img.height); GrayS16 Lxy = new GrayS16(img.width,img.height); - ConvolveImage.horizontal(HessianThree.kernelXXYY_I32,img,Lxx,border); - ConvolveImage.vertical(HessianThree.kernelXXYY_I32,img,Lyy,border); - ConvolveImage.convolve(HessianThree.kernelCross_I32,img,Lxy,border); + ConvolveImage.horizontal(HessianThree.kernelXX_I32,img,Lxx,border); + ConvolveImage.vertical(HessianThree.kernelXX_I32,img,Lyy,border); + ConvolveImage.convolve(HessianThree.kernelXY_I32,img,Lxy,border); GrayS16 expected =new GrayS16(img.width,img.height); @@ -114,9 +114,9 @@ public static GrayF32 hessianDetFromConv_F32_F32(GrayF32 img) { GrayF32 Lyy = new GrayF32(img.width,img.height); GrayF32 Lxy = new GrayF32(img.width,img.height); - ConvolveImage.horizontal(HessianThree.kernelXXYY_F32,img,Lxx,border); - ConvolveImage.vertical(HessianThree.kernelXXYY_F32,img,Lyy,border); - ConvolveImage.convolve(HessianThree.kernelCross_F32,img,Lxy,border); + ConvolveImage.horizontal(HessianThree.kernelXX_F32,img,Lxx,border); + ConvolveImage.vertical(HessianThree.kernelXX_F32,img,Lyy,border); + ConvolveImage.convolve(HessianThree.kernelXY_F32,img,Lxy,border); GrayF32 expected = new GrayF32(img.width,img.height); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Inner.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Inner.java index 71e74f267d..0ecc497aa5 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Inner.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThreeDeterminant_Inner.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -36,8 +36,7 @@ public class TestHessianThreeDeterminant_Inner extends BoofStandardJUnit { private final int width = 8; private final int height = 9; - @Test - void process_U8_S16() { + @Test void process_U8_S16() { GrayU8 img = new GrayU8(width, height); ImageMiscOps.fillUniform(img, rand, 0, 100); @@ -52,8 +51,7 @@ public void process_U8_S16( GrayU8 img, GrayS16 deriv ) { BoofTesting.assertEqualsInner(expected, deriv, 0, 2, 2, false); } - @Test - void process_U8_F32() { + @Test void process_U8_F32() { GrayU8 img = new GrayU8(width, height); ImageMiscOps.fillUniform(img, rand, 0, 100); @@ -69,8 +67,7 @@ public void process_U8_F32( GrayU8 img, GrayF32 deriv ) { BoofTesting.assertEqualsInner(expected, deriv, 1e-4, 2, 2, false); } - @Test - void process_F32() { + @Test void process_F32() { GrayF32 img = new GrayF32(width, height); ImageMiscOps.fillUniform(img, rand, 0, 1); @@ -91,9 +88,9 @@ public static GrayS16 hessianDetFromConv_U8_S16( GrayU8 img ) { GrayS16 Lyy = new GrayS16(img.width, img.height); GrayS16 Lxy = new GrayS16(img.width, img.height); - ConvolveImage.horizontal(HessianThree.kernelXXYY_I32, img, Lxx, border); - ConvolveImage.vertical(HessianThree.kernelXXYY_I32, img, Lyy, border); - ConvolveImage.convolve(HessianThree.kernelCross_I32, img, Lxy, border); + ConvolveImage.horizontal(HessianThree.kernelXX_I32, img, Lxx, border); + ConvolveImage.vertical(HessianThree.kernelXX_I32, img, Lyy, border); + ConvolveImage.convolve(HessianThree.kernelXY_I32, img, Lxy, border); GrayS16 expected = new GrayS16(img.width, img.height); @@ -111,9 +108,9 @@ public static GrayF32 hessianDetFromConv_F32_F32( GrayF32 img ) { GrayF32 Lyy = new GrayF32(img.width, img.height); GrayF32 Lxy = new GrayF32(img.width, img.height); - ConvolveImage.horizontal(HessianThree.kernelXXYY_F32, img, Lxx, border); - ConvolveImage.vertical(HessianThree.kernelXXYY_F32, img, Lyy, border); - ConvolveImage.convolve(HessianThree.kernelCross_F32, img, Lxy, border); + ConvolveImage.horizontal(HessianThree.kernelXX_F32, img, Lxx, border); + ConvolveImage.vertical(HessianThree.kernelXX_F32, img, Lyy, border); + ConvolveImage.convolve(HessianThree.kernelXY_F32, img, Lxy, border); GrayF32 expected = new GrayF32(img.width, img.height); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThree_Standard.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThree_Standard.java index 1f4f85cc49..f5bcc07627 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThree_Standard.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/derivative/impl/TestHessianThree_Standard.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -36,9 +36,9 @@ public class TestHessianThree_Standard extends BoofStandardJUnit { validator.setTarget(HessianThree_Standard.class.getMethod("process", GrayU8.class, GrayS16.class, GrayS16.class, GrayS16.class)); - validator.setKernel(0,HessianThree.kernelXXYY_I32,true); - validator.setKernel(1,HessianThree.kernelXXYY_I32,false); - validator.setKernel(2,HessianThree.kernelCross_I32); + validator.setKernel(0,HessianThree.kernelXX_I32,true); + validator.setKernel(1,HessianThree.kernelXX_I32,false); + validator.setKernel(2,HessianThree.kernelXY_I32); GrayU8 input = new GrayU8(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); @@ -54,9 +54,9 @@ public class TestHessianThree_Standard extends BoofStandardJUnit { validator.setTarget(HessianThree_Standard.class.getMethod("process", GrayF32.class, GrayF32.class, GrayF32.class, GrayF32.class )); - validator.setKernel(0,HessianThree.kernelXXYY_F32,true); - validator.setKernel(1,HessianThree.kernelXXYY_F32,false); - validator.setKernel(2,HessianThree.kernelCross_F32); + validator.setKernel(0,HessianThree.kernelXX_F32,true); + validator.setKernel(1,HessianThree.kernelXX_F32,false); + validator.setKernel(2,HessianThree.kernelXY_F32); GrayF32 input = new GrayF32(width,height); ImageMiscOps.fillUniform(input, rand, 0, 10); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/filter/kernel/TestKernelMath.java b/main/boofcv-ip/src/test/java/boofcv/alg/filter/kernel/TestKernelMath.java index 9799e3d8af..d088c90b30 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/filter/kernel/TestKernelMath.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/filter/kernel/TestKernelMath.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -158,11 +158,11 @@ public class TestKernelMath extends BoofStandardJUnit { } @Test void convolve1D_F32() { - Kernel1D_F32 k1 = new Kernel1D_F32(new float[]{1,2,3,4,5},5); - Kernel1D_F32 k2 = new Kernel1D_F32(new float[]{6,7,8},3); + Kernel1D_F32 k1 = new Kernel1D_F32(5, new float[]{1,2,3,4,5}); + Kernel1D_F32 k2 = new Kernel1D_F32(3, new float[]{6,7,8}); // computed using conv() in octave - Kernel1D_F32 expected = new Kernel1D_F32(new float[]{6,19,40,61,82,67,40},7); + Kernel1D_F32 expected = new Kernel1D_F32(7, new float[]{6,19,40,61,82,67,40}); Kernel1D_F32 c = KernelMath.convolve1D_F32(k1,k2); for( int i = 0; i < 7; i++ ) { @@ -176,8 +176,8 @@ public class TestKernelMath extends BoofStandardJUnit { } @Test void convolve2D_1D_F32() { - Kernel1D_F32 k1 = new Kernel1D_F32(new float[]{4,3,2},3,1); - Kernel1D_F32 k2 = new Kernel1D_F32(new float[]{9,5,1},3,1); + Kernel1D_F32 k1 = new Kernel1D_F32(3, 1, new float[]{4,3,2}); + Kernel1D_F32 k2 = new Kernel1D_F32(3, 1, new float[]{9,5,1}); Kernel2D_F32 c = KernelMath.convolve2D(k1, k2); assertEquals(3,c.width); @@ -191,8 +191,8 @@ public class TestKernelMath extends BoofStandardJUnit { } @Test void convolve_1D_F64() { - Kernel1D_F64 k1 = new Kernel1D_F64(new double[]{4,3,2},3,1); - Kernel1D_F64 k2 = new Kernel1D_F64(new double[]{9,5,1},3,1); + Kernel1D_F64 k1 = new Kernel1D_F64(3, 1, new double[]{4,3,2}); + Kernel1D_F64 k2 = new Kernel1D_F64(3, 1, new double[]{9,5,1}); Kernel2D_F64 c = KernelMath.convolve2D(k1, k2); assertEquals(3,c.width); @@ -206,8 +206,8 @@ public class TestKernelMath extends BoofStandardJUnit { } @Test void convolve_1D_I32() { - Kernel1D_S32 k1 = new Kernel1D_S32(new int[]{4,3,2},3,1); - Kernel1D_S32 k2 = new Kernel1D_S32(new int[]{9,5,1},3,1); + Kernel1D_S32 k1 = new Kernel1D_S32(3, 1, new int[]{4,3,2}); + Kernel1D_S32 k2 = new Kernel1D_S32(3, 1, new int[]{9,5,1}); Kernel2D_S32 c = KernelMath.convolve2D(k1, k2); assertEquals(3,c.width); @@ -369,7 +369,7 @@ public class TestKernelMath extends BoofStandardJUnit { } @Test void convert_1D_F32_to_I32() { - Kernel1D_F32 orig = new Kernel1D_F32(new float[]{0.1f,1,1e-8f,-1,-0.1f}, 5, 1); + Kernel1D_F32 orig = new Kernel1D_F32(5, 1, new float[]{0.1f,1,1e-8f,-1,-0.1f}); Kernel1D_S32 found = KernelMath.convert(orig,1f/60f); assertEquals(orig.offset,found.offset); @@ -382,7 +382,7 @@ public class TestKernelMath extends BoofStandardJUnit { } @Test void convert_1D_F64_to_I32() { - Kernel1D_F64 orig = new Kernel1D_F64(new double[]{0.1,1,1e-8,-1,-0.1}, 5, 1); + Kernel1D_F64 orig = new Kernel1D_F64(5, 1, new double[]{0.1,1,1e-8,-1,-0.1}); Kernel1D_S32 found = KernelMath.convert(orig,1f/60f); assertEquals(orig.offset,found.offset); diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_F32.java b/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_F32.java index bc546d8306..17783302f8 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_F32.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_F32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -69,9 +69,7 @@ protected float compute(GrayF32 img, float x, float y) { return total; } - private static class Dummy extends KernelContinuous1D_F32 - { - + private static class Dummy extends KernelContinuous1D_F32 { private Dummy() { super(5); } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_S16.java b/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_S16.java index c7e9c8eb97..d929f2d9a6 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_S16.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_S16.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -23,28 +23,27 @@ import boofcv.struct.convolve.KernelContinuous1D_F32; import boofcv.struct.image.GrayS16; -public class TestImplInterpolatePixelConvolution_S16 - extends GeneralChecksInterpolationPixelS -{ +public class TestImplInterpolatePixelConvolution_S16 + extends GeneralChecksInterpolationPixelS { public TestImplInterpolatePixelConvolution_S16() { exceptionOutside = false; } @Override - protected GrayS16 createImage(int width, int height) { + protected GrayS16 createImage( int width, int height ) { return new GrayS16(width, height); } @Override - protected InterpolatePixelS wrap(GrayS16 image, int minValue, int maxValue) { - InterpolatePixelS ret = new ImplInterpolatePixelConvolution_S16(new Dummy(),0,255); + protected InterpolatePixelS wrap( GrayS16 image, int minValue, int maxValue ) { + InterpolatePixelS ret = new ImplInterpolatePixelConvolution_S16(new Dummy(), 0, 255); ret.setImage(image); return ret; } @Override - protected float compute(GrayS16 img, float x, float y) { + protected float compute( GrayS16 img, float x, float y ) { int xx = (int)x; int yy = (int)y; @@ -53,50 +52,44 @@ protected float compute(GrayS16 img, float x, float y) { int y0 = yy - 2; int y1 = yy + 2; - if( x0 < 0 ) x0 = 0; - if( x1 >= img.width ) x1 = img.width-1; - if( y0 < 0 ) y0 = 0; - if( y1 >= img.height ) y1 = img.height-1; + if (x0 < 0) x0 = 0; + if (x1 >= img.width) x1 = img.width - 1; + if (y0 < 0) y0 = 0; + if (y1 >= img.height) y1 = img.height - 1; float total = 0; - for( int i = y0; i <= y1; i++ ) { - for( int j = x0; j <= x1; j++ ) { - total += img.get(j,i); + for (int i = y0; i <= y1; i++) { + for (int j = x0; j <= x1; j++) { + total += img.get(j, i); } } - total /= (1+x1-x0)*(1+y1-y0); + total /= (1 + x1 - x0)*(1 + y1 - y0); return total; } - private static class Dummy extends KernelContinuous1D_F32 - { + private static class Dummy extends KernelContinuous1D_F32 { private Dummy() { super(5); } - @Override - public double getDouble(int index) { + @Override public double getDouble( int index ) { return 0; } - @Override - public void setD(int index, double value) {} + @Override public void setD( int index, double value ) {} - @Override - public boolean isInteger() { + @Override public boolean isInteger() { return false; } - @Override - public T copy() { + @Override public T copy() { return null; } - @Override - public float compute(float x) { + @Override public float compute( float x ) { return 1.0f/5.0f; } } diff --git a/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_U8.java b/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_U8.java index 1f0578b7cd..5e1b6efe2a 100644 --- a/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_U8.java +++ b/main/boofcv-ip/src/test/java/boofcv/alg/interpolate/impl/TestImplInterpolatePixelConvolution_U8.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -70,33 +70,26 @@ protected float compute(GrayU8 img, float x, float y) { return total; } - private static class Dummy extends KernelContinuous1D_F32 - { - + private static class Dummy extends KernelContinuous1D_F32 { private Dummy() { super(5); } - @Override - public double getDouble(int index) { + @Override public double getDouble(int index) { return 0; } - @Override - public void setD(int index, double value) {} + @Override public void setD(int index, double value) {} - @Override - public boolean isInteger() { + @Override public boolean isInteger() { return false; } - @Override - public T copy() { + @Override public T copy() { return null; } - @Override - public float compute(float x) { + @Override public float compute(float x) { return 1.0f/5.0f; } } diff --git a/main/boofcv-ip/src/test/java/boofcv/core/image/TestPyramidGradient.java b/main/boofcv-ip/src/test/java/boofcv/core/image/TestPyramidGradient.java index bdd73768ce..e02bb5f9cf 100644 --- a/main/boofcv-ip/src/test/java/boofcv/core/image/TestPyramidGradient.java +++ b/main/boofcv-ip/src/test/java/boofcv/core/image/TestPyramidGradient.java @@ -48,6 +48,8 @@ class TestPyramidGradient extends BoofStandardJUnit { count++; } + @Override public int divisor() {return 2;} + @Override public ImageType getInputType() {return ImageType.SB_F32;} }; diff --git a/main/boofcv-recognition/src/main/java/boofcv/alg/template/TemplateMatching.java b/main/boofcv-recognition/src/main/java/boofcv/alg/template/TemplateMatching.java index b970d1889b..5b1dd49653 100644 --- a/main/boofcv-recognition/src/main/java/boofcv/alg/template/TemplateMatching.java +++ b/main/boofcv-recognition/src/main/java/boofcv/alg/template/TemplateMatching.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -77,7 +77,7 @@ public TemplateMatching( TemplateMatchingIntensity match ) { config = new ConfigExtract(2, -Float.MAX_VALUE, 0, true, true, false); } - extractor = FactoryFeatureExtractor.nonmax(config); + extractor = FactoryFeatureExtractor.nonmax(config, 1); } /** diff --git a/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/sfot/SparseFlowObjectTracker.java b/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/sfot/SparseFlowObjectTracker.java index 7f94fa1c31..67d05ea630 100644 --- a/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/sfot/SparseFlowObjectTracker.java +++ b/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/sfot/SparseFlowObjectTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -218,12 +218,12 @@ private void trackFeatures( Image input, RectangleRotate_F64 region ) { track.x = xx; track.y = yy; - klt.setImage(previousImage, previousDerivX, previousDerivY); + klt.setImage(previousImage, previousDerivX, previousDerivY, gradient.divisor()); if (!klt.setDescription(track)) { continue; } - klt.setImage(currentImage, currentDerivX, currentDerivY); + klt.setImage(currentImage, currentDerivX, currentDerivY, gradient.divisor()); KltTrackFault fault = klt.track(track); if (fault != KltTrackFault.SUCCESS) { continue; @@ -236,7 +236,7 @@ private void trackFeatures( Image input, RectangleRotate_F64 region ) { if (!klt.setDescription(track)) { continue; } - klt.setImage(previousImage, previousDerivX, previousDerivY); + klt.setImage(previousImage, previousDerivX, previousDerivY, gradient.divisor()); fault = klt.track(track); if (fault != KltTrackFault.SUCCESS) { continue; diff --git a/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/tld/TldRegionTracker.java b/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/tld/TldRegionTracker.java index 29e33f604e..de263d65b1 100644 --- a/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/tld/TldRegionTracker.java +++ b/main/boofcv-recognition/src/main/java/boofcv/alg/tracker/tld/TldRegionTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -235,7 +235,7 @@ protected boolean trackFeature() { float prevY = t.klt.y; // track in forwards direction - tracker.setImage(currentImage, currentDerivX, currentDerivY); + tracker.setImage(currentImage, currentDerivX, currentDerivY, gradient.divisor()); KltTrackFault result = tracker.track(t.klt); if (result != KltTrackFault.SUCCESS) { t.active = false; @@ -247,7 +247,7 @@ protected boolean trackFeature() { // track in reverse direction tracker.setDescription(t.klt); - tracker.setImage(previousImage, previousDerivX, previousDerivY); + tracker.setImage(previousImage, previousDerivX, previousDerivY, gradient.divisor()); result = tracker.track(t.klt); if (result != KltTrackFault.SUCCESS) { t.active = false; @@ -299,7 +299,7 @@ protected void spawnGrid( Rectangle2D_F64 prevRect ) { double spawnHeight = spawnRect.getHeight(); // try spawning features at evenly spaced points inside the grid - tracker.setImage(previousImage, previousDerivX, previousDerivY); + tracker.setImage(previousImage, previousDerivX, previousDerivY, gradient.divisor()); for (int i = 0; i < gridWidth; i++) { diff --git a/main/boofcv-recognition/src/test/java/boofcv/alg/template/GeneralTemplateMatchTests.java b/main/boofcv-recognition/src/test/java/boofcv/alg/template/GeneralTemplateMatchTests.java index 05bbe60cef..b679fcb303 100644 --- a/main/boofcv-recognition/src/test/java/boofcv/alg/template/GeneralTemplateMatchTests.java +++ b/main/boofcv-recognition/src/test/java/boofcv/alg/template/GeneralTemplateMatchTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -50,7 +50,7 @@ public abstract class GeneralTemplateMatchTests> extends // if a perfect match is zero additional tests can be done boolean isPerfectZero; - protected GeneralTemplateMatchTests(TemplateMatchingIntensity alg, Class imageType) { + protected GeneralTemplateMatchTests( TemplateMatchingIntensity alg, Class imageType ) { this.alg = alg; image = GeneralizedImageOps.createSingleBand(imageType, 30, 40); @@ -60,8 +60,8 @@ protected GeneralTemplateMatchTests(TemplateMatchingIntensity alg, Class i GImageMiscOps.fillUniform(template, rand, 50, 60); } - protected GeneralTemplateMatchTests(TemplateIntensityImage.EvaluatorMethod method, Class imageType) { - this(new TemplateIntensityImage<>(method),imageType); + protected GeneralTemplateMatchTests( TemplateIntensityImage.EvaluatorMethod method, Class imageType ) { + this(new TemplateIntensityImage<>(method), imageType); } /** @@ -72,10 +72,10 @@ void border_nomask() { alg.setInputImage(image); alg.process(template); - assertEquals(template.width/2,alg.getBorderX0()); - assertEquals(template.height/2,alg.getBorderY0()); - assertEquals(template.width-template.width/2,alg.getBorderX1()); - assertEquals(template.height-template.height/2,alg.getBorderY1()); + assertEquals(template.width/2, alg.getBorderX0()); + assertEquals(template.height/2, alg.getBorderY0()); + assertEquals(template.width - template.width/2, alg.getBorderX1()); + assertEquals(template.height - template.height/2, alg.getBorderY1()); } @Test @@ -83,10 +83,10 @@ void border_Mask() { alg.setInputImage(image); alg.process(template, mask); - assertEquals(template.width/2,alg.getBorderX0()); - assertEquals(template.height/2,alg.getBorderY0()); - assertEquals(template.width-template.width/2,alg.getBorderX1()); - assertEquals(template.height-template.height/2,alg.getBorderY1()); + assertEquals(template.width/2, alg.getBorderX0()); + assertEquals(template.height/2, alg.getBorderY0()); + assertEquals(template.width - template.width/2, alg.getBorderX1()); + assertEquals(template.height - template.height/2, alg.getBorderY1()); } @Test @@ -117,10 +117,10 @@ void negativeCase_nomask() { @Test void negativeCase_Mask() { GImageMiscOps.fillUniform(image, rand, 0, 200); - GImageMiscOps.fill(mask,1); + GImageMiscOps.fill(mask, 1); alg.setInputImage(image); - alg.process(template,mask); + alg.process(template, mask); if (isPerfectZero) { // there should be no perfect matches @@ -157,25 +157,24 @@ void singleCase() { checkExpected(false, new Point2D_I32(locationX, locationY)); // uniform mask should produce identical results - GImageMiscOps.fill(mask,1); + GImageMiscOps.fill(mask, 1); alg.process(template, mask); checkExpected(false, new Point2D_I32(locationX, locationY)); } @Test void same_size_img_template() { - BoofConcurrency.USE_CONCURRENT=false; + BoofConcurrency.USE_CONCURRENT = false; GImageMiscOps.fillUniform(image, rand, 0, 200); alg.setInputImage(image); alg.process(image); checkExpected(true, new Point2D_I32(0, 0)); - T mask = this.mask.createNew(image.width,image.height); - GImageMiscOps.fill(mask,1); - alg.process(image,mask); + T mask = this.mask.createNew(image.width, image.height); + GImageMiscOps.fill(mask, 1); + alg.process(image, mask); checkExpected(true, new Point2D_I32(0, 0)); - } /** @@ -186,8 +185,9 @@ void uniformImage() { uniformImage(0); uniformImage(30); } - void uniformImage(float value) { - GImageMiscOps.fill(image,value); + + void uniformImage( float value ) { + GImageMiscOps.fill(image, value); setTemplate(5, 7); @@ -202,8 +202,8 @@ void uniformImage(float value) { } // Same test with a mask - GImageMiscOps.fill(mask,1); - alg.process(template,mask); + GImageMiscOps.fill(mask, 1); + alg.process(template, mask); intensity = alg.getIntensity(); for (int i = 0; i < intensity.totalPixels(); i++) { assertFalse(UtilEjml.isUncountable(intensity.data[i])); @@ -222,11 +222,11 @@ void zeroMask() { setTemplate(locationX, locationY); - GImageMiscOps.fill(mask,0); - GImageMiscOps.fill(alg.getIntensity(),0); + GImageMiscOps.fill(mask, 0); + GImageMiscOps.fill(alg.getIntensity(), 0); alg.setInputImage(image); alg.process(template, mask); - assertEquals(0, ImageStatistics.maxAbs(alg.getIntensity()),1e-4f); + assertEquals(0, ImageStatistics.maxAbs(alg.getIntensity()), 1e-4f); } /** @@ -247,7 +247,7 @@ void multipleCases() { checkExpected(false, a, b); // uniform mask should produce identical results - GImageMiscOps.fill(mask,1); + GImageMiscOps.fill(mask, 1); alg.process(template, mask); checkExpected(false, a, b); } @@ -259,67 +259,67 @@ void multipleCases() { void maskDifferentiate() { GImageMiscOps.fillUniform(image, rand, 0, 200); - int x=10,y=12,tw=15,th=15; + int x = 10, y = 12, tw = 15, th = 15; - T template = image.createNew(tw,th); + T template = image.createNew(tw, th); GImageMiscOps.fillUniform(template, rand, 0, 200); - GImageMiscOps.fillBorder(template,150,2); + GImageMiscOps.fillBorder(template, 150, 2); - image.subimage(x-tw/2,y-th/2,x-tw/2+tw,y-th/2+th).setTo(template); + image.subimage(x - tw/2, y - th/2, x - tw/2 + tw, y - th/2 + th).setTo(template); // ShowImages.showWindow(image,"foo",true); // BoofMiscOps.sleep(10000); - GImageMiscOps.fillBorder(template,20,2); // change the template's border so that it won't match - GImageMiscOps.fillBorder(template,50,1); // change the template's border so that it won't match + GImageMiscOps.fillBorder(template, 20, 2); // change the template's border so that it won't match + GImageMiscOps.fillBorder(template, 50, 1); // change the template's border so that it won't match alg.setInputImage(image); alg.process(template); - float valueNoMask = fractionBest(alg.isMaximize(),alg.getIntensity(),x,y); - float averageNoMask = fractionAverage(alg.isMaximize(),alg.getIntensity(),x,y); + float valueNoMask = fractionBest(alg.isMaximize(), alg.getIntensity(), x, y); + float averageNoMask = fractionAverage(alg.isMaximize(), alg.getIntensity(), x, y); - T mask = image.createNew(tw,th); + T mask = image.createNew(tw, th); double v = image.getImageType().getDataType().isInteger() ? 100 : 1; - GImageMiscOps.fill(mask,v); - GImageMiscOps.fillBorder(mask,0,2); // ignore the border + GImageMiscOps.fill(mask, v); + GImageMiscOps.fillBorder(mask, 0, 2); // ignore the border alg.setInputImage(image); - alg.process(template,mask); + alg.process(template, mask); - float valueMask = fractionBest(alg.isMaximize(),alg.getIntensity(), x, y); - float averageMask = fractionAverage(alg.isMaximize(),alg.getIntensity(), x, y); + float valueMask = fractionBest(alg.isMaximize(), alg.getIntensity(), x, y); + float averageMask = fractionAverage(alg.isMaximize(), alg.getIntensity(), x, y); // this score is designed to reduce the affect of the change in template "size" float scoreNoMask = valueNoMask/averageNoMask; float scoreMask = valueMask/averageMask; - assertTrue(valueMask >= valueNoMask ); + assertTrue(valueMask >= valueNoMask); // when masked it should be better at differentiating it from background noise - assertTrue(scoreMask*0.9 > scoreNoMask ); + assertTrue(scoreMask*0.9 > scoreNoMask); } - float fractionBest(boolean maximize, GrayF32 intensity , int x , int y ) { + float fractionBest( boolean maximize, GrayF32 intensity, int x, int y ) { float min = ImageStatistics.min(intensity); float max = ImageStatistics.max(intensity); - float value = intensity.get(x,y); + float value = intensity.get(x, y); - if( maximize ) - return (value-min)/(max-min); + if (maximize) + return (value - min)/(max - min); else - return (max-value)/(max-min); + return (max - value)/(max - min); } - float fractionAverage(boolean maximize, GrayF32 intensity , int x , int y ) { + float fractionAverage( boolean maximize, GrayF32 intensity, int x, int y ) { float min = ImageStatistics.min(intensity); float max = ImageStatistics.max(intensity); float average = (float)ImageStatistics.mean(intensity); - if( maximize ) - return (average-min)/(max-min); + if (maximize) + return (average - min)/(max - min); else - return (max-average)/(max-min); + return (max - average)/(max - min); } /** @@ -344,13 +344,13 @@ void subImage() { // uniform mask should produce identical results T subMask = BoofTesting.createSubImageOf(mask); - GImageMiscOps.fill(subMask,1); + GImageMiscOps.fill(subMask, 1); alg.setInputImage(subImage); - alg.process(subTemplate,subMask); + alg.process(subTemplate, subMask); checkExpected(false, a, b); } - private void checkExpected(boolean strict, Point2D_I32... points) { + private void checkExpected( boolean strict, Point2D_I32... points ) { // I'm being lazy, update this in the future assertFalse(alg.isBorderProcessed()); @@ -363,20 +363,20 @@ private void checkExpected(boolean strict, Point2D_I32... points) { // solutions should be local maximums NonMaxSuppression extractor; ConfigExtract config = new ConfigExtract(2, -Float.MAX_VALUE, 0, true); - if( !alg.isMaximize() ) { + if (!alg.isMaximize()) { config.detectMaximums = false; config.detectMinimums = true; } - extractor = FactoryFeatureExtractor.nonmax(config); + extractor = FactoryFeatureExtractor.nonmax(config, 1); - QueueCorner found = new QueueCorner(10); + var found = new QueueCorner(10); - GrayF32 intensity = alg.getIntensity().subimage(x0,y0,image.width-x1+1,image.height-y1+1); + GrayF32 intensity = alg.getIntensity().subimage(x0, y0, image.width - x1 + 1, image.height - y1 + 1); - if( alg.isMaximize() ) - extractor.process(intensity, null,null,null, found); + if (alg.isMaximize()) + extractor.process(intensity, null, null, null, found); else - extractor.process(intensity, null,null,found, null); + extractor.process(intensity, null, null, found, null); assertTrue(found.size >= points.length); @@ -385,18 +385,18 @@ private void checkExpected(boolean strict, Point2D_I32... points) { int numMatches = 0; for (Point2D_I16 f : found.toList()) { - if ( expected.distance(f.x,f.y) == 0.0 ) + if (expected.distance(f.x, f.y) == 0.0) numMatches++; } assertEquals(1, numMatches); } - if( strict ) - assertEquals(points.length,found.size); + if (strict) + assertEquals(points.length, found.size); } - private void setTemplate(int x, int y) { + private void setTemplate( int x, int y ) { image.subimage(x, y, x + template.width, y + template.height, null).setTo(template); } } diff --git a/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoMotion2D_to_MonocularPlaneVisualOdometry.java b/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoMotion2D_to_MonocularPlaneVisualOdometry.java index 70cbfaf2dc..182cba61e9 100644 --- a/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoMotion2D_to_MonocularPlaneVisualOdometry.java +++ b/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoMotion2D_to_MonocularPlaneVisualOdometry.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -28,7 +28,7 @@ public class TestMonoMotion2D_to_MonocularPlaneVisualOdometry extends CheckVisualOdometryMonoPlaneSim { public TestMonoMotion2D_to_MonocularPlaneVisualOdometry() { - super(GrayU8.class, -20, 0.04); // angle selected to include ground points and points far away + super(GrayU8.class, -20, 0.05); // angle selected to include ground points and points far away setAlgorithm(createAlgorithm()); } diff --git a/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoOverhead_to_MonocularPlaneVisualOdometry.java b/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoOverhead_to_MonocularPlaneVisualOdometry.java index 6c6c54ca3e..63a630b88c 100644 --- a/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoOverhead_to_MonocularPlaneVisualOdometry.java +++ b/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestMonoOverhead_to_MonocularPlaneVisualOdometry.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -21,6 +21,7 @@ import boofcv.abst.feature.detect.interest.ConfigPointDetector; import boofcv.abst.feature.detect.interest.PointDetectorTypes; import boofcv.abst.tracker.PointTracker; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.factory.sfm.FactoryVisualOdometry; import boofcv.factory.tracker.FactoryPointTracker; @@ -48,7 +49,7 @@ protected MonocularPlaneVisualOdometry createAlgorithm() { configDetector.general.radius = 3; configDetector.general.threshold = 1; - PointTracker tracker = FactoryPointTracker.klt(config, configDetector, + PointTracker tracker = FactoryPointTracker.klt(config, DerivativeType.SOBEL, configDetector, GrayU8.class, GrayS16.class); double cellSize = 0.015; diff --git a/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestWrapVisOdomStereoMonoPnP.java b/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestWrapVisOdomStereoMonoPnP.java index 536cebc5e9..3307cc5709 100644 --- a/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestWrapVisOdomStereoMonoPnP.java +++ b/main/boofcv-sfm/src/test/java/boofcv/abst/sfm/d3/TestWrapVisOdomStereoMonoPnP.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -21,6 +21,7 @@ import boofcv.abst.disparity.StereoDisparitySparse; import boofcv.abst.feature.detect.interest.ConfigPointDetector; import boofcv.abst.tracker.PointTracker; +import boofcv.alg.filter.derivative.DerivativeType; import boofcv.alg.tracker.klt.ConfigPKlt; import boofcv.factory.disparity.ConfigDisparityBM; import boofcv.factory.disparity.FactoryStereoDisparity; @@ -72,14 +73,14 @@ public StereoVisualOdometry createAlgorithm() { configKLT.pyramidLevels = ConfigDiscreteLevels.levels(4); configKLT.templateRadius = 3; - ConfigPointDetector configDetector = new ConfigPointDetector(); + var configDetector = new ConfigPointDetector(); configDetector.general.maxFeatures = 600; configDetector.general.radius = 3; configDetector.general.threshold = 1; configDetector.shiTomasi.radius = 3; StereoDisparitySparse disparity = FactoryStereoDisparity.sparseRectifiedBM(configBM, GrayF32.class); - PointTracker tracker = FactoryPointTracker.klt(configKLT, configDetector, + PointTracker tracker = FactoryPointTracker.klt(configKLT, DerivativeType.SOBEL, configDetector, GrayF32.class, GrayF32.class); return FactoryVisualOdometry.stereoMonoPnP(configPnP, disparity, tracker, GrayF32.class); diff --git a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_F64.java b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_F64.java index dc1823ec40..932d010870 100644 --- a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_F64.java +++ b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_F64.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,6 +18,9 @@ package boofcv.struct.convolve; +import org.ejml.MatrixFormattable; +import org.ejml.MatrixPrintFormat; + /** * Floating point 1D convolution kernel that extends {@link Kernel1D}. * @@ -28,7 +31,7 @@ * @author Peter Abeles */ @SuppressWarnings({"NullAway.Init"}) -public class Kernel1D_F64 extends Kernel1D { +public class Kernel1D_F64 extends Kernel1D implements MatrixFormattable { public double[] data; @@ -36,21 +39,21 @@ public class Kernel1D_F64 extends Kernel1D { * Creates a new kernel whose initial values are specified by "data" and length is "width". * The offset will be set to width/2 * + * @param width The kernel's width. * @param data The value of the kernel. Not modified. Reference is not saved. - * @param width The kernels width. */ - public Kernel1D_F64( double[] data, int width ) { - this(data, width, width/2); + public Kernel1D_F64( int width, double[] data ) { + this(width, width/2, data); } /** * Creates a kernel with elements equal to 'data' and with the specified 'width' plus 'offset' * - * @param data The value of the kernel. Not modified. Reference is not saved. - * @param width The kernels width. + * @param width The kernel's width. * @param offset Location of the origin in the array + * @param data The value of the kernel. Not modified. Reference is not saved. */ - public Kernel1D_F64( double[] data, int width, int offset ) { + public Kernel1D_F64( int width, int offset, double[] data ) { super(width, offset); this.data = new double[width]; @@ -80,13 +83,11 @@ public Kernel1D_F64( int width, int offset ) { protected Kernel1D_F64() {} - @Override - public /**/double /**/getDouble( int index ) { + @Override public /**/double /**/getDouble( int index ) { return data[index]; } - @Override - public void setD( int index, /**/double value ) { + @Override public void setD( int index, /**/double value ) { data[index] = (double) value; } @@ -100,26 +101,29 @@ public void setD( int index, /**/double value ) { * @return A new kernel. */ public static Kernel1D_F64 wrap( double[] data, int width, int offset ) { - Kernel1D_F64 ret = new Kernel1D_F64(); + var ret = new Kernel1D_F64(); ret.data = data; ret.width = width; ret.offset = offset; - return ret; } - @Override - public Kernel1D_F64 copy() { - Kernel1D_F64 ret = new Kernel1D_F64(width, offset); + @Override public Kernel1D_F64 copy() { + var ret = new Kernel1D_F64(width, offset); System.arraycopy(data, 0, ret.data, 0, ret.width); return ret; } - @Override - public boolean isInteger() { + @Override public boolean isInteger() { return false; } + @Override public String format( MatrixPrintFormat format ) { + var builder = new StringBuilder(); + format.row(builder, width, ( col ) -> data[col]); + return builder.toString(); + } + public double get( int i ) { return data[i]; } @@ -143,4 +147,8 @@ public void print() { } System.out.println(); } + + @Override public String toString() { + return getClass().getSimpleName() + " width=" + width + " offset=" + offset + "\n" + format(); + } } diff --git a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_S32.java b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_S32.java index d193cb7e7f..b4c6266cdf 100644 --- a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_S32.java +++ b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel1D_S32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,6 +18,9 @@ package boofcv.struct.convolve; +import org.ejml.MatrixFormattable; +import org.ejml.MatrixPrintFormat; + /** * Floating point 1D convolution kernel that extends {@link Kernel1D}. * @@ -28,7 +31,7 @@ * @author Peter Abeles */ @SuppressWarnings({"NullAway.Init"}) -public class Kernel1D_S32 extends Kernel1D { +public class Kernel1D_S32 extends Kernel1D implements MatrixFormattable { public int[] data; @@ -36,21 +39,21 @@ public class Kernel1D_S32 extends Kernel1D { * Creates a new kernel whose initial values are specified by "data" and length is "width". * The offset will be set to width/2 * + * @param width The kernel's width. * @param data The value of the kernel. Not modified. Reference is not saved. - * @param width The kernels width. */ - public Kernel1D_S32( int[] data, int width ) { - this(data, width, width/2); + public Kernel1D_S32( int width, int[] data ) { + this(width, width/2, data); } /** * Creates a kernel with elements equal to 'data' and with the specified 'width' plus 'offset' * - * @param data The value of the kernel. Not modified. Reference is not saved. - * @param width The kernels width. + * @param width The kernel's width. * @param offset Location of the origin in the array + * @param data The value of the kernel. Not modified. Reference is not saved. */ - public Kernel1D_S32( int[] data, int width, int offset ) { + public Kernel1D_S32( int width, int offset, int[] data ) { super(width, offset); this.data = new int[width]; @@ -133,14 +136,59 @@ public int computeSum() { return sum; } + @Override public String format( MatrixPrintFormat format ) { + var builder = new StringBuilder(); + format.row(builder, width, ( col ) -> data[col]); + return builder.toString(); + } + public int[] getData() { return data; } + /// How deep is the zero border around this kernel + public int countZeroBorder() { + for (int depth = 0; depth <= width/2; depth++) { + if (get(depth) != 0) + return depth; + if (get(width - depth - 1) != 0) + return depth; + } + // everything is zeros + return width/2 + 1; + } + + /// Removes the border up to the specified depth + public Kernel1D_S32 trimBorder( int border ) { + if (border <= 0) + return this.copy(); + + var out = new Kernel1D_S32(width - 2*border); + out.offset = this.offset - border; + for (int i = 0; i < out.width; i++) { + out.data[i] = this.data[i + border]; + } + return out; + } + public void print() { for (int i = 0; i < width; i++) { System.out.printf("%6d ", data[i]); } System.out.println(); } + + @Override public String toString() { + return getClass().getSimpleName() + " width=" + width + " offset=" + offset + "\n" + format(); + } + + public boolean isIdentical( Kernel1D_S32 a ) { + if (a.width != width || a.offset != offset) + return false; + for (int i = 0; i < a.width; i++) { + if (a.data[i] != data[i]) + return false; + } + return true; + } } diff --git a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_F64.java b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_F64.java index 4dd1e3d91a..ac6c31350b 100644 --- a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_F64.java +++ b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_F64.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,6 +18,9 @@ package boofcv.struct.convolve; +import org.ejml.MatrixFormattable; +import org.ejml.MatrixPrintFormat; + /** * This is a kernel in a 2D convolution. The convolution is performed by * convolving this kernel across a 2D array/image. The kernel is square and has @@ -31,7 +34,7 @@ * @author Peter Abeles */ @SuppressWarnings({"NullAway.Init"}) -public class Kernel2D_F64 extends Kernel2D { +public class Kernel2D_F64 extends Kernel2D implements MatrixFormattable { public double[] data; @@ -132,6 +135,20 @@ public void print() { System.out.println(); } + @Override public String format( MatrixPrintFormat format ) { + var builder = new StringBuilder(); + builder.append(format.getPrefix()); + for (int row = 0; row < width; row++) { + format.rowPadding(row == 0, builder); + int _row = row; + format.row(builder, width, ( col ) -> data[_row*width + col]); + if (row < width - 1) + builder.append(format.getRowSeparator()); + } + builder.append(format.getSuffix()); + return builder.toString(); + } + @Override public Kernel2D_F64 copy() { Kernel2D_F64 ret = new Kernel2D_F64(width); @@ -144,4 +161,8 @@ public Kernel2D_F64 copy() { public /**/double /**/getDouble( int x, int y ) { return get(x, y); } + + @Override public String toString() { + return getClass().getSimpleName() + " width=" + width + " offset=" + offset + "\n" + format(); + } } diff --git a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_S32.java b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_S32.java index 5bcf9d7325..8be2846f4e 100644 --- a/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_S32.java +++ b/main/boofcv-types/src/main/java/boofcv/struct/convolve/Kernel2D_S32.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * @@ -18,6 +18,9 @@ package boofcv.struct.convolve; +import org.ejml.MatrixFormattable; +import org.ejml.MatrixPrintFormat; + /** * This is a kernel in a 2D convolution. The convolution is performed by * convolving this kernel across a 2D array/image. The kernel is square and has @@ -31,7 +34,7 @@ * @author Peter Abeles */ @SuppressWarnings({"NullAway.Init"}) -public class Kernel2D_S32 extends Kernel2D { +public class Kernel2D_S32 extends Kernel2D implements MatrixFormattable { public int[] data; @@ -132,6 +135,65 @@ public void print() { System.out.println(); } + /// How deep is the zero border around this kernel + public int countZeroBorder() { + for (int depth = 0; depth <= width/2; depth++) { + for (int i = depth; i < width - depth; i++) { + if (get(i, depth) != 0) + return depth; + if (get(i, width - depth - 1) != 0) + return depth; + if (get(depth, i) != 0) + return depth; + if (get(width - depth - 1, i) != 0) + return depth; + } + } + // everything is zeros + return width/2 + 1; + } + + /// Removes the border up to the specified depth + public Kernel2D_S32 trimBorder( int border ) { + if (border <= 0) + return this.copy(); + + var out = new Kernel2D_S32(width - 2*border); + out.offset = this.offset - border; + for (int i = 0; i < out.width; i++) { + for (int j = 0; j < out.width; j++) { + out.set(j, i, this.get(j + border, i + border)); + } + } + return out; + } + + @Override public String format( MatrixPrintFormat format ) { + var builder = new StringBuilder(); + builder.append(format.getPrefix()); + for (int row = 0; row < width; row++) { + format.rowPadding(row == 0, builder); + int _row = row; + format.row(builder, width, ( col ) -> data[_row*width + col]); + if (row < width - 1) + builder.append(format.getRowSeparator()); + } + builder.append(format.getSuffix()); + return builder.toString(); + } + + public boolean isIdentical( Kernel2D_S32 a ) { + if (width != a.width || offset != a.offset) + return false; + final int N = width*width; + + for (int i = 0; i < N; i++) { + if (data[i] != a.data[i]) + return false; + } + return true; + } + @Override public Kernel2D_S32 copy() { Kernel2D_S32 ret = new Kernel2D_S32(width); @@ -144,4 +206,8 @@ public Kernel2D_S32 copy() { public double getDouble( int x, int y ) { return get(x, y); } + + @Override public String toString() { + return getClass().getSimpleName() + " width=" + width + " offset=" + offset + "\n" + format(); + } } diff --git a/main/boofcv-types/src/main/java/boofcv/struct/convolve/KernelBase.java b/main/boofcv-types/src/main/java/boofcv/struct/convolve/KernelBase.java index 34cf866ec5..d5c900cb88 100644 --- a/main/boofcv-types/src/main/java/boofcv/struct/convolve/KernelBase.java +++ b/main/boofcv-types/src/main/java/boofcv/struct/convolve/KernelBase.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Peter Abeles. All Rights Reserved. + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. * * This file is part of BoofCV (http://boofcv.org). * diff --git a/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel1D_F64.java b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel1D_F64.java new file mode 100644 index 0000000000..8ee139d252 --- /dev/null +++ b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel1D_F64.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.struct.convolve; + +import boofcv.testing.BoofStandardJUnit; +import org.ejml.MatrixPrintFormat; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestKernel1D_F64 extends BoofStandardJUnit { + @Test void format() { + var format = new MatrixPrintFormat(1, ", ", ",\n", "{", "}", "{", "}"); + + var a = new Kernel1D_F64(3, new double[]{-7, 800.1234, 9}); + + // By default, it will align columns + assertEquals("{-7 , 800.1234, 9 }", a.format(format)); + + format.setAligned(false); + String found = a.format(format); + assertEquals("{-7, 800.1, 9}", found); + } +} diff --git a/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel1D_S32.java b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel1D_S32.java new file mode 100644 index 0000000000..3d7c3b56e2 --- /dev/null +++ b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel1D_S32.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.struct.convolve; + +import boofcv.testing.BoofStandardJUnit; +import org.ejml.MatrixPrintFormat; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestKernel1D_S32 extends BoofStandardJUnit { + @Test void format() { + var format = new MatrixPrintFormat(0, ", ", ",\n", "{", "}", "{", "}"); + + var a = new Kernel1D_S32(3, new int[]{-7, 800, 9}); + + // By default, it will align columns + assertEquals("{-7 , 800 , 9 }", a.format(format)); + + format.setAligned(false); + String found = a.format(format); + assertEquals("{-7, 800, 9}", found); + } +} diff --git a/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel2D_F64.java b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel2D_F64.java new file mode 100644 index 0000000000..514fca2f4e --- /dev/null +++ b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel2D_F64.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.struct.convolve; + +import boofcv.testing.BoofStandardJUnit; +import org.ejml.MatrixPrintFormat; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestKernel2D_F64 extends BoofStandardJUnit { + @Test void format() { + var format = new MatrixPrintFormat(1, ", ", ",\n", "{", "}", "{", "}"); + + var a = new Kernel2D_F64(3, new double[]{1, 2, -3, 4, -5, 6, -7, 800.1234, 9}); + + // By default, it will align columns + assertEquals("{{1 , 2 , -3 },\n {4 , -5 , 6 },\n {-7 , 800.1234, 9 }}", a.format(format)); + + format.setAligned(false); + String found = a.format(format); + assertEquals("{{1, 2, -3},\n{4, -5, 6},\n{-7, 800.1, 9}}", found); + } +} diff --git a/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel2D_S32.java b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel2D_S32.java new file mode 100644 index 0000000000..06d8511aee --- /dev/null +++ b/main/boofcv-types/src/test/java/boofcv/struct/convolve/TestKernel2D_S32.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2026, Peter Abeles. All Rights Reserved. + * + * This file is part of BoofCV (http://boofcv.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package boofcv.struct.convolve; + +import boofcv.testing.BoofStandardJUnit; +import org.ejml.MatrixPrintFormat; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestKernel2D_S32 extends BoofStandardJUnit { + @Test void format() { + var format = new MatrixPrintFormat(0, ", ", ",\n", "{", "}", "{", "}"); + + var a = new Kernel2D_S32(3, new int[]{1, 2, -3, 4, -5, 6, -7, 800, 9}); + + // By default, it will align columns + assertEquals("{{1 , 2 , -3 },\n {4 , -5 , 6 },\n {-7 , 800 , 9 }}", a.format(format)); + + format.setAligned(false); + String found = a.format(format); + assertEquals("{{1, 2, -3},\n{4, -5, 6},\n{-7, 800, 9}}", found); + } + + @Test void countZeroBorder() { + var c0 = new Kernel2D_S32(3); + assertEquals(2, c0.countZeroBorder()); + c0.set(0, 0, 1); + assertEquals(0, c0.countZeroBorder()); + c0.set(0, 0, 0); + c0.set(2, 2, 1); + assertEquals(0, c0.countZeroBorder()); + c0.set(2, 2, 0); + c0.set(1, 1, 1); + assertEquals(1, c0.countZeroBorder()); + c0.set(1, 1, 0); + c0.set(2, 2, 1); + assertEquals(0, c0.countZeroBorder()); + + var c1 = new Kernel2D_S32(5); + assertEquals(3, c1.countZeroBorder()); + c1.set(0, 1, 1); + assertEquals(0, c1.countZeroBorder()); + c1.set(0, 1, 0); + c1.set(1, 0, 1); + assertEquals(0, c1.countZeroBorder()); + c1.set(1, 0, 0); + c1.set(2, 2, 1); + assertEquals(2, c1.countZeroBorder()); + } + + @Test void trimBorder() { + var in = new Kernel2D_S32(5); + for (int i = 0; i < in.width; i++) { + in.set(i, i, i + 1); + } + + Kernel2D_S32 found = in.trimBorder(1); + assertEquals(3, found.width); + for (int i = 0; i < found.width; i++) { + assertEquals(i + 2, found.get(i, i)); + } + } +}