Search in sources :

Example 1 with ConvertBufferedImage

use of boofcv.io.image.ConvertBufferedImage in project BoofCV by lessthanoptimal.

the class ExampleImageConvert method convert.

void convert() {
    // Converting between BoofCV image types is easy with ConvertImage. ConvertImage copies
    // the value of a pixel in one image into another image. When doing so you need to take
    // in account the storage capabilities of these different class types.
    // Going from an unsigned 8-bit image to unsigned 16-bit image is no problem
    var imageU16 = new GrayU16(gray.width, gray.height);
    ConvertImage.convert(gray, imageU16);
    // You can convert back into the 8-bit image from the 16-bit image with no problem
    // in this situation because imageU16 does not use the full range of 16-bit values
    ConvertImage.convert(imageU16, gray);
    // Here is an example where you over flow the image after converting
    // There won't be an exception or any error messages but the output image will be corrupted
    var imageBad = new GrayU8(derivX.width, derivX.height);
    ConvertImage.convert(derivX, imageBad);
    // One way to get around this problem rescale and adjust the pixel values so that they
    // will be within a valid range.
    var scaledAbs = new GrayS16(derivX.width, derivX.height);
    GPixelMath.abs(derivX, scaledAbs);
    GPixelMath.multiply(scaledAbs, 255.0 / ImageStatistics.max(scaledAbs), scaledAbs);
    // If you just want to see the values of a 16-bit image there are built in utility functions
    // for visualizing their values too
    BufferedImage colorX = VisualizeImageData.colorizeSign(derivX, null, -1);
    // Let's see what all the bad image looks like
    // ConvertBufferedImage is similar to ImageConvert in that it does a direct coversion with out
    // adjusting the pixel's value
    var outBad = new BufferedImage(imageBad.width, imageBad.height, BufferedImage.TYPE_INT_RGB);
    var outScaled = new BufferedImage(imageBad.width, imageBad.height, BufferedImage.TYPE_INT_RGB);
    var panel = new ListDisplayPanel();
    panel.addImage(ConvertBufferedImage.convertTo(scaledAbs, outScaled), "Scaled");
    panel.addImage(colorX, "Visualized");
    panel.addImage(ConvertBufferedImage.convertTo(imageBad, outBad), "Bad");
    ShowImages.showWindow(panel, "Image Convert", true);
}
Also used : ListDisplayPanel(boofcv.gui.ListDisplayPanel) GrayU16(boofcv.struct.image.GrayU16) GrayS16(boofcv.struct.image.GrayS16) GrayU8(boofcv.struct.image.GrayU8) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Aggregations

ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 GrayS16 (boofcv.struct.image.GrayS16)1 GrayU16 (boofcv.struct.image.GrayU16)1 GrayU8 (boofcv.struct.image.GrayU8)1 BufferedImage (java.awt.image.BufferedImage)1