@Override public float colorDistance(float[] color, int index) { final int numBands = input.getNumBands(); float total = 0; for( int i = 0; i < numBands; i++ ) { float diff = (input.getBand(i).data[index] & 0xFF) - color[i]; total += diff*diff; } return total; }
/** * Converts a {@link InterleavedU8} into the equivalent {@link Planar} * * @param input (Input) ImageInterleaved that is being converted. Not modified. * @param output (Optional) The output image. If null a new image is created. Modified. * @return Converted image. */ public static Planar<GrayU8> convert( InterleavedU8 input , Planar<GrayU8> output ) { if (output == null) { output = new Planar<GrayU8>(GrayU8.class,input.width, input.height,input.numBands); } else { output.reshape(input.width,input.height,input.numBands); } ImplConvertImage.convert(input,output); return output; }
@Override public int getHeight() {return image.getHeight();}
@Override public float getIntensity(int x, int y) { final int numBands = input.getNumBands(); final int index = input.getIndex(x,y); float total = 0; for( int i = 0; i < numBands; i++ ) { total += input.getBand(i).data[index] & 0xFF; } return total/numBands; } }
/** * Returns a new {@link Planar} which references the same internal single band images at this one. * * @param which List of bands which will comprise the new image * @return New image */ public Planar<T> partialSpectrum(int ...which ) { Planar<T> out = new Planar<>(getBandType(), which.length); out.setWidth(width); out.setHeight(height); out.setStride(stride); for (int i = 0; i < which.length; i++) { out.setBand(i,getBand(which[i])); } return out; }
public static Planar<GrayF32> tensorToImage( Tensor_F32 input , Planar<GrayF32> output , int miniBatch ) { if( input.getDimension() != 4 ) throw new IllegalArgumentException("Input should be 4-DOF. batch + spatial (channel,height,width)"); int bands = input.length(1); int height = input.length(2); int width = input.length(3); if( output == null ) { output = new Planar<>(GrayF32.class,width,height,bands); } else { if (input.length(1) != output.getNumBands()) throw new IllegalArgumentException("Number of bands don't match"); if (input.length(2) != output.getHeight()) throw new IllegalArgumentException("Spatial height doesn't match"); if (input.length(3) != output.getWidth()) throw new IllegalArgumentException("Spatial width doesn't match"); } for (int i = 0; i < bands; i++) { int indexIn = input.idx(miniBatch,i,0,0); GrayF32 band = output.getBand(i); int length = output.width*output.height; System.arraycopy(input.d,indexIn,band.data,0,length); } return output; } }
/** * Constructs an N-D histogram from a {@link Planar} {@link GrayF32} image. * * @param image input image * @param histogram preconfigured histogram to store the output */ public static void histogram_F32(Planar<GrayF32> image , Histogram_F64 histogram ) { if (image.getNumBands() != histogram.getDimensions()) throw new IllegalArgumentException("Number of bands in the image and histogram must be the same"); if( !histogram.isRangeSet() ) throw new IllegalArgumentException("Must specify range along each dimension in histogram"); final int D = histogram.getDimensions(); int coordinate[] = new int[ D ]; histogram.fill(0); for (int y = 0; y < image.getHeight(); y++) { int imageIndex = image.getStartIndex() + y*image.getStride(); for (int x = 0; x < image.getWidth(); x++ , imageIndex++) { for (int i = 0; i < D; i++) { coordinate[i] = histogram.getDimensionIndex(i,image.getBand(i).data[imageIndex]); } int index = histogram.getIndex(coordinate); histogram.value[index] += 1; } } }
@Override public void initialize(int backgroundWidth, int backgroundHeight, Motion homeToWorld) { background.reshape(backgroundWidth,backgroundHeight); for (int i = 0; i < background.getNumBands(); i+=2) { GImageMiscOps.fill(background.getBand(i),0); GImageMiscOps.fill(background.getBand(i+1),-1); } this.homeToWorld.set(homeToWorld); this.homeToWorld.invert(worldToHome); this.backgroundWidth = backgroundWidth; this.backgroundHeight = backgroundHeight; }
/** * Sets the values of each pixel equal to the pixels in the specified matrix. * Automatically resized to match the input image. * * @param orig The original image whose value is to be copied into this one */ @Override public void setTo( Planar<T> orig) { if (orig.width != width || orig.height != height) reshape(orig.width,orig.height); if( orig.getBandType() != getBandType() ) throw new IllegalArgumentException("The band type must be the same"); int N = orig.getNumBands(); if( N != getNumBands() ) { setNumberOfBands(orig.getNumBands()); } for( int i = 0; i < N; i++ ) { bands[i].setTo(orig.getBand(i)); } }
/** * Computes the average for each pixel across all bands in the {@link Planar} image. * * @param input Planar image * @param output Gray scale image containing average pixel values */ public static void averageBand(Planar<GrayS8> input , GrayS8 output ) { final int h = input.getHeight(); final int w = input.getWidth(); GrayS8[] bands = input.bands; for (int y = 0; y < h; y++) { int indexInput = input.getStartIndex() + y * input.getStride(); int indexOutput = output.getStartIndex() + y * output.getStride(); int indexEnd = indexInput+w; // for(int x = 0; x < w; x++ ) { for (; indexInput < indexEnd; indexInput++, indexOutput++ ) { int total = 0; for( int i = 0; i < bands.length; i++ ) { total += bands[i].data[ indexInput ]; } output.data[indexOutput] = (byte)(total / bands.length); } } }
/** * Converts an image into a spatial tensor * * @param input BoofCV planar image * @param output Tensor * @param miniBatch Which mini-batch in the tensor should the image be written to */ public static void imageToTensor(Planar<GrayF32> input , Tensor_F32 output , int miniBatch) { if( input.isSubimage()) throw new RuntimeException("Subimages not accepted"); if( output.getDimension() != 4 ) throw new IllegalArgumentException("Output should be 4-DOF. batch + spatial (channel,height,width)"); if( output.length(1) != input.getNumBands() ) throw new IllegalArgumentException("Number of bands don't match"); if( output.length(2) != input.getHeight() ) throw new IllegalArgumentException("Spatial height doesn't match"); if( output.length(3) != input.getWidth() ) throw new IllegalArgumentException("Spatial width doesn't match"); for (int i = 0; i < input.getNumBands(); i++) { GrayF32 band = input.getBand(i); int indexOut = output.idx(miniBatch,i,0,0); int length = input.width*input.height; System.arraycopy(band.data, 0,output.d,indexOut,length); } }
public static <T extends Planar> T createSubImageOf_PL(T input) { T ret = (T)new Planar(input.type,input.width,input.height,input.getNumBands()); for( int i = 0; i < input.getNumBands(); i++ ) { ret.bands[i] = createSubImageOf_S(input.getBand(i)); } ret.stride = ret.bands[0].stride; ret.startIndex = ret.bands[0].startIndex; return ret; }
/** * Applies mean box filter to a {@link Planar} * * @param input Input image. Not modified. * @param output (Optional) Storage for output image, Can be null. Modified. * @param radius Radius of the box blur function. * @param storage (Optional) Storage for intermediate results. Same size as input image. Can be null. * @param <T> Input image type. * @return Output blurred image. */ public static <T extends ImageGray<T>> Planar<T> mean(Planar<T> input, @Nullable Planar<T> output, int radius , @Nullable T storage ) { if( storage == null ) storage = GeneralizedImageOps.createSingleBand(input.getBandType(),input.width,input.height); if( output == null ) output = input.createNew(input.width,input.height); for( int band = 0; band < input.getNumBands(); band++ ) { GBlurImageOps.mean(input.getBand(band),output.getBand(band),radius, storage); } return output; }
if( image.getNumBands() == 3 ) { int bufferedImageType = input.getType(); if( bufferedImageType == BufferedImage.TYPE_3BYTE_BGR || bufferedImageType == BufferedImage.TYPE_INT_BGR ) { T tmp = image.getBand(0); image.bands[0] = image.getBand(2); image.bands[2] = tmp; } else if( image.getNumBands() == 4 ) { T[] temp = (T[])Array.newInstance(image.getBandType(),4); temp[0] = image.getBand(3); temp[1] = image.getBand(0); temp[2] = image.getBand(1); temp[3] = image.getBand(2); } else if( bufferedImageType == BufferedImage.TYPE_4BYTE_ABGR ) { temp[0] = image.getBand(3); temp[1] = image.getBand(2); temp[2] = image.getBand(1); temp[3] = image.getBand(0);
/** * Creates a new image of the same type and number of bands * * @param imgWidth image width * @param imgHeight image height * @return new image */ @Override public Planar<T> createNew(int imgWidth, int imgHeight) { return new Planar<>(type, imgWidth, imgHeight, bands.length); }
/** * Converts Bitmap image into Planar image of the appropriate type. * * @see #declareStorage(android.graphics.Bitmap, byte[]) * * @param input Input Bitmap image. * @param output Output image. If null a new one will be declared. * @param type The type of internal single band image used in the Planar image. * @param storage Byte array used for internal storage. If null it will be declared internally. * @return The converted Planar image. */ public static <T extends ImageGray> Planar<T> bitmapToMS( Bitmap input , Planar<T> output , Class<T> type , byte[] storage ) { if( output == null ) { output = new Planar<>(type, input.getWidth(), input.getHeight(), 4); } 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)); if( type == GrayU8.class ) ImplConvertBitmap.arrayToMulti_U8(storage, input.getConfig(), (Planar)output); else if( type == GrayF32.class ) ImplConvertBitmap.arrayToMulti_F32(storage, input.getConfig(), (Planar)output); else throw new IllegalArgumentException("Unsupported BoofCV Type"); return output; }
@Override public void initialize(int backgroundWidth, int backgroundHeight, Motion homeToWorld) { background.reshape(backgroundWidth,backgroundHeight); GImageMiscOps.fill(background.getBand(0),0); GImageMiscOps.fill(background.getBand(1),-1); this.homeToWorld.set(homeToWorld); this.homeToWorld.invert(worldToHome); this.backgroundWidth = backgroundWidth; this.backgroundHeight = backgroundHeight; }
/** * Converts Planar image into Bitmap. * * @see #declareStorage(android.graphics.Bitmap, byte[]) * * @param input Input Planar image. * @param output Output Bitmap image. * @param storage Byte array used for internal storage. If null it will be declared internally. */ public static <T extends ImageGray<T>> void planarToBitmap(Planar<T> 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); if( input.getBandType() == GrayU8.class ) ImplConvertBitmap.planarToArray_U8((Planar)input, storage,output.getConfig()); else if( input.getBandType() == GrayF32.class ) ImplConvertBitmap.planarToArray_F32((Planar)input, storage,output.getConfig()); else throw new IllegalArgumentException("Unsupported BoofCV Type"); output.copyPixelsFromBuffer(ByteBuffer.wrap(storage)); }