@Override public float getIntensity(int x, int y) { return input.get(x,y); } }
public static void vertical(Kernel1D_S32 kernel, GrayU8 input, GrayI8 output ) { final int offset = kernel.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 weight = 0; int startY = y - offset; int endY = startY + kernel.getWidth(); if( startY < 0 ) startY = 0; if( endY > height ) endY = height; for( int i = startY; i < endY; i++ ) { int v = kernel.get(i-y+offset); total += input.get(x,i)*v; weight += v; } output.set(x,y, (total+weight/2)/weight ); } } }
public static void horizontal(Kernel1D_S32 kernel, GrayU8 input, GrayI8 output ) { final int offset = kernel.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 weight = 0; int startX = x - offset; int endX = startX+kernel.getWidth(); if( startX < 0 ) startX = 0; if( endX > width ) endX = width; for( int j = startX; j < endX; j++ ) { int v = kernel.get(j-x+offset); total += input.get(j,y)*v; weight += v; } output.set(x,y, (total+weight/2)/weight ); } } }
@Override public Number get(int x, int y) { return image.get(x,y); }
public static void convolve(Kernel2D_S32 kernel, GrayU8 input, GrayI8 output ) { final int offset = kernel.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 startX = x - offset; int endX = startX + kernel.getWidth(); if( startX < 0 ) startX = 0; if( endX > width ) endX = width; int startY = y - offset; int endY = startY + kernel.getWidth(); if( startY < 0 ) startY = 0; if( endY > height ) endY = height; int total = 0; int weight = 0; for( int i = startY; i < endY; i++ ) { for( int j = startX; j < endX; j++ ) { int v = kernel.get(j-x+offset,i-y+offset); total += input.get(j,i)*v; weight += v; } } output.set(x,y, (total+weight/2)/weight ); } } }
/** * If a point is inside the image true is returned if its value is not zero, otherwise true is returned. */ public static boolean getT(GrayU8 image, int x, int y) { if (image.isInBounds(x, y)) { return image.get(x, y) != 0; } else { return true; } }
/** * If a point is inside the image true is returned if its value is not zero, otherwise false is returned. */ public static boolean getF(GrayU8 image, int x, int y) { if (image.isInBounds(x, y)) { return image.get(x, y) != 0; } else { return false; } } }
public static void vertical(Kernel1D_S32 kernel, GrayU8 input, GrayI8 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 ); } } }
public static void horizontal(Kernel1D_S32 kernel, GrayU8 input, GrayI8 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 convolve(Kernel2D_S32 kernel, GrayU8 input, GrayI8 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 ); } } }
public static void edge4(GrayU8 input, GrayU8 output) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { if (getT(input, x - 1, y) && getT(input, x + 1, y) && getT(input, x, y - 1) && getT(input, x, y + 1)) output.set(x, y, 0); else output.set(x, y, input.get(x, y)); } } }
public static void erode4(GrayU8 input, GrayU8 output) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { if (input.get(x, y) != 0 && getT(input, x - 1, y) && getT(input, x + 1, y) && getT(input, x, y - 1) && getT(input, x, y + 1)) output.set(x, y, 1); else output.set(x, y, 0); } } }
public static void dilate4(GrayU8 input, GrayU8 output) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { if (input.get(x, y) != 0 || getF(input, x - 1, y) || getF(input, x + 1, y) || getF(input, x, y - 1) || getF(input, x, y + 1)) output.set(x, y, 1); else output.set(x, y, 0); } } }
public static void edge8(GrayU8 input, GrayU8 output) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { if (getT(input, x - 1, y) && getT(input, x + 1, y) && getT(input, x, y - 1) && getT(input, x, y + 1) && getT(input, x - 1, y + 1) && getT(input, x + 1, y + 1) && getT(input, x - 1, y - 1) && getT(input, x + 1, y - 1)) output.set(x, y, 0); else output.set(x, y, input.get(x, y)); } } }
public static void dilate8(GrayU8 input, GrayU8 output) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { if (input.get(x, y) != 0 || getF(input, x - 1, y) || getF(input, x + 1, y) || getF(input, x, y - 1) || getF(input, x, y + 1) || getF(input, x - 1, y + 1) || getF(input, x + 1, y + 1) || getF(input, x - 1, y - 1) || getF(input, x + 1, y - 1)) output.set(x, y, 1); else output.set(x, y, 0); } } }
public static void erode8(GrayU8 input, GrayU8 output) { output = InputSanityCheck.checkDeclare(input, output); for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { if (input.get(x, y) != 0 && getT(input, x - 1, y) && getT(input, x + 1, y) && getT(input, x, y - 1) && getT(input, x, y + 1) && getT(input, x - 1, y + 1) && getT(input, x + 1, y + 1) && getT(input, x - 1, y - 1) && getT(input, x + 1, y - 1)) output.set(x, y, 1); else output.set(x, y, 0); } } }
/** * Binary operation which is designed to remove small bits of spurious noise. An 8-neighborhood is used. * If a pixel is connected to less than 2 neighbors then its value zero. If connected to more than 6 then * its value is one. Otherwise it retains its original value. * * @param input Input image. Not modified. * @param output If not null, the output image. If null a new image is declared and returned. Modified. * @return Output image. */ public static void removePointNoise(GrayU8 input, GrayU8 output) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { int num = 0; if (getF(input, x - 1, y + 1)) num++; if (getF(input, x, y + 1)) num++; if (getF(input, x + 1, y + 1)) num++; if (getF(input, x + 1, y)) num++; if (getF(input, x + 1, y - 1)) num++; if (getF(input, x, y - 1)) num++; if (getF(input, x - 1, y - 1)) num++; if (getF(input, x - 1, y)) num++; if (num < 2) output.set(x, y, 0); else if (num > 6) output.set(x, y, 1); else output.set(x, y, input.get(x, y)); } } }