/** * 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); } } }
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 ); } } }
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 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 ); } } }
public static void horizontal(Kernel1D_S32 kernel, GrayS16 input, GrayI16 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 ); } } }
public static void vertical(Kernel1D_S32 kernel, GrayS16 input, GrayI16 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 vertical(Kernel1D_S32 kernelX, Kernel1D_S32 kernelY, GrayS32 input, GrayI16 output ) { final int offsetX = kernelX.getOffset(); final int offsetY = kernelY.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 weightY = 0; int startY = y - offsetY; int endY = startY + kernelY.getWidth(); if (startY < 0) startY = 0; if (endY > height) endY = height; for (int i = startY; i < endY; i++) { int v = kernelY.get(i - y + offsetY); total += input.get(x, i) * v; weightY += v; } int kerX0 = Math.max(0, offsetX - x); int kerX1 = Math.min(kernelX.getWidth(), width - x + offsetX); int weightX = 0; for (int i = kerX0; i < kerX1; i++) { weightX += kernelX.get(i); } int weight = weightX * weightY; output.set(x,y, (total+weight/2)/weight ); } } }
public static void convolve(Kernel2D_S32 kernel, GrayS16 input, GrayI16 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 ); } } }