ImagePlusUtil { public static boolean isBinaryColour(final ImagePlus image) { return Arrays.stream(image.getStatistics().histogram).filter(p -> p > 0) .count() <= 2; } private  ImagePlusUtil(); static double anisotropy(final ImagePlus image); static ImagePlus cleanDuplicate(final ImagePlus image); static boolean is3D(final ImagePlus image); static boolean isBinaryColour(final ImagePlus image);  }
@Test public void testIsBinaryColour() { final ImagePlus testImage = mock(ImagePlus.class); final ImageStatistics binaryStats = new ImageStatistics(); binaryStats.pixelCount = 2; binaryStats.histogram = new int[256]; binaryStats.histogram[0x00] = 1; binaryStats.histogram[0xFF] = 1; when(testImage.getStatistics()).thenReturn(binaryStats); final boolean result = ImagePlusUtil.isBinaryColour(testImage); assertTrue("Image with two colors (black & white) should be binary", result); }
@Test(expected = NullPointerException.class) public void testIsBinaryColourThrowsNPEIfImageNull() { ImagePlusUtil.isBinaryColour(null); }
@Test public void testIsBinaryColourMonochrome() { final ImagePlus testImage = mock(ImagePlus.class); final ImageStatistics binaryStats = new ImageStatistics(); binaryStats.pixelCount = 1; binaryStats.histogram = new int[256]; binaryStats.histogram[0xFF] = 1; when(testImage.getStatistics()).thenReturn(binaryStats); final boolean result = ImagePlusUtil.isBinaryColour(testImage); assertTrue("A monochrome image should be binary", result); }
@Test public void testIsBinaryColourMulticolor() { final ImagePlus testImage = mock(ImagePlus.class); final ImageStatistics binaryStats = new ImageStatistics(); binaryStats.pixelCount = 3; binaryStats.histogram = new int[256]; binaryStats.histogram[0x00] = 1; binaryStats.histogram[0x01] = 1; binaryStats.histogram[0xFF] = 1; when(testImage.getStatistics()).thenReturn(binaryStats); final boolean result = ImagePlusUtil.isBinaryColour(testImage); assertFalse("A multicolor image should be binary", result); }