/** * Adds uniform i.i.d noise to each pixel in the image. Noise range is min ≤ X < max. */ public static void addUniform(GrayS16 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] ) + rand.nextInt(range)+min; if( value < -32768 ) value = -32768; if( value > 32767 ) value = 32767; data[index++] = (short) value; } } }
/** * 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( GrayS16 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]) - mean; variance += d*d; } } return variance/(img.width*img.height); }
/** * 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(GrayS16 input, Random rand , double sigma , int lowerBound , int upperBound ) { 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 = (input.data[index] ) + (int)(rand.nextGaussian()*sigma); if( value < lowerBound ) value = lowerBound; if( value > upperBound ) value = upperBound; input.data[index++] = (short) value; } } }
/** * <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(GrayS16 imgA , GrayS16 imgB , GrayS16 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] ) - (imgB.data[indexB] )); } } }
/** * Bounds image pixels to be between these two values * * @param img Image * @param min minimum value. * @param max maximum value. */ public static void boundImage(GrayS16 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]; if( value < min ) data[index] = (short)min; else if( value > max ) data[index] = (short)max; } } }
/** * <p> * Performs pixel-wise addition<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 add(GrayS16 imgA , GrayS16 imgB , GrayS32 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] = (int)((imgA.data[indexA] ) + (imgB.data[indexB] )); } } }
/** * <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(GrayS16 imgA , GrayS16 imgB , GrayS32 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] = (int)((imgA.data[indexA] ) - (imgB.data[indexB] )); } } }
/** * 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<GrayS16> input , GrayS16 output ) { final int h = input.getHeight(); final int w = input.getWidth(); GrayS16[] 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] = (short)(total / bands.length); } } }
/** * <p> * Performs pixel-wise addition<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 add(GrayS8 imgA , GrayS8 imgB , GrayS16 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] ) + (imgB.data[indexB] )); } } }
/** * <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(GrayS8 imgA , GrayS8 imgB , GrayS16 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] ) - (imgB.data[indexB] )); } } }