Java Reference
In-Depth Information
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f
};
Kernel kernel = new Kernel(3, 3, identityKernel);
This kernel describes a 3-by-3 table of values that's applied to each source pixel
anditseightimmediateneighbors.Toinvolvemoreneighbors,increasethesizeofthe
floating-pointarrayandthenumberofrowsandcolumns.Forexample,youcouldcre-
ate a 5-by-5 kernel that involves the source pixel and its 24 immediate neighbors.
Note
Although
Kernel
doesn'trequireodd-numberedwidthandheightarguments,
you might find kernels with an odd number of columns and an odd number of rows
easier to understand.
Aftercreating akernel,youneedtoconsiderwhathappenswhenthekernelisposi-
tionedoverpixelsattheedgesofanimage.Somekernelelementswillhavenocorres-
pondingimagepixels.Forexample,whena3-by-3kernelispositionedwithitscenter
rowoverthetopimagerow,thekernel'stoprowofneighborvalueshasnocorrespond-
ing row of image pixels.
ConvolveOp
addresses this situation by declaring
EDGE_ZERO_FILL
and
EDGE_NO_OP
constants.Specifying
EDGE_ZERO_FILL
causes
ConvolveOp
toset
edge destination pixels to zero, which is interpreted as black (RGB) or transparent
(ARGB).
EDGE_NO_OP
causes
ConvolveOp
to copy source edge pixels to the des-
tination unchanged.
To perform a convolution using this kernel, first instantiate
ConvolveOp
, as fol-
lows:
BufferedImageOp identityOp = new ConvolveOp(kernel);
RasterOp identityOp = new ConvolveOp(kernel);
The
ConvolveOp(Kernel kernel)
constructor sets the edge behavior to
EDGE_ZERO_FILL
.
Tip
Use the
ConvolveOp(Kernel kernel, int edgeCondition,
RenderingHints hints)
constructortoselecttheedgebehaviorandtherender-
ing hints for controlling the rasterizer.
Continue by invoking a
filter()
method, as follows:
BufferedImage biResult = identityOp.filter(bi, null);
WriteableRaster
wrResult
=
iden-
tityOp.filter(bi.getRaster(), null);