| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.3//EN"> |
| 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 3 | <head> |
| 4 | <title>pamhomography</title> |
| 5 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
| 6 | <script type="text/javascript" src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> |
| 7 | <script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> |
| 8 | </head> |
| 9 | |
| 10 | <body> |
| 11 | |
| 12 | <h1>pamhomography</h1> |
| 13 | |
| 14 | Updated: 04 December 2022 |
| 15 | <br> |
| 16 | <a href="#index">Table of Contents</a> |
| 17 | |
| 18 | |
| 19 | <h2>NAME</h2> |
| 20 | |
| 21 | <p>pamhomography - stretch/shrink a quadrilateral region of an image to |
| 22 | another quadrilateral shape</p> |
| 23 | |
| 24 | |
| 25 | <h2 id="SYNOPSIS">SYNOPSIS</h2> |
| 26 | |
| 27 | <p><b>pamhomography</b> |
| 28 | [<b>-from</b>=<i>coords</i>] |
| 29 | [<b>-to</b>=<i>coords</i>] |
| 30 | [<b>-mapfile</b>=<i>map_file</i>] |
| 31 | [<b>-view</b>=<i>coords</i>] |
| 32 | [<b>-fill</b>=<i>color</i>] |
| 33 | [<b>-verbose</b> |
| 34 | [<i>pam_file</i>] |
| 35 | |
| 36 | <p>You can abbreviate any option to its shortest unique prefix. You can use |
| 37 | two hyphens instead of one to delimit an option. You can separate an option |
| 38 | from its value with whitespace instead of <code>=</code>.</p> |
| 39 | |
| 40 | |
| 41 | <h2 id="DESCRIPTION">DESCRIPTION</h2> |
| 42 | |
| 43 | <p>This program is part |
| 44 | of <a href="http://netpbm.sourceforge.net/">Netpbm</a>.</p> |
| 45 | |
| 46 | <p><b>pamhomography</b> stretches and shrinks an arbitrary quadrilateral |
| 47 | portion of an input image you specify (not necessarily rectangular), into a |
| 48 | new quadrilateral shape you specify, producing a new image. |
| 49 | |
| 50 | <p>You can do any |
| 51 | <a href="https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation">affine image transformation</a>: translation, reflection, scaling, |
| 52 | rotation, and shearing/skewing. However, <b>pamhomography</b> additionally can |
| 53 | do <i>bilinear</i> transforms, which means it can warp any quadrilateral to any |
| 54 | other quadrilateral, even when this mapping cannot be described using a single |
| 55 | set of linear equations. This can be useful, for example, for creating |
| 56 | perspective views of rectangular images or for reverse-mapping a perspective |
| 57 | view back to a rectangular projection.</p> |
| 58 | |
| 59 | |
| 60 | <h2 id="OPTIONS">OPTIONS</h2> |
| 61 | |
| 62 | <p>In addition to the options common to all programs based on libnetpbm (most |
| 63 | notably <b>-quiet</b>, see <a href="index.html#commonoptions">Common |
| 64 | Options</a>), <b>pamhomography</b> recognizes the following command line |
| 65 | options:</p> |
| 66 | |
| 67 | <dl> |
| 68 | |
| 69 | <dt><b>-from</b>=<i>coords</i></dt> |
| 70 | <dd> |
| 71 | |
| 72 | <p>This defines the source quadrilateral. <i>coords</i> is a list of four |
| 73 | integer-valued (<i>x</i>, <i>y</i>) coordinates. If you do not specify the |
| 74 | source with either <b>-from</b> or <b>-mapfile</b>, the source quadrilateral |
| 75 | is the entire input image. |
| 76 | |
| 77 | </dd> |
| 78 | <dt><b>-to</b>=<i>coords</i></dt> |
| 79 | <dd> |
| 80 | |
| 81 | <p>This defines the target quadrilateral. <i>coords</i> is a list of four |
| 82 | integer-valued (<i>x</i>, <i>y</i>) coordinates. If you do not specify the |
| 83 | target with either <b>-to</b> or <b>-mapfile</b>, the target quadrilateral is |
| 84 | the same as the entire input image. |
| 85 | </dd> |
| 86 | <dt><b>-mapfile</b>=<i>map_file</i></dt> |
| 87 | <dd> |
| 88 | |
| 89 | <p>This names a text file that describes the mapping from the source to the |
| 90 | target quadrilateral. The file <i>map_file</i> must contain either eight |
| 91 | integer-valued (<i>x</i>, <i>y</i>) coordinates, being the four source |
| 92 | coordinates followed by the corresponding four target coordinates, or only |
| 93 | four (<i>x</i>, <i>y</i>) coordinates, being only the four target |
| 94 | coordinates. In the latter case, the source quadrilateral is taken to be the |
| 95 | four corners of the input image in clockwise order, starting from the upper |
| 96 | left.</p> |
| 97 | |
| 98 | <p>Anything you specify with <b>-to</b> or <b>-from</b> overrides what is in |
| 99 | this file. |
| 100 | |
| 101 | </dd> |
| 102 | <dt><b>-view</b>=<i>coords</i></dt> |
| 103 | <dd> |
| 104 | |
| 105 | <p>This defines the target view. <i>coords</i> is a list of two integer-valued |
| 106 | (<i>x</i>, <i>y</i>) coordinates: the upper left and lower right boundaries, |
| 107 | respectively, of the pixels that will be visible in the output image. If |
| 108 | <b>-view</b> is not specified, the target view will fit precisely the target |
| 109 | quadrilateral.</p> |
| 110 | |
| 111 | </dd> |
| 112 | <dt><b>-fill</b>=<i>color</i></dt> |
| 113 | <dd> |
| 114 | |
| 115 | <p>This is the color with which the program fills all pixels that lie outside |
| 116 | of the target quadrilateral. Specify the color as described for the |
| 117 | <a href="libnetpbm_image.html#colorname"> |
| 118 | argument of the pnm_parsecolor() library routine</a>. |
| 119 | |
| 120 | <p>The default is black, and for images with a transparency plane, transparent. |
| 121 | |
| 122 | </dd> |
| 123 | |
| 124 | <dt><b>-verbose</b> |
| 125 | <dd> |
| 126 | This makes the program issue some informational messages about what it is |
| 127 | doing. |
| 128 | |
| 129 | </dl> |
| 130 | |
| 131 | |
| 132 | |
| 133 | <p>Cooordinates should normally be specified in clockwise order. The syntax is |
| 134 | fairly flexible: all characters other than the plus sign, minus sign, and |
| 135 | digits are treated as separators. Although coordinates need to be integers, |
| 136 | they may lie outside the image's boundary.</p> |
| 137 | |
| 138 | <h2 id="PARAMETERS">PARAMETERS</h2> |
| 139 | |
| 140 | <p><b>pamhomography</b>'s only parameter, <i>pam_file</i>, is the name of the |
| 141 | file containing the input image. If you don't specify <i>pam_file</i>, the |
| 142 | image comes from Standard Input. |
| 143 | |
| 144 | |
| 145 | <h2 id="NOTES">NOTES</h2> |
| 146 | |
| 147 | <p>The output image uses the same Netpbm format as the input image.</p> |
| 148 | |
| 149 | <p>Simple transformations are best handled by other Netpbm programs, such as |
| 150 | those listed in the <a href="#SEE-ALSO">"SEE ALSO"</a> section |
| 151 | below. Use <b>pamhomography</b> for more sophisticated transformations such as |
| 152 | perspective adjustments, rotations around an arbitrary point in the image, |
| 153 | extraction of non-rectangular quadrilaterals, shearings by coordinates rather |
| 154 | than by angle, and, in general, all transformations that are most easily |
| 155 | expressed as mapping four points in one image to four points in another |
| 156 | image.</p> |
| 157 | |
| 158 | <h2 id="EXAMPLES">EXAMPLES</h2> |
| 159 | |
| 160 | <p>The following examples use the |
| 161 | <a href="park_row.ppm">park_row.ppm </a> test image, which is a |
| 162 | <a href="https://commons.wikimedia.org/wiki/File:15_Park_Row_3.JPG"> |
| 163 | photograph of New York City's Park Row Building</a>, scaled to |
| 164 | 441×640, converted to a PPM file, and redistributed under the terms of |
| 165 | the <a href="https://en.wikipedia.org/wiki/GNU_Free_Documentation_License"> |
| 166 | GFDL</a>.</p> |
| 167 | |
| 168 | <p>The first example showcases the real power of bilinear transformations. |
| 169 | Assuming <i>park_row_rect.map</i> has the following contents:</p> |
| 170 | |
| 171 | <pre><code> (147, 51) (316, 105) (402, 595) (92, 560) |
| 172 | (0, 0) (440, 0) (440, 639) (0, 639)</code></pre> |
| 173 | |
| 174 | <p>then</p> |
| 175 | |
| 176 | <pre><code> pamhomography -mapfile park_row_rect.map park_row.ppm > park_row_rect.ppm</code></pre> |
| 177 | |
| 178 | <p>projects the building's facade from a perspective view to a rectilinear |
| 179 | front-on view. Remember that <b>pamhomography</b> ignores the parentheses and |
| 180 | commas used in <i>park_row_rect.map</i>; they merely make the file more |
| 181 | human-readable. We equivalently could have written</p> |
| 182 | |
| 183 | <pre><code> 147 51 316 105 402 595 92 560 0 0 440 0 440 639 0 639</code></pre> |
| 184 | |
| 185 | <p>or any of myriad other variations.</p> |
| 186 | |
| 187 | <p><b>pamhomography</b> can warp the image to a trapezoid to make it look like |
| 188 | it's leaning backwards in 3-D:</p> |
| 189 | |
| 190 | <pre><code> pamhomography -to "50,0 390,0 440,200 0,200" park_row.ppm > park_row_trap.ppm</code></pre> |
| 191 | |
| 192 | <p>As a very simple example,</p> |
| 193 | |
| 194 | <pre><code> pamhomography -to "440,0 0,0 0,639 440,639" park_row.ppm > park_row_flip.ppm</code></pre> |
| 195 | |
| 196 | <p>flips the image left-to-right. Note that in this case the target |
| 197 | quadrilateral's coordinates are listed in counterclockwise order because |
| 198 | that represents the correspondence between points (0, 0) ↔ (440, 0) and |
| 199 | (0, 639) ↔ (639, 0).</p> |
| 200 | |
| 201 | <p>Scaling is also straightforward. The following command scales down the |
| 202 | image from 441×640 to 341×540:</p> |
| 203 | |
| 204 | <pre><code> pamhomography -to "0,0 340,0 340,539 0,539" park_row.ppm > park_row_small.ppm</code></pre> |
| 205 | |
| 206 | <p>Let's add 100 pixels of tan border to the above. We use <b>-view</b> and |
| 207 | <b>-fill</b> to accomplish that task:</p> |
| 208 | |
| 209 | <pre><code> pamhomography -to "0,0 340,0 340,539 0,539" -view "-100,-100 440,639" -fill tan park_row.ppm > park_row_small_border.ppm</code></pre> |
| 210 | |
| 211 | <p>We can add a border without having to scale the image:</p> |
| 212 | |
| 213 | <pre><code> pamhomography -view "-100,-100 540,739" -fill tan park_row.ppm > park_row_border.ppm</code></pre> |
| 214 | |
| 215 | <p>The <b>-view</b> option can also be used to extract a rectangle out of an |
| 216 | image, discarding the rest of the image:</p> |
| 217 | |
| 218 | <pre><code> pamhomography -view "130,10 205,80" park_row.ppm > park_row_cut.ppm</code></pre> |
| 219 | |
| 220 | <p>Specifying the same set of coordinates to <b>-from</b> and <b>-to</b> has |
| 221 | the same effect but also allows you to extract non-rectangular quadrilaterals |
| 222 | from an image:</p> |
| 223 | |
| 224 | <pre><code> pamhomography -from "185,300 310,325 320,425 180,405" -to "185,300 310,325 320,425 180,405" park_row.ppm > park_row_cut_2.ppm</code></pre> |
| 225 | |
| 226 | <p>Rotation is doable but takes some effort. The challenge is that you need to |
| 227 | compute the rotated coordinates yourself. The matrix expression to rotate |
| 228 | points \((x_1, y_1)\) \((x_2, y_2)\), \((x_3, y_3)\), and \((x_4, y_4)\) |
| 229 | clockwise by \(\theta\) degrees around point \((c_x, c_y)\) is</p> |
| 230 | |
| 231 | <p>\[ \begin{bmatrix} 1 & 0 & c_x \\ 0 & 1 & c_y \\ 0 & 0 |
| 232 | & 1 \end{bmatrix} \begin{bmatrix} \cos \theta & -\sin \theta & 0 |
| 233 | \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} |
| 234 | \begin{bmatrix} 1 & 0 & -c_x \\ 0 & 1 & -c_y \\ 0 & 0 |
| 235 | & 1 \end{bmatrix} \begin{bmatrix} x_1 & x_2 & x_3 & x_4 \\ y_1 |
| 236 | & y_2 & y_3 & y_4 \\ 1 & 1 & 1 & 1 \end{bmatrix} |
| 237 | \quad. \]</p> |
| 238 | |
| 239 | <p>For example, to rotate <i>park_row.ppm</i> 30° clockwise around (220, |
| 240 | 320) you would compute</p> |
| 241 | |
| 242 | <p>\[ \begin{bmatrix} 1 & 0 & 220 \\ 0 & 1 & 320 \\ 0 & 0 |
| 243 | & 1 \end{bmatrix} \begin{bmatrix} \cos 30^{\circ} & -\sin 30^{\circ} |
| 244 | & 0 \\ \sin 30^{\circ} & \cos 30^{\circ} & 0 \\ 0 & 0 & 1 |
| 245 | \end{bmatrix} \begin{bmatrix} 1 & 0 & -220 \\ 0 & 1 & -320 \\ |
| 246 | 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 0 & 440 & 440 & 0 |
| 247 | \\ 0 & 0 & 639 & 639 \\ 1 & 1 & 1 & 1 \end{bmatrix} = |
| 248 | \begin{bmatrix} 189.4744 & 570.5256 & 251.0256 & -130.0256 \\ |
| 249 | -67.1281 & 152.8719 & 706.2621 & 486.2621 \\ 1.0000 & 1.0000 |
| 250 | & 1.0000 & 1.0000 \end{bmatrix} \quad, \]</p> |
| 251 | |
| 252 | <p>round these coordinates to integers, transpose the matrix, and produce the |
| 253 | following map file, <i>park_row_rot30.map</i>:</p> |
| 254 | |
| 255 | <pre><code> 189 -67 |
| 256 | 571 153 |
| 257 | 251 706 |
| 258 | -130 486</code></pre> |
| 259 | |
| 260 | <p>(These are the "to" coordinates; we use the default, full-image |
| 261 | "from" coordinates.) The mapping then works as in all of the |
| 262 | preceding examples:</p> |
| 263 | |
| 264 | <pre><code> pamhomography -mapfile park_row_rot30.map park_row.ppm > park_row_rot30.ppm</code></pre> |
| 265 | |
| 266 | |
| 267 | <h2 id="SEE-ALSO">SEE ALSO</h2> |
| 268 | |
| 269 | <ul> |
| 270 | <li><a href="pamcut.html">pamcut</a> |
| 271 | <li><a href="pamenlarge.html">pamenlarge</a> |
| 272 | <li><a href="pamflip.html">pamflip</a> |
| 273 | <li><a href="pamperspective.html">pamperspective</a> |
| 274 | <li><a href="pamscale.html">pamscale</a> |
| 275 | <li><a href="pamstretch.html">pamstretch</a> |
| 276 | <li><a href="pam.html">pam</a> |
| 277 | <li><a href="pnmmargin.html">pnmmargin</a> |
| 278 | <li><a href="pnmpad.html">pnmpad</a> |
| 279 | <li><a href="pnmrotate.html">pnmrotate</a> |
| 280 | <li><a href="pnmshear.html">pnmshear</a> |
| 281 | </ul> |
| 282 | |
| 283 | |
| 284 | <h2 id="HISTORY">HISTORY</h2> |
| 285 | |
| 286 | <p><b>pamhomography</b> was new in Netpbm 10.94 (March 2021). |
| 287 | |
| 288 | |
| 289 | <h2 id="AUTHOR">AUTHOR</h2> |
| 290 | |
| 291 | <p>Copyright © 2020 Scott |
| 292 | Pakin, <a href="mailto:[email protected]">[email protected]</a></p> |
| 293 | |
| 294 | |
| 295 | <h2 id="index">Table of Contents</h2> |
| 296 | |
| 297 | <ul> |
| 298 | <li><a href="#SYNOPSIS">SYNOPSIS</a></li> |
| 299 | <li><a href="#DESCRIPTION">DESCRIPTION</a></li> |
| 300 | <li><a href="#OPTIONS">OPTIONS</a></li> |
| 301 | <li><a href="#PARAMETERS">PARAMETERS</a></li> |
| 302 | <li><a href="#NOTES">NOTES</a></li> |
| 303 | <li><a href="#EXAMPLES">EXAMPLES</a></li> |
| 304 | <li><a href="#SEE-ALSO">SEE ALSO</a></li> |
| 305 | <li><a href="#HISTORY">HISTORY</a></li> |
| 306 | <li><a href="#AUTHOR">AUTHOR</a></li> |
| 307 | </ul> |
| 308 | |
| 309 | </body> |
| 310 | </html> |
| 311 | |
| 312 |