Difference between revisions of "Example Image Convert"
From BoofCV
Jump to navigationJump to searchm |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<center> | <center> | ||
<gallery widths=200px heights=200px> | <gallery widths=200px heights=200px> | ||
Line 12: | Line 10: | ||
Example Code: | Example Code: | ||
* [https://github.com/lessthanoptimal/BoofCV/blob/v0. | * [https://github.com/lessthanoptimal/BoofCV/blob/v0.40/examples/src/main/java/boofcv/examples/imageprocessing/ExampleImageConvert.java ExampleImageConvert.java] | ||
Concepts: | Concepts: | ||
Line 27: | Line 25: | ||
*/ | */ | ||
public class ExampleImageConvert { | public class ExampleImageConvert { | ||
// image loaded from a file | // image loaded from a file | ||
BufferedImage image; | BufferedImage image; | ||
// gray scale image with element values from 0 to 255 | // gray scale image with element values from 0 to 255 | ||
GrayU8 gray; | |||
// Derivative of gray image. | // Derivative of gray image. Elements are 16-bit signed integers | ||
GrayS16 derivX, derivY; | |||
void convert() { | void convert() { | ||
// Converting between BoofCV image types is easy with ConvertImage. | // Converting between BoofCV image types is easy with ConvertImage. ConvertImage copies | ||
// the value of a pixel in one image into another image. | // 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. | // 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 | // 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); | ConvertImage.convert(gray, imageU16); | ||
// You can convert back into the 8-bit image from the 16-bit image with no problem | // 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 | // in this situation because imageU16 does not use the full range of 16-bit values | ||
ConvertImage.convert(imageU16,gray); | ConvertImage.convert(imageU16, gray); | ||
// Here is an example where you over flow the image after converting | // 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 | // 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); | ConvertImage.convert(derivX, imageBad); | ||
// One way to get around this problem rescale and adjust the pixel values so that they | // One way to get around this problem rescale and adjust the pixel values so that they | ||
// will be within a valid range. | // will be within a valid range. | ||
var scaledAbs = new GrayS16(derivX.width, derivX.height); | |||
GPixelMath.abs(derivX,scaledAbs); | GPixelMath.abs(derivX, scaledAbs); | ||
GPixelMath.multiply(scaledAbs, 255.0 / ImageStatistics.max(scaledAbs), 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 | // If you just want to see the values of a 16-bit image there are built in utility functions | ||
Line 66: | Line 63: | ||
// ConvertBufferedImage is similar to ImageConvert in that it does a direct coversion with out | // ConvertBufferedImage is similar to ImageConvert in that it does a direct coversion with out | ||
// adjusting the pixel's value | // 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); | |||
ShowImages.showWindow( | |||
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); | |||
} | } | ||
Line 75: | Line 77: | ||
*/ | */ | ||
public void createImages() { | public void createImages() { | ||
image = UtilImageIO. | image = UtilImageIO.loadImageNotNull(UtilIO.pathExample("standard/barbara.jpg")); | ||
gray = ConvertBufferedImage.convertFromSingle(image, null, | gray = ConvertBufferedImage.convertFromSingle(image, null, GrayU8.class); | ||
derivX = GeneralizedImageOps.createSingleBand( | derivX = GeneralizedImageOps.createSingleBand(GrayS16.class, gray.getWidth(), gray.getHeight()); | ||
derivY = GeneralizedImageOps.createSingleBand( | derivY = GeneralizedImageOps.createSingleBand(GrayS16.class, gray.getWidth(), gray.getHeight()); | ||
GImageDerivativeOps. | GImageDerivativeOps.gradient(DerivativeType.SOBEL, gray, derivX, derivY, BorderType.EXTENDED); | ||
} | } | ||
public static void main( String | public static void main( String[] args ) { | ||
var app = new ExampleImageConvert(); | |||
app.createImages(); | app.createImages(); | ||
app.convert(); | app.convert(); |
Latest revision as of 15:01, 17 January 2022
Demonstrates how to convert between different types of BoofCV image types. When converting images into different types one needs to make sure the image which is being written into can store the full range of values of the original image, otherwise the output will be garbage. It is also demonstrated how to visualize a signed image using a built in function.
Example Code:
Concepts:
- Images
- Visualization
Example Code
/**
* Demonstrates how to convert between different BoofCV image types.
*
* @author Peter Abeles
*/
public class ExampleImageConvert {
// image loaded from a file
BufferedImage image;
// gray scale image with element values from 0 to 255
GrayU8 gray;
// Derivative of gray image. Elements are 16-bit signed integers
GrayS16 derivX, derivY;
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);
}
/**
* Load and generate images
*/
public void createImages() {
image = UtilImageIO.loadImageNotNull(UtilIO.pathExample("standard/barbara.jpg"));
gray = ConvertBufferedImage.convertFromSingle(image, null, GrayU8.class);
derivX = GeneralizedImageOps.createSingleBand(GrayS16.class, gray.getWidth(), gray.getHeight());
derivY = GeneralizedImageOps.createSingleBand(GrayS16.class, gray.getWidth(), gray.getHeight());
GImageDerivativeOps.gradient(DerivativeType.SOBEL, gray, derivX, derivY, BorderType.EXTENDED);
}
public static void main( String[] args ) {
var app = new ExampleImageConvert();
app.createImages();
app.convert();
}
}