summary refs log tree commit diff
path: root/pambackground.html
blob: 6b0d99533f3b36a14d6e39d8ecc59a6f7a0c2617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.3//EN">
<html><head><title>Pambackground User Manual</title></head>
<body>
<h1>pambackground</h1>
Updated: 24 November 2014
<br>
<a href="#index">Table Of Contents</a>

<h2>NAME</h2>
pambackground - create a mask of the background area of an image

<h2 id="synopsis">SYNOPSIS</h2>

<b>pambackground</b>

[<i>netpbmfile</i>]

[<b>-verbose</b>]

<p>Minimum unique abbreviations of options are acceptable.  You may
use double hyphens instead of single hyphen to denote options.  You
may use white space in place of the equals sign to separate an option
name from its value.


<h2 id="description">DESCRIPTION</h2>

<p>This program is part of <a href="index.html">Netpbm</a>.

<p><b>pambackground</b> reads a PNM or PAM image as input.  It generates as
output a PAM image that identifies the background area of the image (a mask).

<p>To identify the background, <b>pambackground</b> assumes the image is a
foreground image, smaller than the total image size, placed over a
single-color background.  It assumes that foreground image is solid -- it does
not have holes through which the background can be seen.  So in
specific, <b>pambackground</b> first identifies the background color, then
finds all contiguous pixels of that color in regions touching any edge of the
image.  Think of it as starting at each of the four edges and moving inward
and spreading out as far as possible until it hits pixels of another color
(the foreground image).

<p><b>pambackground</b> identifies the background color as follows:
If any 3 corners of the image are the same color, that's the background
color.  If not, but 2 corners are the same color, the background color
is the color of a pair of identically colored corners in this priority
order: top, right, left, bottom.  If no two corners have the same color,
the background color is the color of the upper left corner.

<p>In a typical photograph, the area that you would consider the
background is many shades of a color, so to <b>pambackground</b> it is
multiple colors and <b>pambackground</b> will not meaningfully
identify the background of your image.  To use <b>pambackground</b> in
this case, you might use <b>ppmchange</b> to change all similar colors
to a single one first.  For example, if the photograph is a building
against a blue sky, where nothing remotely sky-blue appears in the
building, you could use <b>ppmchange</b> to change all pixels within
20% of "SkyBlue" to SkyBlue, then run <b>pambackground</b>
on it.

<p>You might even extract the argument for <b>ppmchange</b> from the image in
question, using <b>pamgetcolor</b>.  In the foregoing example, we knew the
background was approximately SkyBlue, but if we didn't we could just get the
color of the top left pixel, in a form suitable for the color arguments
of <b>ppmchange</b> like this:

<pre>
<kbd>
    $ color=$(pamgetcolor 0,0 -infile=/tmp/bodyskl|cut --fields=2 -delim=' ')
</kbd>
</pre>

<p>A more convenient means of dealing with a multi-shade background is
to use <b>pnmquant</b> to produce a version of the image with a very small
number of colors.  The background would likely then be all one color.

<p>If the <b>pnmquant</b> and <b>ppmchange</b> methods above do not adequately
distinguish foreground colors from background colors, you can try a more
elaborate method using <b>pnmremap</b>.  If you can manually create a palette
with one color to which all the background pixels are similar, and other
colors to which the foreground pixels are similar, you can use it as input to
<b>pnmremap</b> to create a smarter version of what you get with the
<b>pnmquant</b> or <b>ppmchange</b> methods, so that <b>pambackground</b> is
more likely to separate background from foreground as your eye does.

<p>The PAM that <b>pambackground</b> creates has a single plane, with a maxval
of 1.  The sample value 1 means background; 0 means foreground.  There is no
tuple type.  Some older programs (but none that are part of Netpbm) don't know
what a PAM is and expect a mask to be in the form of a PGM or PBM image.  To
convert <b>pambackground</b>'s output to PBM, use <b>pamtopnm -assume</b>.  To
convert to PGM, use <b>pgmtopgm</b>.

<p><i>netpbmfile</i> is the file specification of the input file, or
<b>-</b> to indicate Standard Input.  The default is Standard Input.

<p>A common use for a background mask is with <b>pamcomp</b>.  You
could replace the entire background (or foreground) of your 
image with something else.

<p>Another common use is to make an image with the background transparent (in
some image format that has a concept of transparency) so that image can be
overlaid onto another image later.  Netpbm's converters to image formats that
have transparency (e.g. PNG) let you use the mask that <b>pambackground</b>
generates to identify the transparent areas for the output.  You can create
a PAM image with transparency with <b>pamstack</b>.

<p>To simply make a mask of all the areas of a specified color, use
<b>ppmcolormask</b>.  If you have a unique background color (one that
doesn't occur in the foreground) and know what it is, this can create
a background mask in cases that <b>pambackground</b> cannot: where there
are see-through holes in the foreground image.


<h2 id="options">OPTIONS</h2>

<p>In addition to the options common to all programs based on libnetpbm
(most notably <b>-quiet</b>, see <a href="index.html#commonoptions">
Common Options</a>), <b>pambackground</b> recognizes the following
command line option:

<dl>

<dt><b>-verbose</b>

<dd>Tell interesting facts about the process.

</dl>

<h2 id="examples">EXAMPLES</h2>

<pre>
<kbd>
    $ pambackground test.ppm | pnminvert &gt;/tmp/bgmask.pgm
    $ pamcomp -alpha=bgmask.pgm test.ppm wallpaper.ppm &gt;output.ppm
</kbd>
</pre>
<pre>
<kbd>
    $ pnmquant 5 test.pgm | pambackground test.ppm &gt;/tmp/bgmask.pam
</kbd>
</pre>


<h2 id="seealso">SEE ALSO</h2>

<b><a href="ppmcolormask.html">ppmcolormask</a></b>,
<b><a href="pamcomp.html">pamcomp</a></b>,
<b><a href="ppmchange.html">ppmchange</a></b>,
<b><a href="pnmquant.html">pnmquant</a></b>,
<b><a href="pnmremap.html">pnmremap</a></b>,
<b><a href="pamtopnm.html">pamtopnm</a></b>,
<b><a href="pgmtopgm.html">pgmtopgm</a></b>,
<b><a href="pamstack.html">pamstack</a></b>,
<b><a href="pamstack.html">pamgetcolor</a></b>,
<b><a href="pamstack.html">pbmmaskd</a></b>,
<b><a href="pnm.html">pnm</a></b>,
<b><a href="pam.html">pam</a></b>,

<h2 id="history">HISTORY</h2>

<p><b>pambackground</b> was new in Netpbm 10.37 (December 2006).

<hr>
<h2 id="index">Table Of Contents</h2>
<ul>
<li><a href="#synopsis">SYNOPSIS</a>
<li><a href="#description">DESCRIPTION</a>
<li><a href="#options">OPTIONS</a>
<li><a href="#examples">EXAMPLES</a>
<li><a href="#seealso">SEE ALSO</a>
<li><a href="#history">HISTORY</a>
</ul>
</body>
</html>