Example Image Convert

From BoofCV
Revision as of 13:58, 22 July 2012 by Peter (talk | contribs) (Created page with "= Converting Image Types = <center> <gallery widths=200px heights=200px> File:Example_convert_bad.jpg | Bad conversion from 16 to 8 bit image. File:Example_convert_scaled.jpg...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Converting Image Types

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
	ImageUInt8 gray;
	// Derivative of gray image.  Elements are 16-bit signed integers
	ImageSInt16 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
		ImageUInt16 imageU16 = new ImageUInt16(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
		ImageUInt8 imageBad = new ImageUInt8(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.
		ImageSInt16 scaledAbs = new ImageSInt16(derivX.width,derivX.height);
		GPixelMath.abs(derivX,scaledAbs);
		GPixelMath.multiply(scaledAbs,scaledAbs,255/GPixelMath.max(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
		ShowImages.showWindow(ConvertBufferedImage.convertTo(imageBad,null),"Bad Conversion");
		ShowImages.showWindow(ConvertBufferedImage.convertTo(scaledAbs,null),"Scaled");
		ShowImages.showWindow(colorX,"Visualized");
	}

	/**
	 * Load and generate images
	 */
	public void createImages() {
		image = UtilImageIO.loadImage("../data/evaluation/standard/barbara.png");

		gray = ConvertBufferedImage.convertFromSingle(image, null, ImageUInt8.class);
		derivX = GeneralizedImageOps.createSingleBand(ImageSInt16.class, gray.getWidth(), gray.getHeight());
		derivY = GeneralizedImageOps.createSingleBand(ImageSInt16.class, gray.getWidth(), gray.getHeight());

		GImageDerivativeOps.sobel(gray,derivX,derivY, BorderType.EXTENDED);
	}

	public static void main( String args[] ) {
		ExampleImageConvert app = new ExampleImageConvert();

		app.createImages();
		app.convert();
	}
}