/** * 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); } }
/** * 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; } } }
/** * 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; } } }
} /** * 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); } } }
/** * Sets each value in the image to a value drawn from a Gaussian distribution. A user * specified lower and upper bound is provided to ensure that the values are within a legal * range. A drawn value outside the allowed range will be set to the closest bound. * * @param input Input image. Modified. * @param rand Random number generator * @param mean Distribution's mean. * @param sigma Distribution's standard deviation. * @param lowerBound Lower bound of value clip * @param upperBound Upper bound of value clip */ public static void fillGaussian(GrayI16 input, Random rand , double mean , double sigma , int lowerBound , int upperBound ) { 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 = (int)(rand.nextGaussian()*sigma+mean); if( value < lowerBound ) value = lowerBound; if( value > upperBound ) value = upperBound; data[index++] = (short)value; } } }
/** * Extracts a single band from a multi-band image * * @param input Multi-band image * @param band which bad is to be extracted * @param output The single band image */ public static void extractBand( InterleavedI16 input, int band , GrayI16 output) { final int numBands = input.numBands; for (int y = 0; y < input.height; y++) { int indexIn = input.getStartIndex() + y * input.getStride() + band; int indexOut = output.getStartIndex() + y * output.getStride(); int end = indexOut + output.width; for (; indexOut < end; indexIn += numBands , indexOut++ ) { output.data[indexOut] = input.data[indexIn]; } } }
/** * Inserts a single band into a multi-band image overwriting the original band * * @param input Single band image * @param band Which band the image is to be inserted into * @param output The multi-band image which the input image is to be inserted into */ public static void insertBand( GrayI16 input, int band , InterleavedI16 output) { final int numBands = output.numBands; for (int y = 0; y < input.height; y++) { int indexIn = input.getStartIndex() + y * input.getStride(); int indexOut = output.getStartIndex() + y * output.getStride() + band; int end = indexOut + output.width*numBands - band; for (; indexOut < end; indexOut += numBands , indexIn++ ) { output.data[indexOut] = input.data[indexIn]; } } }
/** * <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(GrayU8 imgA , GrayU8 imgB , GrayI16 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] = (short)((imgA.data[indexA] & 0xFF) - (imgB.data[indexB] & 0xFF)); } } }