public static void vertical9( Kernel1D_I32 kernel, ImageSInt16 image, ImageInt16 dest, int divisor, boolean includeBorder) { final short[] dataSrc = image.data; final short[] dataDst = dest.data; final int k1 = kernel.data[0]; final int k2 = kernel.data[1]; final int k3 = kernel.data[2]; final int k4 = kernel.data[3]; final int k5 = kernel.data[4]; final int k6 = kernel.data[5]; final int k7 = kernel.data[6]; final int k8 = kernel.data[7]; final int k9 = kernel.data[8]; final int radius = kernel.getRadius(); final int imgWidth = dest.getWidth(); final int imgHeight = dest.getHeight(); final int halfDivisor = divisor / 2; final int yEnd = imgHeight - radius; final int xBorder = includeBorder ? 0 : radius; for (int y = radius; y < yEnd; y++) { int indexDst = dest.startIndex + y * dest.stride + xBorder; int i = image.startIndex + (y - radius) * image.stride; final int iEnd = i + imgWidth - xBorder; for (i += xBorder; i < iEnd; i++) { int indexSrc = i; int total = (dataSrc[indexSrc]) * k1; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k2; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k3; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k4; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k5; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k6; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k7; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k8; indexSrc += image.stride; total += (dataSrc[indexSrc]) * k9; dataDst[indexDst++] = (short) ((total + halfDivisor) / divisor); } } }
/** Sets a rectangle inside the image with the specified value. */ public static void fillRectangle( ImageInt16 img, int value, int x0, int y0, int width, int height) { int x1 = x0 + width; int y1 = y0 + height; for (int y = y0; y < y1; y++) { for (int x = x0; x < x1; x++) { if (img.isInBounds(x, y)) img.set(x, y, value); } } }
/** * Fills the whole image with the specified pixel value * * @param img An image. * @param value The value that the image is being filled with. */ public static void fill(ImageInt16 img, int value) { final int h = img.getHeight(); final int w = img.getWidth(); short[] data = img.data; for (int y = 0; y < h; y++) { int index = img.getStartIndex() + y * img.getStride(); for (int x = 0; x < w; x++) { data[index++] = (short) value; } } }
/** * Computes the mean squared error (MSE) between the two images. * * @param imgA first image. Not modified. * @param imgB second image. Not modified. * @return error between the two images. */ public static double computeMeanSquaredError(ImageInt16 imgA, ImageInt16 imgB) { final int h = imgA.getHeight(); final int w = imgA.getWidth(); double total = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double difference = imgA.get(x, y) - imgB.get(x, y); total += difference * difference; } } return total / (w * h); }
/** * Sets each value in the image to a value drawn from an uniform distribution that has a range of * min <= X < max. */ public static void randomize(ImageInt16 img, Random rand, int min, int max) { final int h = img.getHeight(); final int w = img.getWidth(); int range = max - min; short[] data = img.data; for (int y = 0; y < h; y++) { int index = img.getStartIndex() + y * img.getStride(); for (int x = 0; x < w; x++) { data[index++] = (short) (rand.nextInt(range) + min); } } }
/** Flips the image from top to bottom */ public static void flipVertical(ImageInt16 img) { int h2 = img.height / 2; for (int y = 0; y < h2; y++) { int index1 = img.getStartIndex() + y * img.getStride(); int index2 = img.getStartIndex() + (img.height - y - 1) * img.getStride(); int end = index1 + img.width; while (index1 < end) { int tmp = img.data[index1]; img.data[index1++] = img.data[index2]; img.data[index2++] = (short) tmp; } } }
/** * 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); } } } } }