Difference between revisions of "Example Interpolation"

From BoofCV
Jump to navigationJump to search
(Created page with "<center> <gallery caption="Enlarging a small image with different interpolation methods" heights=160 widths=350 perrow=2 > file:Example_nearestneighbor_eye.png|Nearest Neighbo...")
 
m
Line 11: Line 11:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.26/examples/src/boofcv/examples/imageprocessing/ExampleInterpolation.java ExampleInterpolation.java]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.37/examples/src/main/java/boofcv/examples/imageprocessing/ExampleInterpolation.java ExampleInterpolation.java]


Concepts:
Concepts:
Line 30: Line 30:
  */
  */
public class ExampleInterpolation {
public class ExampleInterpolation {
 
public static void main( String[] args ) {
public static void main(String[] args) {
String imagePath;
String imagePath;
imagePath = "eye01.jpg";
imagePath = "eye01.jpg";
Line 39: Line 38:
ListDisplayPanel gui = new ListDisplayPanel();
ListDisplayPanel gui = new ListDisplayPanel();


gui.addImage(buffered,"Original");
gui.addImage(buffered, "Original");


// For sake of simplicity assume it's a gray scale image.  Interpolation functions exist for planar and
// For sake of simplicity assume it's a gray scale image.  Interpolation functions exist for planar and
// interleaved color images too
// interleaved color images too
GrayF32 input = ConvertBufferedImage.convertFrom(buffered, (GrayF32)null);
GrayF32 input = ConvertBufferedImage.convertFrom(buffered, (GrayF32)null);
GrayF32 scaled = input.createNew(500,500*input.height/input.width);
GrayF32 scaled = input.createNew(500, 500*input.height/input.width);


for( InterpolationType type : InterpolationType.values() ) {
for (InterpolationType type : InterpolationType.values()) {
// Create the single band (gray scale) interpolation function for the input image
// Create the single band (gray scale) interpolation function for the input image
InterpolatePixelS<GrayF32> interp = FactoryInterpolation.
InterpolatePixelS<GrayF32> interp = FactoryInterpolation.
createPixelS(0,255,type,BorderType.EXTENDED,input.getDataType());
createPixelS(0, 255, type, BorderType.EXTENDED, input.getDataType());


// Tell it which image is being interpolated
// Tell it which image is being interpolated
Line 64: Line 63:
float origX = x*input.width/(float)scaled.width;
float origX = x*input.width/(float)scaled.width;


scaled.data[indexScaled++] = interp.get(origX,origY);
scaled.data[indexScaled++] = interp.get(origX, origY);
}
}
}
}


// Add the results to the output
// Add the results to the output
BufferedImage out = ConvertBufferedImage.convertTo(scaled,null,true);
BufferedImage out = ConvertBufferedImage.convertTo(scaled, null, true);
gui.addImage(out,type.toString());
gui.addImage(out, type.toString());
}
}


ShowImages.showWindow(gui,"Example Interpolation", true);
ShowImages.showWindow(gui, "Example Interpolation", true);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 19:22, 21 December 2020

BoofCV provides abstract interfaces to apply different interpolation methods to all of its images. Bilinear is by far the most widely used interpolation method in computer vision.

Example Code:

Concepts:

  • Interpolation
  • Image Distortion


Example Code

/**
 * Interpolation is used to convert an image, which is discrete by its nature, into a (piecewise) smooth function.
 * Interpolation is in many CV applications, such as feature detection, and when distorting images.  In this
 * example a low resolution is scaled up using several different techniques to make the differences easily visible.
 * For computer vision applications bilinear interpolation is almost always used.
 *
 * @author Peter Abeles
 */
public class ExampleInterpolation {
	public static void main( String[] args ) {
		String imagePath;
		imagePath = "eye01.jpg";
//		imagePath = "small_sunflower.jpg";

		BufferedImage buffered = UtilImageIO.loadImage(UtilIO.pathExample(imagePath));
		ListDisplayPanel gui = new ListDisplayPanel();

		gui.addImage(buffered, "Original");

		// For sake of simplicity assume it's a gray scale image.  Interpolation functions exist for planar and
		// interleaved color images too
		GrayF32 input = ConvertBufferedImage.convertFrom(buffered, (GrayF32)null);
		GrayF32 scaled = input.createNew(500, 500*input.height/input.width);

		for (InterpolationType type : InterpolationType.values()) {
			// Create the single band (gray scale) interpolation function for the input image
			InterpolatePixelS<GrayF32> interp = FactoryInterpolation.
					createPixelS(0, 255, type, BorderType.EXTENDED, input.getDataType());

			// Tell it which image is being interpolated
			interp.setImage(input);

			// Manually apply scaling to the input image.  See FDistort() for a built in function which does
			// the same thing and is slightly more efficient
			for (int y = 0; y < scaled.height; y++) {
				// iterate using the 1D index for added performance.  Altertively there is the set(x,y) operator
				int indexScaled = scaled.startIndex + y*scaled.stride;
				float origY = y*input.height/(float)scaled.height;

				for (int x = 0; x < scaled.width; x++) {
					float origX = x*input.width/(float)scaled.width;

					scaled.data[indexScaled++] = interp.get(origX, origY);
				}
			}

			// Add the results to the output
			BufferedImage out = ConvertBufferedImage.convertTo(scaled, null, true);
			gui.addImage(out, type.toString());
		}

		ShowImages.showWindow(gui, "Example Interpolation", true);
	}
}