public ThresholdBlockMean_U8(ConfigLength requestedBlockWidth, double scale , boolean down, boolean thresholdFromLocalBlocks ) { super(requestedBlockWidth,thresholdFromLocalBlocks,GrayU8.class); this.stats = new GrayU8(1,1); this.scale = scale; this.down = down; }
public static void printDiffBinary(GrayU8 imgA, GrayU8 imgB) { System.out.println("------- Difference -----------"); for (int y = 0; y < imgA.getHeight(); y++) { for (int x = 0; x < imgA.getWidth(); x++) { if( imgA.unsafe_get(x,y) != imgB.unsafe_get(x,y)) System.out.print(" x"); else System.out.print(" ."); } System.out.println(); } } }
public static GrayU8 yuvToGray(ByteBuffer bufferY , int width , int height, int strideRow, GrayU8 output ) { if( output != null ) { output.reshape(width,height); } else { output = new GrayU8(width,height); } int indexDst = 0; for (int y = 0, indexRow=0; y < height; y++,indexRow += strideRow, indexDst += width) { bufferY.position(indexRow); bufferY.get(output.data,indexDst,width); } return output; }
/** * 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; } }
/** * Computes derivative of GrayU8. None of the images can be sub-images. */ public static void process_I8(GrayU8 orig, GrayS16 derivX, GrayS16 derivY) { final byte[] data = orig.data; final short[] imgX = derivX.data; final short[] imgY = derivY.data; final int width = orig.getWidth(); final int height = orig.getHeight() - 1; for (int y = 1; y < height; y++) { int endX = width * y + width - 1; for (int index = width * y + 1; index < endX; index++) { int v = (data[index + width + 1] & 0xFF) - (data[index - width - 1] & 0xFF); int w = (data[index + width - 1] & 0xFF) - (data[index - width + 1] & 0xFF); imgY[index] = (short) (((data[index + width] & 0xFF) - (data[index - width] & 0xFF)) * 2 + v + w); imgX[index] = (short) (((data[index + 1] & 0xFF) - (data[index - 1] & 0xFF)) * 2 + v - w); } } }
/** * Draws border pixels of each region using the specified color. * * @param pixelToRegion Conversion from pixel to region * @param borderColor RGB value of border pixel * @param output Storage for output image. Can be null. * @return Output image. */ public static BufferedImage regionBorders( GrayS32 pixelToRegion , int borderColor , BufferedImage output ) { if( output == null ) output = new BufferedImage(pixelToRegion.width,pixelToRegion.height,BufferedImage.TYPE_INT_RGB); GrayU8 binary = new GrayU8(pixelToRegion.width,pixelToRegion.height); ImageSegmentationOps.markRegionBorders(pixelToRegion, binary); for( int y = 0; y < binary.height; y++ ) { for( int x = 0; x < binary.width; x++ ) { if( binary.unsafe_get(x,y) == 1 ) { output.setRGB(x,y,borderColor); } } } return output; } }
protected static float getBorderT(GrayU8 imageA, GrayU8 imageB, int x, int y) { if( x < 0 ) x = 0; else if( x >= imageA.width ) x = imageA.width-1; if( y < 0 ) y = 0; else if( y >= imageA.height ) y = imageA.height-1; return imageB.unsafe_get(x,y) - imageA.unsafe_get(x,y); }
/** * Converts Bitmap image into GrayU8. * * @param input Input Bitmap image. * @param output Output image. If null a new one will be declared. * @param storage Byte array used for internal storage. If null it will be declared internally. * @return The converted gray scale image. */ public static GrayU8 bitmapToGray( Bitmap input , GrayU8 output , byte[] storage ) { if( output == null ) { output = new GrayU8( input.getWidth() , input.getHeight() ); } else if( output.getWidth() != input.getWidth() || output.getHeight() != input.getHeight() ) { throw new IllegalArgumentException("Image shapes are not the same"); } if( storage == null ) storage = declareStorage(input,null); input.copyPixelsToBuffer(ByteBuffer.wrap(storage)); ImplConvertBitmap.arrayToGray(storage, input.getConfig(), output); return output; }
/** * Scales down the input by a factor of 2. Every other pixel along both axises is skipped. */ public static void scaleDown2(GrayU8 input , GrayU8 output ) { output.reshape(input.width / 2, input.height / 2); for (int y = 0; y < output.height; y++) { int indexInput = 2*y*input.stride; int indexOutput = y*output.stride; for (int x = 0; x < output.width; x++,indexInput+=2) { output.data[indexOutput++] = input.data[indexInput]; } } }
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 convert( GrayU8 from, GrayI8 to ) { if (from.isSubimage() || to.isSubimage()) { for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < from.width; x++) { to.data[indexTo++] = ( byte )( from.data[indexFrom++] & 0xFF); } } } else { final int N = from.width * from.height; System.arraycopy(from.data, 0, to.data, 0, N); } }
int inputValue = input.unsafe_get(radius, y); int sum = 0; for( int i = 0; i <= inputValue; i++ ) { output.set(radius,y, (sum*maxValue)/area );
@Override public void process(GrayU8 gray) { detector.process(gray); synchronized ( lockGui ) { List<FiducialDetector.Detected> found = detector.getDetected(); // Select the largest quadrilaterals if( found.size() <= detected.length ) { numDetected = found.size(); for (int i = 0; i < numDetected; i++) { detected[i].binary.setTo(found.get(i).binary); detected[i].location.set(found.get(i).location); } } else { indexes.resize( found.size() ); area.resize( found.size()); for (int i = 0; i < found.size(); i++) { area.set(i, -Area2D_F64.quadrilateral(found.get(i).location)); } QuickSelect.selectIndex(area.data, detected.length, found.size(), indexes.data); numDetected = detected.length; for (int i = 0; i < numDetected; i++) { int index = indexes.data[i]; detected[i].binary.setTo(found.get(index).binary); detected[i].location.set(found.get(index).location); } } } }
@Override public synchronized void processImage(int sourceID, long frameID, final BufferedImage buffered, ImageBase input) { if( buffered != null ) { original = ConvertBufferedImage.checkCopy(buffered,original); work = ConvertBufferedImage.checkDeclare(buffered,work); binary.reshape(input.getWidth(), input.getHeight()); filtered.reshape(input.getWidth(),input.getHeight()); inputPrev.setTo((GrayU8)input); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Dimension d = gui.getPreferredSize(); if( d.getWidth() < buffered.getWidth() || d.getHeight() < buffered.getHeight() ) { gui.setPreferredSize(new Dimension(buffered.getWidth(), buffered.getHeight())); } }}); } else { input = inputPrev; } process((GrayU8)input); }
/** * Scales an image up using interpolation */ public static void scaleImageUp(GrayU8 input , GrayU8 output , int scale , InterpolatePixelS<GrayU8> interp ) { output.reshape(input.width*scale,input.height*scale); float fdiv = 1/(float)scale; interp.setImage(input); for (int y = 0; y < output.height; y++) { float inputY = y*fdiv; int indexOutput = output.getIndex(0,y); for (int x = 0; x < output.width; x++) { float inputX = x*fdiv; output.data[indexOutput++] = (byte)interp.get(inputX,inputY); } } }
public static void horizontal( Kernel1D_S32 kernel , GrayU8 image, GrayS32 dest ) { final byte[] dataSrc = image.data; final int[] dataDst = dest.data; final int[] dataKer = kernel.data; final int offset = kernel.getOffset(); final int kernelWidth = kernel.getWidth(); final int width = image.getWidth(); for( int i = 0; i < image.height; i++ ) { int indexDst = dest.startIndex + i*dest.stride+offset; int j = image.startIndex + i*image.stride; final int jEnd = j+width-(kernelWidth-1); for( ; j < jEnd; j++ ) { int total = 0; int indexSrc = j; for( int k = 0; k < kernelWidth; k++ ) { total += (dataSrc[indexSrc++] & 0xFF) * dataKer[k]; } dataDst[indexDst++] = total; } } }