├── demo ├── __init.py__ ├── UVtruth.inr ├── demo_3dshift.png ├── demo_3dvortex.png ├── run010050000.tif ├── run010050010.tif ├── demo_particles.png └── inr.py ├── doc ├── dfd.png └── dfd_func.png ├── README.md ├── LICENSE ├── lib └── highorder.py └── pytyphoon.py /demo/__init.py__: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /doc/dfd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/doc/dfd.png -------------------------------------------------------------------------------- /demo/UVtruth.inr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/demo/UVtruth.inr -------------------------------------------------------------------------------- /doc/dfd_func.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/doc/dfd_func.png -------------------------------------------------------------------------------- /demo/demo_3dshift.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/demo/demo_3dshift.png -------------------------------------------------------------------------------- /demo/demo_3dvortex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/demo/demo_3dvortex.png -------------------------------------------------------------------------------- /demo/run010050000.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/demo/run010050000.tif -------------------------------------------------------------------------------- /demo/run010050010.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/demo/run010050010.tif -------------------------------------------------------------------------------- /demo/demo_particles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pderian/PyTyphoon/HEAD/demo/demo_particles.png -------------------------------------------------------------------------------- /demo/inr.py: -------------------------------------------------------------------------------- 1 | ### 2 | import re 3 | ### 4 | import numpy 5 | ### 6 | 7 | ### .INR MOTION/IMAGES ### 8 | ########################## 9 | 10 | def readINR(fileName, printInfo=False): 11 | """ 12 | Read an INR image file. 13 | 14 | Arguments: 15 | * fileName the .inr filename 16 | * printInfo print image info 17 | 18 | Note: to preserve memory continuity, image is loaded with dimensions in the following order: 19 | 0-V (channel), 1-Z (depth), 2-Y (height), 3-X (width). 20 | """ 21 | # open file 22 | with open(fileName, 'rb') as inrFile: 23 | ##### HEADER ##### 24 | ################## 25 | # read the header: chunk of 256 char 26 | header = inrFile.read(256).decode('utf-8') 27 | # the keys we look for 28 | dimKey=['VDIM','ZDIM','YDIM', 'XDIM'] 29 | typeKey='TYPE' 30 | sizeKey='PIXSIZE' 31 | # default values 32 | dataDim = [] 33 | dataType = '' 34 | dataBitSize = 0 35 | # read dimensions 36 | for key in dimKey: 37 | match = re.search(key+'=(?P[0-9]+)', header) 38 | if match: 39 | dataDim.append(int(match.group('val'))) 40 | else: 41 | print(str(key)+' not found !') 42 | # read type 43 | key = typeKey 44 | match = re.search(key+'=(?P[a-z]+)', header) 45 | if match: 46 | dataType = match.group('val') 47 | else: 48 | print(str(key)+' not found !') 49 | # read pixel size (in BITS) 50 | key = sizeKey 51 | match = re.search(key+'=(?P[0-9]+) bits', header) 52 | if match: 53 | dataBitSize = int(match.group('val')) 54 | else: 55 | print(str(key)+' not found !') 56 | # print info 57 | if printInfo: 58 | print('File: '+fileName+'\nDimensions: '+str(dataDim)+'\nType: '\ 59 | +str(dataType)+'\nPixel Size: '+str(dataBitSize)) 60 | ##### DATA ##### 61 | ################ 62 | # number of bytes to read 63 | nByte = dataDim[0]*dataDim[1]*dataDim[2]*dataDim[3]*(dataBitSize//8) 64 | # create a 1D float array 65 | values = numpy.frombuffer(inrFile.read(nByte), dtype=numpy.dtype('{:s}{:d}'.format(dataType, dataBitSize))) 66 | # resize to 4-D array 67 | values.shape = dataDim 68 | # clean shape from dimensions equal to 1 and return 69 | return values.squeeze() 70 | 71 | def readMotion(fileName, printInfo=False, reverse=False): 72 | """ 73 | Read a motion from an INR file. 74 | 75 | Arguments: 76 | 77 | * fileName the .inr file 78 | * printInfo print motion info. 79 | * reverse reverse the vertical axis and the sign of vertical component. 80 | """ 81 | # read INR file 82 | motion = readINR(fileName,printInfo) 83 | v1 = motion[0,:,:] 84 | v2 = motion[1,:,:] 85 | # reverse reference maybe 86 | if reverse: 87 | v1 = numpy.flipud(v1) 88 | v2 = -numpy.flipud(v2) 89 | return v1,v2 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyTyphoon 2 | Python implementation of [Typhoon motion estimator](http://www.pierrederian.net/typhoon.html): dense estimation of 2D/3D optical flow on wavelet bases, primarily aimed at **fluid motion estimation**. 3 | 4 | ## Important remarks 5 | At the moment, the wavelet-based data DFD term [(Dérian et al., 2013)] only is provided: the **high-order regularizers** [(Kadri-Harouna et al., 2013)] **are not included** in this implementation. 6 | 7 | The reference implementation used in [(Dérian et al., 2015)] and [(Dérian et al., 2017)] is written in C++ and GPU-accelerated with CUDA, and contains the high-order regularizers. It is the property of Inria (FR) and the CSU Chico Research 8 | Foundation (Ca, USA), and can be licensed from these institutions. 9 | **This Python implementation is not the same as the 10 | reference** for many reasons, and it is obviously much slower. 11 | 12 | ## Requirements 13 | - [Numpy, Scipy](https://scipy.org/); 14 | - [PyWavelets](https://github.com/PyWavelets/pywt); 15 | - [Pillow](https://pillow.readthedocs.io/) and [Matplotlib](https://matplotlib.org/) for the demos. 16 | 17 | Tested with Anaconda Python 3.6.1, Numpy 1.12.1, Scipy 0.19.1, PyWavelet 0.5.2. 18 | 19 | ## Usage 20 | The `Typhoon` class can be imported from other modules/scripts to perform estimations as needed. 21 | 22 | The script can also work as a standalone estimator in simple cases, e.g.: 23 | ``` 24 | python pytyphoon.py -i0 path/to/im0.jpg -i1 path/to/im1.jpg -wav 'db3' --display 25 | ``` 26 | will solve the problem for image pair (`im0.jpg`, `im1.jpg`) and wavelet Daubechies-3. 27 | See `python pytyphoon.py -h` for the complete list of parameters. 28 | 29 | ## How does it work? 30 | 31 | Typhoon solves a **dense variational optical flow** problem, that is to say: (i) it provides one motion vector at every pixel of input images and (ii) it estimates the entire vector field altogether. 32 | 33 | To do so, it looks for the motion field which minimizes the **displaced frame difference** (DFD): 34 | 35 | ![DFD](doc/dfd.png) 36 | 37 | This is achieved by minimizing the following functional: 38 | 39 | ![DFD functional](doc/dfd_func.png) 40 | 41 | where the integral is taken over the image. 42 | The functional above is **non-linear with respect to the motion field**. 43 | This has the advantage of better handling large displacement, but complicates the minimization process. 44 | 45 | For the non-linear minimization to succeed, the solution should lie reasonable "close" to the first guess. This is where **wavelets bases** come into play: by providing a multiscale representation of the motion field, they enable to estimate the motion iteratively from its coarsest scales to the finests. 46 | 47 | The minimization is handled by L-BFGS, which is efficient memory-wise and only requires the functional value and its gradient. 48 | 49 | ## Demos 50 | 51 | These demos are shipped with the project. 52 | 53 | ### (2D) Synthetic particle images 54 | Simple 2d estimation using synthetic particle images (256x256 pixels) originally created for the [FLUID project](http://fluid.irisa.fr/data-eng.htm) (image database #1). Run: 55 | ``` 56 | python pytyphoon.py --demo particles 57 | ``` 58 | ![Particle results](demo/demo_particles.png) 59 | 60 | ### (3D) Homogeneous shift 61 | Simple 3d estimation using synthetic images (64x64x64 pixels) obtained by filtering random normal noise at various scales. The displacements are integer shifts along each of the 3 dimensions. Run: 62 | ``` 63 | python pytyphoon.py --demo 3dshift 64 | ``` 65 | ![3dshift results](demo/demo_3dshift.png) 66 | 67 | ### (3D) Column vortex 68 | Simple 3d estimation using synthetic images (96x96x96 pixels) obtained by filtering random normal noise at various scales. The displacement field is a column vortex (first two axes) with an updraft increasing linearly along the third axis. Run: 69 | ``` 70 | python pytyphoon.py --demo 3dvortex 71 | ``` 72 | ![3dshift results](demo/demo_3dvortex.png) 73 | 74 | ## References 75 | - [(Dérian et al., 2017)] 76 | Dérian, P. & Almar, R. 77 | "Wavelet-based Optical Flow Estimation of Instant Surface Currents from Shore-based and UAV Video". 78 | _IEEE Transactions on Geoscience and Remote Sensing_, Vol. 55, pp. 5790-5797, 2017. 79 | - [(Dérian et al., 2015)] 80 | Dérian, P.; Mauzey, C. F. and Mayor, S. D. 81 | "Wavelet-based optical flow for two-component wind field estimation from single aerosol lidar data". 82 | _Journal of Atmospheric and Oceanic Technology_, Vol. 32, pp. 1759-1778, 2015. 83 | - [(Dérian et al., 2013)] 84 | Dérian, P.; Héas, P.; Herzet, C. & Mémin, E. 85 | "Wavelets and Optical Flow Motion Estimation". 86 | _Numerical Mathematics: Theory, Method and Applications_, Vol. 6, pp. 116-137, 2013. 87 | - [(Kadri-Harouna et al., 2013)] Kadri Harouna, S.; Dérian, P.; Héas, P. and Mémin, E. 88 | "Divergence-free Wavelets and High Order Regularization". 89 | _International Journal of Computer Vision_, Vol. 103, pp. 80-99, 2013. 90 | 91 | [(Dérian et al., 2017)]: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7964775&isnumber=8049540 92 | [(Dérian et al., 2015)]: http://journals.ametsoc.org/doi/abs/10.1175/JTECH-D-15-0010.1 93 | [(Dérian et al., 2013)]: https://www.cambridge.org/core/journals/numerical-mathematics-theory-methods-and-applications/article/wavelets-and-optical-flow-motion-estimation/2A9D13B316F000F0530AD42621B42FFD 94 | [(Kadri-Harouna et al., 2013)]: https://link.springer.com/article/10.1007/s11263-012-0595-7 95 | 96 | ## Todo 97 | - **NetCDF for output results?** in standalone mode; 98 | - support of masks; 99 | - some regularizers; 100 | - alternative penalization functions; 101 | - divergence-free wavelets; 102 | - ... 103 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /lib/highorder.py: -------------------------------------------------------------------------------- 1 | """[WIP] High-order regularization terms for PyTyphoon. 2 | 3 | Note: 4 | - the computation of the regularization terms in HighOrderRegularizerConv 5 | is a convolution-based one, likely closer to what was introduced in [1]. 6 | - the computation of the regularization terms in HighOrderRegularizerMat 7 | is a matrix-based one as in [2], [3]. 8 | - largely untested, use with care. 9 | 10 | References: 11 | [1] BEYLKIN, Gregory. 12 | On the representation of operators in bases of compactly supported wavelets. 13 | SIAM Journal on Numerical Analysis, 1992, vol. 29, no 6, p. 1716-1740. 14 | [2] KADRI-HAROUNA, S., DÉRIAN, Pierre, HÉAS, Patrick, et al. 15 | Divergence-free wavelets and high order regularization. 16 | International journal of computer vision, 2013, vol. 103, no 1, p. 80-99. 17 | [3] DÉRIAN, Pierre. 18 | Wavelets and Fluid Motion Estimation. 19 | PhD thesis, MATISSE doctoral school, Université Rennes 1, 2012. 20 | """ 21 | # Standard 22 | import logging 23 | 24 | # Third-party 25 | import numpy as np 26 | import pywt 27 | import scipy.ndimage as ndimage 28 | import scipy.optimize as optimize 29 | 30 | 31 | LOGGER = logging.getLogger(__name__) 32 | 33 | 34 | def connection_coefficients(wav, order): 35 | """Find the connection coefficients of the wavelet at given order. 36 | 37 | :param wav: a pywt.Wavelet; 38 | :param order: the derivation order; 39 | :param return: a vector of coefficients. 40 | 41 | This is the evaluation of L2 dot-products of the form: 42 | \int[ Phi(x) (d^(n)/dx^n){Phi}(x) ]dx 43 | where Phi is the mother wavelet. 44 | 45 | Written by P. DERIAN 2018-01-09. 46 | Updated by P. DERIAN 2019-02-15: using reconstruction filter. 47 | """ 48 | ctol = 1e-15 # Tolerance for coefficients 49 | etol = 1e-4 # Tolerance for eigenvalues 50 | # Get the low-pass reconstruction filter 51 | lo = wav.rec_lo 52 | len_lo = len(lo) 53 | # Create the matrix 54 | dim = 2*len_lo - 3 55 | matrix = np.zeros((dim, dim)) 56 | for m in range(dim): 57 | for n in range(dim): 58 | tmp = 0. 59 | # for each filter value 60 | for p, lo_p in enumerate(lo): 61 | idx = m - 2*n + p + len_lo - 2 62 | if (idx>=0 and idxctol: 66 | matrix[n,m] = tmp 67 | # Find coefficient vector: solve eigenvalue problem 68 | eval, evec = np.linalg.eig(matrix) 69 | # Check if any REAL eigenvalue matches our order 70 | sigma = 1./float(2**order) 71 | ev_found = False 72 | for i, ev in enumerate(eval): 73 | if (np.abs(np.real(ev)-sigma) eps) 616 | # axes[1].imshow(abs(N1) > eps) 617 | # axes[2].imshow(abs(N2) > eps) 618 | axes[0].imshow(N0, cmap='RdYlBu_r', vmin=-1, vmax=1) 619 | axes[1].imshow(N1, cmap='RdYlBu_r', vmin=-1, vmax=1) 620 | axes[2].imshow(N2, cmap='RdYlBu_r', vmin=-1, vmax=1) 621 | plt.show() 622 | 623 | def demo_highorder_conv(): 624 | """ 625 | 626 | Written by P. DERIAN 2018-01-09. 627 | """ 628 | # Parameters 629 | wav = pywt.Wavelet('bior6.8') 630 | wav_levels = 3 # Note: levels have no influence with the convolution-based form. 631 | fd_order = 8 # Order of finite-differences filters for truth 632 | U2, U1 = inr.readMotion('../demo/UVtruth.inr') 633 | # Wavelet-based computations 634 | hor_conv = HighOrderRegularizerConv(wav) 635 | C1 = pywt.wavedecn(U1, hor_conv.wav, level=wav_levels, mode=hor_conv.mode) 636 | C2 = pywt.wavedecn(U2, hor_conv.wav, level=wav_levels, mode=hor_conv.mode) 637 | l2norm_hor, _ = hor_conv.evaluate(C1, C2, 'l2norm') 638 | hornschunk_hor, _ = hor_conv.evaluate(C1, C2, 'hornschunck') 639 | divergence_hor, _ = hor_conv.evaluate(C1, C2, 'div') 640 | curl_hor, _ = hor_conv.evaluate(C1, C2, 'curl') 641 | laplacian_hor, _ = hor_conv.evaluate(C1, C2, 'laplacian') 642 | graddiv_hor, _ = hor_conv.evaluate(C1, C2, 'graddiv') 643 | gradcurl_hor, _ = hor_conv.evaluate(C1, C2, 'gradcurl') 644 | # Compute the truth 645 | # Note: not real "truth" since gradients are computed by finite differences. 646 | d1 = d1_filters[fd_order] 647 | d2 = d2_filters[fd_order] 648 | l2norm_truth = l2norm(U1, U2) 649 | hornschunk_truth = hornschunk(U1, U2, d1) 650 | divergence_truth = divergence(U1, U2, d1) 651 | curl_truth = curl(U1, U2, d1) 652 | laplacian_truth = laplacian(U1, U2, d2) 653 | graddiv_truth = graddiv(U1, U2, d1) 654 | gradcurl_truth = gradcurl(U1, U2, d1) 655 | # Print out 656 | print('\n** Convolution-based high-order computations') 657 | print('Wavelet: {}'.format(wav)) 658 | print('Truth: centered finite-differences, order-{} accuracy'.format(fd_order)) 659 | print('High-order terms:') 660 | for label, test, truth in zip( 661 | ['l2-norm', 'H&S', 'divergence', 'curl', 'laplacian', 'graddiv', 'gradcurl'], 662 | [l2norm_hor, hornschunk_hor, divergence_hor, curl_hor, laplacian_hor, graddiv_hor, gradcurl_hor], 663 | [l2norm_truth, hornschunk_truth, divergence_truth, curl_truth, laplacian_truth, graddiv_truth, gradcurl_truth], 664 | ): 665 | print('\t{:>10}: rel. err={:.3f} % (wav={:.3f}, ref={:.3f})'.format( 666 | label, 100.*np.abs(test-truth)/truth, test, truth)) 667 | 668 | def demo_highorder_mat(): 669 | """ 670 | 671 | Written by P. DERIAN 2019-02-18. 672 | """ 673 | # Parameters 674 | wav = pywt.Wavelet('bior6.8') 675 | wav_levels = 2 # Note: levels have no influence with the convolution-based form. 676 | fd_order = 8 # Order of finite-differences filters for truth 677 | U2, U1 = inr.readMotion('../demo/UVtruth.inr') 678 | # Wavelet-based computations 679 | hor_mat = HighOrderRegularizerMat(wav, wav_levels, U1.shape) 680 | C1, _ = pywt.coeffs_to_array(pywt.wavedec2( 681 | U1, hor_mat.wav, level=wav_levels, mode=hor_mat.mode)) 682 | C2, _ = pywt.coeffs_to_array(pywt.wavedec2( 683 | U2, hor_mat.wav, level=wav_levels, mode=hor_mat.mode)) 684 | l2norm_hor, _ = hor_mat.evaluate(C1, C2, 'l2norm') 685 | hornschunk_hor, _ = hor_mat.evaluate(C1, C2, 'hornschunck') 686 | divergence_hor, _ = hor_mat.evaluate(C1, C2, 'div') 687 | curl_hor, _ = hor_mat.evaluate(C1, C2, 'curl') 688 | laplacian_hor, _ = hor_mat.evaluate(C1, C2, 'laplacian') 689 | graddiv_hor, _ = hor_mat.evaluate(C1, C2, 'graddiv') 690 | gradcurl_hor, _ = hor_mat.evaluate(C1, C2, 'gradcurl') 691 | # Note: not actual "truth" since gradients are computed by finite differences. 692 | d1 = d1_filters[fd_order] 693 | d2 = d2_filters[fd_order] 694 | l2norm_truth = l2norm(U1, U2) 695 | hornschunk_truth = hornschunk(U1, U2, d1) 696 | divergence_truth = divergence(U1, U2, d1) 697 | curl_truth = curl(U1, U2, d1) 698 | laplacian_truth = laplacian(U1, U2, d2) 699 | graddiv_truth = graddiv(U1, U2, d1) 700 | gradcurl_truth = gradcurl(U1, U2, d1) 701 | # Print out 702 | print('\n** Matrix-based high-order computations') 703 | print('Wavelet: {}'.format(wav)) 704 | print('Truth: centered finite-differences, order-{} accuracy'.format(fd_order)) 705 | print('High-order terms:') 706 | for label, test, truth in zip( 707 | ['l2-norm', 'H&S', 'divergence', 'curl', 'laplacian', 'grad(div)', 'grad(curl)'], 708 | [l2norm_hor, hornschunk_hor, divergence_hor, curl_hor, laplacian_hor, graddiv_hor, gradcurl_hor], 709 | [l2norm_truth, hornschunk_truth, divergence_truth, curl_truth, laplacian_truth, graddiv_truth, gradcurl_truth], 710 | ): 711 | print('\t{:>10}: rel. err={:.3f} % (wav={:.3f}, ref={:.3f})'.format( 712 | label, 100.*np.abs(test-truth)/truth, test, truth)) 713 | 714 | def compare_highorders(name='laplacian'): 715 | """ 716 | """ 717 | # Parameters 718 | wav = pywt.Wavelet('bior6.8') 719 | wav_levels = 3 # Note: levels have no influence with the convolution-based form. 720 | fd_order = 8 # Order of finite-differences filters for truth 721 | U2, U1 = inr.readMotion('../demo/UVtruth.inr') 722 | # Regularizers 723 | hor_conv = HighOrderRegularizerConv(wav) 724 | hor_mat = HighOrderRegularizerMat(wav, wav_levels, U1.shape) 725 | # Wavelet coeffs 726 | C1 = pywt.wavedecn(U1.astype(np.float64), wav, level=wav_levels, mode='periodization') 727 | C2 = pywt.wavedecn(U2.astype(np.float64), wav, level=wav_levels, mode='periodization') 728 | C1c, _ = pywt.coeffs_to_array(C1) 729 | C2c, _ = pywt.coeffs_to_array(C2) 730 | # Compute terms 731 | value_conv, grad_conv = hor_conv.evaluate(C1, C2, name) 732 | value_mat, grad_mat = hor_mat.evaluate(C1c, C2c, name) 733 | # Display 734 | print('\n** Comparison of convolution vs Matrix high-order computations') 735 | print('{}: conv={:.3f}, mat={:.3f}'.format(name, value_conv, value_mat)) 736 | fig, axes = plt.subplots(2, 4) 737 | vmin = -0.5 738 | vmax = 0.5 739 | # Conv-based 740 | g1_conv, _ = pywt.coeffs_to_array(grad_conv[0]) 741 | g2_conv, _ = pywt.coeffs_to_array(grad_conv[1]) 742 | axes[0, 0].imshow(g1_conv, vmin=vmin, vmax=vmax) 743 | axes[1, 0].imshow(g2_conv, vmin=vmin, vmax=vmax) 744 | # Matrix-based 745 | g1_mat, g2_mat = grad_mat 746 | axes[0, 1].imshow(g1_mat, vmin=vmin, vmax=vmax) 747 | axes[1, 1].imshow(g2_mat, vmin=vmin, vmax=vmax) 748 | # Errors 749 | axes[0, 2].imshow(np.log10(np.abs(g1_conv-g1_mat)/abs(g1_mat)), cmap='RdYlBu_r', vmin=-5, vmax=5) 750 | axes[1, 2].imshow(np.log10(np.abs(g2_conv-g2_mat)/abs(g2_mat)), cmap='RdYlBu_r', vmin=-5, vmax=5.) 751 | # Ratios 752 | axes[0, 3].imshow(np.abs(g1_conv/g1_mat), cmap='RdYlBu_r', vmin=0.5, vmax=2.) 753 | axes[1, 3].imshow(np.abs(g2_conv/g2_mat), cmap='RdYlBu_r', vmin=0.5, vmax=2.) 754 | plt.show() 755 | 756 | def check_conv(): 757 | """ 758 | """ 759 | # Data parameters 760 | fine_scale = 7 761 | nx = 1 762 | ny = 3 763 | sizex = nx * (2**fine_scale) 764 | sizey = ny * (2**fine_scale) 765 | wx = 3. # x-freq 766 | wy = 10. # y-freq 767 | # Wavelet parameters 768 | wav = pywt.Wavelet('db10') 769 | wav_levels = 3 # Note: levels have no influence with the convolution-based form. 770 | # The grid 771 | x, dx = np.linspace(0, 2.*np.pi*nx, sizex, endpoint=False, retstep=True) 772 | y, dy = np.linspace(0, 2.*np.pi*ny, sizey, endpoint=False, retstep=True) 773 | yy, xx = np.meshgrid(y, x) 774 | print(yy.size, np.sqrt(yy.size)) 775 | # The data 776 | f = np.cos(wx*xx) + np.sin(wy*yy) 777 | fx = -wx*np.sin(wx*xx) 778 | fy = wy*np.cos(wy*yy) 779 | fxx = -(wx**2)*np.cos(wx*xx) 780 | fyy = -(wy**2)*np.sin(wy*yy) 781 | fxy = np.zeros_like(xx) 782 | # Theoretical norms 783 | l2t = { 784 | 'f': 4. * (np.pi**2) * nx *ny, 785 | 'fx': 2 * (np.pi**2) * nx * ny * (wx**2), 786 | 'fy': 2 * (np.pi**2) * nx * ny * (wy**2), 787 | 'fxx': 2 * (np.pi**2) * nx * ny * (wx**4), 788 | 'fyy': 2 * (np.pi**2) * nx * ny * (wy**4), 789 | 'fxy': 0., 790 | } 791 | # Rectangle quadrature formulas 792 | def l2norm_rectangle(field): 793 | return (dx*dy)*np.square(field).sum() 794 | l2r_f = l2norm_rectangle(f) 795 | l2r_fx = l2norm_rectangle(fx) 796 | l2r_fy = l2norm_rectangle(fy) 797 | l2r_fxx = l2norm_rectangle(fxx) 798 | l2r_fyy = l2norm_rectangle(fyy) 799 | l2r_fxy = l2norm_rectangle(fxy) 800 | # Wavelet 801 | hor_conv = HighOrderRegularizerConv(wav) 802 | cf = pywt.wavedecn(f, hor_conv.wav, level=wav_levels, mode=hor_conv.mode) 803 | l2w_f = hor_conv.norm(cf, 0, 0) 804 | l2w_fx = hor_conv.norm(cf, 1, 0) 805 | l2w_fy = hor_conv.norm(cf, 0, 1) 806 | l2w_fxx = hor_conv.norm(cf, 2, 0) 807 | l2w_fyy = hor_conv.norm(cf, 0, 2) 808 | l2w_fxy = hor_conv.norm(cf, 1, 1) 809 | # Print out 810 | for label, l2r, l2w in zip( 811 | ['f', 'fx', 'fy', 'fxx', 'fyy', 'fxy'], 812 | [l2r_f, l2r_fx, l2r_fy, l2r_fxx, l2r_fyy, l2r_fxy], 813 | [l2w_f, l2w_fx, l2w_fy, l2w_fxx, l2w_fyy, l2w_fxy], 814 | ): 815 | print('{:>3}: exact={:.3f}, rectangle approx.={:.3f} ; wavelet approx.={:.3f}'.format( 816 | label, 817 | l2t[label], 818 | l2r, 819 | l2w, 820 | )) 821 | print('\trect/exact={:.3f} ; wav/exact={:.3f}'.format( 822 | l2r / l2t[label], 823 | l2w / l2t[label], 824 | )) 825 | # Display 826 | fig, ax = plt.subplots() 827 | ax.imshow(f, extent=[y[0], y[-1], x[0], x[-1]], origin='lower') 828 | ax.set_xlabel('y') 829 | ax.set_ylabel('x') 830 | ax.set_title('f(x, y)') 831 | plt.show() 832 | 833 | 834 | # demo_mass_matrix() 835 | demo_highorder_conv() 836 | demo_highorder_mat() 837 | compare_highorders('l2norm') 838 | compare_highorders('hornschunck') 839 | compare_highorders('div') 840 | compare_highorders('curl') 841 | compare_highorders('laplacian') 842 | check_conv() -------------------------------------------------------------------------------- /pytyphoon.py: -------------------------------------------------------------------------------- 1 | """Python implementation of the Typhoon algorithm solving dense 2D optical flow problems. 2 | 3 | Description: This module provides pure Python implementation of Typhoon. At the moment, 4 | only the data term [1] is provided. The high-order regularizers [2] are not implemented 5 | in this module. 6 | 7 | Disclaimer: The reference implementation used in [3], [4] is written in C++ and 8 | GPU-accelerated with CUDA. It is the property of Inria (FR) and the CSU Chico Research 9 | Foundation (Ca, USA). This Python implementation is *not* exactly the same as the 10 | reference for many reasons, and it is obviously much slower. 11 | 12 | Dependencies: 13 | - numpy, scipy; 14 | - Pillow; 15 | - pywavelets. 16 | 17 | References: 18 | [1] Data-term & basic algorithm: 19 | Derian, P.; Heas, P.; Herzet, C. & Memin, E. 20 | "Wavelets and Optical Flow Motion Estimation". 21 | Numerical Mathematics: Theory, Method and Applications, Vol. 6, pp. 116-137, 2013. 22 | [2] High-order regularization terms: 23 | Kadri Harouna, S.; Derian, P.; Heas, P. & Memin, E. 24 | "Divergence-free Wavelets and High Order Regularization". 25 | International Journal of Computer Vision, Vol. 103, pp. 80-99, 2013. 26 | [3] Application to wind estimation from lidar images: 27 | Derian, P.; Mauzey, C. F. & Mayor, S. D. 28 | "Wavelet-based optical flow for two-component wind field estimation from single aerosol lidar data" 29 | Journal of Atmospheric and Oceanic Technology, Vol. 32, pp. 1759-1778, 2015. 30 | [4] Application to near-shore surface current estimation from shore and UAV cameras: 31 | Derian, P. & Almar, R. 32 | "Wavelet-based Optical Flow Estimation of Instant Surface Currents from Shore-based and UAV Video" 33 | IEEE Transactions on Geoscience and Remote Sensing, Vol. 55, pp. 5790-5797, 2017. 34 | 35 | Written by P. DERIAN 2018-01-09. 36 | www.pierrederian.net - contact@pierrederian.net 37 | """ 38 | __version__ = 0.1 39 | ### 40 | import numpy 41 | import pywt 42 | import scipy 43 | import scipy.ndimage as ndimage 44 | import scipy.optimize as optimize 45 | ### 46 | 47 | class OpticalFlowCore: 48 | """Core functions for optical flow. 49 | 50 | Written by P. DERIAN 2018-01-09. 51 | Updated by P. DERIAN 2018-01-11: generic n-d version, added __str__(). 52 | """ 53 | 54 | def __init__(self, shape, dtype=numpy.float32, interpolation_order=3, sigma_blur=0.5): 55 | """Constructor. 56 | 57 | :param shape: the grid shape; 58 | :param dtype: numpy.float32 or numpy.float64. 59 | :param interpolation_order: spline interpolation order>0, faster when lower. 60 | :param sigma_blur: sigma of gaussian blur applied before spatial gradient computation. 61 | 62 | Written by P. DERIAN 2018-01-09. 63 | Updated by P. DERIAN 2018-01-11: generic n-d version, changed boundary condition. 64 | """ 65 | self.dtype = dtype 66 | ### grid coordinates 67 | self.shape = shape 68 | self.ndim = len(self.shape) 69 | # 1D 70 | self.x = tuple(numpy.arange(s, dtype=self.dtype) for s in self.shape) 71 | # N-D 72 | self.X = numpy.indices(self.shape, dtype=self.dtype) 73 | self.Xcoords = numpy.vstack((X.ravel() for X in self.X)) 74 | ### Misc parameters 75 | self.sigma_blur = sigma_blur #gaussian blur sigma before spatial gradient computation 76 | self.interpolation_order = interpolation_order #pixel interpolation order 77 | self.boundary_condition = 'mirror' #boundary condition 78 | # Note: setting the bc to 'constant', 'reflect' or 'wrap' seems to cause issues...? 79 | # while 'nearest', 'mirror' are OK. 80 | ### Buffer 81 | self.buffer = numpy.zeros(numpy.prod(self.shape), dtype=dtype) 82 | 83 | def __str__(self): 84 | """ 85 | Written by P. DERIAN 2018-10-11. 86 | """ 87 | param_str = '\n\tshape={}'.format(self.shape) 88 | param_str += '\n\tdtype={}'.format(self.dtype.__name__) 89 | param_str += '\n\tinterpolation order={}'.format(self.interpolation_order) 90 | param_str += '\n\tsigma blur={}'.format(self.sigma_blur) 91 | return self.__class__.__name__ + param_str 92 | 93 | def DFD(self, im0, im1, U): 94 | """Compute the DFD. 95 | 96 | :param im0: the first (grayscale) image; 97 | :param im1: the second (grayscale) image; 98 | :param U: displacements (U1, U2, ...) along the first, second, ... axis; 99 | :return: the DFD. 100 | 101 | Written by P. DERIAN 2018-01-09. 102 | Updated by P. DERIAN 2018-01-11: generic n-d version. 103 | """ 104 | # warp im1 105 | map_coords = self.Xcoords.copy() 106 | for n, Ui in enumerate(U): 107 | map_coords[n] += Ui.ravel() 108 | im1_warp = ndimage.interpolation.map_coordinates(im1, map_coords, 109 | order=self.interpolation_order, 110 | mode=self.boundary_condition) 111 | # difference 112 | return im1_warp.reshape(self.shape) - im0 113 | 114 | def DFD_gradient(self, im0, im1, U): 115 | """Compute the displaced frame difference (DFD) functional value and its gradients. 116 | 117 | :param im0: the first (grayscale) image; 118 | :param im1: the second (grayscale) image; 119 | :param U: displacements (U1, U2, ...) along the first, second, ... axis; 120 | :return: dfd, (grad1, grad2, ...) 121 | - DFD: the value of the functional; 122 | - (grad1, grad2, ...): the gradient of the DFD functional w.r.t. component U1, U2, .... 123 | 124 | Written by P. DERIAN 2018-01-09. 125 | Updated by P. DERIAN 2018-01-11: generic n-d version. 126 | """ 127 | # warp im1->buffer 128 | map_coords = self.Xcoords.copy() 129 | for n, Ui in enumerate(U): 130 | map_coords[n] += Ui.ravel() 131 | ndimage.interpolation.map_coordinates(im1, map_coords, 132 | output=self.buffer, 133 | order=self.interpolation_order, 134 | mode=self.boundary_condition) 135 | im1_warp = self.buffer.reshape(self.shape) 136 | # difference 137 | dfd = im1_warp - im0 138 | # spatial gradients 139 | # [TODO] use derivative of gaussians? 140 | if self.sigma_blur>0: 141 | im1_warp = ndimage.filters.gaussian_filter(im1_warp, self.sigma_blur) 142 | grad = numpy.gradient(im1_warp) 143 | grad = (g*dfd for g in grad) 144 | # return 145 | return 0.5*numpy.sum(dfd**2), grad 146 | 147 | class Typhoon: 148 | """Implements the Typhoon algorithm: dense optical flow estimation on wavelet bases. 149 | 150 | Written by P. DERIAN 2018-01-09. 151 | Updated by P. DERIAN 2018-01-10: added default values and solve_fast(). 152 | """ 153 | 154 | DEFAULT_WAV = 'haar' #default wavelet name 155 | DEFAULT_MODE = 'zero' #default wavelet signal extension mode 156 | 157 | def __init__(self, shape=None): 158 | """Instance constructor with optional shape. 159 | 160 | :param shape: optional shape of the problem. 161 | 162 | Written by P. DERIAN 2018-01-09. 163 | """ 164 | self.core = OpticalFlowCore(shape) if (shape is not None) else None 165 | 166 | def solve(self, im0, im1, wav=None, mode=None, 167 | levels_decomp=3, levels_estim=None, U0=None, 168 | interpolation_order=3, sigma_blur=0.5, 169 | ): 170 | """Solve the optical flow problem for given images and wavelet. 171 | 172 | :param im0: the first (grayscale) image; 173 | :param im1: the second (grayscale) image; 174 | :param wav: the name of the wavelet; 175 | :param mode: the signal extension mode ('zero', 'periodization'), see [a]. 176 | :param levels_decomp: number of decomposition levels; 177 | :param levels_estim: number of estimation levels (<=levels_decomp); 178 | :param U0: optional first guess for U as (U1_0, U2_0, ...). 179 | :param interpolation_order: spline interpolation order>0, faster when lower. 180 | :param sigma_blur: sigma of gaussian blur applied before spatial gradient computation. 181 | :return: U=(U1, U2, ...) the estimated displacement along the first, second, ... axes. 182 | 183 | Notes: 184 | - without explicit regularization terms, it is necessary to set 185 | levels_estimlevels_max: 209 | print('[!] too many decomposition levels ({}) requested for given wavelet/shape, set to {}.'.format( 210 | levels_decomp, levels_max)) 211 | levels_decomp = levels_max 212 | # check levels_estim argument w.r.t levels_decomp, if not None 213 | if (levels_estim is not None) and (levels_estim>levels_decomp): 214 | print('[!] too many estimation levels ({}) requested for decomposition, set to {}.'.format( 215 | levels_estim, levels_decomp)) 216 | levels_estim = levels_decomp 217 | # set the final levels values 218 | self.levels_decomp = levels_decomp 219 | self.levels_estim = levels_estim if (levels_estim is not None) else self.levels_decomp-1 220 | 221 | ### Images and core 222 | # make sure the images have size compatible with decomposition levels, padding 223 | # if necessary with zeros 224 | block_size = 2**self.levels_decomp 225 | # how much is missing in each axis 226 | self.pad_size = tuple((block_size - (s%block_size))%block_size for s in im0.shape) 227 | # the slice to crop back the original area 228 | self.crop_slice = tuple(slice(None, -p if p else None, None) for p in self.pad_size) 229 | # padd if necessary 230 | if any(self.pad_size): 231 | padding = [(0, p) for p in self.pad_size] 232 | im0 = numpy.pad(im0, padding, mode='constant') 233 | im1 = numpy.pad(im1, padding, mode='constant') 234 | # create a new core if the image shape is not compatible 235 | if (self.core is None) or (not numpy.testing.assert_equal(self.core.shape, im0.shape)): 236 | self.core = OpticalFlowCore(im0.shape, interpolation_order=interpolation_order, 237 | sigma_blur=sigma_blur) 238 | print(self.core) 239 | # and the images 240 | self.im0 = im0.astype(self.core.dtype) 241 | self.im1 = im1.astype(self.core.dtype) 242 | 243 | ### Motion fields 244 | # get a sequence of the proper length 245 | if U0 is None: 246 | U0 = [None,]*self.core.ndim 247 | # for each component 248 | U = [] 249 | for Ui in U0: 250 | # if None, initialize with zeros 251 | if Ui is None: 252 | Ui = numpy.zeros(self.core.shape, dtype=self.core.dtype) 253 | # else pad the given field if necessary 254 | elif any(self.pad_size): 255 | padding = [(0, p) for p in self.pad_size] 256 | Ui = numpy.pad(Ui, padding, mode='constant') 257 | U.append(Ui) 258 | # as a tuple 259 | U = tuple(U) 260 | # the corresponding wavelet coefficients 261 | self.C_list = [pywt.wavedecn(Ui, self.wav, level=self.levels_decomp, 262 | mode=self.wav_boundary_condition) for Ui in U] 263 | # which we reshape as arrays to get the slices for future manipulations. 264 | self.slices = tuple(pywt.coeffs_to_array(Ci)[1] for Ci in self.C_list) 265 | 266 | ### Solve 267 | print('Decomposition over {} scales of details, {} estimated'.format( 268 | self.levels_decomp, self.levels_estim)) 269 | # for each level 270 | for level in range(self.levels_estim+1): 271 | print('details ({})'.format(level) if level else 'approx. (0)') 272 | # the initial condition, as array (Note: flattened by l-bfgs) 273 | C_array = (pywt.coeffs_to_array(Ci[:level+1])[0] for Ci in self.C_list) 274 | C_array = numpy.vstack((Ci[numpy.newaxis,...] for Ci in C_array)) 275 | # so that C_array[i] contains all coefficients of component i. 276 | # we remember the shape for future manipulations. 277 | C_shape = C_array.shape 278 | # create the cost function for this step 279 | f_g = self.create_cost_function(level, C_shape) 280 | # minimize 281 | C_array, min_value, optim_info = optimize.fmin_l_bfgs_b(f_g, 282 | C_array.astype(numpy.float64), 283 | factr=1000., 284 | iprint=0) 285 | print('\tl-bfgs completed with status {warnflag} - {nit} iterations, {funcalls} calls'.format(**optim_info)) 286 | print('\tcurrent functional value: {:.2f}'.format(min_value)) 287 | # store result in main coefficients 288 | C_array = C_array.reshape(C_shape) 289 | C_list = (pywt.array_to_coeffs(Ci, self.slices[i][:level+1], output_format='wavedecn') 290 | for i, Ci in enumerate(C_array)) 291 | for n, Ci in enumerate(C_list): 292 | for l in range(level+1): 293 | self.C_list[n][l] = Ci[l] 294 | 295 | ### Rebuild field and return 296 | # cropping the relevant area 297 | U = tuple(pywt.waverecn(Ci, self.wav, mode=self.wav_boundary_condition)[self.crop_slice] 298 | for Ci in self.C_list) 299 | return U 300 | 301 | def solve_fast(self, im0, im1, wav='haar', 302 | levels_decomp=3, levels_estim=None, U0=None): 303 | """Fastest estimation (but lower accuracy). 304 | 305 | :param im0: the first (grayscale) image; 306 | :param im1: the second (grayscale) image; 307 | :param wav: the name of the wavelet; 308 | :param levels_decomp: number of decomposition levels; 309 | :param levels_estim: number of estimation levels (<=levels_decomp); 310 | :param U0: optional first guess for U as (U1_0, U2_0, ...). 311 | :return: U=(U1, U2, ...) the estimated displacement along the first, second, ... axes. 312 | 313 | Note: uses linear (order 1) interpolation, no blurring and 'periodization' mode. 314 | 315 | Written by P. DERIAN 2018-01-11. 316 | """ 317 | return self.solve(im0, im1, wav=wav, mode='periodization', 318 | levels_decomp=levels_decomp, levels_estim=levels_estim, 319 | interpolation_order=1, sigma_blur=0.) 320 | 321 | def create_cost_function(self, step, shape): 322 | """The cost function takes wavelet coefficient as input parameters; 323 | and returns (f, grad). 324 | 325 | :param step: 0 (coarse), 1 (first level of details, etc); 326 | :param shape: the shape to reshape x. 327 | :return: the cost function for l-bfgs. 328 | 329 | Written by P. DERIAN 2018-01-09. 330 | """ 331 | def f_g(x): 332 | """Compute the coast function and its gradient for given input x. 333 | 334 | :param x: input point, ndarray of float64. 335 | :return: f, g 336 | - f the function value, scalar; 337 | - g the gradient, ndarray of same size and type as x. 338 | 339 | Notes: 340 | - We cast from (at input) and to (at output) float64, as l-bgs wants 8-byte floats. 341 | - There are losts of shape manipulations, some of them could possibly be 342 | avoided. This is due to l-bfgs using 1d arrays whereas pywt relies on lists 343 | of (tupples of) arrays. Here it was chosen to be as explicit and clear 344 | as possible, possibly sacrificing some speed along the way. 345 | 346 | Written by P. DERIAN 2018-01-09. 347 | Updated by P. DERIAN 2018-01-11: generic n-d version. 348 | """ 349 | ### rebuild motion field 350 | # reshape 1d vector to 3d array 351 | x = x.reshape(shape).astype(self.core.dtype) 352 | # extract coefficients, complement with finer scales and reshape for pywt 353 | # Note: ideally we would not complement, as finer scales are zeros. This is made 354 | # necessary by pywt. 355 | C_list = (pywt.array_to_coeffs(xi, self.slices[i][:step+1], 356 | output_format='wavedecn') 357 | + self.C_list[i][step+1:] for i, xi in enumerate(x)) 358 | # rebuild motion field 359 | U = (pywt.waverecn(Ci, self.wav, mode=self.wav_boundary_condition) for Ci in C_list) 360 | ### evaluate DFD and gradient 361 | func_value, G = self.core.DFD_gradient(self.im0, self.im1, U) 362 | # decompose gradient over wavelet basis, keep coefficients only up to current step 363 | G_list = (pywt.wavedecn(Gi, self.wav, level=self.levels_decomp, 364 | mode=self.wav_boundary_condition)[:step+1] 365 | for Gi in G) 366 | # reshape as array, flatten and concatenate for l-bfgs 367 | G_array = numpy.hstack( 368 | (pywt.coeffs_to_array(Gi)[0].ravel() for Gi in G_list)) 369 | ### evaluate regularizer and its gradient 370 | # [TODO] 371 | ### return DFD (+ regul) value, and its gradient w.r.t. x 372 | return func_value, G_array.astype(numpy.float64) 373 | return f_g 374 | 375 | @staticmethod 376 | def solve_pyramid(im0, im1, levels_pyr=1, solve_fast=False, 377 | wav='haar', levels_decomp=3, levels_estim=None, 378 | **kwargs): 379 | """Wrapper for pyramidal estimation. 380 | 381 | When very large displacements are involved, the usual pyramidal approach 382 | can be employed to help achieving a correct estimation. 383 | 384 | :param im0: the first (grayscale) image; 385 | :param im1: the second (grayscale) image; 386 | :param levels_pyr: number of pyramid levels; 387 | :param fast: if True, use faster (but less accurate) estimation; 388 | :param wav: the name of the wavelet; 389 | :param levels_decomp: number of decomposition levels; 390 | :param levels_estim: number of estimation levels (<=levels_decomp); 391 | :param **kwargs: remaining arguments passed to Typhoon.solve(). 392 | :return: U, typhoon 393 | U=(U1, U2, ...) the estimated displacement along the first, second, ... axes. 394 | typhoon the instance of Typhoon used for the last (finest) pyramid level. 395 | 396 | Written by P. DERIAN 2018-01-11. 397 | """ 398 | downscale = 0.5 399 | upscale = 1./downscale 400 | ### create the pyramid of images 401 | pyr_im0 = [im0,] 402 | pyr_im1 = [im1,] 403 | for l in range(levels_pyr): 404 | # filter image and interpolate 405 | tmp_im = ndimage.gaussian_filter(pyr_im0[0], 2./3.) 406 | pyr_im0.insert(0, ndimage.interpolation.zoom(tmp_im, downscale)) 407 | tmp_im = ndimage.gaussian_filter(pyr_im1[0], 2./3.) 408 | pyr_im1.insert(0, ndimage.interpolation.zoom(tmp_im, downscale)) 409 | ### for each level: 410 | for l, (im0, im1) in enumerate(zip(pyr_im0, pyr_im1)): 411 | # if not the first, use previous motion as first guess 412 | if l: 413 | U0 = [ndimage.interpolation.zoom(Ui, upscale) for Ui in U] 414 | else: 415 | U0 = None 416 | typhoon = Typhoon() 417 | if solve_fast: 418 | U = typhoon.solve_fast(im0, im1, wav=wav, 419 | levels_decomp=levels_decomp, 420 | levels_estim=levels_estim) 421 | else: 422 | U = typhoon.solve(im0, im1, U0=U0, wav=wav, 423 | levels_decomp=levels_decomp, 424 | levels_estim=levels_estim, 425 | **kwargs) 426 | ### return the last estimates 427 | return U, typhoon 428 | 429 | ### Helpers ### 430 | 431 | def RMSE(Ua, Ub): 432 | """Root Mean Squared Error (RMSE). 433 | 434 | :param Ua: (Ua1, Ua2, ...) first vector field; 435 | :param Ub: (Ub1, Ub2, ...) second vector field. 436 | :return: the RMSE. 437 | 438 | Written by P. DERIAN 2018-01-09. 439 | """ 440 | return numpy.sqrt(numpy.mean(numpy.sum(( 441 | numpy.asarray(Ua) - numpy.asarray(Ub))**2, axis=0))) 442 | 443 | ### Demonstrations ### 444 | 445 | if __name__=="__main__": 446 | ### 447 | import argparse 448 | import os.path 449 | import sys 450 | import time 451 | ### 452 | import matplotlib 453 | import matplotlib.pyplot as pyplot 454 | from PIL import Image 455 | ### 456 | import demo.inr as inr 457 | ### 458 | 459 | def print_versions(): 460 | print("\n* Module versions:") 461 | print('Python:', sys.version) 462 | print('Numpy:', numpy.__version__) 463 | print('Scipy:', scipy.__version__) 464 | print('PyWavelet:', pywt.__version__) 465 | print('Matplotlib:', matplotlib.__version__) 466 | print('PyTyphoon (this):', __version__) 467 | 468 | def demo_particles(): 469 | """Demo with the synthetic particle images. 470 | 471 | Written by P. DERIAN 2018-01-09. 472 | """ 473 | print('\n* PyTyphoon {} ({}) – "particles" demo'.format(__version__, __file__)) 474 | 475 | ### load data 476 | im0 = numpy.array(Image.open('demo/run010050000.tif').convert(mode='L')).astype(float)/255. 477 | im1 = numpy.array(Image.open('demo/run010050010.tif').convert(mode='L')).astype(float)/255. 478 | 479 | # Note: 480 | # - U1, V1 are vertical (1st axis) components; 481 | # - U2, V2 are horizontal (2nd axis) components. 482 | # - inr.ReadMotion() returns (horizontal, vertical). 483 | V2, V1 = inr.readMotion('demo/UVtruth.inr') 484 | 485 | ### solve OF 486 | typhoon = Typhoon() 487 | tstart = time.clock() 488 | U1, U2 = typhoon.solve(im0, im1, wav='db2', mode='periodization', 489 | levels_decomp=3, levels_estim=1) 490 | tend = time.clock() 491 | print('Estimation completed in {:.2f} s.'.format(tend-tstart)) 492 | 493 | ### post-process & display 494 | rmse = RMSE((U1, U2), (V1, V2)) 495 | dpi = 72. 496 | fig, axes = pyplot.subplots(2,4, figsize=(800./dpi, 450./dpi)) 497 | # images 498 | for ax, var, label in zip(axes[:,0], [im0, im1], ['image #0', 'image #1']): 499 | pi = ax.imshow(var, vmin=0., vmax=1., interpolation='nearest', cmap='gray') 500 | ax.set_title(label) 501 | ax.set_xticks([]) 502 | ax.set_yticks([]) 503 | # velocity fields 504 | for ax, var, label in zip(axes[:,1:-1].flat, 505 | [U1, V1, U2, V2], 506 | ['estim U1', 'true U1', 'estim U2', 'true U2']): 507 | pf = ax.imshow(var, vmin=-3., vmax=3., interpolation='nearest', cmap='RdYlBu_r') 508 | ax.set_title(label) 509 | ax.set_xticks([]) 510 | ax.set_yticks([]) 511 | # error maps 512 | for ax, var, label in zip(axes[:,-1], 513 | [V1-U1, V2-U2], 514 | ['abs(error U1)', 'abs(error U2)']): 515 | pe = ax.imshow(numpy.abs(var), vmin=0, vmax=0.3, interpolation='nearest') 516 | ax.set_title(label) 517 | ax.set_xticks([]) 518 | ax.set_yticks([]) 519 | # colormaps 520 | pyplot.subplots_adjust(bottom=0.25, top=0.94, left=.05, right=.95) 521 | axf = fig.add_axes([1.25/8., 0.16, 2.5/8., 0.03]) 522 | pyplot.colorbar(pf, cax=axf, orientation='horizontal') 523 | axf.set_title('displacement (pixel)', fontdict={'fontsize':'medium'}) 524 | axe = fig.add_axes([4.5/8., 0.16, 2.5/8., 0.03]) 525 | pyplot.colorbar(pe, cax=axe, orientation='horizontal') 526 | axe.set_title('error (pixel)', fontdict={'fontsize':'medium'}) 527 | # labels 528 | pyplot.figtext( 529 | 0.05, 0.015, 'PyTyphoon {} "particles" demo\n"{}" wavelet, {} scales decomp., {} scales estim., no regularizer.'.format( 530 | __version__, typhoon.wav.name, typhoon.levels_decomp, typhoon.levels_estim), 531 | size='medium', ha='left', va='bottom') 532 | pyplot.figtext( 533 | 0.95, 0.015, 'RMSE={:.2f} pixel'.format(rmse), 534 | size='medium', ha='right', va='bottom') 535 | pyplot.show() 536 | 537 | def demo_3dshift(seed=123456789): 538 | """Demo of 3D estimation with simple shift. 539 | 540 | :param seed: seed for images generation. Pass None for random images. 541 | 542 | Synthetic images are generated by filtering gaussian noise. 543 | The true motion is a simple integer shift in all directions. 544 | Estimation is performed with the basic "fast" solver. 545 | 546 | Written by P. DERIAN 2018-10-12. 547 | """ 548 | print('\n* PyTyphoon {} ({}) – "3dshift" demo'.format(__version__, __file__)) 549 | 550 | ### create synthetic images 551 | print('Generating images...') 552 | # sum of random normal noise filtered by different gaussian kernels. 553 | size = 64 554 | sigma_list = [1., 3., 9.] 555 | im0 = numpy.zeros((size, size, size)) 556 | numpy.random.seed(seed) #seed the generator for repeatability 557 | for sigma in sigma_list: 558 | tmp = numpy.random.randn(size, size, size) 559 | im0 += ndimage.gaussian_filter(tmp, sigma, mode='wrap') 560 | # rescale to [0, 1] 561 | min = im0.min() 562 | max = im0.max() 563 | im0 = (im0-min)/(max-min) 564 | # second image: simply shift 565 | shift = (-1, 2, 3) 566 | im1 = numpy.roll(im0, shift=shift, axis=(0,1,2)) 567 | 568 | ### perform estimation 569 | typhoon = Typhoon() 570 | tstart = time.clock() 571 | U = typhoon.solve_fast(im0, im1, wav='db2', levels_decomp=3, levels_estim=1) 572 | tend = time.clock() 573 | print('Estimation completed in {:.2f} s.'.format(tend-tstart)) 574 | 575 | ### post-process and display 576 | truth_label = 'true shift: ({}) pixel'.format(', '.join('{:.2f}'.format(s) for s in shift)) 577 | estim_label = 'estimated mean displacement: ({}) pixel'.format(', '.join('{:.2f}'.format(Ui.mean()) for Ui in U)) 578 | dpi = 72. 579 | fig, axes = pyplot.subplots(3, 5, figsize=(800./dpi, 450./dpi)) 580 | # show the 3 component in the middle plane along each axis 581 | slice_all = slice(None, None, None) 582 | for i in range(3): 583 | # slice of the mid-plane along this axis 584 | slice_mid = [slice_all, slice_all, slice_all] 585 | slice_mid[i] = size//2 586 | # for each image 587 | for j, (ax, imj) in enumerate(zip(axes[i,:2], [im0, im1])): 588 | ax.imshow(imj[slice_mid], interpolation='nearest', cmap='gray', 589 | vmin=0, vmax=1) 590 | ax.set_xticks([]) 591 | ax.set_yticks([]) 592 | ax.set_title('im{} @ mid-ax{}'.format(j, i+1)) 593 | # for each component 594 | for j, (ax, Uj) in enumerate(zip(axes[i,2:], U), 1): 595 | pc = ax.imshow(Uj[slice_mid], interpolation='nearest', cmap='RdYlBu_r', 596 | vmin=-3, vmax=3) 597 | ax.set_xticks([]) 598 | ax.set_yticks([]) 599 | ax.set_title('U{} @ mid-ax{}'.format(j, i+1)) 600 | pyplot.subplots_adjust(left=.1, right=.9, bottom=.14, top=.9, hspace=.3) 601 | # colorbar 602 | cax = fig.add_axes([.75, 0.05, .2, 0.03]) 603 | cb = pyplot.colorbar(pc, cax=cax, orientation='horizontal') 604 | cb.ax.set_title('displacement (pixel)', fontdict={'fontsize':'medium'}) 605 | # labels 606 | pyplot.figtext(0.5, 0.98, truth_label+' - '+estim_label, 607 | ha='center', va='top', size='medium') 608 | pyplot.figtext( 609 | 0.05, 0.015, 'PyTyphoon {} "3dshift" demo\n"{}"wavelet, {} scales decomp., {} scales estim., no regularizer, "fast" solver.'.format( 610 | __version__, typhoon.wav.name, typhoon.levels_decomp, typhoon.levels_estim), 611 | size='medium', ha='left', va='bottom') 612 | pyplot.show() 613 | 614 | def demo_3dvortex(seed=123456789): 615 | """Demo of 3D estimation with vortex motion. 616 | 617 | :param seed: seed for images generation. Pass None for random images. 618 | 619 | Synthetic images are generated by filtering gaussian noise. 620 | Estimation is performed with the basic "fast" solver. 621 | 622 | Written by P. DERIAN 2018-10-12. 623 | """ 624 | print('\n* PyTyphoon {} ({}) – "3dvortex" demo'.format(__version__, __file__)) 625 | 626 | ### create synthetic images 627 | print('Generating images...') 628 | # sum of random normal noise filtered by different gaussian kernels. 629 | size = 96 630 | margin = 4 #add a margin on every dimension 631 | sigma_list = [1., 3., 9.] #a list of gaussian blur sigma 632 | size_tmp = size + 2*margin 633 | im0 = numpy.zeros((size_tmp, size_tmp, size_tmp)) 634 | numpy.random.seed(seed) #seed the generator for repeatability 635 | for sigma in sigma_list: 636 | tmp = numpy.random.randn(size_tmp, size_tmp, size_tmp) 637 | im0 += ndimage.gaussian_filter(tmp, sigma, mode='wrap') 638 | # rescale to [0, 1] 639 | min = im0.min() 640 | max = im0.max() 641 | im0 = (im0-min)/(max-min) 642 | ### create synthetic motion 643 | # the coordinates 644 | X1, X2, X3 = numpy.indices(im0.shape, dtype=float) 645 | # tubular vortex: from streamfunction 646 | sigma = float(size)/3. #make sure it decays enough near edges 647 | alpha = float(size) #amplitude factor 648 | beta = 3. #amplitude max of 3rd component 649 | x0 = [size_tmp//2 for _ in range(3)] #coordinates of center of volume 650 | SF = alpha*numpy.exp((-1./sigma**2)*( (X1-x0[0])**2 + (X2-x0[1])**2)) 651 | G1, G2, _ = numpy.gradient(SF) 652 | # first 2 components are vortex, third increase linearly from zero to beta 653 | U_truth = (G2, 654 | -G1, 655 | (X3 - margin)*(beta/float(size))) #divide by n_adv due to multiple steps 656 | # displacement coordinates 657 | n_adv = 30 #number of advection steps 658 | map_coords = numpy.vstack((X.ravel() for X in (X1, X2, X3))) #for interpolation 659 | for n, Ui in enumerate(U_truth): 660 | map_coords[n] -= (1./float(n_adv))*Ui.ravel() 661 | # very crude advection 662 | im1 = im0 663 | for _ in range(n_adv): 664 | im1 = ndimage.interpolation.map_coordinates(im1, map_coords, order=3, 665 | mode='constant').reshape(im0.shape) 666 | # finally retain only the working area 667 | if margin>0: 668 | im0 = im0[margin:-margin, margin:-margin, margin:-margin] 669 | im1 = im1[margin:-margin, margin:-margin, margin:-margin] 670 | U_truth = tuple(Ui[margin:-margin, margin:-margin, margin:-margin] for Ui in U_truth) 671 | 672 | ### perform estimation 673 | # instance typhoon so we can use its internal coordinates for simplicity. 674 | typhoon = Typhoon() 675 | tstart = time.clock() 676 | U = typhoon.solve_fast(im0, im1, wav='db3', levels_decomp=3, levels_estim=0) 677 | tend = time.clock() 678 | print('Estimation completed in {:.2f} s.'.format(tend-tstart)) 679 | 680 | ### post-process and display 681 | # show the 3 component in the middle plane along each axis 682 | rmse = RMSE(U_truth, U) 683 | dpi = 72. 684 | fig, axes = pyplot.subplots(3, 5, figsize=(800./dpi, 450./dpi)) 685 | # for each axis 686 | slice_all = slice(None, None, None) 687 | for i in range(3): 688 | # slice of the mid-plane along this axis 689 | slice_mid = [slice_all, slice_all, slice_all] 690 | slice_mid[i] = size//2 691 | # for each image 692 | for j, (ax, imj) in enumerate(zip(axes[i,:2], [im0, im1])): 693 | ax.imshow(imj[slice_mid], interpolation='nearest', cmap='gray', 694 | vmin=0, vmax=1) 695 | ax.autoscale(tight=True) 696 | ax.set_xticks([]) 697 | ax.set_yticks([]) 698 | ax.set_title('im{} @ mid-ax{}'.format(j, i+1)) 699 | # for each component 700 | for j, (ax, Uj) in enumerate(zip(axes[i,2:], U), 1): 701 | pc = ax.imshow(Uj[slice_mid], interpolation='nearest', cmap='RdYlBu_r', 702 | vmin=-3, vmax=3) 703 | ax.set_aspect('equal') 704 | ax.set_xticks([]) 705 | ax.set_yticks([]) 706 | ax.set_title('U{} @ mid-ax{}'.format(j, i+1)) 707 | pyplot.subplots_adjust(left=.1, right=.9, bottom=.14, top=.95, hspace=.3) 708 | # colorbar 709 | cax = fig.add_axes([.565, 0.05, .2, 0.03]) 710 | cb = pyplot.colorbar(pc, cax=cax, orientation='horizontal') 711 | cb.ax.set_title('displacement (pixel)', fontdict={'fontsize':'medium'}) 712 | # labels 713 | pyplot.figtext( 714 | 0.05, 0.015, 'PyTyphoon {} "3dvortex" demo\n"{}"wavelet, {} scales decomp., {} scales estim., no regularizer, "fast" solver.'.format( 715 | __version__, typhoon.wav.name, typhoon.levels_decomp, typhoon.levels_estim), 716 | size='medium', ha='left', va='bottom') 717 | pyplot.figtext( 718 | 0.95, 0.015, 'RMSE={:.2f} pixel'.format(rmse), 719 | size='medium', ha='right', va='bottom') 720 | pyplot.show() 721 | 722 | def main(argv): 723 | """Entry point. 724 | 725 | Written by P. DERIAN 2018-01-11. 726 | """ 727 | ### Arguments 728 | # create parser 729 | parser = argparse.ArgumentParser( 730 | description="Python implementation of Typhoon motion estimator.", 731 | ) 732 | # estimation parameters 733 | parser.add_argument('-i0', dest="im0", type=str, default='', 734 | help="first image") 735 | parser.add_argument('-i1', dest="im1", type=str, default='', 736 | help="second image") 737 | parser.add_argument('-w', '--wav', dest="wav", type=str, default=None, 738 | help="wavelet name") 739 | parser.add_argument('-d', '--decomp', dest="levels_decomp", type=int, default=3, 740 | help="number of decomposition levels") 741 | parser.add_argument('-e', '--estim', dest="levels_estim", type=int, default=None, 742 | help="number of estimation levels") 743 | parser.add_argument('-p', '--pyr', dest="levels_pyr", type=int, default=0, 744 | help="pyramidal estimation levels") 745 | parser.add_argument('--order', dest="interpolation_order", type=int, default=3, 746 | help="image interpolation order") 747 | parser.add_argument('--blur', dest="sigma_blur", type=float, default=0.5, 748 | help="gaussian blur sigma") 749 | parser.add_argument('--mode', dest="mode", type=str, default=None, 750 | help="signal extension mode") 751 | parser.add_argument('--fast', dest="solve_fast", action='store_true', 752 | help="faster estimation (less accurate)") 753 | parser.add_argument('--display', dest="display_result", action='store_true', 754 | help="display estimation results") 755 | # misc arguments 756 | parser.add_argument('--demo', dest="demo_name", type=str, default='', 757 | help="name of the demo") 758 | parser.add_argument('--version', dest="print_version", action='store_true', 759 | help="print module versions") 760 | #parser.add_argument('-q', "--quiet", dest="verbose", action='store_false', 761 | # help="set quiet mode") 762 | # set defaults and parse 763 | parser.set_defaults(verbose=True, print_versions=False, display_result=False, 764 | solve_fast=False) 765 | args = parser.parse_args(argv) 766 | 767 | ### Versions 768 | if args.print_version: 769 | print_versions() 770 | return 771 | 772 | ### Demos 773 | # list of available demos 774 | demos = {'particles': demo_particles, 775 | '3dshift': demo_3dshift, 776 | '3dvortex': demo_3dvortex, 777 | } 778 | default_demo = lambda : print("[!] Unkown demo '{}', valid demos: {}".format( 779 | args.demo_name, list(demos.keys()))) 780 | # start a demo 781 | if args.demo_name: 782 | demos.get(args.demo_name, default_demo)() 783 | return 784 | 785 | ### Single estimation 786 | if args.im0 and args.im1: 787 | print('\nEstimation:\n\t{}\n\t{}'.format(args.im0, args.im1)) 788 | ### load images 789 | im0 = numpy.array(Image.open(args.im0).convert(mode='L')).astype(numpy.float32)/255. 790 | im1 = numpy.array(Image.open(args.im1).convert(mode='L')).astype(numpy.float32)/255. 791 | ### Solve problem 792 | tstart = time.clock() 793 | (U1, U2), typhoon = Typhoon.solve_pyramid(im0, im1, levels_pyr=args.levels_pyr, 794 | solve_fast=args.solve_fast, 795 | wav=args.wav, mode=args.mode, 796 | levels_decomp=args.levels_decomp, 797 | levels_estim=args.levels_estim, 798 | interpolation_order=args.interpolation_order, 799 | sigma_blur=args.sigma_blur) 800 | tend = time.clock() 801 | print('Estimation completed in {:.2f} s.'.format(tend-tstart)) 802 | # export 803 | # [TODO] retrieve estim parameters and save as well. 804 | # display 805 | # [TODO] move to function? 806 | if args.display_result: 807 | dpi = 72. 808 | fig, axes = pyplot.subplots(2,3, figsize=(800./dpi, 600./dpi)) 809 | # images 810 | for ax, var, label in zip(axes[0,1:], [im0, im1], ['image #0', 'image #1']): 811 | pi = ax.imshow(var, vmin=0., vmax=1., interpolation='nearest', cmap='gray') 812 | ax.set_title(label) 813 | ax.set_xticks([]) 814 | ax.set_yticks([]) 815 | axes[0,0].remove() 816 | # velocity fields 817 | pf = [] 818 | for ax, var, label in zip(axes[1,1:], [U1, U2], ['estim U1', 'estim U2']): 819 | pf.append(ax.imshow(var, interpolation='nearest', cmap='RdYlBu_r')) 820 | ax.set_title(label) 821 | ax.set_xticks([]) 822 | ax.set_yticks([]) 823 | # colorbars? 824 | # [TODO] 825 | # quiver - note that array axes order is reversed 826 | ax = axes[1,0] 827 | ax.set_aspect('equal') 828 | qstep = 8 829 | ax.quiver(typhoon.core.x[1][::qstep], typhoon.core.x[0][::qstep], 830 | U2[::qstep, ::qstep], U1[::qstep, ::qstep], 831 | pivot='middle', color='b', units='xy', angles='xy') 832 | ax.set_xlim(typhoon.core.x[1][0], typhoon.core.x[1][-1]) 833 | ax.set_ylim(typhoon.core.x[0][0], typhoon.core.x[0][-1]) 834 | ax.invert_yaxis() 835 | ax.set_title('motion field') 836 | ax.set_xticks([]) 837 | ax.set_yticks([]) 838 | pyplot.subplots_adjust(left=.05, right=.95, top=.95, bottom=.1) 839 | # labels 840 | pyplot.figtext(0.05, 0.015, 'PyTyphoon {}'.format(__version__), 841 | size='medium', ha='left', va='bottom') 842 | pyplot.figtext(0.05, 0.95, 843 | 'im0: {}\nim1: {}\nlevels pyramid: {}\nwavelet: {}\nwav. decomp. lvls: {}\nwav. estim. lvls: {}'.format( 844 | os.path.basename(args.im0), 845 | os.path.basename(args.im1), 846 | args.levels_pyr, 847 | typhoon.wav.name, 848 | typhoon.levels_decomp, 849 | typhoon.levels_estim), 850 | size='medium', ha='left', va='top') 851 | pyplot.show() 852 | 853 | main(sys.argv[1:]) 854 | --------------------------------------------------------------------------------