/** * Converts a {@link InterleavedU16} into a {@link GrayU16} 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 GrayU16 average( InterleavedU16 input , GrayU16 output ) { if (output == null) { output = new GrayU16(input.width, input.height); } else { output.reshape(input.width,input.height); } ConvertInterleavedToSingle.average(input, output); return output; }
@Override public Number get(int x, int y) { return image.get(x,y); }
/** * Bounds image pixels to be between these two values * * @param img Image * @param min minimum value. * @param max maximum value. */ public static void boundImage(GrayU16 img , int min , int max ) { 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(); int indexEnd = index+w; // for(int x = 0; x < w; x++ ) { for (; index < indexEnd; index++) { int value = data[index]& 0xFFFF; if( value < min ) data[index] = (short)min; else if( value > max ) data[index] = (short)max; } } }
/** * Computes the variance of pixel intensity values inside the image. * * @param img Input image. Not modified. * @param mean Mean pixel intensity value. * @return Pixel variance */ public static double variance( GrayU16 img , double mean ) { double variance = 0; for (int y = 0; y < img.height; y++) { int index = img.getStartIndex() + y * img.getStride(); int indexEnd = index+img.width; // for(int x = 0; x < img.width; x++ ) { for (; index < indexEnd; index++ ) { double d = (img.data[index]& 0xFFFF) - mean; variance += d*d; } } return variance/(img.width*img.height); }
public static void convert( GrayU16 from, GrayS32 to ) { if (from.isSubimage() || to.isSubimage()) { for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < from.width; x++) { to.data[indexTo++] = ( from.data[indexFrom++] & 0xFFFF); } } } else { final int N = from.width * from.height; for (int i = 0; i < N; i++) { to.data[i] = ( from.data[i] & 0xFFFF); } } }
public static void vertical(Kernel1D_S32 kernelX, Kernel1D_S32 kernelY, GrayU16 input, GrayI8 output ) { final int offsetX = kernelX.getOffset(); final int offsetY = kernelY.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 weightY = 0; int startY = y - offsetY; int endY = startY + kernelY.getWidth(); if (startY < 0) startY = 0; if (endY > height) endY = height; for (int i = startY; i < endY; i++) { int v = kernelY.get(i - y + offsetY); total += input.get(x, i) * v; weightY += v; } int kerX0 = Math.max(0, offsetX - x); int kerX1 = Math.min(kernelX.getWidth(), width - x + offsetX); int weightX = 0; for (int i = kerX0; i < kerX1; i++) { weightX += kernelX.get(i); } int weight = weightX * weightY; output.set(x,y, (total+weight/2)/weight ); } } }
public static void parseDepth( String fileName , GrayU16 depth , GrowQueue_I8 data ) throws IOException { DataInputStream in = new DataInputStream(new FileInputStream(fileName)); String s[] = readLine(in).split(" "); int w = Integer.parseInt(s[0]); int h = Integer.parseInt(s[1]); int length = w*h*2; if( data == null ) data = new GrowQueue_I8(length); else data.resize(length); in.read(data.data,0,length); depth.reshape(w,h); UtilOpenKinect.bufferDepthToU16(data.data, depth); }
int inputValue = input.unsafe_get(radius, y); int sum = 0; for( int i = 0; i <= inputValue; i++ ) { output.set(radius,y, (sum*maxValue)/area );
@Override public int unsafe_get(int x, int y) { return data[getIndex(x, y)] & 0xFFFF; }
@Override public void set(int x, int y, Number num) { image.set(x,y,num.intValue()); }
public static void convert( GrayU16 from, GrayI8 to ) { if (from.isSubimage() || to.isSubimage()) { for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < from.width; x++) { to.data[indexTo++] = ( byte )( from.data[indexFrom++] & 0xFFFF); } } } else { final int N = from.width * from.height; for (int i = 0; i < N; i++) { to.data[i] = ( byte )( from.data[i] & 0xFFFF); } } }
/** * Adds uniform i.i.d noise to each pixel in the image. Noise range is min ≤ X < max. */ public static void addUniform(GrayU16 input, Random rand , int min , int max) { int range = max-min; short[] data = input.data; for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); for (int x = 0; x < input.width; x++) { int value = (data[index] & 0xFFFF) + rand.nextInt(range)+min; if( value < 0 ) value = 0; if( value > 65535 ) value = 65535; data[index++] = (short) value; } } }
depth.reshape(w,h); rgb.reshape(w,h);
public static <T extends ImageGray<T>> T create(Class<T> type , int width , int height ) { if( type == GrayU8.class) { return (T)new GrayU8(width,height); } else if( type == GrayS8.class) { return (T)new GrayS8(width,height); } else if( type == GrayU16.class) { return (T)new GrayU16(width,height); } else if( type == GrayS16.class) { return (T)new GrayS16(width,height); } else if( type == GrayS32.class) { return (T)new GrayS32(width,height); } else if( type == GrayF32.class) { return (T)new GrayF32(width,height); } else{ throw new IllegalArgumentException("Unknown image type: "+type); } } }
/** * Converts a {@link Planar} into a {@link ImageGray} by computing the average value of each pixel * across all the bands. * * @param input Input Planar image 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 GrayU16 average( Planar<GrayU16> input , GrayU16 output ) { if (output == null) { output = new GrayU16(input.width, input.height); } else { output.reshape(input.width,input.height); } ImplConvertPlanarToGray.average(input, output); return output; }
/** * <p> * Computes the absolute value of the difference between each pixel in the two images.<br> * d(x,y) = |img1(x,y) - img2(x,y)| * </p> * @param imgA Input image. Not modified. * @param imgB Input image. Not modified. * @param diff Absolute value of difference image. Modified. */ public static void diffAbs(GrayU16 imgA , GrayU16 imgB , GrayU16 diff ) { InputSanityCheck.checkSameShape(imgA,imgB,diff); 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 indexDiff = diff.getStartIndex() + y * diff.getStride(); int indexEnd = indexA+w; // for(int x = 0; x < w; x++ ) { for (; indexA < indexEnd; indexA++, indexB++, indexDiff++ ) { diff.data[indexDiff] = (short)Math.abs((imgA.data[indexA] & 0xFFFF) - (imgB.data[indexB] & 0xFFFF)); } } }
public static void convert( GrayU16 from, GrayI16 to ) { if (from.isSubimage() || to.isSubimage()) { for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < from.width; x++) { to.data[indexTo++] = ( short )( from.data[indexFrom++] & 0xFFFF); } } } else { final int N = from.width * from.height; System.arraycopy(from.data, 0, to.data, 0, N); } }