Difference between revisions of "Example Image Pyramid"

From BoofCV
Jump to navigationJump to search
m
m
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Image Pyramid Example =
<center>
<center>
<gallery widths=300px heights=200px>
<gallery widths=300px heights=200px>
Line 9: Line 8:
[http://en.wikipedia.org/wiki/Pyramid_%28image_processing Image pyramids] are a common way to represent multi-resolution image information.  In an image pyramid, upper layers are lower resolution versions of the lower layers.  BoofCV provides two types of image pyramids built in; PyramidDiscrete and PyramidFloat.  PyramidDiscrete only allows the ratio between adjacent to have positive integer values, while PyramidFloat allows any arbitrary positive value.  Discrete pyramids are much faster than float pyramids, but much more restrictive.
[http://en.wikipedia.org/wiki/Pyramid_%28image_processing Image pyramids] are a common way to represent multi-resolution image information.  In an image pyramid, upper layers are lower resolution versions of the lower layers.  BoofCV provides two types of image pyramids built in; PyramidDiscrete and PyramidFloat.  PyramidDiscrete only allows the ratio between adjacent to have positive integer values, while PyramidFloat allows any arbitrary positive value.  Discrete pyramids are much faster than float pyramids, but much more restrictive.


Two code examples are provided below which demonstrate how to work with each type of pyramid.
Inside of BoofCV several algorithms make use of these two types of pyramids.  The KLT feature tracker uses a discrete pyramid and is the fastest feature tracker.  Pyramid based scale space feature detectors use the float pyramid. 
 
Two code examples are provided below which demonstrate how to construct and visualize each type of pyramid.


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/master/examples/src/boofcv/examples/ExamplePyramidDiscrete.java ExamplePyramidDiscrete.java ]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.25/examples/src/boofcv/examples/imageprocessing/ExamplePyramidDiscrete.java ExamplePyramidDiscrete.java ]
* WRITE FLOAT EXAMPLE
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.25/examples/src/boofcv/examples/imageprocessing/ExamplePyramidFloat.java ExamplePyramidFloat.java ]


Concepts:
Concepts:
Line 23: Line 24:
* [[Applet_Pyramid_Discrete| Discrete Pyramid]]
* [[Applet_Pyramid_Discrete| Discrete Pyramid]]
* [[Applet_Pyramid_Float| Float Pyramid]]
* [[Applet_Pyramid_Float| Float Pyramid]]
* [[Applet_Scale_Space_Point| Scale-Space Feature Detection]]


= Image Pyramid Discrete =
= Image Pyramid Discrete =
Line 35: Line 34:
  * @author Peter Abeles
  * @author Peter Abeles
  */
  */
public class ExamplePyramidDiscrete<T extends ImageBase> {
public class ExamplePyramidDiscrete<T extends ImageGray> {


// specifies the image type
// specifies the image type
Line 41: Line 40:
// The pyramid data structure
// The pyramid data structure
PyramidDiscrete<T> pyramid;
PyramidDiscrete<T> pyramid;
// update the pyramid given from
PyramidUpdaterDiscrete<T> updater;


public ExamplePyramidDiscrete(Class<T> imageType) {
public ExamplePyramidDiscrete(Class<T> imageType) {
Line 53: Line 50:
public void standard() {
public void standard() {
// Each level in the pyramid must have a ratio with the previously layer that is an integer value
// Each level in the pyramid must have a ratio with the previously layer that is an integer value
pyramid = new PyramidDiscrete<T>(imageType,true,1,2,4,8);
pyramid = FactoryPyramid.discreteGaussian(new int[]{1,2,4,8},-1,2,true,imageType);
 
// In most cases sub-sampling with a Gaussian is preferred
updater = FactoryPyramid.discreteGaussian(imageType,-1,2);
}
}


Line 64: Line 58:
public void unusual() {
public void unusual() {
// Note that the first level does not have to be one
// Note that the first level does not have to be one
pyramid = new PyramidDiscrete<T>(imageType,true,2,6);
pyramid = FactoryPyramid.discreteGaussian(new int[]{2,6},-1,2,true,imageType);


// Other kernels can also be used besides Gaussian
// Other kernels can also be used besides Gaussian
Line 73: Line 67:
kernel = FactoryKernel.table1D_I32(2);
kernel = FactoryKernel.table1D_I32(2);
}
}
updater = new PyramidUpdateIntegerDown<T>(kernel,imageType);
}
}


Line 81: Line 73:
*/
*/
public void process( BufferedImage image ) {
public void process( BufferedImage image ) {
T input = ConvertBufferedImage.convertFrom(image,null,imageType);
T input = ConvertBufferedImage.convertFromSingle(image, null, imageType);
updater.update(input,pyramid);
pyramid.process(input);


DiscretePyramidPanel gui = new DiscretePyramidPanel();
DiscretePyramidPanel gui = new DiscretePyramidPanel();
Line 89: Line 81:


ShowImages.showWindow(gui,"Image Pyramid");
ShowImages.showWindow(gui,"Image Pyramid");
// To get an image at any of the scales simply call this get function
T imageAtScale = pyramid.getLayer(1);
ShowImages.showWindow(ConvertBufferedImage.convertTo(imageAtScale,null,true),"Image at layer 1");
}
}


public static void main( String[] args ) {
public static void main( String[] args ) {
BufferedImage image = UtilImageIO.loadImage("data/standard/barbara.png");
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("standard/barbara.jpg"));


ExamplePyramidDiscrete<ImageFloat32> app = new ExamplePyramidDiscrete<ImageFloat32>(ImageFloat32.class);
ExamplePyramidDiscrete<GrayF32> app = new ExamplePyramidDiscrete<GrayF32>(GrayF32.class);
// ExamplePyramidDiscrete<ImageUInt8> app = new ExamplePyramidDiscrete<ImageUInt8>(ImageUInt8.class);
// ExamplePyramidDiscrete<GrayU8> app = new ExamplePyramidDiscrete<GrayU8>(GrayU8.class);


app.standard();
app.standard();
Line 108: Line 104:
= Image Pyramid Float =
= Image Pyramid Float =


NEED TO WRITE THIS
<syntaxhighlight lang="java">
/**
* Demonstrates how to construct and display a {@link PyramidFloat}.  Float pyramids require only require
* that each layer's scale be larger than the scale of the previous layer. Interpolation is used to allow
* sub-sampling at arbitrary scales.  All of this additional flexibility comes at the cost of speed
* when compared to a {@link PyramidDiscrete}.
*
* @author Peter Abeles
*/
public class ExamplePyramidFloat<T extends ImageGray> {
 
// specifies the image type
Class<T> imageType;
// The pyramid data structure
PyramidFloat<T> pyramid;
 
public ExamplePyramidFloat(Class<T> imageType) {
this.imageType = imageType;
}
 
/**
* Creates a fairly standard pyramid and updater.
*/
public void standard() {
// Scale factory for each layer can be any floating point value which is larger than
// the previous layer's scale.
double scales[] = new double[]{1,1.5,2,2.5,3,5,8,15};
// the amount of blur which is applied to each layer in the pyramid after the previous layer has been sampled
double sigmas[] = new double[]{1,1,1,1,1,1,1,1};
pyramid = FactoryPyramid.floatGaussian(scales,sigmas,imageType);
}
 
/**
* Updates and displays the pyramid.
*/
public void process( BufferedImage image ) {
T input = ConvertBufferedImage.convertFromSingle(image, null, imageType);
pyramid.process(input);
 
ImagePyramidPanel<T> gui = new ImagePyramidPanel<>();
gui.set(pyramid, true);
gui.render();
 
ShowImages.showWindow(gui,"Image Pyramid Float");
 
// To get an image at any of the scales simply call this get function
T imageAtScale = pyramid.getLayer(1);
 
ShowImages.showWindow(ConvertBufferedImage.convertTo(imageAtScale,null,true),"Image at layer 1");
}
 
 
public static void main( String[] args ) {
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("standard/barbara.jpg"));
 
ExamplePyramidFloat<GrayF32> app = new ExamplePyramidFloat<>(GrayF32.class);
// ExamplePyramidFloat<GrayU8> app = new ExamplePyramidFloat<>(GrayU8.class);
 
app.standard();
app.process(image);
}
}
</syntaxhighlight>

Revision as of 16:49, 1 December 2016

Image pyramids are a common way to represent multi-resolution image information. In an image pyramid, upper layers are lower resolution versions of the lower layers. BoofCV provides two types of image pyramids built in; PyramidDiscrete and PyramidFloat. PyramidDiscrete only allows the ratio between adjacent to have positive integer values, while PyramidFloat allows any arbitrary positive value. Discrete pyramids are much faster than float pyramids, but much more restrictive.

Inside of BoofCV several algorithms make use of these two types of pyramids. The KLT feature tracker uses a discrete pyramid and is the fastest feature tracker. Pyramid based scale space feature detectors use the float pyramid.

Two code examples are provided below which demonstrate how to construct and visualize each type of pyramid.

Example Code:

Concepts:

  • Multi-resolution image processing
  • Discrete Vs. Float pyramids
  • Image scaling

Relevant Applets:

Image Pyramid Discrete

/**
 * Demonstrates how to construct and display a {@link PyramidDiscrete}.  Discrete pyramids require that
 * each level has a relative scale with an integer ratio and is updated by sparsely sub-sampling.  These
 * restrictions allows a very quick update across scale space.
 *
 * @author Peter Abeles
 */
public class ExamplePyramidDiscrete<T extends ImageGray> {

	// specifies the image type
	Class<T> imageType;
	// The pyramid data structure
	PyramidDiscrete<T> pyramid;

	public ExamplePyramidDiscrete(Class<T> imageType) {
		this.imageType = imageType;
	}

	/**
	 * Creates a fairly standard pyramid and updater.
	 */
	public void standard() {
		// Each level in the pyramid must have a ratio with the previously layer that is an integer value
		pyramid = FactoryPyramid.discreteGaussian(new int[]{1,2,4,8},-1,2,true,imageType);
	}

	/**
	 * Creates a more unusual pyramid and updater.
	 */
	public void unusual() {
		// Note that the first level does not have to be one
		pyramid = FactoryPyramid.discreteGaussian(new int[]{2,6},-1,2,true,imageType);

		// Other kernels can also be used besides Gaussian
		Kernel1D kernel;
		if(GeneralizedImageOps.isFloatingPoint(imageType) ) {
			kernel = FactoryKernel.table1D_F32(2,true);
		} else {
			kernel = FactoryKernel.table1D_I32(2);
		}
	}

	/**
	 * Updates and displays the pyramid.
	 */
	public void process( BufferedImage image ) {
		T input = ConvertBufferedImage.convertFromSingle(image, null, imageType);
		pyramid.process(input);

		DiscretePyramidPanel gui = new DiscretePyramidPanel();
		gui.setPyramid(pyramid);
		gui.render();

		ShowImages.showWindow(gui,"Image Pyramid");

		// To get an image at any of the scales simply call this get function
		T imageAtScale = pyramid.getLayer(1);

		ShowImages.showWindow(ConvertBufferedImage.convertTo(imageAtScale,null,true),"Image at layer 1");
	}

	public static void main( String[] args ) {
		BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("standard/barbara.jpg"));

		ExamplePyramidDiscrete<GrayF32> app = new ExamplePyramidDiscrete<GrayF32>(GrayF32.class);
//		ExamplePyramidDiscrete<GrayU8> app = new ExamplePyramidDiscrete<GrayU8>(GrayU8.class);

		app.standard();
//		app.unusual();
		app.process(image);
	}
}


Image Pyramid Float

/**
 * Demonstrates how to construct and display a {@link PyramidFloat}.  Float pyramids require only require
 * that each layer's scale be larger than the scale of the previous layer. Interpolation is used to allow
 * sub-sampling at arbitrary scales.  All of this additional flexibility comes at the cost of speed
 * when compared to a {@link PyramidDiscrete}.
 *
 * @author Peter Abeles
 */
public class ExamplePyramidFloat<T extends ImageGray> {

	// specifies the image type
	Class<T> imageType;
	// The pyramid data structure
	PyramidFloat<T> pyramid;

	public ExamplePyramidFloat(Class<T> imageType) {
		this.imageType = imageType;
	}

	/**
	 * Creates a fairly standard pyramid and updater.
	 */
	public void standard() {
		// Scale factory for each layer can be any floating point value which is larger than
		// the previous layer's scale.
		double scales[] = new double[]{1,1.5,2,2.5,3,5,8,15};
		// the amount of blur which is applied to each layer in the pyramid after the previous layer has been sampled
		double sigmas[] = new double[]{1,1,1,1,1,1,1,1};
		pyramid = FactoryPyramid.floatGaussian(scales,sigmas,imageType);
	}

	/**
	 * Updates and displays the pyramid.
	 */
	public void process( BufferedImage image ) {
		T input = ConvertBufferedImage.convertFromSingle(image, null, imageType);
		pyramid.process(input);

		ImagePyramidPanel<T> gui = new ImagePyramidPanel<>();
		gui.set(pyramid, true);
		gui.render();

		ShowImages.showWindow(gui,"Image Pyramid Float");

		// To get an image at any of the scales simply call this get function
		T imageAtScale = pyramid.getLayer(1);

		ShowImages.showWindow(ConvertBufferedImage.convertTo(imageAtScale,null,true),"Image at layer 1");
	}


	public static void main( String[] args ) {
		BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("standard/barbara.jpg"));

		ExamplePyramidFloat<GrayF32> app = new ExamplePyramidFloat<>(GrayF32.class);
//		ExamplePyramidFloat<GrayU8> app = new ExamplePyramidFloat<>(GrayU8.class);

		app.standard();
		app.process(image);
	}
}