public static void convert( InterleavedU8 from, InterleavedI8 to ) { if (from.isSubimage() || to.isSubimage()) { final int N = from.width * from.getNumBands(); 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 < N; x++) { to.data[indexTo++] = ( byte )( from.data[indexFrom++] & 0xFF); } } } else { final int N = from.width * from.height * from.getNumBands(); System.arraycopy(from.data, 0, to.data, 0, N); } }
/** * <p> * Converts an {@link boofcv.struct.image.InterleavedS32} into a {@link boofcv.struct.image.InterleavedU8}. * </p> * * @param input Input image which is being converted. Not modified. * @param output (Optional) The output image. If null a new image is created. Modified. * @return Converted image. */ public static InterleavedU8 convert(InterleavedS32 input, InterleavedU8 output) { if (output == null) { output = new InterleavedU8(input.width, input.height, input.numBands); } else { output.reshape(input.width,input.height,input.numBands); } ImplConvertImage.convert(input, output); return output; }
public static void convolve(Kernel2D_S32 kernel , InterleavedU8 src , InterleavedI16 dst ) { final int[] dataKernel = kernel.data; final byte[] dataSrc = src.data; final short[] dataDst = dst.data; final int width = src.getWidth(); final int height = src.getHeight(); final int numBands = src.getNumBands(); int offsetL = kernel.offset; int offsetR = kernel.width-kernel.offset-1; for( int y = offsetL; y < height-offsetR; y++ ) { int indexDst = dst.startIndex + y*dst.stride+offsetL*numBands; for( int x = offsetL; x < width-offsetR; x++ ) { int indexSrcStart = src.startIndex + (y-offsetL)*src.stride + (x-offsetL)*numBands; for (int band = 0; band < numBands; band++) { int total = 0; int indexKer = 0; for( int ki = 0; ki < kernel.width; ki++ ) { int indexSrc = indexSrcStart+ki*src.stride + band; for( int kj = 0; kj < kernel.width; kj++ ) { total += (dataSrc[indexSrc] & 0xFF)* dataKernel[indexKer++]; indexSrc += numBands; } } dataDst[indexDst++] = (short)total; } } } }
@Override public void setF(int index, float[] value) { for( int i = 0; i < image.getNumBands(); i++ ) { image.data[index++] = (byte)value[i]; } }
final int width = input.getWidth(); final int height = input.getHeight(); final int numBands = input.getNumBands(); input.get(j,y, pixel); for (int band = 0; band < numBands; band++) { total[band] += pixel[band]*v;
/** * Converts an NV21 image into a {@link InterleavedU8} RGB image. * * @param data Input: NV21 image data * @param width Input: NV21 image width * @param height Input: NV21 image height * @param output Output: Optional storage for output image. Can be null. */ public static InterleavedU8 nv21ToInterleaved( byte[] data , int width , int height , InterleavedU8 output ) { if( output == null ) { output = new InterleavedU8(width,height,3); } else if( output.width != width || output.height != height ) throw new IllegalArgumentException("output width and height must be "+width+" "+height); else if( output.getNumBands() != 3 ) throw new IllegalArgumentException("three bands expected"); ImplConvertNV21.nv21ToInterleaved_U8(data, output); return output; }
public static void average( InterleavedU8 from , GrayU8 to ) { final int numBands = from.getNumBands(); int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); System.arraycopy(from.data,indexFrom,to.data,indexTo,from.width); int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); int indexEndTo = indexTo + from.width; int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); int indexEndTo = indexTo + from.width; int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y);
private void checkPixelOutside( int x , int y ) { if( !binary.isInBounds(x,y)) return; binary.get(x, y, data); if( data[0] == 0 ) { if( computeDistance(x,y) ) return; // If distances are similar or its a better fit for the 1 pixels, set value to 1 if( numOut <= 2 || Math.abs(distOut - distIn) < 40 || ( distIn < distOut && distIn < maxDistance) ) { changeValue(x,y,1); // } else { // System.out.println(" check outside no change: "+distIn+" "+distOut); } } }
/** * Returns the value of the specified band in the specified pixel. * * @param x pixel coordinate. * @param y pixel coordinate. * @param band which color band in the pixel * @return an intensity value. */ @Override public int getBand(int x, int y, int band) { if (!isInBounds(x, y)) throw new ImageAccessException("Requested pixel is out of bounds."); if (band < 0 || band >= numBands) throw new ImageAccessException("Invalid band requested."); return data[getIndex(x, y, band)] & 0xFF; }
@Override public InterleavedU8 createNew(int imgWidth, int imgHeight) { if (imgWidth == -1 || imgHeight == -1) return new InterleavedU8(); return new InterleavedU8(imgWidth, imgHeight, numBands); } }
public void process( BufferedImage image , Gaussian3D_F64 model ) { binary.reshape(image.getWidth(),image.getHeight()); colorRGB.reshape(image.getWidth(),image.getHeight()); ConvertBufferedImage.convertFrom(image, colorRGB, true); GaussianColorClassifier.classify(colorRGB,model,12,binary); int color = 0xe394bb; for( int y = 0; y < binary.height; y++ ) { for( int x = 0; x < binary.width; x++ ) { int[] data = new int[1]; binary.get(x,y, data); if( data[0] != 0 ) { image.setRGB(x,y,color); } } } ShowImages.showWindow(image,"Segmented"); }
/** * For BufferedImage stored as a byte array internally it extracts an * interleaved image. The input image and the returned image will both * share the same internal data array. Using this function allows unnecessary * memory copying to be avoided. * * @param img Image whose internal data is extracted and wrapped. * @return An image whose internal data is the same as the input image. */ public static InterleavedU8 extractInterleavedU8(BufferedImage img) { if (img.getRaster() instanceof ByteInterleavedRaster && img.getType() != BufferedImage.TYPE_BYTE_INDEXED ) { ByteInterleavedRaster raster = (ByteInterleavedRaster) img.getRaster(); InterleavedU8 ret = new InterleavedU8(); ret.width = img.getWidth(); ret.height = img.getHeight(); ret.stride = raster.getScanlineStride(); ret.startIndex = raster.getDataOffset(0)-raster.getPixelStride()+1; ret.setNumBands( raster.getNumBands() ); ret.data = raster.getDataStorage(); return ret; } throw new IllegalArgumentException("Buffered image does not have an interleaved byte raster"); }
@Override public void unsafe_get(int x, int y, int[] storage) { int index = getIndex(x, y, 0); for (int i = 0; i < numBands; i++, index++) { storage[i] = data[index] & 0xFF; } }
public void mouseDragged(MouseEvent e) { // compute coordinates in the original image int x = (int)(e.getX()/scale); int y = (int)(e.getY()/scale); if( !selected.isInBounds(x,y) ) return; int drawRadius = ((Number) spinnerRadius.getValue()).intValue(); // mark pixels near the selected point ImageRectangle r = new ImageRectangle(x-drawRadius,y-drawRadius,x+drawRadius+1,y+drawRadius+1); BoofMiscOps.boundRectangleInside(selected,r); ImageMiscOps.fillRectangle(selected, (byte)1,r.x0,r.y0,r.x1-r.x0,r.y1-r.y0); // visualize these changes synchronized ( work ) { int color = 0x22FF45; for( int i = r.y0;i < r.y1; i++ ) { for( int j = r.x0; j < r.x1; j++ ) { work.setRGB(j,i,color); } } } repaint((int)(r.x0*scale),(int)(r.y0*scale),(int)((r.x1-r.x0)*scale),(int)((r.y1-r.y0)*scale)); }
private void examineChanges() { for( int i = 0; i < changeOld.size; i++ ) { ChangeInfo p = changeOld.get(i); computeDistance(p.x,p.y); binary.get(p.x,p.y, data); if( data[0] == 1 ) { if( distOut < distIn && distOut < maxDistance ) { changeValue(p.x,p.y,0); } } else { // If distances are similar or its a better fit for the 1 pixels, set value to 1 if( Math.abs(distOut - distIn) < 20 || ( distIn < distOut && distIn < maxDistance) ) { changeValue(p.x,p.y,1); } } } }
this.binary = binary; changed.reshape(binary.width,binary.height);
if (img.isSubimage()) throw new IllegalArgumentException("Sub-images are not supported for this operation");
@Override public void getF(int index, float[] value) { for( int i = 0; i < image.getNumBands(); i++ ) { value[i] = image.data[index++] & 0xFF; } }
public static void vertical(Kernel1D_S32 kernel, InterleavedU8 input, InterleavedI8 output ) { final int offset = kernel.getOffset(); final int width = input.getWidth(); final int height = input.getHeight(); final int numBands = input.getNumBands(); final int[] pixel = new int[ numBands ]; final int[] total = new int[ numBands ]; for (int y = 0; y < height; y++) { for( int x = 0; x < width; x++ ) { Arrays.fill(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); input.get(x,i, pixel); for (int band = 0; band < numBands; band++) { total[band] += pixel[band]*v; } weight += v; } for (int band = 0; band < numBands; band++) { total[band] = (total[band]+weight/2)/weight; } output.set(x,y, total); } } }