Difference between revisions of "Example Planar Image"

From BoofCV
Jump to navigationJump to search
(Created page for MultiSpectral example)
 
(updated with much more info)
Line 1: Line 1:
= MultiSpectral Images =
= MultiSpectral Images =


MultiSpectral are used to store color images where each color or band is stored as an independent gray scale image.  This allows for each band in the image to be independently processed or modified easily.
MultiSpectral are used to store color images where each color or band is stored as an independent gray scale image.  This allows for each band in the image to be independently processed or modified easily.  MultiSpectral images are different from how color images are most commonly stored, which is in an interleaved format.  The advantage of using independent gray scale images for each band is that they can be easily processed by algorithms written for ImageSingleBand.


Example code detailing how to convert, use, and manipulate MultiSpectral images is provided here:
In the example code below it is demonstrated how to convert MultiSpectral to and from BufferedImages, how to process each band in a MultiSpectral independently, and how to access the values of pixels inside the MultiSpectral image.


[https://github.com/lessthanoptimal/BoofCV/blob/master/examples/src/boofcv/examples/ExampleMultiSpectralImages.java MultiSpectral Example Code]
Example File: [https://github.com/lessthanoptimal/BoofCV/blob/master/examples/src/boofcv/examples/ExampleMultiSpectralImages.java ExampleMultiSpectralImages.java]


For now only a reference to the example code is provided because the code is not compatible with the current "stable" release, v0.3Just after the release of v0.3 a bug was discovered where the color bands were not properly ordered when converting a MultiSpectral image into a BGR BufferedImage.  The fix for this issue required a change the API and how ConvertRaster behavesTo get access to this fix check out the source code from github.
Concepts:
* Color image processing
 
= Example Code =
 
 
<syntaxhighlight lang="java">
/**
* Many operations designed to only work on {@link boofcv.struct.image.ImageSingleBand} can be applied
* to a MultiSpectral image by feeding in each band one at a time.
*/
public static void independent( BufferedImage input ) {
// convert the BufferedImage into a MultiSpectral
MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);
 
// declare the output blurred image
MultiSpectral<ImageUInt8> blurred =
new MultiSpectral<ImageUInt8>(ImageUInt8.class,image.width,image.height,image.getNumBands());
// Apply Gaussian blur to each band in the image
for( int i = 0; i < image.getNumBands(); i++ ) {
// note that the generalized version of BlurImageOps is not being used, but the type
// specific version.
BlurImageOps.gaussian(image.getBand(i),blurred.getBand(i),-1,5,null);
}
// Declare the BufferedImage manually to ensure that the color bands have the same ordering on input
// and output
BufferedImage output = new BufferedImage(image.width,image.height,input.getType());
ConvertBufferedImage.convertTo(blurred, output);
ShowImages.showWindow(input,"Input");
ShowImages.showWindow(output,"Ouput");
}
 
/**
* BufferedImage support many different image formats internallyMore often than not the order
* of its bands are not RGB, which can cause problems when you expect it to be RGB. A function
* is provided that will swap the bands of a MultiSpectral image created from a BufferedImage
* to ensure that it is in RGB ordering.
*/
public static void convertBufferedToRGB( BufferedImage input ) {
 
// convert the BufferedImage into a MultiSpectral
MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);
 
int x=15,y=15;
// print the value of "red" at a pixel to make it easy to see the change
System.out.print("before: ");
for( int i = 0; i < image.getNumBands(); i++ )
System.out.print(image.getBand(i).get(x,y)+" ");
System.out.println();
// change the bands
ConvertBufferedImage.orderBandsIntoRGB(image,input);
 
// THe value of red should be different of the BufferedImage was not in RGB format.
System.out.print("After: ");
for( int i = 0; i < image.getNumBands(); i++ )
System.out.print(image.getBand(i).get(x,y)+" ");
System.out.println();
}
 
/**
* Values of pixels can be read and modified by accessing the internal {@link boofcv.struct.image.ImageSingleBand}.
*/
public static void pixelAccess(  BufferedImage input ) {
// convert the BufferedImage into a MultiSpectral
MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);
 
int x = 10, y = 10;
 
// to access a pixel you first access the gray image for the each band
for( int i = 0; i < image.getNumBands(); i++ )
System.out.println("Original "+i+" = "+image.getBand(i).get(x,y));
 
// change the value in each band
for( int i = 0; i < image.getNumBands(); i++ )
image.getBand(i).set(x, y, 100 + i);
 
// to access a pixel you first access the gray image for the each band
for( int i = 0; i < image.getNumBands(); i++ )
System.out.println("Result  "+i+" = "+image.getBand(i).get(x,y));
}
 
/**
* There is no real perfect way that everyone agrees on for converting color images into gray scale
* imagesTwo examples of how to convert a MultiSpectral image into a gray scale image are shown
* in this example.
*/
public static void convertToGray( BufferedImage input ) {
// convert the BufferedImage into a MultiSpectral
MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);
ImageUInt8 gray = new ImageUInt8( image.width,image.height);
// creates a gray scale image by averaging intensity value across pixels
GPixelMath.bandAve(image, gray);
BufferedImage outputAve = ConvertBufferedImage.convertTo(gray,null);
 
// create an output image just from the first band
BufferedImage outputBand0 = ConvertBufferedImage.convertTo(image.getBand(0),null);
 
ShowImages.showWindow(outputAve,"Average");
ShowImages.showWindow(outputBand0,"Band 0");
}
public static void main( String args[] ) {
BufferedImage input = UtilImageIO.loadImage("../data/applet/apartment_building_02.jpg");
 
// Uncomment lines below to run each example
 
ExampleMultiSpectralImages.independent(input);
// ExampleMultiSpectralImages.convertBufferedToRGB(input);
// ExampleMultiSpectralImages.pixelAccess(input);
// ExampleMultiSpectralImages.convertToGray(input);
}
</syntaxhighlight>

Revision as of 13:23, 6 January 2012

MultiSpectral Images

MultiSpectral are used to store color images where each color or band is stored as an independent gray scale image. This allows for each band in the image to be independently processed or modified easily. MultiSpectral images are different from how color images are most commonly stored, which is in an interleaved format. The advantage of using independent gray scale images for each band is that they can be easily processed by algorithms written for ImageSingleBand.

In the example code below it is demonstrated how to convert MultiSpectral to and from BufferedImages, how to process each band in a MultiSpectral independently, and how to access the values of pixels inside the MultiSpectral image.

Example File: ExampleMultiSpectralImages.java

Concepts:

  • Color image processing

Example Code

	/**
	 * Many operations designed to only work on {@link boofcv.struct.image.ImageSingleBand} can be applied
	 * to a MultiSpectral image by feeding in each band one at a time.
	 */
	public static void independent( BufferedImage input ) {
		// convert the BufferedImage into a MultiSpectral
		MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);

		// declare the output blurred image
		MultiSpectral<ImageUInt8> blurred =
				new MultiSpectral<ImageUInt8>(ImageUInt8.class,image.width,image.height,image.getNumBands());
		
		// Apply Gaussian blur to each band in the image
		for( int i = 0; i < image.getNumBands(); i++ ) {
			// note that the generalized version of BlurImageOps is not being used, but the type
			// specific version.
			BlurImageOps.gaussian(image.getBand(i),blurred.getBand(i),-1,5,null);
		}
		
		// Declare the BufferedImage manually to ensure that the color bands have the same ordering on input
		// and output
		BufferedImage output = new BufferedImage(image.width,image.height,input.getType());
		ConvertBufferedImage.convertTo(blurred, output);
		ShowImages.showWindow(input,"Input");
		ShowImages.showWindow(output,"Ouput");
	}

	/**
	 * BufferedImage support many different image formats internally.  More often than not the order
	 * of its bands are not RGB, which can cause problems when you expect it to be RGB.  A function
	 * is provided that will swap the bands of a MultiSpectral image created from a BufferedImage
	 * to ensure that it is in RGB ordering.
	 */
	public static void convertBufferedToRGB( BufferedImage input ) {

		// convert the BufferedImage into a MultiSpectral
		MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);

		int x=15,y=15;
		
		// print the value of "red" at a pixel to make it easy to see the change
		System.out.print("before: ");
		for( int i = 0; i < image.getNumBands(); i++ )
			System.out.print(image.getBand(i).get(x,y)+" ");
		System.out.println();
		
		// change the bands
		ConvertBufferedImage.orderBandsIntoRGB(image,input);

		// THe value of red should be different of the BufferedImage was not in RGB format.
		System.out.print("After:  ");
		for( int i = 0; i < image.getNumBands(); i++ )
			System.out.print(image.getBand(i).get(x,y)+" ");
		System.out.println();
	}

	/**
	 * Values of pixels can be read and modified by accessing the internal {@link boofcv.struct.image.ImageSingleBand}.
	 */
	public static void pixelAccess(  BufferedImage input ) {
		// convert the BufferedImage into a MultiSpectral
		MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);

		int x = 10, y = 10;

		// to access a pixel you first access the gray image for the each band
		for( int i = 0; i < image.getNumBands(); i++ )
			System.out.println("Original "+i+" = "+image.getBand(i).get(x,y));

		// change the value in each band
		for( int i = 0; i < image.getNumBands(); i++ )
			image.getBand(i).set(x, y, 100 + i);

		// to access a pixel you first access the gray image for the each band
		for( int i = 0; i < image.getNumBands(); i++ )
			System.out.println("Result   "+i+" = "+image.getBand(i).get(x,y));
	}

	/**
	 * There is no real perfect way that everyone agrees on for converting color images into gray scale
	 * images.  Two examples of how to convert a MultiSpectral image into a gray scale image are shown 
	 * in this example.
	 */
	public static void convertToGray( BufferedImage input ) {
		// convert the BufferedImage into a MultiSpectral
		MultiSpectral<ImageUInt8> image = ConvertBufferedImage.convertFromMulti(input,null,ImageUInt8.class);
		
		ImageUInt8 gray = new ImageUInt8( image.width,image.height);
		
		// creates a gray scale image by averaging intensity value across pixels
		GPixelMath.bandAve(image, gray);
		BufferedImage outputAve = ConvertBufferedImage.convertTo(gray,null);

		// create an output image just from the first band
		BufferedImage outputBand0 = ConvertBufferedImage.convertTo(image.getBand(0),null);

		ShowImages.showWindow(outputAve,"Average");
		ShowImages.showWindow(outputBand0,"Band 0");
	}
	
	public static void main( String args[] ) {
		BufferedImage input = UtilImageIO.loadImage("../data/applet/apartment_building_02.jpg");

		// Uncomment lines below to run each example

		ExampleMultiSpectralImages.independent(input);
//		ExampleMultiSpectralImages.convertBufferedToRGB(input);
//		ExampleMultiSpectralImages.pixelAccess(input);
//		ExampleMultiSpectralImages.convertToGray(input);
	}