Manipulating and Displaying Images |
The Java 2D API defines several filtering operations forBufferedImage
objects. Each image processing operation is embodied in a class that implements theBufferedImageOp
interface. The actual image manipulation is performed in the image operation'sfilter
method. TheBufferedImageOp
classes in the Java 2D API support:
- Affine transformation
- Amplitude scaling
- Lookup-table modification
- Linear combination of bands
- Color conversion
- Convolution
To filter a
BufferedImage
using one of the image operation classes, you:
- Construct an instance of one of the
BufferedImageOp
classes:AffineTransformOp
,BandCombineOp
,ColorConvertOp
,ConvolveOp
,LookupOp
,RescaleOp
, orThresholdOp
- Call the image operation's
filter
method, passing in theBufferedImage
that you want to filter and theBufferedImage
where you want to store the results.Example: ImageOps
The
You can see the complete code for this program inImageOps
program illustrates the use of four different image filter operations: low-pass, sharpen, lookup, and rescale.ImageOps.java
. To run the applet, you will also need this HTML file,ImageOps.html
, which includes the applet and these two images files:bld.jpg
andboat.gif
.The sharpen filter is performed using a
ConvolveOp
. Convolution is the process of weighting or averaging the value of each pixel in an image with the values of neighboring pixels. Most spatial filtering algorithms are based on convolution operations.Here is the code that constructs and applies the sharpen filter to the
BufferedImage
:Thefloat[][] data = {{0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f}, SHARPEN3x3_3}; ... int iw = bi[i].getWidth(this); int ih = bi[i].getHeight(this); int x = 0, y = 0; ... AffineTransform at = new AffineTransform(); at.scale((w-14)/2.0/iw, (h-34)/2.0/ih); BufferedImage bimg = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB); ... x = i==0?5:w/2+3; y = 15; Kernel kernel = new Kernel(3,3,data[i]); ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); cop.filter(bi[i],bimg);Kernel
object mathematically defines how each output pixel is affected by pixels in its immediate area.The definition of theKernel
determines the results of the filter. For more information about how kernels work withConvolveOp
, see the Image Processing and Enhancement section in the Java 2D Programmer's Guide.
Manipulating and Displaying Images