323-329.html
来自「dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh」· HTML 代码 · 共 318 行
HTML
318 行
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:An Introduction to Image Processing</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=7//-->
<!--PAGES=323-329//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="319-323.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="329-331.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>To set a pixel at location (x,y):
</P>
<!-- CODE SNIP //-->
<PRE>
pp.setPixel( x, y, r, g, b, a);
</PRE>
<!-- END CODE SNIP //-->
<P>To set a pixel equal to the packed <I>pel</I> at location (x,y):</P>
<!-- CODE SNIP //-->
<PRE>
pp.setPixel(x, y, pel);
</PRE>
<!-- END CODE SNIP //-->
<P>To get a packed pixel at location (x,y):
</P>
<!-- CODE SNIP //-->
<PRE>
pel = pp.getPixel(x, y);
</PRE>
<!-- END CODE SNIP //-->
<P>To get a packed pixel from the linear array at location <I>i</I>:</P>
<!-- CODE SNIP //-->
<PRE>
pel = pp.getPixel(i);
</PRE>
<!-- END CODE SNIP //-->
<P>To print the size of the <I>PixelPlane</I> instance to the console:</P>
<!-- CODE SNIP //-->
<PRE>
pp.printSize();
}
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading20"></A><FONT COLOR="#000077">The ProcessPlane Class</FONT></H3>
<P>The <I>ProcessPlane</I> class resides in the <I>lyon.ipl</I> package. It consists of an extension to the <I>PixelPlane</I> class. By creating instances of the <I>ProcessPlane</I> class, we add methods to the <I>PixelPlane</I> instance that provide for elementary image processing services. The services themselves are not profound, but the way they are implemented in Java is of interest.</P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
package lyon.ipl;
import java.awt.*;
public class ProcessPlane extends PixelPlane {
public ProcessPlane (double x_, double y_)
public ProcessPlane (int x_, int y_)
public void cornergray() {
public ProcessPlane scale(int scale) {
public void threshold(int konst)
public void diagGray()
public ProcessPlane edge()
public void linearComb(double konstD, double akD)
public void Subimage(ProcessPlane pp)
public void makeGray()
public void randResample()
public void shadow()
public void negate()
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following variables are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
ProcessPlane pp, pp2;
int height, width;
double heightD, widthD, a, b;
int scale, konst;
</PRE>
<!-- END CODE SNIP //-->
<P>To make an instance of a <I>ProcessPlane</I> that allocates internal pixel storage for a <I>height</I> × <I>width</I> by 32-color, RGBA (Red, green, blue, and alpha) image, use this code:</P>
<!-- CODE SNIP //-->
<PRE>
pp = new ProcessPlane(height, width);
</PRE>
<!-- END CODE SNIP //-->
<P>To make an instance of a <I>ProcessPlane</I> that uses doubles for size and casts the doubles to <I>int</I> before allocating storage:</P>
<!-- CODE SNIP //-->
<PRE>
pp = new ProcessPlane(heightD, widthD);
</PRE>
<!-- END CODE SNIP //-->
<P>To make an image of the previous image’s size and fill it with a <I>cornergray</I> test pattern, as shown in Figure 7.4:</P>
<!-- CODE SNIP //-->
<PRE>
pp.cornergray();
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/07-04.jpg',163,102 )"><IMG SRC="images/07-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-04.jpg',163,102)"><FONT COLOR="#000077"><B>Figure 7.4</B></FONT></A> The cornergray method output.</P>
<P>To scale an image by a positive integer using pixel replication:
</P>
<!-- CODE SNIP //-->
<PRE>
pp.scale(konst);
</PRE>
<!-- END CODE SNIP //-->
<P>To threshold an image using an integer value that ranges from 0 to 255, as shown in Figure 7.5:
</P>
<!-- CODE SNIP //-->
<PRE>
pp.threshold(konst);
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/07-05.jpg',251,254 )"><IMG SRC="images/07-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-05.jpg',251,254)"><FONT COLOR="#000077"><B>Figure 7.5</B></FONT></A> Effect of threshold on an image.</P>
<P>To make a diagonal test pattern using the existing image’s dimensions, as shown in Figure 7.6:
</P>
<!-- CODE SNIP //-->
<PRE>
pp.diagGray();
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/07-06.jpg',251,251 )"><IMG SRC="images/07-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-06.jpg',251,251)"><FONT COLOR="#000077"><B>Figure 7.6</B></FONT></A> The effect of the diagGray method.</P>
<P>To make a single pixel-width wide edge from the leftmost edge of the image, at the center of a thick bright stripe, as shown in Figure 7.7, use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
pp.edge();
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/07-07.jpg',105,162 )"><IMG SRC="images/07-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-07.jpg',105,162)"><FONT COLOR="#000077"><B>Figure 7.7</B></FONT></A> Effect of the edge method.</P>
<P>The <I>edge</I> method uses a domain-specific edge detector that is useful for range-finding via diffraction. See the following section for more information about the implementation.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>This edge detection method will produce unsatisfactory results when applied to images outside the intended domain.<HR></FONT>
</BLOCKQUOTE>
<P>To perform the operation
</P>
<P ALIGN="CENTER"><IMG SRC="images/07-01d.jpg"></P>
<P>where
</P>
<P ALIGN="CENTER"><IMG SRC="images/07-02d.jpg"></P>
<!-- CODE SNIP //-->
<PRE>
pp.linearComb(a, b);
</PRE>
<!-- END CODE SNIP //-->
<P>To subtract <I>pp2</I> from <I>pp</I>, leaving the result in <I>pp</I> (i.e., <I>pp</I> = <I>pp</I> - <I>pp2</I>):</P>
<!-- CODE SNIP //-->
<PRE>
pp.Subimage(pp2);
</PRE>
<!-- END CODE SNIP //-->
<P>To make <I>pp</I> a gray image by copying the red plane to the green and blue planes:</P>
<!-- CODE SNIP //-->
<PRE>
pp.makeGray();
</PRE>
<!-- END CODE SNIP //-->
<P>To perform a stochastic (random) resampling of the image, an effect illustrated in Figure 7.8:
</P>
<!-- CODE SNIP //-->
<PRE>
pp.randResample();
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/07-08.jpg',219,229 )"><IMG SRC="images/07-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-08.jpg',219,229)"><FONT COLOR="#000077"><B>Figure 7.8</B></FONT></A> The effect of randResample.</P>
<P>To perform a shadow mask on the image (as shown in Figure 7.9):
</P>
<!-- CODE SNIP //-->
<PRE>
pp.shadow();
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/07-09.jpg',180,223 )"><IMG SRC="images/07-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-09.jpg',180,223)"><FONT COLOR="#000077"><B>Figure 7.9</B></FONT></A> Effect of the shadow method.</P>
<P>To negate the image (as shown in Figure 7.10):
</P>
<!-- CODE SNIP //-->
<PRE>
pp.negate();
</PRE>
<!-- END CODE SNIP //-->
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/07-10.jpg',180,219 )"><IMG SRC="images/07-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-10.jpg',180,219)"><FONT COLOR="#000077"><B>Figure 7.10</B></FONT></A> Effect of the negate method.</P>
<P>This section showed a collection of image processing services performed by instances of the <I>ProcessPlane</I> class. Although all the images are shown as gray-scale images, this is a limitation of the printing process. All the operations are 24-bit color operations.</P>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">Class Implementation: The negate Method</FONT></H4>
<P>None of the image processing operations shown in this section is remarkable. Perhaps the more interesting issue is how the operations are implemented. We start with the simplest of the image processing operations, the <I>negate</I> method:</P>
<!-- CODE //-->
<PRE>
public void negate() {
int r,g,b,a;
for (int y=0; y < getHeight(); y++)
for (int x =0 ; x < getWidth(); x++) {
r = getRed (x,y);
g = getGreen(x,y);
b = getBlue (x,y);
a = getAlpha(x,y);
setPixel(x, y, 255 - r,255 - g,255 - b, a);
}
}
</PRE>
<!-- END CODE //-->
<P>The <I>negate</I> method is added to the <I>ProcessPlane</I> class to enable any instance of a <I>ProcessPlane</I> to negate itself.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Because we’re extending the <I>PixelPlane</I> class, we inherit all its methods. Also, we do not negate the alpha plane, because that would make the image black.<HR></FONT>
</BLOCKQUOTE>
<P>In the <I>ImageFrame</I> class is a <I>MenuItem</I> instance called <I>negate_mi</I>:</P>
<!-- CODE SNIP //-->
<PRE>
MenuItem negate_mi = addItem("[n]negate");
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>addItem</I> method adds the <I>MenuItem</I> instance to the main menu bar:</P>
<!-- CODE SNIP //-->
<PRE>
public MenuItem addItem(String itemName) {
MenuItem mi = new MenuItem(itemName);
m.add(mi);
return(mi);
}
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, we handle the event using the <I>Evt</I>’s <I>match</I> method to select either the keyboard event or the menu selection event:</P>
<!-- CODE SNIP //-->
<PRE>
public boolean handleEvent(Event e) {
if (Evt.match(e,negate_mi)) {
negate();
return true;
}...
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">Class Implementation: The shadow Method</FONT></H4>
<P>The <I>shadow</I> method is a filter that moves a 2 × 2 pixel window, one pixel at a time, across an image. It replaces the pixel at the upper-left corner of the window with the value of the pixel minus one-half the value of the pixel at the lower-right corner.</P>
<!-- CODE //-->
<PRE>
public void shadow() {
int r,g,b,a;
for (int y=0; y < getHeight()-2; y++)
for (int x =0 ; x < getWidth()-2; x++) {
r = getRed (x,y);
g = getGreen(x,y);
b = getBlue (x,y);
a = getAlpha(x,y);
r = r + (127 - getRed (x+2,y+2));
g = g + (127 - getGreen(x+2,y+2));
b = b + (127 - getBlue (x+2,y+2));
setPixel(x,y,r,g,b,a);
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="319-323.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="329-331.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?