public static void vertical3(Kernel1D_S32 kernel , GrayS16 image, GrayI16 dest ) { 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 radius = kernel.getRadius(); final int imgWidth = dest.getWidth(); final int imgHeight = dest.getHeight(); final int yEnd = imgHeight-radius; for( int y = radius; y < yEnd; y++ ) { int indexDst = dest.startIndex+y*dest.stride; int i = image.startIndex + (y-radius)*image.stride; final int iEnd = i+imgWidth; for( ; 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; dataDst[indexDst++] = ( short )total; } } }
public static void convert( GrayS16 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++] = ( from.data[indexFrom++] ); } } } else { final int N = from.width * from.height; System.arraycopy(from.data, 0, to.data, 0, N); } }
/** * Flips the image from top to bottom */ public static void flipVertical( GrayI16 input ) { int h2 = input.height/2; for( int y = 0; y < h2; y++ ) { int index1 = input.getStartIndex() + y * input.getStride(); int index2 = input.getStartIndex() + (input.height - y - 1) * input.getStride(); int end = index1 + input.width; while( index1 < end ) { int tmp = input.data[index1]; input.data[index1++] = input.data[index2]; input.data[index2++] = (short)tmp; } } }
/** * Sets the value of the specified pixel. * * @param x pixel coordinate. * @param y pixel coordinate. * @param value The pixel's new value. */ @Override public void set(int x, int y, int value) { if (!isInBounds(x, y)) throw new ImageAccessException("Requested pixel is out of bounds"); data[getIndex(x, y)] = (short) value; }
/** * Draws a filled rectangle that is aligned along the image axis inside the image. * * @param img Image the rectangle is drawn in. Modified * @param value Value of the rectangle * @param x0 Top left x-coordinate * @param y0 Top left y-coordinate * @param width Rectangle width * @param height Rectangle height */ public static void fillRectangle(GrayI16 img, int value, int x0, int y0, int width, int height) { int x1 = x0 + width; int y1 = y0 + height; if( x0 < 0 ) x0 = 0; if( x1 > img.width ) x1 = img.width; if( y0 < 0 ) y0 = 0; if( y1 > img.height ) y1 = img.height; for (int y = y0; y < y1; y++) { for (int x = x0; x < x1; x++) { img.set(x, y, value); } } }
/** * Rotates the image 90 degrees in the counter-clockwise direction. */ public static void rotateCCW( GrayI16 input , GrayI16 output ) { if( input.width != output.height || input.height != output.width ) throw new IllegalArgumentException("Incompatible shapes"); int w = input.width-1; for( int y = 0; y < input.height; y++ ) { int indexIn = input.startIndex + y*input.stride; for (int x = 0; x < input.width; x++) { output.unsafe_set(y,w-x,input.data[indexIn++]); } } }
/** * Sets the value of the specified pixel. * * @param x pixel coordinate. * @param y pixel coordinate. * @param value The pixel's new value. */ @Override public void unsafe_set(int x, int y, int value) { data[getIndex(x, y)] = (short) value; }
public static double get(ImageGray img, int x, int y) { if (img instanceof GrayI8) { return ((GrayI8) img).get(x, y); } else if (img instanceof GrayI16) { return ((GrayI16) img).get(x, y); } else if (img instanceof GrayS32) { return ((GrayS32) img).get(x, y); } else if (img instanceof GrayF32) { return ((GrayF32) img).get(x, y); } else if (img instanceof GrayF64) { return ((GrayF64) img).get(x, y); } else if (img instanceof GrayS64) { return ((GrayS64) img).get(x, y); } else { throw new IllegalArgumentException("Unknown or incompatible image type: " + img.getClass().getSimpleName()); } }
public static void vertical(Kernel1D_S32 kernel, GrayS16 input, GrayI16 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++ ) { int total = 0; int 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++ ) { int v = kernel.get(i-y+radius); total += input.get(x,i)*v; div += v; } output.set(x,y/skip, (total+div/2)/div ); } } }
/** * Rotates the image 90 degrees in the clockwise direction. */ public static void rotateCW( GrayI16 input , GrayI16 output ) { if( input.width != output.height || input.height != output.width ) throw new IllegalArgumentException("Incompatible shapes"); int h = input.height-1; for( int y = 0; y < input.height; y++ ) { int indexIn = input.startIndex + y*input.stride; for (int x = 0; x < input.width; x++) { output.unsafe_set(h-y,x,input.data[indexIn++]); } } }
public static void vertical3(Kernel1D_S32 kernel , GrayS16 image, GrayI16 dest , int divisor ) { 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 radius = kernel.getRadius(); final int imgWidth = dest.getWidth(); final int imgHeight = dest.getHeight(); final int halfDivisor = divisor/2; final int yEnd = imgHeight-radius; for( int y = radius; y < yEnd; y++ ) { int indexDst = dest.startIndex+y*dest.stride; int i = image.startIndex + (y-radius)*image.stride; final int iEnd = i+imgWidth; for( ; 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; dataDst[indexDst++] = ( short )((total+halfDivisor)/divisor); } } }
/** * Flips the image from left to right */ public static void flipHorizontal( GrayI16 input ) { int w2 = input.width/2; for( int y = 0; y < input.height; y++ ) { int index1 = input.getStartIndex() + y * input.getStride(); int index2 = index1 + input.width-1; int end = index1 + w2; while( index1 < end ) { int tmp = input.data[index1]; input.data[index1++] = input.data[index2]; input.data[index2--] = (short)tmp; } } }
public static void convert( GrayS32 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++] ); } } } else { final int N = from.width * from.height; for (int i = 0; i < N; i++) { to.data[i] = ( short )( from.data[i] ); } } }
public static void horizontal(Kernel1D_S32 kernel, GrayS16 input, GrayI16 output , int skip ) { final int radius = kernel.getRadius(); final int width = input.width - input.width % skip; final int height = input.height; for (int y = 0; y < height; y++) { for( int x = 0; x < width; x += skip ) { int total = 0; int div = 0; int startX = x - radius; int endX = x + radius; if( startX < 0 ) startX = 0; if( endX >= input.width ) endX = input.width-1; for( int j = startX; j <= endX; j++ ) { int v = kernel.get(j-x+radius); total += input.get(j,y)*v; div += v; } output.set(x/skip,y, (total+div/2)/div); } } }
public static void vertical3(Kernel1D_S32 kernel , GrayU8 image, GrayI16 dest ) { final byte[] 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 radius = kernel.getRadius(); final int imgWidth = dest.getWidth(); final int imgHeight = dest.getHeight(); final int yEnd = imgHeight-radius; for( int y = radius; y < yEnd; y++ ) { int indexDst = dest.startIndex+y*dest.stride; int i = image.startIndex + (y-radius)*image.stride; final int iEnd = i+imgWidth; for( ; i < iEnd; i++ ) { int indexSrc = i; int total = (dataSrc[indexSrc]& 0xFF) * k1; indexSrc += image.stride; total += (dataSrc[indexSrc]& 0xFF)*k2; indexSrc += image.stride; total += (dataSrc[indexSrc]& 0xFF)*k3; dataDst[indexDst++] = ( short )total; } } }
/** * Fills the whole image with the specified value * * @param input An image. * @param value The value that the image is being filled with. */ public static void fill(GrayI16 input, int value) { for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); Arrays.fill(input.data,index,index+input.width, (short)value); } }
public static void convert( GrayS64 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++] ); } } } else { final int N = from.width * from.height; for (int i = 0; i < N; i++) { to.data[i] = ( short )( from.data[i] ); } } }
public static void convolve(Kernel2D_S32 kernel, GrayS16 input, GrayI16 output , int skip ) { final int radius = kernel.getRadius(); final int width = input.width - input.width % skip; final int height = input.height - input.height % skip; for (int y = 0; y < height; y += skip ) { for( int x = 0; x < width; x += skip ) { int startX = x - radius; int endX = x + radius; if( startX < 0 ) startX = 0; if( endX >= input.width ) endX = input.width-1; int startY = y - radius; int endY = y + radius; if( startY < 0 ) startY = 0; if( endY >= input.height ) endY = input.height-1; int total = 0; int div = 0; for( int i = startY; i <= endY; i++ ) { for( int j = startX; j <= endX; j++ ) { int v = kernel.get(j-x+radius,i-y+radius); total += input.get(j,i)*v; div += v; } } output.set(x/skip,y/skip, (total+div/2)/div ); } } }
} /** * Sets each value in the image to a value drawn from an uniform distribution that has a range of min ≤ X < max. * * @param img Image which is to be filled. Modified, * @param rand Random number generator * @param min Minimum value of the distribution, inclusive * @param max Maximum value of the distribution, exclusive */ public static void fillUniform(GrayI16 img, Random rand , int min , int max) { int range = max-min; short[] data = img.data; for (int y = 0; y < img.height; y++) { int index = img.getStartIndex() + y * img.getStride(); for (int x = 0; x < img.width; x++) { data[index++] = (short)(rand.nextInt(range)+min); } } }