📄 ch29.htm
字号:
items in $y<BR>
107 return undef if ($ct1 !=
$ct2) ;<BR>
108<BR>
109 for ($i=0;$i<$ct1;$i++)
{<BR>
110 $sum += $$x[$i] * $$y[$i];
<BR>
111 }<BR>
112 return $sum;<BR>
113 }<BR>
114<BR>
115 # ------------------------------------------------------------
<BR>
116 # Returns an array that is the cross product of two vectors.
<BR>
117 # ------------------------------------------------------------
<BR>
118 sub xProduct { # (\@\@); Declare as taking two
pointers to arrays<BR>
119 my ($x,$y) = @_;<BR>
120 my @array;<BR>
121 my $result = \@array;<BR>
122 my $i, $j;<BR>
123 my $ct1 = $#{$x} + 1; #
items in $x<BR>
124 my $ct2 = $#{$y} + 1; #
items in $y<BR>
125<BR>
126 for ($i=0;$i<$ct1;$i++)
{<BR>
127 for
($j=0;$j<$ct2;$j++) {<BR>
128 $$result[$i][$j]
= $$x[$i] * $$y[$i];<BR>
129 #
print " $i, $j, $$result[$i][$j] \n";<BR>
130 }
<BR>
131 }<BR>
132 return @array;<BR>
133 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<H2><A NAME="UsingMatrices"><B><FONT SIZE=5 COLOR=#FF0000>Using
Matrices</FONT></B></A></H2>
<P>
Just like with vectors, you can use Perl references on matrices.
As an example of developing code for a prototype, this section
covers the following items:
<UL>
<LI><FONT COLOR=#000000>Reading an ASCII PBM file</FONT>
<LI><FONT COLOR=#000000>Collecting histogram information to show
work with matrices in Perl</FONT>
<LI><FONT COLOR=#000000>Applying a 3</FONT><FONT FACE="Symbol">¥</FONT>3
convolution smoothing filter on the image
<LI><FONT COLOR=#000000>Writing a resulting ASCII PBM file</FONT>
</UL>
<P>
This section covers the image shown in Figure 29.1. The image
is a black-and-white cartoon, but the techniques you'll learn
here can be applied to color images as well. Listing 29.5 contains
the complete code for developing the images shown in Figures 29.2
and 29.3.
<P>
<A HREF="f29-1.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f29-1.gif" ><B>Figure 29.1 : </B><I>The unfiltered image.</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 29.5. The complete listing for reading and writing
PBM files.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2<BR>
3 #-------------------------------------------------------
<BR>
4 # Read and write ASCII PPM
files<BR>
5 #-------------------------------------------------------
<BR>
6 # Author: Kamran Husain 4.4.96<BR>
7 # NO WARRANTIES WHATSOEVER APPLY HERE!! Copy freely,
use<BR>
8 # at will, with no restrictions.<BR>
9 #-------------------------------------------------------
<BR>
10 use Getopt::Long;<BR>
11 GetOptions('out=s');<BR>
12<BR>
13 open (TEXT,"pirate.ppm") || die "\n Cannot
open $!\n";<BR>
14 @image = ();<BR>
15 @hist = ();<BR>
16 ($wd, $ht, @image) = &readImage;<BR>
17 close <TEXT>;<BR>
18<BR>
19 print "@image ";<BR>
20 @hist = &getHistogram($ht,$wd,@image);<BR>
21<BR>
22 $ctr = 0;<BR>
23 $hi = $#hist + 1;<BR>
24<BR>
25 # ------------------------------------------------------
<BR>
26 # Display histogram of image in memory<BR>
27 # ------------------------------------------------------
<BR>
28 print "Histogram of image\n";<BR>
29 for ($i = 0; $i < $hi; $i++) {<BR>
30 if ($hist[$i] != 0) {<BR>
31 printf "[%3d]
= %5d ", $i, $hist[$i] ; $ctr++;<BR>
32 if
($ctr >= 5) {<BR>
33 $ctr
= 0;<BR>
34 print
"\n"<BR>
35 }
<BR>
36 }<BR>
37 }<BR>
38<BR>
39 # ------------------------------------------------------
<BR>
40 # Write to disk as unfiltered.<BR>
41 # ------------------------------------------------------
<BR>
42 @convolve = ( 0.1, 0.1, 0.1,<BR>
43 0.1, 0.1,
0.1,<BR>
44 0.1, 0.1,
0.1);<BR>
45 print "\n Filter 1 applied";<BR>
46 &applyFilter3($wd,$ht,\@convolve,\@image);<BR>
47 &dumpImage ('filt1.ppm', $wd, $ht);<BR>
48<BR>
49 @convolve = ( 0.1, 0.0, 0.1,<BR>
50 0.0, 0.5,
0.0,<BR>
51 0.1, 0.0,
0.1);<BR>
52 print "\n Filter 2 applied";<BR>
53 &applyFilter3($wd,$ht,\@convolve,\@image);<BR>
54<BR>
55 &dumpImage ('filt2.ppm', $wd, $ht);<BR>
56<BR>
57 exit(0);<BR>
58<BR>
59 # ------------------------------------------------------
<BR>
60 # Dump PPM file to disk given
file name,<BR>
61 # ht and width of image<BR>
62 # ------------------------------------------------------
<BR>
63 sub dumpImage {<BR>
64<BR>
65 my $fname = shift
@_;<BR>
66 my $wd = shift @_;
<BR>
67 my $ht = shift @_;
<BR>
68 my $i,$j,$k,$v;<BR>
69<BR>
70 print "\n Writing file $fname $wd by $ht";
<BR>
71<BR>
72 open (OUTPUT,">$fname") || die "Cannot
open $fname $! ";<BR>
73 select OUTPUT;<BR>
74 print "P3\n";<BR>
75 print "# Test output\n";<BR>
76 print "$wd $ht\n";<BR>
77 print "255\n";<BR>
78<BR>
79 $count = 0;<BR>
80 for($i=0;$i<$ht;$i++)
{<BR>
81 for($j=0;$j<$wd;$j++)
{<BR>
82 $v
= $$image[$i][$j];<BR>
83 printf
"%3d %3d %3d ", $v,$v,$v;<BR>
84 $count++;
<BR>
85 if
(($count % 5) == 0) {<BR>
86 $count
= 0;<BR>
87 print
"\n";}<BR>
88 }
<BR>
89 }<BR>
90 close OUTPUT;<BR>
91 select STDOUT;<BR>
92 }<BR>
93<BR>
94<BR>
95 # ------------------------------------------------------
<BR>
96 # Read PPM file from disk given
file name,<BR>
97 # Return b/w version back along
with ht and width of<BR>
98 # image.<BR>
99 # ------------------------------------------------------
<BR>
100 sub readImage { # (\@) for image data;<BR>
101 my @image;<BR>
102 my $result = \@image;<BR>
103 my $format = <TEXT>;
<BR>
104 my $comment = <TEXT>;
<BR>
105 $a = <TEXT>;<BR>
106 chop $a;<BR>
107 local ($cols, $rows) =
split(' ',$a);<BR>
108 local $colors = <TEXT>;
<BR>
109 my $row = 0;<BR>
110 my $col = 0;<BR>
111 my $a;<BR>
112<BR>
113 $rows = int($rows);<BR>
114 $cols = int($cols);<BR>
115<BR>
116 while ($a = <TEXT>)
{<BR>
117 chop $a;<BR>
118 @words = split(' ',$a);
<BR>
119 $count = $#words;<BR>
120<BR>
121 while (@words) {<BR>
122 ($r,$g,$b)
= splice(@words,0,3);<BR>
123 $$image[$row][$col]
= ($r+$g+$b)/3;<BR>
124 $col++;
<BR>
125 if ($col
>= $cols) { $row++; $col = 0 }<BR>
126 }<BR>
127 }<BR>
128 return ($cols,$rows,@image);<BR>
129 }<BR>
130<BR>
131<BR>
132 # ------------------------------------------------------<BR>
133 # Calculate histogram of up to 256
colors in<BR>
134 # the passed image bytes.<BR>
135 # ------------------------------------------------------<BR>
136 sub getHistogram {<BR>
137 my ($rows,$cols,$img) =
@_;<BR>
138 my @image = @$img;<BR>
139 my @refered = ();<BR>
140 my $hst = \@refered;<BR>
141<BR>
142 my $i,$j,$k;<BR>
143<BR>
144 for($i=0;$i<$rows;$i++)
{<BR>
145
for($j=0;$j<$cols;$j++) {<BR>
146 $k
= $$image[$i][$j];<BR>
147 $$hst[$k]
+= 1;<BR>
148 }
<BR>
149 }<BR>
150 return (@refered);<BR>
151 }<BR>
152<BR>
153<BR>
154 # ------------------------------------------------------<BR>
155 # Apply 3x3 filter to the image<BR>
156 # Return resulting image.<BR>
157 # ------------------------------------------------------<BR>
158 sub applyFilter3 {<BR>
159 my ($rows,$cols,$convolve,$img)
= @_;<BR>
160 my @fir = @$convolve;<BR>
161 my @image = @$img;<BR>
162 my $i,$j,$k,$v;<BR>
163<BR>
164 print "\n
Filter: $rows X $cols ";<BR>
165 for ($i=0; $i<9;$i++)
{<BR>
166 print "\[
$fir[$i] \]";<BR>
167 }<BR>
168 for($i=1;$i<$rows -1;$i++)
{<BR>
169 for($j=1;$j<$cols
- 1;$j++) {<BR>
170 $k
= $$image[$i-1][$j-1] * $fir[0] +
<BR>
171 $$image[$i][$j-1] *
$fir[1] +<BR>
172 $$image[$i+1][$j-1]
* $fir[2] +<BR>
173 $$image[$i-1][$j] *
$fir[3] +<BR>
174 $$image[$i][$j] *
$fir[4] +<BR>
175 $$image[$i+1][$j] *
$fir[5] +<BR>
176 $$image[$i-1][$j+1] *
$fir[6] +<BR>
177 $$image[$i][$j+1] *
$fir[7] +<BR>
178 $$image[$i+1][$j+1]
* $fir[8];<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -