/** Checks to see if only the image borders are equal to each other within tolerance */ public static void assertEqualsBorder( ImageSingleBand imgA, ImageSingleBand imgB, double tol, int borderX, int borderY) { if (imgA.getWidth() != imgB.getWidth()) throw new RuntimeException("Widths are not equals"); if (imgA.getHeight() != imgB.getHeight()) throw new RuntimeException("Heights are not equals"); GImageSingleBand a = FactoryGImageSingleBand.wrap(imgA); GImageSingleBand b = FactoryGImageSingleBand.wrap(imgB); for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < borderX; x++) { compareValues(tol, a, b, x, y); } for (int x = imgA.getWidth() - borderX; x < imgA.getWidth(); x++) { compareValues(tol, a, b, x, y); } } for (int x = borderX; x < imgA.getWidth() - borderX; x++) { for (int y = 0; y < borderY; y++) { compareValues(tol, a, b, x, y); } for (int y = imgA.getHeight() - borderY; y < imgA.getHeight(); y++) { compareValues(tol, a, b, x, y); } } }
public static BufferedImage standard(ImageSingleBand<?> src, BufferedImage dst) { if (src.getDataType().isInteger()) { ImageInteger srcInt = (ImageInteger) src; if (src.getDataType().isSigned()) { double max = GImageStatistics.maxAbs(srcInt); return colorizeSign(srcInt, dst, (int) max); } else { if (src.getDataType().getNumBits() == 8) { dst = ConvertBufferedImage.convertTo((ImageUInt8) src, dst); } else { double max = GImageStatistics.maxAbs(srcInt); dst = grayUnsigned(srcInt, dst, (int) max); } } } else if (ImageFloat32.class.isAssignableFrom(src.getClass())) { ImageFloat32 img = (ImageFloat32) src; float max = ImageStatistics.maxAbs(img); boolean hasNegative = false; for (int i = 0; i < img.getHeight(); i++) { for (int j = 0; j < img.getWidth(); j++) { if (img.get(j, i) < 0) { hasNegative = true; break; } } } if (hasNegative) return colorizeSign(img, dst, (int) max); else return grayMagnitude((ImageFloat32) src, dst, max); } return dst; }
public static void set(ImageSingleBand img, int x, int y, double value) { if (ImageInteger.class.isAssignableFrom(img.getClass())) { ((ImageInteger) img).set(x, y, (int) value); } else if (img instanceof ImageFloat32) { ((ImageFloat32) img).set(x, y, (float) value); } else if (img instanceof ImageFloat64) { ((ImageFloat64) img).set(x, y, value); } else { throw new IllegalArgumentException( "Unknown or incompatible image type: " + img.getClass().getSimpleName()); } }
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(); } }
/** * Converts an image from one type to another type. * * @param src Input image. Not modified. * @param dst Converted output image. Modified. * @return Converted image. */ @SuppressWarnings({"unchecked"}) public static void convert(ImageSingleBand<?> src, ImageSingleBand<?> dst) { if (src.getClass() == dst.getClass()) { ((ImageSingleBand) dst).setTo(src); return; } Method m = BoofTesting.findMethod(ImplConvertImage.class, "convert", src.getClass(), dst.getClass()); try { m.invoke(null, src, dst); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } }
/** * Renders a gray scale image using color values from cold to hot. * * @param disparity Input disparity image * @param dst Where the image is rendered into. If null a new BufferedImage will be created and * return. * @param minDisparity Minimum disparity that can be computed * @param maxDisparity Maximum disparity that can be computed * @param invalidColor RGB value for invalid pixels. Try 0xFF << 8 for green * @return Rendered image. */ public static BufferedImage disparity( ImageSingleBand disparity, BufferedImage dst, int minDisparity, int maxDisparity, int invalidColor) { if (dst == null) dst = new BufferedImage( disparity.getWidth(), disparity.getHeight(), BufferedImage.TYPE_INT_RGB); if (disparity.getDataType().isInteger()) { return disparity((ImageInteger) disparity, dst, minDisparity, maxDisparity, invalidColor); } else if (disparity instanceof ImageFloat32) { return disparity((ImageFloat32) disparity, dst, minDisparity, maxDisparity, invalidColor); } else { throw new RuntimeException("Add support"); } }
/** * Renders a gray scale image using color values from cold to hot. * * @param src Input single band image. * @param dst Where the image is rendered into. If null a new BufferedImage will be created and * return. * @param normalize Used to normalize the input image. * @return Rendered image. */ public static BufferedImage grayMagnitudeTemp( ImageSingleBand src, BufferedImage dst, double normalize) { if (normalize < 0) normalize = GImageStatistics.maxAbs(src); dst = checkInputs(src, dst); if (src.getDataType().isInteger()) { return grayMagnitudeTemp((ImageInteger) src, dst, (int) normalize); } else { throw new RuntimeException("Add support"); } }
/** * Renders a gray scale image of the input image's intensity.<br> * <br> * dst(i,j) = 255*abs(src(i,j))/normalize * * @param src Input single band image. * @param dst Where the image is rendered into. If null a new BufferedImage will be created and * return. * @param normalize Used to normalize the input image. If < 0 then this value is automatically * computed. * @return Rendered image. */ public static BufferedImage grayMagnitude( ImageSingleBand src, BufferedImage dst, double normalize) { if (normalize < 0) normalize = GImageStatistics.maxAbs(src); dst = checkInputs(src, dst); if (src.getDataType().isInteger()) { return grayMagnitude((ImageInteger) src, dst, (int) normalize); } else { return grayMagnitude((ImageFloat32) src, dst, (float) normalize); } }
public static double get(ImageSingleBand img, int x, int y) { if (img instanceof ImageInt8) { return ((ImageInt8) img).get(x, y); } else if (img instanceof ImageInt16) { return ((ImageInt16) img).get(x, y); } else if (img instanceof ImageSInt32) { return ((ImageSInt32) img).get(x, y); } else if (img instanceof ImageFloat32) { return ((ImageFloat32) img).get(x, y); } else if (img instanceof ImageFloat64) { return ((ImageFloat64) img).get(x, y); } else if (img instanceof ImageSInt64) { return ((ImageSInt64) img).get(x, y); } else { throw new IllegalArgumentException( "Unknown or incompatible image type: " + img.getClass().getSimpleName()); } }
/** * Renders a colored image where the color indicates the sign and intensity its magnitude. The * input is divided by normalize to render it in the appropriate scale. * * @param src Input single band image. * @param dst Where the image is rendered into. If null a new BufferedImage will be created and * return. * @param normalize Used to normalize the input image. If <= 0 then the max value will be used * @return Rendered image. */ public static BufferedImage colorizeSign( ImageSingleBand src, BufferedImage dst, double normalize) { dst = checkInputs(src, dst); if (normalize <= 0) { normalize = GImageStatistics.maxAbs(src); } if (normalize == 0) { // sets the output to black ConvertBufferedImage.convertTo(src, dst, true); return dst; } if (src.getClass().isAssignableFrom(ImageFloat32.class)) { return colorizeSign((ImageFloat32) src, dst, (float) normalize); } else { return colorizeSign((ImageInteger) src, dst, (int) normalize); } }