/** * Converts a buffered image into an image of the specified type. In a 'dst' image is provided * it will be used for output, otherwise a new image will be created. */ public static <T extends ImageGray> T convertFromSingle(BufferedImage src, T dst, Class<T> type) { if (type == GrayU8.class) { return (T) convertFrom(src, (GrayU8) dst); } else if( GrayI16.class.isAssignableFrom(type) ) { return (T) convertFrom(src, (GrayI16) dst,(Class)type); } else if (type == GrayF32.class) { return (T) convertFrom(src, (GrayF32) dst); } else { throw new IllegalArgumentException("Unknown type " + type); } }
public void process(BufferedImage input) { this.input = input; grayImage = ConvertBufferedImage.convertFromSingle(input, null, imageType); workImage = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_BGR); SwingUtilities.invokeLater(new Runnable() { public void run() { doRefreshAll(); } }); }
private void renderPinhole() { if( distortImage == null ) return; imageRendered.reshape(pinholeModel.width,pinholeModel.height); distortImage.apply(imageFisheye,imageRendered); bufferedRendered = ConvertBufferedImage.checkDeclare( imageRendered.width,imageRendered.height,bufferedRendered,BufferedImage.TYPE_INT_RGB); ConvertBufferedImage.convertTo(imageRendered,bufferedRendered,true); }
/** * Converts a buffered image into an image of the specified type. * * @param src Input BufferedImage which is to be converted * @param dst The image which it is being converted into * @param orderRgb If applicable, should it adjust the ordering of each color band to maintain color consistency */ public static <T extends ImageBase> void convertFrom(BufferedImage src, T dst , boolean orderRgb) { if( dst instanceof ImageGray) { ImageGray sb = (ImageGray)dst; convertFromSingle(src, sb, (Class<ImageGray>) sb.getClass()); } else if( dst instanceof Planar) { Planar ms = (Planar)dst; convertFromMulti(src,ms,orderRgb,ms.getBandType()); } else if( dst instanceof ImageInterleaved ) { convertFromInterleaved(src, (ImageInterleaved) dst, orderRgb); } else { throw new IllegalArgumentException("Unknown type " + dst.getClass().getSimpleName()); } }
@Override public void processImage(int sourceID, long frameID, final BufferedImage buffered, ImageBase input) { System.out.flush(); original = ConvertBufferedImage.checkCopy(buffered,original); work = ConvertBufferedImage.checkDeclare(buffered,work); final double timeInSeconds; synchronized (lockProcessing) { long before = System.nanoTime(); detector.process((GrayF32)input); long after = System.nanoTime(); timeInSeconds = (after-before)*1e-9; } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { controls.setProcessingTime(timeInSeconds); viewUpdated(); } }); }
private void undoRadialDistortion(BufferedImage image) { if( undoRadial == null ) return; ConvertBufferedImage.convertFrom(image,origMS,true); if( correctedMS.getNumBands() != origMS.getNumBands() ) correctedMS.setNumberOfBands(origMS.getNumBands()); correctedMS.reshape(origMS.width,origMS.height); for( int i = 0; i < origMS.getNumBands(); i++ ) { GrayF32 in = origMS.getBand(i); GrayF32 out = correctedMS.getBand(i); undoRadial.apply(in,out); } undistorted = ConvertBufferedImage.checkDeclare(origMS.width,origMS.height,undistorted,image.getType()); ConvertBufferedImage.convertTo(correctedMS,undistorted,true); }
Planar<GrayU8> color = ConvertBufferedImage.convertFromMulti(img,null,true,GrayU8.class); savePPM(color, fileName, null); } else if( fileName.endsWith("pgm") || fileName.endsWith("PGM") ) { GrayU8 gray = ConvertBufferedImage.convertFrom(img, (GrayU8) null); savePGM(gray, fileName); }else
private void undoRadialDistortion(BufferedImage image) { ConvertBufferedImage.convertFromMulti(image, origMS,true, GrayF32.class); for( int i = 0; i < origMS.getNumBands(); i++ ) { GrayF32 in = origMS.getBand(i); GrayF32 out = correctedMS.getBand(i); undoRadial.apply(in,out); } if( correctedMS.getNumBands() == 3 ) ConvertBufferedImage.convertTo(correctedMS,undistorted,true); else if( correctedMS.getNumBands() == 1 ) ConvertBufferedImage.convertTo(correctedMS.getBand(0),undistorted); else throw new RuntimeException("What kind of image has "+correctedMS.getNumBands()+"???"); }
private void setDescriptorInput() { if( describe != null ) { if( describe.getImageType().getFamily() == ImageType.Family.GRAY) { T input = ConvertBufferedImage.convertFromSingle(image, null, imageType); describe.setImage(input); } else { Planar<T> input = ConvertBufferedImage.convertFromPlanar(image, null, true, imageType); describe.setImage(input); } } }
@Override protected void handleInputChange(int source, InputMethod method, int width, int height) { imageBinary.reshape(width, height); visualizedBinary = ConvertBufferedImage.checkDeclare(width, height, visualizedBinary,BufferedImage.TYPE_INT_BGR); }
public static void main(String[] args) { BufferedImage original = UtilImageIO.loadImage(UtilIO.pathExample("simple_objects.jpg")); Planar<GrayF32> input = new Planar<>(GrayF32.class, original.getWidth(),original.getHeight(),3); ConvertBufferedImage.convertFromPlanar(original,input,true,GrayF32.class); Planar<GrayF32> output = new Planar<>(GrayF32.class, original.getWidth()/3,original.getHeight()/3,3); Planar<GrayF32> output2 = new Planar<>(GrayF32.class, original.getWidth()/3,original.getHeight()/3,3); AverageDownSampleOps.down(input, output); new FDistort(input,output2).scaleExt().apply(); BufferedImage outputFull = ConvertBufferedImage.convertTo_F32(output, null, true); BufferedImage outputFull2 = ConvertBufferedImage.convertTo_F32(output2, null, true); ShowImages.showWindow(original,"Original"); ShowImages.showWindow(outputFull,"3x small average"); ShowImages.showWindow(outputFull2,"3x small bilinear"); } }
public void process( final BufferedImage image ) { input.reshape(image.getWidth(),image.getHeight()); output.reshape(image.getWidth(),image.getHeight()); ConvertBufferedImage.convertFromPlanar(image, input,true, GrayF32.class); SwingUtilities.invokeLater(new Runnable() { public void run() { setInputImage(image); doRefreshAll(); }}); }
public void setSelected( int selected ) { this.selectedImage = selected; this.isUndistorted = false; if( origMS == null ) { BufferedImage image = images.get(selected); // the number of bands can be difficult to ascertain without digging deep into the data structure // so just declare a new one using convert origMS = ConvertBufferedImage.convertFromMulti(image,null,true,GrayF32.class); correctedMS = ConvertBufferedImage.convertFromMulti(image,null,true,GrayF32.class); undistorted = new BufferedImage(image.getWidth(),image.getHeight(),image.getType()); } }
ConvertBufferedImage.convertFrom(buff[0], color1, true); ConvertBufferedImage.convertFrom(buff[1], color2, true); rectColor1, rectColor2,rectMask, rectifiedK, rectifiedR); visualRect1 = ConvertBufferedImage.checkDeclare( rectColor1.width, rectColor1.height, visualRect1, visualRect1.getType()); visualRect2 = ConvertBufferedImage.checkDeclare( rectColor2.width, rectColor2.height, visualRect2, visualRect2.getType()); ConvertBufferedImage.convertTo(rectColor1, visualRect1, true); ConvertBufferedImage.convertTo(rectColor2, visualRect2, true); BoofSwingUtil.invokeNowOrLater(() -> { rectifiedPanel.setImages(visualRect1, visualRect2); visualDisparity = ConvertBufferedImage.checkDeclare( disparity.width,disparity.height,visualDisparity,visualDisparity.getType());
public static <T extends ImageBase> T convertFrom(BufferedImage src , boolean orderRgb , T output ) { ImageType<T> imageType = output.getImageType(); switch( imageType.getFamily() ) { case GRAY: convertFromSingle(src, (ImageGray)output, imageType.getImageClass()); break; case PLANAR: convertFromMulti(src, (Planar) output, orderRgb, imageType.getImageClass()); break; case INTERLEAVED: convertFromInterleaved(src, (ImageInterleaved) output, orderRgb); break; default: throw new RuntimeException("Not supported yet"); } return output; }
@Override public void processImage(int sourceID, long frameID, final BufferedImage buffered, ImageBase input) { synchronized (this) { original = ConvertBufferedImage.checkCopy(buffered, original); work = ConvertBufferedImage.checkDeclare(buffered, work); } // SwingUtilities.invokeLater(new Runnable() { // @Override // public void run() { // Dimension d = guiImage.getPreferredSize(); // if( d.getWidth() < buffered.getWidth() || d.getHeight() < buffered.getHeight() ) { // guiImage.setPreferredSize(new Dimension(buffered.getWidth(), buffered.getHeight())); // } // }}); synchronized (this) { inputToBinary.process((T)input,binary); contourAlg.process(binary,labeled); } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { viewUpdated(); } }); }
@Override public synchronized void paintComponent(Graphics g) { super.paintComponent(g); projectScene(); imageOutput = ConvertBufferedImage.checkDeclare(imageRgb.width,imageRgb.height,imageOutput,BufferedImage.TYPE_INT_RGB); DataBufferInt buffer = (DataBufferInt)imageOutput.getRaster().getDataBuffer(); System.arraycopy(imageRgb.data,0,buffer.getData(),0,imageRgb.width*imageRgb.height); g.drawImage(imageOutput,0,0,null); }
/** * The actual classification of the images by using the pre-trained model. */ private static Entry<String, Double> classifyWithModel(ImageClassifierVggCifar10 classifier, BufferedImage image) { Planar<GrayF32> planar = new Planar<>(GrayF32.class, image.getWidth(), image.getHeight(), 3); ConvertBufferedImage.convertFromPlanar(image, planar, true, GrayF32.class); classifier.classify(planar); return classifier.getAllResults().stream() .map(score -> entry(classifier.getCategories().get(score.category), score.score)) .max(Comparator.comparing(Entry::getValue)).get(); }
@Override public T next() { if( bufferedImage == null ) bufferedImage = webcam.getImage(); ConvertBufferedImage.convertFrom(bufferedImage, output, true); return output; }