public static void assertEqualsRelative(ImageBase imgA, ImageBase imgB, double tolFrac) { // if no specialized check exists, use a slower generalized approach if (imgA instanceof ImageSingleBand) { GImageSingleBand a = FactoryGImageSingleBand.wrap((ImageSingleBand) imgA); GImageSingleBand b = FactoryGImageSingleBand.wrap((ImageSingleBand) imgB); for (int y = 0; y < imgA.height; y++) { for (int x = 0; x < imgA.width; x++) { double valA = a.get(x, y).doubleValue(); double valB = b.get(x, y).doubleValue(); double difference = valA - valB; double max = Math.max(Math.abs(valA), Math.abs(valB)); if (max == 0) max = 1; if (Math.abs(difference) / max > tolFrac) throw new RuntimeException( "Values not equal at (" + x + "," + y + ") " + valA + " " + valB); } } } else if (imgA instanceof MultiSpectral) { MultiSpectral a = (MultiSpectral) imgA; MultiSpectral b = (MultiSpectral) imgB; if (a.getNumBands() != b.getNumBands()) throw new RuntimeException("Number of bands not equal"); for (int band = 0; band < a.getNumBands(); band++) { assertEqualsRelative(a.getBand(band), b.getBand(band), tolFrac); } } else { throw new RuntimeException("Unknown image type"); } }
private static void compareValues( double tol, GImageSingleBand a, GImageSingleBand b, int x, int y) { double normalizer = Math.abs(a.get(x, y).doubleValue()) + Math.abs(b.get(x, y).doubleValue()); if (normalizer < 1.0) normalizer = 1.0; if (Math.abs(a.get(x, y).doubleValue() - b.get(x, y).doubleValue()) / normalizer > tol) throw new RuntimeException( "values not equal at (" + x + " " + y + ") " + a.get(x, y) + " " + b.get(x, y)); }
public static void assertEquals(float a[], float b[], float tol) { for (int i = 0; i < a.length; i++) { double diff = Math.abs(a[i] - b[i]); if (diff > tol) throw new RuntimeException("Element " + i + " not equals. " + a[i] + " " + b[i]); } }
public static void assertEquals(double a[], int b[]) { for (int i = 0; i < a.length; i++) { double diff = Math.abs((int) a[i] - b[i]); if (diff != 0) throw new RuntimeException("Element " + i + " not equals. " + a[i] + " " + b[i]); } }
private static BufferedImage grayMagnitudeTemp( ImageInteger src, BufferedImage dst, int maxValue) { int halfValue = maxValue / 2 + maxValue % 2; for (int y = 0; y < src.height; y++) { for (int x = 0; x < src.width; x++) { int v = Math.abs(src.get(x, y)); int r, b; if (v >= halfValue) { r = 255 * (v - halfValue) / halfValue; b = 0; } else { r = 0; b = 255 * v / halfValue; } if (v == 0) { r = b = 0; } else { r = 255 * v / maxValue; b = 255 * (maxValue - v) / maxValue; } dst.setRGB(x, y, r << 16 | b); } } return dst; }
/** * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8 * * @param imgA BufferedImage * @param imgB ImageUInt8 */ public static void checkEquals(BufferedImage imgA, ImageInt16 imgB) { if (imgA.getRaster() instanceof ByteInterleavedRaster && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) { ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster(); if (raster.getNumBands() == 1) { int strideA = raster.getScanlineStride(); int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1; // handle a special case where the RGB conversion is screwed for (int i = 0; i < imgA.getHeight(); i++) { for (int j = 0; j < imgA.getWidth(); j++) { int valB = imgB.get(j, i); int valA = raster.getDataStorage()[offsetA + i * strideA + j]; if (!imgB.getTypeInfo().isSigned()) valA &= 0xFFFF; if (valA != valB) throw new RuntimeException("Images are not equal: " + valA + " " + valB); } } return; } } else if (imgA.getRaster() instanceof ShortInterleavedRaster) { ShortInterleavedRaster raster = (ShortInterleavedRaster) imgA.getRaster(); if (raster.getNumBands() == 1) { int strideA = raster.getScanlineStride(); int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1; // handle a special case where the RGB conversion is screwed for (int i = 0; i < imgA.getHeight(); i++) { for (int j = 0; j < imgA.getWidth(); j++) { int valB = imgB.get(j, i); int valA = raster.getDataStorage()[offsetA + i * strideA + j]; if (!imgB.getTypeInfo().isSigned()) valA &= 0xFFFF; if (valA != valB) throw new RuntimeException("Images are not equal: " + valA + " " + valB); } } } } else { for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { int rgb = imgA.getRGB(x, y); int gray = ((((rgb >>> 16) & 0xFF) + ((rgb >>> 8) & 0xFF) + (rgb & 0xFF)) / 3); int grayB = imgB.get(x, y); if (!imgB.getTypeInfo().isSigned()) gray &= 0xFFFF; if (Math.abs(gray - grayB) != 0) { throw new RuntimeException( "images are not equal: (" + x + " , " + y + ") A = " + gray + " B = " + grayB); } } } } }
private static BufferedImage grayMagnitude(ImageInteger src, BufferedImage dst, int maxValue) { for (int y = 0; y < src.height; y++) { for (int x = 0; x < src.width; x++) { int v = Math.abs(src.get(x, y)); int rgb = 255 * v / maxValue; dst.setRGB(x, y, rgb << 16 | rgb << 8 | rgb); } } return dst; }
/** * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8 * * @param imgA BufferedImage * @param imgB ImageUInt8 */ public static void checkEquals(BufferedImage imgA, ImageFloat32 imgB, float tol) { if (imgA.getRaster() instanceof ByteInterleavedRaster && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) { ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster(); if (raster.getNumBands() == 1) { int strideA = raster.getScanlineStride(); int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1; // handle a special case where the RGB conversion is screwed for (int i = 0; i < imgA.getHeight(); i++) { for (int j = 0; j < imgA.getWidth(); j++) { float valB = imgB.get(j, i); int valA = raster.getDataStorage()[offsetA + i * strideA + j]; valA &= 0xFF; if (Math.abs(valA - valB) > tol) throw new RuntimeException("Images are not equal: A = " + valA + " B = " + valB); } } return; } } for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { int rgb = imgA.getRGB(x, y); float gray = (((rgb >>> 16) & 0xFF) + ((rgb >>> 8) & 0xFF) + (rgb & 0xFF)) / 3.0f; float grayB = imgB.get(x, y); if (Math.abs(gray - grayB) > tol) { throw new RuntimeException("images are not equal: A = " + gray + " B = " + grayB); } } } }
public static void assertEqualsInner( ImageBase imgA, ImageBase imgB, double tol, int borderX, int borderY, boolean relative) { // if no specialized check exists, use a slower generalized approach if (imgA instanceof ImageSingleBand) { GImageSingleBand a = FactoryGImageSingleBand.wrap((ImageSingleBand) imgA); GImageSingleBand b = FactoryGImageSingleBand.wrap((ImageSingleBand) imgB); for (int y = borderY; y < imgA.height - borderY; y++) { for (int x = borderX; x < imgA.width - borderX; x++) { double valA = a.get(x, y).doubleValue(); double valB = b.get(x, y).doubleValue(); double error = Math.abs(valA - valB); if (relative) { double denominator = Math.abs(valA) + Math.abs(valB); if (denominator == 0) denominator = 1; error /= denominator; } if (error > tol) throw new RuntimeException( "Values not equal at (" + x + "," + y + ") " + valA + " " + valB); } } } else if (imgA instanceof MultiSpectral) { MultiSpectral a = (MultiSpectral) imgA; MultiSpectral b = (MultiSpectral) imgB; if (a.getNumBands() != b.getNumBands()) throw new RuntimeException("Number of bands not equal"); for (int band = 0; band < a.getNumBands(); band++) { assertEqualsInner(a.getBand(band), b.getBand(band), tol, borderX, borderY, relative); } } else { throw new RuntimeException("Unknown image type"); } }
public static void printDiff(ImageSingleBand imgA, ImageSingleBand imgB) { GImageSingleBand a = FactoryGImageSingleBand.wrap(imgA); GImageSingleBand b = FactoryGImageSingleBand.wrap(imgB); System.out.println("------- Difference -----------"); for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { double diff = Math.abs(a.get(x, y).doubleValue() - b.get(x, y).doubleValue()); System.out.printf("%2d ", (int) diff); } System.out.println(); } }
private static BufferedImage grayMagnitude( ImageFloat32 src, BufferedImage dst, float maxAbsValue) { for (int y = 0; y < src.height; y++) { for (int x = 0; x < src.width; x++) { float v = Math.abs(src.get(x, y)); int rgb = (int) (255 * v / maxAbsValue); dst.setRGB(x, y, rgb << 16 | rgb << 8 | rgb); } } return dst; }
/** * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8 * * @param imgA BufferedImage * @param imgB ImageUInt8 */ public static void checkEquals(BufferedImage imgA, InterleavedU8 imgB) { if (imgA.getRaster() instanceof ByteInterleavedRaster) { ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster(); if (raster.getNumBands() == 1) { // handle a special case where the RGB conversion is screwed for (int i = 0; i < imgA.getHeight(); i++) { for (int j = 0; j < imgA.getWidth(); j++) { int valB = imgB.getBand(j, i, 0); int valA = raster.getDataStorage()[i * imgA.getWidth() + j] & 0xFF; if (valA != valB) throw new RuntimeException("Images are not equal"); } } return; } } for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { int rgb = imgA.getRGB(x, y); int r = (rgb >>> 16) & 0xFF; int g = (rgb >>> 8) & 0xFF; int b = rgb & 0xFF; if (Math.abs(b - imgB.getBand(x, y, 0) & 0xFF) != 0) throw new RuntimeException("images are not equal: "); if (Math.abs(g - imgB.getBand(x, y, 1) & 0xFF) != 0) throw new RuntimeException("images are not equal: "); if (Math.abs(r - imgB.getBand(x, y, 2) & 0xFF) != 0) throw new RuntimeException("images are not equal: "); } } }
public static void checkEquals( BufferedImage imgA, MultiSpectral imgB, boolean boofcvBandOrder, float tol) { if (imgA.getRaster() instanceof ByteInterleavedRaster && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) { ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster(); if (raster.getNumBands() == 1) { GImageSingleBand band = FactoryGImageSingleBand.wrap(imgB.getBand(0)); int strideA = raster.getScanlineStride(); int offsetA = raster.getDataOffset(0) - raster.getPixelStride() + 1; // handle a special case where the RGB conversion is screwed for (int i = 0; i < imgA.getHeight(); i++) { for (int j = 0; j < imgA.getWidth(); j++) { double valB = band.get(j, i).doubleValue(); int valA = raster.getDataStorage()[offsetA + i * strideA + j]; valA &= 0xFF; if (Math.abs(valA - valB) > tol) throw new RuntimeException("Images are not equal: A = " + valA + " B = " + valB); } } return; } } int bandOrder[]; if (boofcvBandOrder) { if (imgB.getNumBands() == 4) { bandOrder = new int[] {1, 2, 3, 0}; } else { bandOrder = new int[] {0, 1, 2}; } } else { if (imgA.getType() == BufferedImage.TYPE_INT_RGB) { bandOrder = new int[] {0, 1, 2}; } else if (imgA.getType() == BufferedImage.TYPE_INT_BGR || imgA.getType() == BufferedImage.TYPE_3BYTE_BGR) { bandOrder = new int[] {2, 1, 0}; } else if (imgA.getType() == BufferedImage.TYPE_4BYTE_ABGR) { bandOrder = new int[] {0, 3, 2, 1}; } else if (imgA.getType() == BufferedImage.TYPE_INT_ARGB) { bandOrder = new int[] {0, 1, 2, 3}; } else { bandOrder = new int[] {0, 1, 2}; } } int expected[] = new int[4]; for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { // getRGB() automatically converts the band order to ARGB int rgb = imgA.getRGB(x, y); expected[0] = ((rgb >>> 24) & 0xFF); // alpha expected[1] = ((rgb >>> 16) & 0xFF); // red expected[2] = ((rgb >>> 8) & 0xFF); // green expected[3] = (rgb & 0xFF); // blue if (imgB.getNumBands() == 4) { for (int i = 0; i < 4; i++) { double found = GeneralizedImageOps.get(imgB.getBand(bandOrder[i]), x, y); if (Math.abs(Math.exp(expected[i] - found)) > tol) { for (int j = 0; j < 4; j++) { System.out.println( expected[j] + " " + GeneralizedImageOps.get(imgB.getBand(bandOrder[j]), x, y)); } throw new RuntimeException( "Images are not equal: band - " + i + " type " + imgA.getType()); } } } else if (imgB.getNumBands() == 3) { for (int i = 0; i < 3; i++) { double found = GeneralizedImageOps.get(imgB.getBand(bandOrder[i]), x, y); if (Math.abs(expected[i + 1] - found) > tol) { for (int j = 0; j < 3; j++) { System.out.println( expected[j + 1] + " " + GeneralizedImageOps.get(imgB.getBand(bandOrder[j]), x, y)); } throw new RuntimeException( "Images are not equal: band - " + i + " type " + imgA.getType()); } } } else { throw new RuntimeException("Unexpectd number of bands"); } } } }
public static void checkEquals(WritableRaster imgA, MultiSpectral imgB, float tol) { if (imgA.getNumBands() != imgB.getNumBands()) { throw new RuntimeException("Number of bands not equals"); } if (imgA instanceof ByteInterleavedRaster) { ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA; byte dataA[] = raster.getDataStorage(); int strideA = raster.getScanlineStride(); int offsetA = raster.getDataOffset(0) - raster.getPixelStride() + 1; // handle a special case where the RGB conversion is screwed for (int y = 0; y < imgA.getHeight(); y++) { int indexA = offsetA + strideA * y; for (int x = 0; x < imgA.getWidth(); x++) { for (int k = 0; k < imgB.getNumBands(); k++) { int valueA = dataA[indexA++] & 0xFF; double valueB = GeneralizedImageOps.get(imgB.getBand(k), x, y); if (Math.abs(valueA - valueB) > tol) throw new RuntimeException("Images are not equal: A = " + valueA + " B = " + valueB); } } } } else if (imgA instanceof IntegerInterleavedRaster) { IntegerInterleavedRaster raster = (IntegerInterleavedRaster) imgA; int dataA[] = raster.getDataStorage(); int strideA = raster.getScanlineStride(); int offsetA = raster.getDataOffset(0) - raster.getPixelStride() + 1; // handle a special case where the RGB conversion is screwed for (int y = 0; y < imgA.getHeight(); y++) { int indexA = offsetA + strideA * y; for (int x = 0; x < imgA.getWidth(); x++) { int valueA = dataA[indexA++]; if (imgB.getNumBands() == 4) { int found0 = (valueA >> 24) & 0xFF; int found1 = (valueA >> 16) & 0xFF; int found2 = (valueA >> 8) & 0xFF; int found3 = valueA & 0xFF; double expected0 = GeneralizedImageOps.get(imgB.getBand(0), x, y); double expected1 = GeneralizedImageOps.get(imgB.getBand(1), x, y); double expected2 = GeneralizedImageOps.get(imgB.getBand(2), x, y); double expected3 = GeneralizedImageOps.get(imgB.getBand(3), x, y); if (Math.abs(found0 - expected0) > tol) throw new RuntimeException("Images are not equal"); if (Math.abs(found1 - expected1) > tol) throw new RuntimeException("Images are not equal"); if (Math.abs(found2 - expected2) > tol) throw new RuntimeException("Images are not equal"); if (Math.abs(found3 - expected3) > tol) throw new RuntimeException("Images are not equal"); } else if (imgB.getNumBands() == 3) { int found0 = (valueA >> 16) & 0xFF; int found1 = (valueA >> 8) & 0xFF; int found2 = valueA & 0xFF; double expected0 = GeneralizedImageOps.get(imgB.getBand(0), x, y); double expected1 = GeneralizedImageOps.get(imgB.getBand(1), x, y); double expected2 = GeneralizedImageOps.get(imgB.getBand(2), x, y); if (Math.abs(found0 - expected0) > tol) throw new RuntimeException("Images are not equal"); if (Math.abs(found1 - expected1) > tol) throw new RuntimeException("Images are not equal"); if (Math.abs(found2 - expected2) > tol) throw new RuntimeException("Images are not equal"); } else { throw new RuntimeException("Unexpectd number of bands"); } } } } else { throw new RuntimeException("Add support for raster type " + imgA.getClass().getSimpleName()); } }