/** * 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(InterleavedF32 input, float value) { for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); int end = index + input.width*input.numBands; Arrays.fill(input.data,index,end, value); } }
/** * Adds uniform i.i.d noise to each pixel in the image. Noise range is min ≤ X < max. */ public static void addUniform(InterleavedF32 input, Random rand , float min , float max) { float range = max-min; float[] data = input.data; int length = input.width*input.numBands; for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); int indexEnd = index+length; while( index < indexEnd ) { float value = data[index] + rand.nextFloat()*range+min; data[index++] = value; } } }
/** * Fills one band in the image with the specified value * * @param input An image. * @param band Which band is to be filled with the specified value * @param value The value that the image is being filled with. */ public static void fillBand(InterleavedF32 input, int band , float value) { final int numBands = input.numBands; for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride() + band; int end = index + input.width*numBands - band; for (; index < end; index += numBands ) { input.data[index] = value; } } }
/** * 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, inclusive */ public static void fillUniform(InterleavedF32 img, Random rand , float min , float max) { float range = max-min; float[] data = img.data; for (int y = 0; y < img.height; y++) { int index = img.getStartIndex() + y * img.getStride(); int end = index + img.width*img.numBands; for (; index < end; index++) { data[index] = rand.nextFloat()*range+min; } } }
/** * Fills each band in the image with the specified values * * @param input An image. * @param values Array which contains the values each band is to be filled with. */ public static void fill(InterleavedF32 input, float[] values) { final int numBands = input.numBands; for (int y = 0; y < input.height; y++) { for( int band = 0; band < numBands; band++ ) { int index = input.getStartIndex() + y * input.getStride() + band; int end = index + input.width*numBands - band; float value = values[band]; for (; index < end; index += numBands ) { input.data[index] = value; } } } }
/** * Adds Gaussian/normal i.i.d noise to each pixel in the image. If a value exceeds the specified * it will be set to the closest bound. * @param input Input image. Modified. * @param rand Random number generator. * @param sigma Distributions standard deviation. * @param lowerBound Allowed lower bound * @param upperBound Allowed upper bound */ public static void addGaussian(InterleavedF32 input, Random rand , double sigma , float lowerBound , float upperBound ) { int length = input.width*input.numBands; for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); int indexEnd = index+length; while( index < indexEnd ) { float value = (input.data[index]) + (float)(rand.nextGaussian()*sigma); if( value < lowerBound ) value = lowerBound; if( value > upperBound ) value = upperBound; input.data[index++] = value; } } }
/** * 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(InterleavedF32 input, Random rand , double mean , double sigma , float lowerBound , float upperBound ) { float[] data = input.data; int length = input.width*input.numBands; for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); int indexEnd = index+length; while( index < indexEnd ) { float value = (float)(rand.nextGaussian()*sigma+mean); if( value < lowerBound ) value = lowerBound; if( value > upperBound ) value = upperBound; data[index++] = value; } } }
/** * 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( GrayF32 input, int band , InterleavedF32 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]; } } }
/** * 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( InterleavedF32 input, int band , GrayF32 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]; } } }