/** * Computes derivative of GrayU8. None of the images can be sub-images. */ public static void process_I8(GrayU8 orig, GrayS16 derivX, GrayS16 derivY) { final byte[] data = orig.data; final short[] imgX = derivX.data; final short[] imgY = derivY.data; final int width = orig.getWidth(); final int height = orig.getHeight() - 1; for (int y = 1; y < height; y++) { int endX = width * y + width - 1; for (int index = width * y + 1; index < endX; index++) { int v = (data[index + width + 1] & 0xFF) - (data[index - width - 1] & 0xFF); int w = (data[index + width - 1] & 0xFF) - (data[index - width + 1] & 0xFF); imgY[index] = (short) (((data[index + width] & 0xFF) - (data[index - width] & 0xFF)) * 2 + v + w); imgX[index] = (short) (((data[index + 1] & 0xFF) - (data[index - 1] & 0xFF)) * 2 + v - w); } } }
private static void _renderBinary(GrayU8 binaryImage, boolean invert, BufferedImage out) { int w = binaryImage.getWidth(); int h = binaryImage.getHeight(); if( invert ) { for (int y = 0; y < h; y++) { int indexSrc = binaryImage.startIndex + y * binaryImage.stride; for (int x = 0; x < w; x++) { int rgb = binaryImage.data[indexSrc++] > 0 ? 0 : 0x00FFFFFF; out.setRGB(x, y, rgb); } } } else { for (int y = 0; y < h; y++) { int indexSrc = binaryImage.startIndex + y * binaryImage.stride; for (int x = 0; x < w; x++) { int rgb = binaryImage.data[indexSrc++] > 0 ? 0x00FFFFFF : 0; out.setRGB(x, y, rgb); } } } }
/** * Computes the derivative along the x and y axes */ public static void process(GrayU8 orig, GrayS16 derivX, GrayS16 derivY) { final byte[] data = orig.data; final short[] imgX = derivX.data; final short[] imgY = derivY.data; final int width = orig.getWidth(); final int height = orig.getHeight(); final int stride = orig.stride; for (int y = 1; y < height; y++) { int indexX = derivX.startIndex + derivX.stride * y + 1; int indexY = derivY.startIndex + derivY.stride * y + 1; int indexSrc = orig.startIndex + stride * y + 1; final int endX = indexSrc + width - 1; for (; indexSrc < endX; indexSrc++) { int val = data[indexSrc] & 0xFF; imgX[indexX++] = (short) (val - (data[indexSrc - 1] & 0xFF)); imgY[indexY++] = (short) (val - (data[indexSrc - stride] & 0xFF)); } } }
/** * Computes the derivative along the x and y axes */ public static void process(GrayU8 orig, GrayS16 derivX, GrayS16 derivY) { final byte[] data = orig.data; final short[] imgX = derivX.data; final short[] imgY = derivY.data; final int width = orig.getWidth(); final int height = orig.getHeight() - 1; final int stride = orig.stride; for (int y = 0; y < height; y++) { int indexX = derivX.startIndex + derivX.stride * y; int indexY = derivY.startIndex + derivY.stride * y; int indexSrc = orig.startIndex + stride * y; final int endX = indexSrc + width - 1; for (; indexSrc < endX; indexSrc++) { int val = data[indexSrc] & 0xFF; imgX[indexX++] = (short) ((data[indexSrc + 1] & 0xFF) - val); imgY[indexY++] = (short) ((data[indexSrc + stride] & 0xFF) - val); } } }
/** * Computes the derivative along the x and y axes */ public static void process(GrayU8 orig, GrayS16 derivX, GrayS16 derivY) { final byte[] data = orig.data; final short[] imgX = derivX.data; final short[] imgY = derivY.data; final int width = orig.getWidth(); final int height = orig.getHeight() - 1; final int stride = orig.stride; for (int y = 1; y < height; y++) { int indexX = derivX.startIndex + derivX.stride * y + 1; int indexY = derivY.startIndex + derivY.stride * y + 1; int indexSrc = orig.startIndex + stride * y + 1; final int endX = indexSrc + width - 2; for (; indexSrc < endX; indexSrc++) { imgX[indexX++] = (short) ((data[indexSrc + 1] & 0xFF) - (data[indexSrc - 1] & 0xFF)); imgY[indexY++] = (short) ((data[indexSrc + stride] & 0xFF) - (data[indexSrc - stride] & 0xFF)); } } }
public static void printDiffBinary(GrayU8 imgA, GrayU8 imgB) { System.out.println("------- Difference -----------"); for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { if( imgA.unsafe_get(x,y) != imgB.unsafe_get(x,y)) System.out.print(" x"); else System.out.print(" ."); } System.out.println(); } } }
/** * Converts ImageGray into Bitmap. * * @see #declareStorage(android.graphics.Bitmap, byte[]) * * @param input Input gray scale image. * @param output Output Bitmap image. * @param storage Byte array used for internal storage. If null it will be declared internally. */ public static void grayToBitmap( GrayU8 input , Bitmap output , byte[] storage) { if( output.getWidth() != input.getWidth() || output.getHeight() != input.getHeight() ) { throw new IllegalArgumentException("Image shapes are not the same"); } if( storage == null ) storage = declareStorage(output,null); ImplConvertBitmap.grayToArray(input, storage,output.getConfig()); output.copyPixelsFromBuffer(ByteBuffer.wrap(storage)); }
/** * Converts ImageGray into Bitmap. * * @see #declareStorage(android.graphics.Bitmap, byte[]) * * @param input Input gray scale image. * @param output Output Bitmap image. * @param storage Byte array used for internal storage. If null it will be declared internally. */ public static void grayToBitmap( GrayU8 input , Bitmap output , byte[] storage) { if( output.getWidth() != input.getWidth() || output.getHeight() != input.getHeight() ) { throw new IllegalArgumentException("Image shapes are not the same"); } if( storage == null ) storage = declareStorage(output,null); ImplConvertBitmap.grayToArray(input, storage,output.getConfig()); output.copyPixelsFromBuffer(ByteBuffer.wrap(storage)); }
/** * Converts Bitmap image into GrayU8. * * @param input Input Bitmap image. * @param output Output image. If null a new one will be declared. * @param storage Byte array used for internal storage. If null it will be declared internally. * @return The converted gray scale image. */ public static GrayU8 bitmapToGray( Bitmap input , GrayU8 output , byte[] storage ) { if( output == null ) { output = new GrayU8( input.getWidth() , input.getHeight() ); } else if( output.getWidth() != input.getWidth() || output.getHeight() != input.getHeight() ) { throw new IllegalArgumentException("Image shapes are not the same"); } if( storage == null ) storage = declareStorage(input,null); input.copyPixelsToBuffer(ByteBuffer.wrap(storage)); ImplConvertBitmap.arrayToGray(storage, input.getConfig(), output); return output; }
private static void renderBinary(GrayU8 binaryImage, boolean invert, IntegerInterleavedRaster raster) { int rasterIndex = 0; int data[] = raster.getDataStorage(); int w = binaryImage.getWidth(); int h = binaryImage.getHeight(); if (invert) { for (int y = 0; y < h; y++) { int indexSrc = binaryImage.startIndex + y * binaryImage.stride; for (int x = 0; x < w; x++) { data[rasterIndex++] = binaryImage.data[indexSrc++] > 0 ? 0 : 0xFFFFFFFF; } } } else { for (int y = 0; y < h; y++) { int indexSrc = binaryImage.startIndex + y * binaryImage.stride; for (int x = 0; x < w; x++) { data[rasterIndex++] = binaryImage.data[indexSrc++] > 0 ? 0xFFFFFFFF : 0; } } } } }
public static void horizontal( Kernel1D_S32 kernel , GrayU8 image, GrayS32 dest ) { final byte[] dataSrc = image.data; final int[] dataDst = dest.data; final int[] dataKer = kernel.data; final int offset = kernel.getOffset(); final int kernelWidth = kernel.getWidth(); final int width = image.getWidth(); for( int i = 0; i < image.height; i++ ) { int indexDst = dest.startIndex + i*dest.stride+offset; int j = image.startIndex + i*image.stride; final int jEnd = j+width-(kernelWidth-1); for( ; j < jEnd; j++ ) { int total = 0; int indexSrc = j; for( int k = 0; k < kernelWidth; k++ ) { total += (dataSrc[indexSrc++] & 0xFF) * dataKer[k]; } dataDst[indexDst++] = total; } } }
public static void horizontal( Kernel1D_S32 kernel , GrayU8 image, GrayI8 dest , int divisor ) { final byte[] dataSrc = image.data; final byte[] dataDst = dest.data; final int[] dataKer = kernel.data; final int offset = kernel.getOffset(); final int kernelWidth = kernel.getWidth(); final int halfDivisor = divisor/2; final int width = image.getWidth(); for( int i = 0; i < image.height; i++ ) { int indexDst = dest.startIndex + i*dest.stride+offset; int j = image.startIndex + i*image.stride; final int jEnd = j+width-(kernelWidth-1); for( ; j < jEnd; j++ ) { int total = 0; int indexSrc = j; for( int k = 0; k < kernelWidth; k++ ) { total += (dataSrc[indexSrc++] & 0xFF) * dataKer[k]; } dataDst[indexDst++] = (byte)((total+halfDivisor)/divisor); } } }
private static void renderBinary(GrayU8 binaryImage, boolean invert, DataBufferInt buffer, WritableRaster raster) { int rasterIndex = 0; int data[] = buffer.getData(); int w = binaryImage.getWidth(); int h = binaryImage.getHeight(); if (invert) { for (int y = 0; y < h; y++) { int indexSrc = binaryImage.startIndex + y * binaryImage.stride; for (int x = 0; x < w; x++) { data[rasterIndex++] = binaryImage.data[indexSrc++] != 0 ? 0 : 0xFFFFFFFF; } } } else { for (int y = 0; y < h; y++) { int indexSrc = binaryImage.startIndex + y * binaryImage.stride; for (int x = 0; x < w; x++) { data[rasterIndex++] = binaryImage.data[indexSrc++] != 0 ? 0xFFFFFFFF : 0; } } } } }
public static void process(GrayU8 orig, GrayF32 deriv) { deriv.reshape(orig.width,orig.height); final byte[] data = orig.data; final float[] out = deriv.data; final int width = orig.getWidth(); final int height = orig.getHeight() - 1; final int stride = orig.stride; for (int y = 1; y < height; y++) { int index = orig.startIndex + stride * y + 1; int indexOut = deriv.startIndex + deriv.stride * y + 1; int endX = index + width - 2; for (; index < endX; index++) { int v = data[index - stride] & 0xFF; v += data[index - 1] & 0xFF; v += -4 * (data[index] & 0xFF); v += data[index + 1] & 0xFF; v += data[index + stride] & 0xFF; out[indexOut++] = v; } } }
public ImageBinaryPanel( GrayU8 binaryImage ) { this.binaryImage = binaryImage; img = new BufferedImage(binaryImage.getWidth(),binaryImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY); VisualizeBinaryData.renderBinary(binaryImage,false,img); setPreferredSize(new Dimension(binaryImage.getWidth(), binaryImage.getHeight())); setMinimumSize(getPreferredSize()); setMaximumSize(getPreferredSize()); }
public ImageBinaryPanel( GrayU8 binaryImage ) { this.binaryImage = binaryImage; img = new BufferedImage(binaryImage.getWidth(),binaryImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY); VisualizeBinaryData.renderBinary(binaryImage,false,img); setPreferredSize(new Dimension(binaryImage.getWidth(), binaryImage.getHeight())); setMinimumSize(getPreferredSize()); setMaximumSize(getPreferredSize()); }
public static void vertical(Kernel1D_S32 kernel, GrayU8 input, GrayI8 output ) { final int offset = kernel.getOffset(); final int width = input.getWidth(); final int height = input.getHeight(); for (int y = 0; y < height; y++) { for( int x = 0; x < width; x++ ) { int total = 0; int weight = 0; int startY = y - offset; int endY = startY + kernel.getWidth(); if( startY < 0 ) startY = 0; if( endY > height ) endY = height; for( int i = startY; i < endY; i++ ) { int v = kernel.get(i-y+offset); total += input.get(x,i)*v; weight += v; } output.set(x,y, (total+weight/2)/weight ); } } }
public static void horizontal(Kernel1D_S32 kernel, GrayU8 input, GrayI8 output ) { final int offset = kernel.getOffset(); final int width = input.getWidth(); final int height = input.getHeight(); for (int y = 0; y < height; y++) { for( int x = 0; x < width; x++ ) { int total = 0; int weight = 0; int startX = x - offset; int endX = startX+kernel.getWidth(); if( startX < 0 ) startX = 0; if( endX > width ) endX = width; for( int j = startX; j < endX; j++ ) { int v = kernel.get(j-x+offset); total += input.get(j,y)*v; weight += v; } output.set(x,y, (total+weight/2)/weight ); } } }
/** * A faster convert that works directly with a specific raster */ public static void bufferedToGray(ByteInterleavedRaster src, GrayU8 dst) { byte[] srcData = src.getDataStorage(); byte[] data = dst.data; int numBands = src.getNumBands(); int size = dst.getWidth() * dst.getHeight(); int srcStride = src.getScanlineStride(); int srcOffset = getOffset(src); int srcStrideDiff = srcStride-src.getPixelStride()*dst.width; if (numBands == 3) { from_3BU8_to_U8(dst, srcData, data, srcOffset, srcStrideDiff); } else if (numBands == 1) { from_1BU8_to_U8(dst, srcData, data, size, srcStride, srcOffset, srcStrideDiff); } else if (numBands == 4) { from_4BU8_to_U8(dst, srcData, data, srcOffset, srcStrideDiff); } else { throw new RuntimeException("Unexpected number of bands found. Bands = "+numBands); } }
@Override public void process(GrayU8 input) { pyramid.process(input); draw(0, 0, pyramid.getLayer(0)); int height = 0; int width = pyramid.getLayer(0).getWidth(); for( int i = 1; i < pyramid.getNumLayers(); i++ ) { GrayU8 l = pyramid.getLayer(i); draw(width, height, l); height += l.getHeight(); } ConvertBitmap.grayToBitmap(this.output, bitmap, bitmapTmp); } }