From a5a21d244d83041cf38212cc38d4f8025967c5b1 Mon Sep 17 00:00:00 2001 From: dlegland Date: Sun, 29 Sep 2024 16:05:51 +0200 Subject: [PATCH] measure/region2d: fix computation of BinaryConfigurationsHistogram2D for a selection of labels within the input image --- .../BinaryConfigurationsHistogram2D.java | 7 ++- .../BinaryConfigurationsHistogram2DTest.java | 60 ++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/main/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2D.java b/src/main/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2D.java index f9e626ada..fd0d1e89 100644 --- a/src/main/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2D.java +++ b/src/main/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2D.java @@ -330,8 +330,11 @@ public int[][] process(ImageProcessor labelImage, int[] labels) localLabels.clear(); for (int label : configValues) { - if (label == 0) - continue; + // do not consider background + if (label == 0) continue; + // do not consider labels not in the requested list + if (!labelIndices.containsKey(label)) continue; + // keep only one instance of each label if (!localLabels.contains(label)) localLabels.add(label); diff --git a/src/test/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2DTest.java b/src/test/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2DTest.java index f6cb3941..fc81d21c 100644 --- a/src/test/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2DTest.java +++ b/src/test/java/inra/ijpb/measure/region2d/BinaryConfigurationsHistogram2DTest.java @@ -24,11 +24,13 @@ */ package inra.ijpb.measure.region2d; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.junit.Test; import ij.process.ByteProcessor; +import ij.process.ImageProcessor; +import inra.ijpb.label.LabelImages; /** * @author dlegland @@ -36,6 +38,39 @@ */ public class BinaryConfigurationsHistogram2DTest { + + /** + * Test method for {@link inra.ijpb.measure.region2d.BinaryConfigurationsHistogram2D#process(ij.process.ImageProcessor, int[])}. + */ + @Test + public final void test_processImageProcessor_allLabels() + { + ImageProcessor image = createFourRegionsLabelMap(); + int[] labels = LabelImages.findAllLabels(image); + + int[][] histo = new BinaryConfigurationsHistogram2D().process(image, labels); + + // check size of histograms + assertEquals(histo.length, labels.length); + assertEquals(histo[0].length, 16); + } + + /** + * Test method for {@link inra.ijpb.measure.region2d.BinaryConfigurationsHistogram2D#process(ij.process.ImageProcessor, int[])}. + */ + @Test + public final void test_processImageProcessor_selectedLabels() + { + ImageProcessor image = createFourRegionsLabelMap(); + int[] labels = new int[] {5, 8, 9}; + + int[][] histo = new BinaryConfigurationsHistogram2D().process(image, labels); + + // check size of histograms + assertEquals(histo.length, labels.length); + assertEquals(histo[0].length, 16); + } + /** * Test method for {@link inra.ijpb.measure.region2d.BinaryConfigurationsHistogram2D#processInnerFrame(ij.process.ImageProcessor)}. */ @@ -93,4 +128,27 @@ public final void testProcessInnerFrame_OhserMuecklich() } } + /** + * Creates a label map containing four regions. + * + * @return a label map containing four regions. + */ + private static final ImageProcessor createFourRegionsLabelMap() + { + ImageProcessor array = new ByteProcessor(8, 8); + array.set(1, 1, 3); + for (int i = 3; i < 7; i++) + { + array.set(i, 1, 5); + array.set(1, i, 8); + } + for (int i = 3; i < 7; i++) + { + for (int j = 3; j < 7; j++) + { + array.set(i, j, 9); + } + } + return array; + } }