@Override public GrayF32 transform(GrayF32 original, GrayF32 transformed) { if( transformed == null ) { ImageDimension d = UtilWavelet.transformDimension(original,numLevels); transformed = new GrayF32(d.width,d.height); } temp.reshape(transformed.width,transformed.height); copy.reshape(original.width,original.height); copy.setTo(original); WaveletTransformOps.transformN(desc,copy,transformed,temp,numLevels); return transformed; }
/** * Computes the derivative of 'orig' along the x and y axes */ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY) { final int width = orig.getWidth(); final int height = orig.getHeight(); for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { float dy = -(orig.get(x - 1, y - 1) * 0.25F + orig.get(x, y - 1) * 0.5F + orig.get(x + 1, y - 1) * 0.25F); dy += (orig.get(x - 1, y + 1) * 0.25F + orig.get(x, y + 1) * 0.5F + orig.get(x + 1, y + 1) * 0.25F); float dx = -(orig.get(x - 1, y - 1) * 0.25F + orig.get(x - 1, y) * 0.5F + orig.get(x - 1, y + 1) * 0.25F); dx += (orig.get(x + 1, y - 1) * 0.25F + orig.get(x + 1, y) * 0.5F + orig.get(x + 1, y + 1) * 0.25F); derivX.set(x, y, dx); derivY.set(x, y, dy); } } }
public static double computeError(GrayF32 imgA, GrayF32 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 = Math.abs(imgA.get(x,y)-imgB.get(x,y)); total += difference; } } return total / (w*h); }
/** * Computes the derivative along the x and y axes */ public static void process(GrayF32 orig, GrayF32 derivX, GrayF32 derivY) { final float[] data = orig.data; final float[] imgX = derivX.data; final float[] 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++] = (data[indexSrc + 1] - data[indexSrc - 1]) * 0.5f; imgY[indexY++] = (data[indexSrc + stride] - data[indexSrc - stride]) * 0.5f; } } }
public static void magnitudeAbs(ImageFlow flowImage, BufferedImage out) { GrayF32 magnitude = new GrayF32(flowImage.width,flowImage.height); float max = 0; for( int y = 0; y < flowImage.height; y++ ) { for( int x = 0; x < flowImage.width; x++ ) { ImageFlow.D f = flowImage.unsafe_get(x,y); if( !f.isValid() ) { out.setRGB(x,y,0xFF); } else { float m = Math.max(Math.abs(f.x),Math.abs(f.y)); if( m > max ) max = m; magnitude.unsafe_set(x, y, m); } } } PixelMath.multiply(magnitude, 255 / max, magnitude); ConvertBufferedImage.convertTo(magnitude,out); }
/** * Converts Bitmap image into GrayF32. * * @see #declareStorage(android.graphics.Bitmap, byte[]) * * @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 GrayF32 bitmapToGray( Bitmap input , GrayF32 output , byte[] storage) { if( output == null ) { output = new GrayF32( 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; }
/** * Scales down the input by a factor of 2. Every other pixel along both axises is skipped. */ public static void scaleDown2(GrayF32 input , GrayF32 output ) { output.reshape(input.width / 2, input.height / 2); for (int y = 0; y < output.height; y++) { int indexInput = 2*y*input.stride; int indexOutput = y*output.stride; for (int x = 0; x < output.width; x++,indexInput+=2) { output.data[indexOutput++] = input.data[indexInput]; } } }
/** * <p> * Performs pixel-wise subtraction.<br> * output(x,y) = imgA(x,y) - imgB(x,y) * </p> * @param imgA Input image. Not modified. * @param imgB Input image. Not modified. * @param output Output image. Modified. */ public static void subtract(GrayF32 imgA , GrayF32 imgB , GrayF32 output ) { InputSanityCheck.checkSameShape(imgA,imgB,output); final int h = imgA.getHeight(); final int w = imgA.getWidth(); for (int y = 0; y < h; y++) { int indexA = imgA.getStartIndex() + y * imgA.getStride(); int indexB = imgB.getStartIndex() + y * imgB.getStride(); int indexOut = output.getStartIndex() + y * output.getStride(); int indexEnd = indexA+w; // for(int x = 0; x < w; x++ ) { for (; indexA < indexEnd; indexA++, indexB++, indexOut++ ) { output.data[indexOut] = ((imgA.data[indexA] ) - (imgB.data[indexB] )); } } }
/** * Converts a {@link InterleavedF32} into a {@link GrayF32} by computing the average value of each pixel * across all the bands. * * @param input (Input) The ImageInterleaved that is being converted. Not modified. * @param output (Optional) The single band output image. If null a new image is created. Modified. * @return Converted image. */ public static GrayF32 average( InterleavedF32 input , GrayF32 output ) { if (output == null) { output = new GrayF32(input.width, input.height); } else { output.reshape(input.width,input.height); } ConvertInterleavedToSingle.average(input, output); return output; }
protected static float getBorderT(GrayF32 imageA, GrayF32 imageB, int x, int y) { if( x < 0 ) x = 0; else if( x >= imageA.width ) x = imageA.width-1; if( y < 0 ) y = 0; else if( y >= imageA.height ) y = imageA.height-1; return imageB.unsafe_get(x,y) - imageA.unsafe_get(x,y); }
@Override protected void computeDerivX(GrayF32 image1, GrayF32 image2, GrayF32 derivX) { int w = image1.width-1; int h = image1.height-1; for( int y = 0; y < h; y++ ) { int index1 = image1.startIndex + y*image1.stride; int index2 = image2.startIndex + y*image2.stride; int indexX = derivX.startIndex + y*derivX.stride; for( int x = 0; x < w; x++ , index1++ , index2++ , indexX++ ) { float d0 = image1.data[index1+1] - image1.data[index1]; float d2 = image2.data[index2+1] - image2.data[index2]; float d1 = image1.data[index1+1+image1.stride] - image1.data[index1+image1.stride]; float d3 = image2.data[index2+1+image2.stride] - image2.data[index2+image2.stride]; derivX.data[indexX] = 0.25f*(d0 + d1 + d2 + d3); } } for( int y = 0; y < image1.height; y++ ) { derivX.unsafe_set(w,y, 0); } for( int x = 0; x < w; x++ ) { float d0 = image1.unsafe_get(x+1,h) - image1.unsafe_get(x,h); float d1 = image2.unsafe_get(x+1,h) - image2.unsafe_get(x,h); derivX.unsafe_set(x,h, 0.5f*(d0+d1)); } }
normalizedTemplate.reshape(template.width,template.height); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if( mask.unsafe_get(x,y) == 0 ) { normalizedTemplate.unsafe_set(x,y,0); if( xx < 0 ) xx = enlargedTemplate.width+xx; enlargedTemplate.unsafe_set(xx,yy, normalizedTemplate.unsafe_get(x,y));
private double computeEdgeMSE(GrayF32 imageInv) { GrayF32 edge = new GrayF32(imageInv.width,imageInv.height); LaplacianEdge.process(image,edge); PixelMath.abs(edge,edge); float max = ImageStatistics.maxAbs(edge); PixelMath.divide(edge,max,edge); float total = ImageStatistics.sum(edge); double error = 0; for( int y = 0; y < image.height; y++ ) { for( int x = 0; x < image.width; x++ ) { double w = edge.get(x,y)/total; double e = (image.get(x,y)-imageInv.get(x,y)); error += (e*e)*w; } } return error; }
@Override public float getIntensity(int x, int y) { return input.get(x,y); } }
/** * Returns the value of the specified pixel. * * @param x pixel coordinate. * @param y pixel coordinate. * @return Pixel intensity value. */ public float get(int x, int y) { if (!isInBounds(x, y)) throw new ImageAccessException("Requested pixel is out of bounds: ( " + x + " , " + y + " )"); return unsafe_get(x,y); }
public static void vertical(Kernel1D_F32 kernel, GrayF32 input, GrayF32 output , int skip ) { final int radius = kernel.getRadius(); final int width = input.width; final int height = input.height - input.height % skip; for (int y = 0; y < height; y += skip) { for( int x = 0; x < width; x++ ) { float total = 0; float div = 0; int startY = y - radius; int endY = y + radius; if( startY < 0 ) startY = 0; if( endY >= input.height ) endY = input.height-1; for( int i = startY; i <= endY; i++ ) { float v = kernel.get(i-y+radius); total += input.get(x,i)*v; div += v; } output.set(x,y/skip, total/div ); } } }