MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
geomTfrm_LensDistortion.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file geomTfrm_LensDistortion.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Read an image, correct it for radial lens discortion, and write out the corrected image.@EOL
7 @copyright
8 @parblock
9 Copyright (c) 1988-2015, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17
18 3. Neither the name of the copyright holder nor the names of its contributors may be used to edorse or promote products derived from this software
19 without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 @endparblock
28 @filedetails
29
30 The geomTfrmRevRPoly() method uses a radial polynomial transform. This method may be used to reproduce the behavior of Imagemagick's barrel distortion
31 transformation. In Imagemagick the following terminology and constraints are used:
32
33 - (X, Y) is the image center for both T & S
34 - A, B, C, & D are constants
35 - A+B+C+D=1 -- if you don't provide D it will be computed
36
37 This is all put together on the command line something like this:
38 @verbatim
39 -distort Barrel "A B C D X Y"
40 @endverbatim
41
42 We can demonstrate the similar behavior between Imagemagick & this example program like this:
43
44 - Make test images
45 @verbatim
46 make test_images
47 ./test_images.exe
48 @endverbatim
49
50 - Compute the new image with imagemagik
51 @verbatim
52 rm test_images_mcgrid_fimg.tiff
53 magick test_images_mcgrid.tiff -background Green -virtual-pixel Background -interpolate Bilinear -filter point -distort barrel "0.0 -0.0160 0.0" test_images_mcgrid_fimg.tiff
54 start test_images_mcgrid_fimg.tiff
55 @endverbatim
56
57 - Compute the new image with MRaster
58 @verbatim
59 make geomTfrm_LensDistortion
60 ./geomTfrm_LensDistortion.exe test_images_mcgrid.tiff
61 mv geomTfrm_LensDistortion.tiff test_images_mcgrid_fmr.tiff
62 start test_images_mcgrid_fmr.tiff
63 @endverbatim
64
65 - Use compare aginst the two imgages
66 @verbatim
67 magick compare -metric mae test_images_mcgrid_fmr.tiff test_images_mcgrid_fimg.tiff out.tiff
68 start out.tiff
69 @endverbatim
70
71 - Subtract the two images
72 @verbatim
73 magick test_images_mcgrid_fmr.tiff test_images_mcgrid_fimg.tiff -compose Mathematics -define compose:args='0,1,-1,0' -composite out.tiff
74 start out.tiff
75 @endverbatim
76
77 Note the images are not identical, but they are very close.
78*/
79/*******************************************************************************************************************************************************.H.E.**/
80/** @cond exj */
81
82//--------------------------------------------------------------------------------------------------------------------------------------------------------------
83#include "ramCanvas.hpp"
84
85//--------------------------------------------------------------------------------------------------------------------------------------------------------------
86int main(int argc, char *argv[]) {
87 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
88 mjr::ramCanvas3c8b dRamCanvas;
89
90 if (argc < 2) {
91 fprintf(stderr, "ERROR argument required!\n");
92 exit(1);
93 }
94
95 int rRet;
96 if((rRet=dRamCanvas.readTIFFfile(argv[1]))) {
97 fprintf(stderr, "ERROR(%d) reading file %s\n", rRet, argv[1]);
98 exit(1);
99 }
100
101 double A = 0.0000;
102 double B = -0.0160;
103 double C = 0.0000;
104 double D = 1.0000 - A - B - C;
105
106 double Xo = dRamCanvas.getNumPixX() / 2.0;
107 double Yo = dRamCanvas.getNumPixY() / 2.0;
108 double Sr = std::min(dRamCanvas.getNumPixX(), dRamCanvas.getNumPixY()) / 2.0;
109 double Sout = 0.9;
110
111 std::vector<double> poly {A, B, C, D};
112
113 mjr::ramCanvas3c8b uRamCanvas = dRamCanvas.geomTfrmRevRPoly(poly, Sr, Xo, Yo, Sout);
114
115 uRamCanvas.writeTIFFfile("geomTfrm_LensDistortion.tiff");
116
117 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
118 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
119
120 return 0;
121}
122/** @endcond */
int main(int argc, char *argv[])