📄 geometric image manipulation.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0101)http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html -->
<HTML><HEAD><TITLE>Geometric Image Manipulation</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content=reference name=collection>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<CENTER><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/JAITOC.fm.html"><IMG
alt=Contents src="Geometric Image Manipulation.files/contents.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Image-enhance.doc.html"><IMG
alt=Previous src="Geometric Image Manipulation.files/previous.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Analysis.doc.html"><IMG
alt=Next src="Geometric Image Manipulation.files/next.gif"></A>
<P><FONT size=5><I>Programming in Java Advanced Imaging</I></FONT> </CENTER><BR>
<CENTER><A name=47227>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right><FONT size=3>C H A P T E R</FONT><FONT size=7><IMG
src="Geometric Image Manipulation.files/sm-space.gif">8</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<CENTER><A name=51140>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right>
<HR noShade SIZE=7>
<FONT size=6>Geometric Image
Manipulation</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<BLOCKQUOTE>
<P><BR><BR><BR>
<P><FONT size=7><B>T</B></FONT>HIS chapter describes the basics of JAI's
geometric image manipulation functions. The geometric image manipulation
operators are all part of the <CODE>javax.media.operator</CODE> package.
<P><A name=50856>
<H2>8.1 <IMG
src="Geometric Image Manipulation.files/space.gif">Introduction</H2></A>The
JAI geometric image manipulation functions are:
<P>
<UL>
<LI>Geometric transformation (<CODE>Translate</CODE>, <CODE>Scale</CODE>,
<CODE>Rotate</CODE>, and <CODE>Affine</CODE>)
<P></P></LI></UL>
<UL>
<LI>Perspective transformation (<CODE>PerspectiveTransform</CODE>)
<P></P></LI></UL>
<UL>
<LI>Transposing (<CODE>Transpose</CODE>)
<P></P></LI></UL>
<UL>
<LI>Shearing (<CODE>Shear</CODE>)
<P></P></LI></UL>
<UL>
<LI>Warping (<CODE>Warp</CODE>, <CODE>WarpAffine</CODE>,
<CODE>WarpPerspective</CODE>, <CODE>WarpPolynomial</CODE>,
<CODE>WarpGeneralPolynomial</CODE>, <CODE>WarpQuadratic</CODE>, and
<CODE>WarpOpImage</CODE>)
<P></P></LI></UL>Most of these geometric functions require an interpolation
argument, so this chapter begins with a discussion of interpolation.
<P><A name=51290>
<H2>8.2 <IMG
src="Geometric Image Manipulation.files/space.gif">Interpolation</H2></A>Several
geometric image operations, such as <CODE>Affine</CODE>, <CODE>Rotate</CODE>,
<CODE>Scale</CODE>, <CODE>Shear</CODE>, <CODE>Translate</CODE>, and
<CODE>Warp</CODE>, use a geometric transformation to compute the coordinate of
a source image point for each destination image pixel. In most cases, the
destination pixel does not lie at a source pixel location, but rather lands
somewhere between neighboring pixels. The estimated value of each pixel is set
in a process called interpolation or <EM>image resampling</EM>.
<P>Resampling is the action of computing a pixel value at a possibly
non-integral position of an image. The image defines pixel values at integer
lattice points, and it is up to the resampler to produce a reasonable value
for positions not falling on the lattice. A number of techniques are used in
practice, the most common being the following:
<P>
<UL>
<LI>Nearest-neighbor, which simply takes the value of the closest lattice
point
<P></P></LI></UL>
<UL>
<LI>Bilinear, which interpolates linearly between the four closest lattice
points
<P></P></LI></UL>
<UL>
<LI>Bicubic, which applies a piecewise polynomial function to a 4 x 4
neighborhood of nearby points
<P></P></LI></UL>The area over which a resampling function needs to be
computed is referred to as its <EM>support</EM>; thus the standard resampling
functions have supports of 1, 4, and 16 pixels respectively. Mathematically,
the ideal resampling function for a band-limited image (one containing no
energy above a given frequency) is the sinc function, equal to sin(x)/x. This
has practical limitations, in particular its infinite support, which lead to
the use of the standard approximations described above.
<P>In interpolation, each pixel in a destination image is located with integer
coordinates at a distinct point <EM>D</EM> in the image plane. The geometric
transform <EM>T</EM> identifies each destination pixel with a corresponding
point <EM>S</EM> in the source image. Thus, <EM>D</EM> is the point that
<EM>T</EM> maps to <EM>S</EM>. In general, <EM>S</EM> doesn't correspond to a
single source pixel; that is, it doesn't have integer coordinates. Therefore,
the value assigned to the pixel <EM>D</EM> must be computed as an interpolated
combination of the pixel values closest to <EM>S</EM> in the source image.
<P>For most geometric transformations, you must specify the interpolation
method to be used in calculating destination pixel values. <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#65017">Table
8-1</A> lists the names used to call the interpolation methods.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B><A name=65017><I>Table 8-1 </I><IMG
src="Geometric Image Manipulation.files/sm-blank.gif" border=0>
Interpolation Types </A></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=65021>Name </A>
<TH><A name=65023>Description </A>
<TR vAlign=top>
<TD><A name=65025>INTERP_NEAREST</A><BR>
<TD><A name=65027>Nearest-neighbor interpolation. Assigns to point D in
the destination image the value of the pixel nearest S in the source
image. See <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#70504">Section
8.2.1, "Nearest-neighbor Interpolation</A>."</A><BR>
<TR vAlign=top>
<TD><A name=65029>INTERP_BILINEAR</A><BR>
<TD><A name=65031>Bilinear interpolation. Assigns to Point D in the
destination a value that is a bilinear function of the four pixels
nearest S in the source image. See <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#55403">Section
8.2.2, "Bilinear Interpolation</A>."</A><BR>
<TR vAlign=top>
<TD><A name=65033>INTERP_BICUBIC</A><BR>
<TD><A name=65035>Bicubic interpolation. Assigns to point D in the
destination image a value that is a bicubic function of the 16 pixels
nearest S in the source image.<A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#55431">Section
8.2.3, "Bicubic Interpolation</A>."</A><BR>
<TR vAlign=top>
<TD><A name=65037>INTERP_BICUBIC2</A><BR>
<TD><A name=65039>Bicubic2 interpolation. Similar to Bicubic, but uses a
different polynomial function. See <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#55447">Section
8.2.4, "Bicubic2 Interpolation</A>."</A><BR></TR></TBODY></TABLE>
<P>Occasionally, these four options do not provide sufficient quality for a
specific operation and a more general form of interpolation is called for. The
more general form of interpolation, called <EM>table interpolation</EM> uses
tables to store the interpolation kernels. See <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#55492">Section
8.2.5, "Table Interpolation</A>."
<P>Other interpolation functions may be required to solve problems other than
the resampling of band-limited image data. When shrinking an image, it is
common to use a function that combines area averaging with resampling to
remove undesirable high frequencies as part of the interpolation process.
Other application areas may use interpolation functions that operate under
other assumptions about image data, such as taking the maximum value of a 2 x
2 neighborhood. The <CODE>Interpolation</CODE> class provides a framework in
which a variety of interpolation schemes may be expressed.
<P>Many Interpolations are separable, that is, they may be equivalently
rewritten as a horizontal interpolation followed by a vertical one (or vice
versa). In practice, some precision may be lost by the rounding and truncation
that takes place between the passes. The <CODE>Interpolation</CODE> class
assumes separability and implements all vertical interpolation methods in
terms of corresponding horizontal methods, and defines
<CODE>isSeparable</CODE> to return true. A subclass may override these methods
to provide distinct implementations of horizontal and vertical interpolation.
Some subclasses may implement the two-dimensional interpolation methods
directly, yielding more precise results, while others may implement these
using a two-pass approach.
<P>When interpolations that require padding the source such as Bilinear or
Bicubic interpolation are specified, the boundary of the source image needs to
be extended such that it has the extra pixels needed to compute all the
destination pixels. This extension is performed via the
<CODE>BorderExtender</CODE> class. The type of border extension can be
specified as a <CODE>RenderingHint</CODE> to the <CODE>JAI.create</CODE>
method. If no border extension type is provided, a default extension of
<CODE>BorderExtender.BORDER_COPY</CODE> will be used to perform the extension.
See <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#55991">Section
3.7.3, "Rendering Hints</A>."
<P><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#69404">Listing
8-1</A> shows a code sample for a <CODE>rotate</CODE> operation. First, the
type of interpolation is specified (<CODE>INTERP_NEAREST</CODE> in this
example) using the <CODE>Interpolation.create</CODE> method. Next, a parameter
block is created and the interpolation method is added to the parameter block,
as are all the other parameters required by the operation. Finally, a
<CODE>rotate</CODE> operation is created with the specified parameter block.
<P><CAPTION><FONT size=-1><B><A name=69404>
<CENTER><FONT size=-1><B><I>Listing 8-1 </I><IMG
src="Geometric Image Manipulation.files/sm-blank.gif" border=0> Example Using
Nearest-neighbor Interpolation</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD><PRE> // Specify the interpolation method to be used
<STRONG><KBD>interp = Interpolation.create(Interpolation.INTERP_NEAREST);
</KBD></STRONG></PRE><TR valign="top"><TD><PRE> // Create the parameter block and add the interpolation to it
ParameterBlock pb = new ParameterBlock();
pb.addSource(im); // The source image
pb.add(0.0F); // The x origin to rotate about
pb.add(0.0F); // The y origin to rotate about
pb.add(theta); // The rotation angle in radians
<STRONG><KBD>pb.add(interp); // The interpolation method
</KBD></STRONG></PRE><TR valign="top"><TD><PRE> // Create the rotation operation and include the parameter
// block
RenderedOp op JAI.create("rotate", pb, null);
</PRE>
<HR>
<P>The <CODE>Interpolation</CODE> class provides methods for the most common
cases of 2 x 1, 1 x 2, 4 x 1, 1 x 4, 2 x 2, and 4 x 4 input grids, some of
which are shown in <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#66155">Figure
8-1</A>. These methods are defined in the superclass
(<CODE>Interpolation</CODE>) to package their arguments into arrays and
forward the call to the array versions, to simplify implementation. These
methods should be called only on <CODE>Interpolation</CODE> objects with the
correct width and height. In other words, an implementor of an
<CODE>Interpolation</CODE> subclass may implement <CODE>interpolateH(int s0,
int s1, int xfrac)</CODE>, assuming that the interpolation width is in fact
equal to 2, and does not need to enforce this constraint.
<P><A name=66153>
<HR>
<CENTER><IMG
src="Geometric Image Manipulation.files/Geom-image-manip.doc.anc2.gif"></CENTER>
<HR>
</A><A name=66155>
<CENTER><FONT size=-1><B><I>Figure 8-1 </I><IMG
src="Geometric Image Manipulation.files/sm-blank.gif" border=0> Interpolation
Samples</B></FONT></CENTER></A>
<P>Another possible source of inefficiency is the specification of the
subsample position. When interpolating integral image data, JAI uses a
fixed-point subsample position specification, that is, a number between 0 and
(2n - 1) for some small value of <EM>n</EM>. The value of <EM>n</EM> in the
horizontal and vertical directions may be obtained by calling the
<CODE>getSubsampleBitsH</CODE> and <CODE>getSubsampleBitsV</CODE> methods. In
general, code that makes use of an externally-provided
<CODE>Interpolation</CODE> object must query that object to determine its
desired positional precision.
<P>For <CODE>float</CODE> and <CODE>double</CODE> images, JAI uses a
<CODE>float</CODE> between 0.0F and 1.0F (not including 1.0F) as a positional
specifier in the interest of greater accuracy.
<P>
<TABLE border=0>
<TBODY>
<TR>
<TD><IMG src="Geometric Image Manipulation.files/cistine.gif"></TD>
<TD>
<HR>
<B>API:</B> <CODE>javax.media.jai.Interpolation </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>static Interpolation getInstance(int type)
<P></P></LI></UL></PRE>
<DL><A name=65047>
<DT>
<DD>creates an interpolation of one of the standard types, where
<CODE>type</CODE> is one of <CODE>INTERP_NEAREST</CODE>,
<CODE>INTERP_BILINEAR</CODE>, <CODE>INTERP_BICUBIC</CODE>, or
<CODE>INTERP_BICUBIC_2</CODE>. </A>
<P></P></DD></DL><PRE><UL>
<LI>int interpolate(int[][] samples, int xfrac, int yfrac)
<P></P></LI></UL></PRE>
<DL><A name=65076>
<DT>
<DD>performs interpolation on a two-dimensional array of integral samples.
By default, this is implemented using a two-pass approach.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD rowSpan=3><EM>Parameters</EM>:
<P></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -