/** * 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(GrayF32 img, float 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); } } }
@Override public void set(int x, int y, Number num) { image.set(x,y,num.floatValue()); }
public static void convolve( GrayF32 integral , IntegralKernel kernel, GrayF32 output ) { for( int y = 0; y < integral.height; y++ ) { for( int x = 0; x < integral.width; x++ ) { float total = 0; for( int i = 0; i < kernel.blocks.length; i++ ) { ImageRectangle b = kernel.blocks[i]; total += block_zero(integral,x+b.x0,y+b.y0,x+b.x1,y+b.y1)*kernel.scales[i]; } output.set(x,y,total); } } }
public static void set(ImageGray img, int x, int y, double value) { if (GrayI.class.isAssignableFrom(img.getClass())) { ((GrayI)img).set(x,y,(int)value); } else if (img instanceof GrayF32) { ((GrayF32) img).set(x, y,(float)value); } else if (img instanceof GrayF64) { ((GrayF64) img).set(x, y, value); } else if (img instanceof GrayS64) { ((GrayS64) img).set(x, y, (long)value); } else { throw new IllegalArgumentException("Unknown or incompatible image type: " + img.getClass().getSimpleName()); } }
@Override public void setOutside(int x, int y, float val) { image.set(colWrap.getIndex(x) , rowWrap.getIndex(y),val); } }
public void set( int x , int y , float val ) { if( image.isInBounds(x,y) ) image.set(x,y,val); setOutside(x,y,val); }
private static void computeHessian(GrayF32 integral, GrayF32 intensity, IntegralKernel kerXX, IntegralKernel kerYY, IntegralKernel kerXY, float norm, int y, int yy, int x, int xx) { float Dxx = IntegralImageOps.convolveSparse(integral,kerXX,xx,yy); float Dyy = IntegralImageOps.convolveSparse(integral,kerYY,xx,yy); float Dxy = IntegralImageOps.convolveSparse(integral,kerXY,xx,yy); Dxx *= norm; Dxy *= norm; Dyy *= norm; float det = Dxx*Dyy-0.81f*Dxy*Dxy; intensity.set(x,y,det); }
private static void computeHessian(GrayS32 integral, GrayF32 intensity, IntegralKernel kerXX, IntegralKernel kerYY, IntegralKernel kerXY, float norm, int y, int yy, int x, int xx) { float Dxx = IntegralImageOps.convolveSparse(integral,kerXX,xx,yy); float Dyy = IntegralImageOps.convolveSparse(integral,kerYY,xx,yy); float Dxy = IntegralImageOps.convolveSparse(integral,kerXY,xx,yy); Dxx *= norm; Dxy *= norm; Dyy *= norm; float det = Dxx*Dyy-0.81f*Dxy*Dxy; intensity.set(x,y,det); }
public static GrayF32 convertToImage(Kernel2D_F32 kernel ) { int w = kernel.getWidth(); GrayF32 ret = new GrayF32(w,w); for( int i = 0; i < w; i++ ) { for( int j = 0; j < w; j++ ) { ret.set(j,i,kernel.get(j,i)); } } return ret; }
/** * Computes the disparity for two stereo images along the image's right axis. Both * image must be rectified. * * @param left Left camera image. * @param right Right camera image. */ public void process( I left , I right , GrayF32 imageDisparity ) { // check inputs and initialize data structures InputSanityCheck.checkSameShape(left,right,imageDisparity); this.imageLeft = left; this.imageRight = right; w = left.width; h = left.height; // Compute disparity for each pixel for( int y = radiusY; y < h-radiusY; y++ ) { for( int x = radiusX+minDisparity; x < w-radiusX; x++ ) { // take in account image border when computing max disparity int max = x-Math.max(radiusX-1,x-score.length); // compute match score across all candidates processPixel( x , y , max ); // select the best disparity imageDisparity.set(x,y,(float)selectBest(max)); } } }
/** * Computes the disparity for two stereo images along the image's right axis. Both * image must be rectified. * * @param left Left camera image. * @param right Right camera image. */ public void process( I left , I right , GrayF32 imageDisparity ) { // check inputs and initialize data structures InputSanityCheck.checkSameShape(left,right,imageDisparity); this.imageLeft = left; this.imageRight = right; w = left.width; h = left.height; // Compute disparity for each pixel for( int y = radiusY*2; y < h-radiusY*2; y++ ) { for( int x = radiusX*2+minDisparity; x < w-radiusX*2; x++ ) { // take in account image border when computing max disparity int max = x-Math.max(radiusX*2-1,x-score.length); // compute match score across all candidates processPixel(x, y, max); // select the best disparity imageDisparity.set(x,y,(float)selectBest(max)); } } }
public static void vertical(Kernel1D_F32 kernel, GrayF32 input, GrayF32 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++ ) { float total = 0; float 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++ ) { float v = kernel.get(i-y+radius); total += input.get(x,i)*v; div += v; } output.set(x,y/skip, total/div ); } } }
public static void horizontal(Kernel1D_F32 kernel, GrayF32 input, GrayF32 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 ) { float total = 0; float 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++ ) { float v = kernel.get(j-x+radius); total += input.get(j,y)*v; div += v; } output.set(x/skip,y, total/div); } } }
public static void convolve(Kernel2D_F32 kernel, GrayF32 input, GrayF32 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; float total = 0; float div = 0; for( int i = startY; i <= endY; i++ ) { for( int j = startX; j <= endX; j++ ) { float 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 ); } } }
double right = Math.sqrt(b * b + dxdy * dxdy); intensity.set(col, row, (float) (left - right));
public static void horizontal(Kernel1D_F32 kernel, GrayF32 input, GrayF32 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++ ) { float total = 0; float 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++ ) { float v = kernel.get(j-x+offset); total += input.get(j,y)*v; weight += v; } output.set(x,y, total/weight ); } } }
public static void vertical(Kernel1D_F32 kernel, GrayF32 input, GrayF32 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++ ) { float total = 0; float 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++ ) { float v = kernel.get(i-y+offset); total += input.get(x,i)*v; weight += v; } output.set(x,y, total/weight ); } } }
/** * Computes the derivative of 'orig' along the x and y axes */ public static void process( GrayF32 orig, GrayF32 derivX, GrayF32 derivY) { final int width = orig.getWidth(); final int height = orig.getHeight(); for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { float dy = -(orig.get(x - 1, y - 1) * 0.25F + orig.get(x, y - 1) * 0.5F + orig.get(x + 1, y - 1) * 0.25F); dy += (orig.get(x - 1, y + 1) * 0.25F + orig.get(x, y + 1) * 0.5F + orig.get(x + 1, y + 1) * 0.25F); float dx = -(orig.get(x - 1, y - 1) * 0.25F + orig.get(x - 1, y) * 0.5F + orig.get(x - 1, y + 1) * 0.25F); dx += (orig.get(x + 1, y - 1) * 0.25F + orig.get(x + 1, y) * 0.5F + orig.get(x + 1, y + 1) * 0.25F); derivX.set(x, y, dx); derivY.set(x, y, dy); } } }
public static void convolve(Kernel2D_F32 kernel, GrayF32 input, GrayF32 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; float total = 0; float weight = 0; for( int i = startY; i < endY; i++ ) { for( int j = startX; j < endX; j++ ) { float v = kernel.get(j-x+offset,i-y+offset); total += input.get(j,i)*v; weight += v; } } output.set(x,y, total/weight ); } } }