├── Datasets └── vase.mat ├── LICENSE ├── README.md ├── Toolbox ├── DCT_Poisson.m ├── DST_Poisson.m ├── FFT_Poisson.m ├── anisotropic_diffusion_integration.m ├── horn_brooks.m ├── make_gradient.m ├── mumford_shah_integration.m ├── phi1_integration.m ├── phi2_integration.m ├── smooth_integration.m └── tv_integration.m ├── demo_1_survey.m ├── demo_2_quadratic.m └── demo_3_discontinuities.m /Datasets/vase.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yqueau/normal_integration/1f69b9f1f35bb79457f6a8af753a5d4978811b11/Datasets/vase.mat -------------------------------------------------------------------------------- /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 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 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 | {project} Copyright (C) {year} {fullname} 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codes_Integration 2 | Matlab codes for integration of normals (gradient) over a non-rectangular 2D grid, without boundary condition. 3 | 4 | ## Introduction 5 | 6 | In many computer vision applications (e.g. photometric stereo, shape-from-shading, shape-from-polarization or deflectometry), one estimates the local surface orientation (i.e., normals) in each pixel. A subsequent step consists in integrating these normals into a depth map. These Matlab codes implement the variational normal integration methods discussed in [1], and three famous methods for solving the Poisson equation, which were discussed in our survey [2]. These journal papers summarize research previously presented in the conference papers [3,4,5]. 7 | 8 | Features: 9 | - Implementations of Poisson solvers using FFT (see Sec. 3.3 in [2]), using DCT (see Sec. 3.4 in [2], and the modified Jacobi scheme of Horn and Brooks (see Sec. 3.2 in [2], and demo 1); 10 | - A fast (almost n log(n)) quadratic integration over a non-rectangular domain, without boundary condition, without parameter to tune (see Sec. 3 in [1], and demo 2); 11 | - Various non-quadratic, discontinuity-preserving integrators: total variation, non-convex, anisotropic diffusion and Mumford-Shah (all are slower than quadratic integration, and require at least one parameter to be tuned) (see Sec. 4 in [1], and demo 3); 12 | - Possibility to include a depth prior in each method (see Sec. 2.4 in [1]). 13 | 14 | 15 | ## Demos 16 | 17 | The following demo files are provided: 18 | 19 | - `demo_1_survey.m` : demo of the methods presented in Sec. 3 of the survey paper [2]. This shows the importance of boundary conditions, hence the superiority of DCT over DST/FFT for solving the Poisson equation. However, it handles only a rectangular domain, hence Horn and Brook's method is much more accurate for non-rectangular domains. Still, the latter is very slow, hence the quadratic method proposed in [1] is much better: it handles free boundary and free-form domain, while being almost as fast as DCT. 20 | 21 | - `demo_2_domain.m` : demo of fast quadratic integration over a non-rectangular grid. Script shows that when explicitly using the domain, integration is faster and way more accurate (no bias on the boundary of the object due to discontinuity). 22 | 23 | - `demo_3_discontinuities.m` : demo of the four discontinuity-preserving methods. They can be used if the domain of integration has not been pre-calculated. 24 | 25 | 26 | 27 | ## Contents 28 | 29 | The main fuctions for the new variational methods in [1] are in the Toolbox/ folder: 30 | - `make_gradient.m`: given a 2D binary mask, returns the matrix differentiation operators in all 4 directions (D_{u/v}^{+/-} in Sec. 3 in [1]) 31 | - `smooth_integration.m`: function for quadratic integration over a non-rectangular grid (Sec. 3 in [1]) 32 | - `tv_integration.m`: function for TV integration over a non-rectangular grid (Sec. 4.1 in [1]) 33 | - `phi1_integration.m`: function for non-convex (Phi_1 estimator) integration over a non-rectangular grid (Sec. 4.2 in [1]) 34 | - `phi2_integration.m`: function for non-convex (Phi_2 estimator) integration over a non-rectangular grid (Sec. 4.2 in [1]) 35 | - `anisotropic_diffusion_integration.m`: function for anisotropic diffusion integration over a non-rectangular grid (Sec. 4.3 in [1]) 36 | - `mumford_shah_integration.m`: function for Mulford-Sjaj integration over a non-rectangular grid (Sec. 4.4 in [1]) 37 | 38 | The four Poisson solvers discussed in [2, Sec. 3] are also provided: 39 | - `horn_brooks.m`: implementation of the modified Horn and Brook's scheme (Jacobi iterations) for Poisson integration over a non-rectangular grid. Needs no boundary condition, but very slow (Sec. 3.2 in [2]). 40 | - `FFT_Poisson.m`: implementation of the FFT integrator. Super fast, but requires a rectangular grid and periodic boundary condition (Sec. 3.3 in [2]) 41 | - `DCT_Poisson`: implementation of the DCT integrator. Still very fast, and requires no boundary condition, but domain must be rectangular (Sec. 3.4 in [2]) 42 | - `DST_Poisson`: implementation of the DST integrator. Still very fast, handles Dirichlet boundary condition. Domain must be rectangular (Sec. 3.4 in [2]). 43 | 44 | 45 | ## Dependencies 46 | 47 | We strongly recommend to use the CMG preconditioner from Koutis et al., which can be downloaded here: 48 | http://www.cs.cmu.edu/~jkoutis/cmg.html 49 | 50 | If CMG it is not installed, set the "precond" parameter to "none" (no preconditioning, can be very slow for large data) or to "ichol" (modified incomplete Cholesky preconditioner advised in [6], slower than CMG but overall OK). This will be slower, but it should run without any additional library. 51 | 52 | 53 | 54 | ## Usage 55 | - All methods require to provide: 56 | * p: estimation of the gradient in bottom direction (matrix) 57 | * q: estimation of the gradient in right direction (matrix) 58 | - Optional parameters common to all methods 59 | * mask: binary mask of the area of interest (matrix) 60 | * lambda: field of regularization weights for depth prior (matrix) 61 | * z0: depth prior (matrix) 62 | - Discontinuity-preserving methods and Horn and Brook's one require a few other settings, see demos 1 and 3 for details. 63 | 64 | ## References 65 | 66 | [1] "Variational Methods for Normal Integration", Quéau et al., Journal of Mathematical Imaging and Vision 60(4), pp 609--632, 2018. (Arxiv preprint: https://arxiv.org/abs/1709.05965) 67 | 68 | [2] "Normal Integration: a Survey", Quéau et al., Journal of Mathematical Imaging and Vision 60(4), pp 576--593, 2018. (Arxiv preprint: https://arxiv.org/abs/1709.05940) 69 | 70 | These methods build upon three previous conference papers. The new quadratic method is an extension of the method in [3]. The non-convex integrator was introduced in [4], and the TV one in [5]. 71 | 72 | [3] "Integration of a Normal Field without Boundary Condition", Durou and Courteille, ICCVW 2007 73 | 74 | [4] "Integrating the Normal Field of a Surface in the Presence of Discontinuities", Durou et al., EMMCVPR2009 75 | 76 | [5] "Edge-Preserving Integration of a Normal Field: Weighted Least Squares and L1 Approaches", Quéau and Durou, SSVM2015 77 | 78 | The modified incomplete Cholesky preconditioner is advised in: 79 | 80 | [6] "Fast and accurate surface normal integration on non-rectangular domains", Bähr et al., Computational Visual Media 3(2), pp. 107--129, 2017 81 | 82 | 83 | Author of codes: Yvain Quéau, Technical University Munich, yvain.queau@tum.de 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Toolbox/DCT_Poisson.m: -------------------------------------------------------------------------------- 1 | function z = DCT_Poisson(p,q) 2 | % An implementation of the use of DCT for solving the Poisson equation, 3 | % (integration with Neumann boundary condition) 4 | % Code is based on the description in [1], Sec. 3.4 5 | % 6 | % [1] Normal Integration: a Survey - Queau et al., 2017 7 | % 8 | % Usage : 9 | % u=DCT_Poisson(p,q) 10 | % where p and q are MxN matrices, solves in the least square sense 11 | % \nabla u = [p,q] , assuming natural Neumann boundary condition 12 | % 13 | % \nabla u \cdot \eta = [p,q] \cdot \eta on boundaries 14 | % 15 | % Axis : O->y 16 | % | 17 | % x 18 | % 19 | % Fast solution is provided by Discrete Cosine Transform 20 | % 21 | % Implementation : Yvain Queau 22 | 23 | % Divergence of (p,q) using central differences 24 | px = 0.5*(p([2:end end],:)-p([1 1:end-1],:)); 25 | qy = 0.5*(q(:,[2:end end])-q(:,[1 1:end-1])); 26 | 27 | % Div(p,q) 28 | f = px+qy; 29 | 30 | % Right hand side of the boundary condition 31 | b = zeros(size(p)); 32 | b(1,2:end-1) = -p(1,2:end-1); 33 | b(end,2:end-1) = p(end,2:end-1); 34 | b(2:end-1,1) = -q(2:end-1,1); 35 | b(2:end-1,end) = q(2:end-1,end); 36 | b(1,1) = (1/sqrt(2))*(-p(1,1)-q(1,1)); 37 | b(1,end) = (1/sqrt(2))*(-p(1,end)+q(1,end)); 38 | b(end,end) = (1/sqrt(2))*(p(end,end)+q(end,end)); 39 | b(end,1) = (1/sqrt(2))*(p(end,1)-q(end,1)); 40 | 41 | % Modification near the boundaries to enforce the non-homogeneous Neumann BC (Eq. 53 in [1]) 42 | f(1,2:end-1) = f(1,2:end-1)-b(1,2:end-1); 43 | f(end,2:end-1) = f(end,2:end-1)-b(end,2:end-1); 44 | f(2:end-1,1) = f(2:end-1,1)-b(2:end-1,1); 45 | f(2:end-1,end) = f(2:end-1,end)-b(2:end-1,end); 46 | 47 | % Modification near the corners (Eq. 54 in [1]) 48 | f(1,end) = f(1,end)-sqrt(2)*b(1,end); 49 | f(end,end) = f(end,end)-sqrt(2)*b(end,end); 50 | f(end,1) = f(end,1)-sqrt(2)*b(end,1); 51 | f(1,1) = f(1,1)-sqrt(2)*b(1,1); 52 | 53 | % Cosine transform of f 54 | fcos=dct2(f); 55 | 56 | 57 | % Cosine transform of z (Eq. 55 in [1]) 58 | [x,y] = meshgrid(0:size(p,2)-1,0:size(p,1)-1); 59 | denom = 4*((sin(0.5*pi*x/size(p,2))).^2 + (sin(0.5*pi*y/size(p,1))).^2); 60 | z_bar_bar = -fcos./max(eps,denom); 61 | 62 | % Inverse cosine transform : 63 | z = idct2(z_bar_bar); 64 | z=z-min(z(:)); % Z known up to a positive constant, so offset it to get from 0 to max 65 | 66 | return 67 | 68 | -------------------------------------------------------------------------------- /Toolbox/DST_Poisson.m: -------------------------------------------------------------------------------- 1 | function z = DST_Poisson(p,q,ub) 2 | % An implementation of the use of DST for solving the Poisson equatio, 3 | % (integration with Dirichlet boundary condition) 4 | % Code is based on the description in [1], Sec. 3.4 5 | % 6 | % [1] Normal Integration: a Survey - Queau et al., 2017 7 | % 8 | % Usage : 9 | % u=DST_Poisson(p,q) 10 | % where p and q are MxN matrices, solves in the least square sense 11 | % \nabla u = [p,q] , assuming homogeneous Dirichlet boundary 12 | % condition u = 0 on the boundary 13 | % 14 | % u=DST_Poisson(p,q,ub) 15 | % where p,q and ub are NxM matrix, such as ub(1,:) contains the values 16 | % on the first line, ub(end,:) contains the values on the last line etc. 17 | % Apart from the boundary ub can be anything 18 | % 19 | % Example (weird) 20 | % p = zeros(100,100); 21 | % q = zeros(100,100); 22 | % ub = zeros(size(p)); 23 | % ub(1,:)=1:1OO; 24 | % u = DST_Poisson(p,q,ub); 25 | % surfl(u) 26 | % 27 | % This performs the least square solution to \nabla u = [p,q], i.e. : 28 | % min \int_\Omega \| \nablua U - [p,q] \|^2 29 | % where \Omega is square and the Dirichlet boundary condition 30 | % u = ub on the boundary of \Omega. 31 | % 32 | % Axis : O->y 33 | % | 34 | % x 35 | % 36 | % Fast solution is provided by Discrete Sine Transform 37 | % 38 | % Implementation : Yvain Queau 39 | 40 | 41 | if(nargin<3) 42 | ub=zeros(size(p)); 43 | end 44 | 45 | % Divergence of (p,q) using central differences 46 | px = 0.5*(p([2:end end],:)-p([1 1:end-1],:)); 47 | qy = 0.5*(q(:,[2:end end])-q(:,[1 1:end-1])); 48 | f = px + qy; 49 | 50 | % Modification near the boundaries (Eq. 46 in [1]) 51 | f(2,3:end-2) = f(2,3:end-2) - ub(1,3:end-2); 52 | f(end-1,3:end-2) = f(end-1,3:end-2) - ub(end,3:end-2); 53 | f(3:end-2,2) = f(3:end-2,2) - ub(3:end-2,1); 54 | f(3:end-2,end-1) = f(3:end-2,end-1) - ub(3:end-2,end); 55 | 56 | % Modification near the corners (Eq. 47 in [1]) 57 | f(2,2) = f(2,2) - ub(2,1) - ub(1,2); 58 | f(2,end-1) = f(2,end-1) - ub(2,end) - ub(1,end-1); 59 | f(end-1,end-1) = f(end-1,end-1) - ub(end-1,end) - ub(end,end-1); 60 | f(end-1,2) = f(end-1,2) - ub(end-1,1) - ub(end,2); 61 | 62 | % Sine transform of f 63 | fsin=dst2(f(2:end-1,2:end-1)); 64 | 65 | % Denominator 66 | [x,y] = meshgrid(0:size(p,2)-1,0:size(p,1)-1); 67 | denom = (sin(0.5*pi*x/size(p,2))).^2 + (sin(0.5*pi*y/size(p,1))).^2; 68 | z_bar = -0.25*fsin./denom(2:end-1,2:end-1); 69 | 70 | % Inverse Sine transform : 71 | z=ub; 72 | z(2:end-1,2:end-1) = idst2(z_bar); 73 | 74 | return 75 | 76 | 77 | function y = dst2(x) 78 | y = dst(dst(x)')'; 79 | return 80 | 81 | function Y=idst2(X); 82 | Z=idst(X'); 83 | Y=idst(Z'); 84 | return 85 | 86 | 87 | -------------------------------------------------------------------------------- /Toolbox/FFT_Poisson.m: -------------------------------------------------------------------------------- 1 | function z = FFT_Poisson(p,q,ub) 2 | % An implementation of the use of FFT for solving the Poisson equation, 3 | % (integration with periodic boundary condition) 4 | % Code is based on the description in [1], Sec. 3.3 5 | % 6 | % [1] Normal Integration: a Survey - Queau et al., 2017 7 | % 8 | % Usage : 9 | % u=FFT_Poisson(p,q) 10 | % where p and q are MxN matrices, solves in the least square sense 11 | % \nabla u = [p,q] , assuming periodic boundary condition 12 | % 13 | % This performs the least square solution to \nabla u = [p,q], i.e. : 14 | % min \int_\Omega \| \nablua U - [p,q] \|^2 15 | % where \Omega is square and periodic boundary condition is enforced 16 | % 17 | % Axis : O->y 18 | % | 19 | % x 20 | % 21 | % Fast solution is provided by Fast Fourier Transform 22 | % 23 | % Implementation : Yvain Queau 24 | 25 | 26 | % Fourier transforms of p and q 27 | p_hat = fft2(p); 28 | q_hat = fft2(q); 29 | 30 | % Fourier transform of z (Eq. 42 in [1]) 31 | [y,x] = meshgrid(0:size(p,2)-1,0:size(p,1)-1); 32 | numerator = sin(2*pi*x/size(p,1)).*p_hat+sin(2*pi*y/size(p,2)).*q_hat; 33 | denominator = max(eps,4*j*((sin(pi*x/size(p,1))).^2+(sin(pi*y/size(p,2))).^2)); 34 | 35 | % Inverse Fourier transform 36 | z = real(ifft2(numerator./denominator)); 37 | z=z-min(z(:)); % Z known up to a positive constant, so offset it to get from 0 to max 38 | 39 | return 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Toolbox/anisotropic_diffusion_integration.m: -------------------------------------------------------------------------------- 1 | function [z,tab_nrj,tab_rmse] = anisotropic_diffusion_integration(p,q,mask,lambda,z0,mu,nu,maxit,tol,zinit,gt) 2 | 3 | % Check arguments 4 | if(nargin < 2) 5 | disp('Error: Not enough arguments'); 6 | return; 7 | end 8 | 9 | [nrows,ncols] = size(p); 10 | 11 | % Set default values for missing arguments 12 | if (~exist('mask','var')|isempty(mask)) mask=ones(nrows,ncols); end; 13 | if (~exist('lambda','var')|isempty(lambda)) lambda = 1e-9*mask; end; 14 | if (~exist('z0','var')|isempty(z0)) z0 = zeros(nrows,ncols); end; 15 | if (~exist('mu','var')|isempty(mu)) mu = 0.01; end; 16 | if (~exist('nu','var')|isempty(nu)) nu = 0.01; end; 17 | if (~exist('maxit','var')|isempty(maxit)) maxit = 100; end; 18 | if (~exist('tol','var')|isempty(tol)) tol = 1e-4; end; 19 | if (~exist('zinit','var')|isempty(zinit)) zinit = z0; end; 20 | if (~exist('gt','var')|isempty(gt)) gt = z0; end; 21 | 22 | 23 | % If lambda is a scalar, make it a matrix 24 | if(size(lambda,1)==1) 25 | lambda = lambda*mask; 26 | end 27 | 28 | % Make finite differences operators 29 | [Dup,Dum,Dvp,Dvm,imask,imaskup,imaskum,imaskvp,imaskvm] = make_gradient(mask); 30 | npix = length(imask); 31 | 32 | % Some stuff used later 33 | Lambda_two = spdiags(lambda(imask),0,npix,npix); % Regularization 34 | if(nargout>1) 35 | tab_nrj = zeros(maxit+1,1); 36 | cpt = 1; 37 | end 38 | if(nargout>2) 39 | tab_rmse = zeros(maxit+1,1); 40 | cpt_rmse = 1; 41 | end 42 | nu2 = nu*nu; 43 | 44 | % Initialization 45 | z = zinit; 46 | App_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1+(Dup*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 47 | Apm_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1+(Dup*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 48 | Amp_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1+(Dum*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 49 | Amm_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1+(Dum*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 50 | Bpp_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1+(Dup*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 51 | Bpm_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1+(Dup*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 52 | Bmp_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1+(Dum*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 53 | Bmm_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1+(Dum*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 54 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+norm(App_mat*(Dup*z(imask)-p(imask)))+norm(Bpp_mat*(Dvp*z(imask)-q(imask)))+norm(Apm_mat*(Dup*z(imask)-p(imask)))+norm(Bpm_mat*(Dvm*z(imask)-q(imask)))+norm(Amp_mat*(Dum*z(imask)-p(imask)))+norm(Bmp_mat*(Dvp*z(imask)-q(imask)))+norm(Amm_mat*(Dum*z(imask)-p(imask)))+norm(Bmm_mat*(Dvm*z(imask)-q(imask))); 55 | if(nargout>1) 56 | tab_nrj(cpt) = energie; 57 | cpt = cpt+1; 58 | end 59 | if(nargout>2) 60 | lambda = -mean(z(imask)-gt(imask)); 61 | zrmse = z+lambda; 62 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 63 | cpt_rmse = cpt_rmse+1; 64 | end 65 | 66 | % Alternating optimisation loops 67 | for k = 1:maxit 68 | 69 | % z update 70 | A = Lambda_two+(Dup'*(App_mat.^2)*Dup)+(Dvp'*(Bpp_mat.^2)*Dvp)+(Dup'*(Apm_mat.^2)*Dup)+(Dvm'*(Bpm_mat.^2)*Dvm)+(Dum'*(Amp_mat.^2)*Dum)+(Dvp'*(Bmp_mat.^2)*Dvp)+(Dum'*(Amm_mat.^2)*Dum)+(Dvm'*(Bmm_mat.^2)*Dvm); 71 | b = Lambda_two*z0(imask)+(Dup'*(App_mat.^2)*p(imask))+(Dvp'*(Bpp_mat.^2)*q(imask))+(Dup'*(Apm_mat.^2)*p(imask))+(Dvm'*(Bpm_mat.^2)*q(imask))+(Dum'*(Amp_mat.^2)*p(imask))+(Dvp'*(Bmp_mat.^2)*q(imask))+(Dum'*(Amm_mat.^2)*p(imask))+(Dvm'*(Bmm_mat.^2)*q(imask)); 72 | %~ precond = cmg_sdd(A); 73 | %~ [z(imask)] = pcg(A,b,1e-4,100,precond,[],z(imask)); 74 | [z(imask)] = A\b; 75 | 76 | % w update 77 | App_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1./mu+(Dup*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 78 | Apm_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1./mu+(Dup*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 79 | Amp_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1./mu+(Dum*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 80 | Amm_mat = spdiags(1./(sqrt(1+p(imask).^2./nu2).*sqrt(1./mu+(Dum*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 81 | Bpp_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1./mu+(Dup*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 82 | Bpm_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1./mu+(Dup*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 83 | Bmp_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1./mu+(Dum*z(imask)./mu).^2+(Dvp*z(imask)./mu).^2)),0,npix,npix); 84 | Bmm_mat = spdiags(1./(sqrt(1+q(imask).^2./nu2).*sqrt(1./mu+(Dum*z(imask)./mu).^2+(Dvm*z(imask)./mu).^2)),0,npix,npix); 85 | 86 | % Check CV 87 | energie_old = energie; 88 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+norm(App_mat*(Dup*z(imask)-p(imask)))+norm(Bpp_mat*(Dvp*z(imask)-q(imask)))+norm(Apm_mat*(Dup*z(imask)-p(imask)))+norm(Bpm_mat*(Dvm*z(imask)-q(imask)))+norm(Amp_mat*(Dum*z(imask)-p(imask)))+norm(Bmp_mat*(Dvp*z(imask)-q(imask)))+norm(Amm_mat*(Dum*z(imask)-p(imask)))+norm(Bmm_mat*(Dvm*z(imask)-q(imask))); 89 | if(nargout>1) 90 | tab_nrj(cpt) = energie; 91 | cpt = cpt+1; 92 | end 93 | if(nargout>2) 94 | lambda = -mean(z(imask)-gt(imask)); 95 | zrmse = z+lambda; 96 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 97 | cpt_rmse = cpt_rmse+1; 98 | end 99 | 100 | %~ figure(1) 101 | %~ surfl(z,[-135 30]); 102 | %~ view(-35,20) 103 | %~ axis ij; 104 | %~ axis equal; 105 | %~ axis([1 320 1 320 -20 70]); 106 | %~ shading flat; 107 | %~ colormap gray; 108 | %~ 109 | %~ grid off 110 | %~ drawnow 111 | %~ 112 | %~ figure(2) 113 | %~ wupdisp = NaN*ones(nrows,ncols); 114 | %~ wupdisp(imask) = spdiags(App_mat,0); 115 | %~ imagesc(wupdisp); 116 | %~ colormap gray 117 | %~ colorbar 118 | 119 | if(nargout>1) 120 | figure(3) 121 | plot(tab_nrj(1:cpt-1)) 122 | drawnow 123 | end 124 | if(nargout>2) 125 | figure(4) 126 | plot(tab_rmse(1:cpt_rmse-1)) 127 | drawnow 128 | end 129 | 130 | relative_residual = abs(energie-energie_old)./abs(energie_old); 131 | disp(sprintf('it %d - EAT = %.4f - res : %.6f',k,energie,relative_residual)); 132 | if(relative_residual < tol & k > 5) 133 | break; 134 | end 135 | end 136 | if(k==maxit) 137 | disp('max number of iterations reached'); 138 | end 139 | 140 | % Put NaNs outside the mask 141 | z(mask==0) = NaN; 142 | wup(mask==0) = NaN; 143 | wum(mask==0) = NaN; 144 | wvp(mask==0) = NaN; 145 | wvm(mask==0) = NaN; 146 | 147 | if(nargout>1) 148 | tab_nrj = tab_nrj(1:k+1); 149 | end 150 | if(nargout>2) 151 | tab_rmse = tab_rmse(1:k+1); 152 | end 153 | end 154 | -------------------------------------------------------------------------------- /Toolbox/horn_brooks.m: -------------------------------------------------------------------------------- 1 | function [u,ma,tab_rmse] = horn_brooks(p,q,omega,it_max,tol,trace,u0,ground_truth) 2 | %hb integrates the gradient field [p,q] by minimizing the 3 | %functional F=\iint_{\Omega} \| \nabla U(x,y) - [p,q](x,y) \|^2 dx dy 4 | %using the improved Horn and Brooks scheme 5 | % 6 | % U = hb(P,Q) uses default values 7 | % [U,ma,tab_rmse] = hb(P,Q,[],[],[],[],GT) also provides the computed masks and the evolution of RMSE between U and GT (ground truth) 8 | % U = hb(P,Q,OMEGA) uses the integration domain OMEGA (default : ones(size(p))) 9 | % U = hb(P,Q,OMEGA) uses the value gamma=ALPHA (default : 1) 10 | % U = hb(P,Q,OMEGA,IT_MAX) performs IT_MAX iterations (default : 100*size(p,1)) 11 | % U = hb(P,Q,OMEGA,IT_MAX,TRACE) : if trace=1, it shows the recovered surface every 100 iterations (default : 0) 12 | % U = hb(P,Q,OMEGA,IT_MAX,TRACE,U0) : starts with surface U=U0 (default : zeros(size(p))) 13 | % U = hb(P,Q,OMEGA,IT_MAX,TRACE,U0,GT) : if a ground-truth is provided, the RMSE can be computed and displayed if TRACE=1 14 | if (~exist('ground_truth','var')|isempty(ground_truth)) 15 | save_rmse=0; 16 | else 17 | save_rmse=1; 18 | end 19 | 20 | if (~exist('tol','var')|isempty(tol)) tol=1e-3; end; 21 | if (~exist('u0','var')|isempty(u0)) u0=zeros(size(p)); end; 22 | if (~exist('trace','var')|isempty(trace)) trace=0; end; 23 | if (~exist('it_max','var')|isempty(it_max)) it_max=100*size(p,1); end; 24 | if (~exist('omega','var')|isempty(omega)) omega=ones(size(p)); end; 25 | 26 | 27 | 28 | 29 | [nrows,ncols]=size(omega); 30 | imask = find(omega>0); 31 | npix=length(imask); 32 | u=u0; 33 | u(omega==0)=NaN; 34 | tab_rmse=[]; 35 | 36 | % Calcul des masques : 37 | % ma1 : voisins de dessous et de droite dans omega 38 | % ma2 : ma0 \ ma1 39 | % ma3 : points de ma1 avec voisins de dessus et de gauche dans ma1 40 | % ma4 : dans ma1, mais pas celui de gauche 41 | % ma5 : gauche dans ma1, mais pas celui de dessus 42 | % ma6 : ni voisin de gauche, ni voisin de dessus dans ma1 43 | % ma7 : ma2 avec voisins de dessus et de gauche dans ma1 44 | % ma8 : dans ma1, mais pas celui de gauche 45 | % ma9 : gauche dans ma1, mais pas celui de dessus 46 | % ma10: ni voisin de gauche, ni voisin de dessus dans ma1 47 | ma=zeros(nrows,ncols,9); 48 | ma(1:end-1,1:end-1,1)=omega(1:end-1,1:end-1).*omega(2:end,1:end-1).*omega(1:end-1,2:end); 49 | ma(:,:,2)=omega.*(~ma(:,:,1)); 50 | ma(2:end,2:end,3)=ma(2:end,2:end,1).*ma(1:end-1,2:end,1).*ma(2:end,1:end-1,1); 51 | ma(2:end,2:end,4)=ma(2:end,2:end,1).*ma(1:end-1,2:end,1).*(~ma(2:end,1:end-1,1)); 52 | ma(2:end,1,4)=ma(2:end,1,1).*ma(1:end-1,1,1); 53 | ma(2:end,2:end,5)=ma(2:end,2:end,1).*(~ma(1:end-1,2:end,1)).*(ma(2:end,1:end-1,1)); 54 | ma(1,2:end,5)=ma(1,2:end,1).*ma(1,1:end-1,1); 55 | ma(2:end,2:end,6)=ma(2:end,2:end,1).*(~ma(1:end-1,2:end,1)).*(~ma(2:end,1:end-1,1)); 56 | ma(2:end,1,6)=ma(2:end,1,1).*(~ma(1:end-1,1,1)); % Added 57 | ma(1,2:end,6)=ma(1,2:end,1).*(~ma(1,1:end-1,1)); % Added 58 | ma(1,1,6)=ma(1,1,1); 59 | ma(2:end,2:end,7)=ma(2:end,2:end,2).*(ma(1:end-1,2:end,1)).*(ma(2:end,1:end-1,1)); 60 | ma(2:end,2:end,8)=ma(2:end,2:end,2).*(ma(1:end-1,2:end,1)).*(~ma(2:end,1:end-1,1)); 61 | ma(2:end,1,8)=ma(2:end,1,2).*ma(1:end-1,1,1); 62 | ma(2:end,2:end,9)=ma(2:end,2:end,2).*(~ma(1:end-1,2:end,1)).*(ma(2:end,1:end-1,1)); 63 | ma(1,2:end,9)=ma(1,2:end,2).*ma(1,1:end-1,1); 64 | 65 | ind3=find(ma(:,:,3)>0); 66 | ind4=find(ma(:,:,4)>0); 67 | ind5=find(ma(:,:,5)>0); 68 | ind6=find(ma(:,:,6)>0); 69 | ind7=find(ma(:,:,7)>0); 70 | ind8=find(ma(:,:,8)>0); 71 | ind9=find(ma(:,:,9)>0); 72 | 73 | p_haut=[zeros(1,ncols);p(1:end-1,:)]; 74 | p_bas =[p(2:end,:);zeros(1,ncols)]; 75 | q_gauche=[zeros(nrows,1),q(:,1:end-1)]; 76 | q_droite=[q(:,2:end),zeros(nrows,1)]; 77 | pq3=0.125*(p_haut(ind3)-p_bas(ind3)+q_gauche(ind3)-q_droite(ind3)); 78 | pq4=(p_haut(ind4)-p_bas(ind4)-q(ind4)-q_droite(ind4))/6; 79 | pq5=(-p(ind5)-p_bas(ind5)+q_gauche(ind5)-q_droite(ind5))/6; 80 | pq6=0.25*(-p(ind6)-p_bas(ind6)-q(ind6)-q_droite(ind6)); 81 | pq7=0.25*(p_haut(ind7)+p(ind7)+q_gauche(ind7)+q(ind7)); 82 | pq8=0.5*(p_haut(ind8)+p(ind8)); 83 | pq9=0.5*(q_gauche(ind9)+q(ind9)); 84 | 85 | if(trace) 86 | h=figure(); 87 | h2=figure(); 88 | end 89 | 90 | for it=1:it_max 91 | 92 | u_prec = u; 93 | 94 | u_haut=[zeros(1,ncols);u(1:end-1,:)]; 95 | u_bas =[u(2:end,:);zeros(1,ncols)]; 96 | u_gauche=[zeros(nrows,1),u(:,1:end-1)]; 97 | u_droite=[u(:,2:end),zeros(nrows,1)]; 98 | u_bd=u_bas+u_droite; 99 | u_hg=u_haut+u_gauche; 100 | u3=0.25*(u_bd(ind3)+u_hg(ind3)); 101 | u4=(u_bd(ind4)+u_haut(ind4))/3; 102 | u5=(u_bd(ind5)+u_gauche(ind5))/3; 103 | u6=0.5*u_bd(ind6); 104 | u7=0.5*u_hg(ind7); 105 | u8=u_haut(ind8); 106 | u9=u_gauche(ind9); 107 | 108 | u(ind3)=u3+pq3; 109 | u(ind4)=u4+pq4; 110 | u(ind5)=u5+pq5; 111 | u(ind6)=u6+pq6; 112 | u(ind7)=u7+pq7; 113 | u(ind8)=u8+pq8; 114 | u(ind9)=u9+pq9; 115 | 116 | % Cas particuliers 117 | u(end,end)=0.5*(u(end-1,end)+u(end,end-1)); 118 | 119 | rel_res = norm(u_prec(imask)-u(imask))/norm(u_prec(imask)); 120 | if( rel_res < tol ) 121 | disp('Convergence reached') 122 | break; 123 | end 124 | 125 | if(save_rmse) 126 | moyenne_ecarts=mean(u(omega>0)-ground_truth(omega>0)); 127 | u=u-moyenne_ecarts; 128 | rmse=sqrt(sum((u(omega>0)-ground_truth(omega>0)).^2)/npix); 129 | tab_rmse=[tab_rmse,rmse]; 130 | end 131 | 132 | if(trace) 133 | % Affichage de la surface 134 | if(mod(it,100)==1) 135 | disp(sprintf('it %d - rel. res : %.9f',it,rel_res)); 136 | figure(h) 137 | surfl((u),[0 90]) 138 | axis ij 139 | view(-45,15) 140 | axis equal 141 | shading flat 142 | colormap gray 143 | 144 | if(save_rmse) 145 | figure(h2) 146 | plot((1:it)/size(p,1),tab_rmse) 147 | xlabel('$k/n$','Interpreter','Latex','FontSize',28) 148 | ylabel('RMSE','Interpreter','Latex','FontSize',28) 149 | set(gca,'FontSize',18) 150 | end 151 | 152 | end 153 | end 154 | end 155 | 156 | 157 | 158 | end 159 | -------------------------------------------------------------------------------- /Toolbox/make_gradient.m: -------------------------------------------------------------------------------- 1 | function [Dup,Dum,Dvp,Dvm,imask,imaskup,imaskum,imaskvp,imaskvm,Sup,Sum,Svp,Svm] = make_gradient(mask) 2 | 3 | [nrows,ncols] = size(mask); 4 | Omega_padded = padarray(mask,[1 1],0); 5 | 6 | % Pixels who have bottom neighbor in mask 7 | Omega(:,:,1) = mask.*Omega_padded(3:end,2:end-1); 8 | % Pixels who have top neighbor in mask 9 | Omega(:,:,2) = mask.*Omega_padded(1:end-2,2:end-1); 10 | % Pixels who have right neighbor in mask 11 | Omega(:,:,3) = mask.*Omega_padded(2:end-1,3:end); 12 | % Pixels who have left neighbor in mask 13 | Omega(:,:,4) = mask.*Omega_padded(2:end-1,1:end-2); 14 | 15 | 16 | imask = find(mask>0); 17 | index_matrix = zeros(nrows,ncols); 18 | index_matrix(imask) = 1:length(imask); 19 | 20 | % Dv matrix 21 | % When there is a neighbor on the right : forward differences 22 | idx_c = find(Omega(:,:,3)>0); 23 | [xc,yc] = ind2sub(size(mask),idx_c); 24 | indices_centre = index_matrix(idx_c); 25 | indices_right = index_matrix(sub2ind(size(mask),xc,yc+1)); 26 | indices_right = indices_right(:); 27 | II = indices_centre; 28 | JJ = indices_right; 29 | KK = ones(length(indices_centre),1); 30 | II = [II;indices_centre]; 31 | JJ = [JJ;indices_centre]; 32 | KK = [KK;-ones(length(indices_centre),1)]; 33 | 34 | Dvp = sparse(II,JJ,KK,length(imask),length(imask)); 35 | Svp = speye(length(imask)); 36 | Svp = Svp(index_matrix(idx_c),:); 37 | imaskvp = index_matrix(idx_c); 38 | 39 | % When there is a neighbor on the left : backward differences 40 | idx_c = find(Omega(:,:,4)>0); 41 | [xc,yc] = ind2sub(size(mask),idx_c); 42 | indices_centre = index_matrix(idx_c); 43 | indices_right = index_matrix(sub2ind(size(mask),xc,yc-1)); 44 | indices_right = indices_right(:); 45 | II = [indices_centre]; 46 | JJ = [indices_right]; 47 | KK = [-ones(length(indices_centre),1)]; 48 | II = [II;indices_centre]; 49 | JJ = [JJ;indices_centre]; 50 | KK = [KK;ones(length(indices_centre),1)]; 51 | 52 | Dvm = sparse(II,JJ,KK,length(imask),length(imask)); 53 | Svm = speye(length(imask)); 54 | Svm = Svm(index_matrix(idx_c),:); 55 | imaskvm = index_matrix(idx_c); 56 | 57 | 58 | % Du matrix 59 | % When there is a neighbor on the bottom : forward differences 60 | idx_c = find(Omega(:,:,1)>0); 61 | [xc,yc] = ind2sub(size(mask),idx_c); 62 | indices_centre = index_matrix(idx_c); 63 | indices_right = index_matrix(sub2ind(size(mask),xc+1,yc)); 64 | indices_right = indices_right(:); 65 | II = indices_centre; 66 | JJ = indices_right; 67 | KK = ones(length(indices_centre),1); 68 | II = [II;indices_centre]; 69 | JJ = [JJ;indices_centre]; 70 | KK = [KK;-ones(length(indices_centre),1)]; 71 | 72 | Dup = sparse(II,JJ,KK,length(imask),length(imask)); 73 | Sup = speye(length(imask)); 74 | Sup = Sup(index_matrix(idx_c),:); 75 | imaskup = index_matrix(idx_c); 76 | 77 | % When there is a neighbor on the top : backward differences 78 | idx_c = find(Omega(:,:,2)>0); 79 | [xc,yc] = ind2sub(size(mask),idx_c); 80 | indices_centre = index_matrix(idx_c); 81 | indices_right = index_matrix(sub2ind(size(mask),xc-1,yc)); 82 | indices_right = indices_right(:); 83 | II = [indices_centre]; 84 | JJ = [indices_right]; 85 | KK = [-ones(length(indices_centre),1)]; 86 | II = [II;indices_centre]; 87 | JJ = [JJ;indices_centre]; 88 | KK = [KK;ones(length(indices_centre),1)]; 89 | 90 | Dum = sparse(II,JJ,KK,length(imask),length(imask)); 91 | Sum = speye(length(imask)); 92 | Sum = Sum(index_matrix(idx_c),:); 93 | imaskum = index_matrix(idx_c); 94 | 95 | end 96 | -------------------------------------------------------------------------------- /Toolbox/mumford_shah_integration.m: -------------------------------------------------------------------------------- 1 | function [z,wup,wum,wvp,wvm,tab_nrj,tab_rmse] = mumford_shah_integration(p,q,mask,lambda,z0,mu,epsilon,maxit,tol,zinit,gt) 2 | 3 | % Check arguments 4 | if(nargin < 2) 5 | disp('Error: Not enough arguments'); 6 | return; 7 | end 8 | 9 | [nrows,ncols] = size(p); 10 | 11 | % Set default values for missing arguments 12 | if (~exist('mask','var')|isempty(mask)) mask=ones(nrows,ncols); end; 13 | if (~exist('lambda','var')|isempty(lambda)) lambda = 1e-9*mask; end; 14 | if (~exist('z0','var')|isempty(z0)) z0 = zeros(nrows,ncols); end; 15 | if (~exist('mu','var')|isempty(mu)) mu = 1e-3; end; 16 | if (~exist('epsilon','var')|isempty(epsilon)) epsilon = 1e-3; end; 17 | if (~exist('maxit','var')|isempty(maxit)) maxit = 100; end; 18 | if (~exist('tol','var')|isempty(tol)) tol = 1e-4; end; 19 | if (~exist('zinit','var')|isempty(zinit)) zinit = z0; end; 20 | if (~exist('gt','var')|isempty(gt)) gt = z0; end; 21 | 22 | 23 | % If lambda is a scalar, make it a matrix 24 | if(size(lambda,1)==1) 25 | lambda = lambda*mask; 26 | end 27 | 28 | % Make finite differences operators 29 | [Dup,Dum,Dvp,Dvm,imask,imaskup,imaskum,imaskvp,imaskvm] = make_gradient(mask); 30 | npix = length(imask); 31 | 32 | % Some stuff used later 33 | L = 0.5*(Dup'*Dup+Dum'*Dum+Dvp'*Dvp+Dvm'*Dvm); % Laplacian 34 | Lambda_two = spdiags(lambda(imask),0,npix,npix); % Regularization 35 | DuptDup = Dup'*Dup; 36 | DumtDum = Dum'*Dum; 37 | DvptDvp = Dvp'*Dvp; 38 | DvmtDvm = Dvm'*Dvm; 39 | bw = 0.5*((1/(4*epsilon))*ones(npix,1)); 40 | if(nargout>5) 41 | tab_nrj = zeros(maxit+1,1); 42 | cpt = 1; 43 | end 44 | if(nargout>6) 45 | tab_rmse = zeros(maxit+1,1); 46 | cpt_rmse = 1; 47 | end 48 | 49 | 50 | % Initialization 51 | z = zinit; 52 | 53 | wup = zeros(npix,1);wup(imaskup) = 1; 54 | wum = zeros(npix,1);wum(imaskum) = 1; 55 | wvp = zeros(npix,1);wvp(imaskvp) = 1; 56 | wvm = zeros(npix,1);wvm(imaskvm) = 1; 57 | Wup2_mat = spdiags(wup.^2,0,npix,npix); 58 | Wum2_mat = spdiags(wum.^2,0,npix,npix); 59 | Wvp2_mat = spdiags(wvp.^2,0,npix,npix); 60 | Wvm2_mat = spdiags(wvm.^2,0,npix,npix); 61 | Eup2_mat = spdiags((Dup*z(imask)-p(imask)).^2,0,npix,npix); 62 | Eum2_mat = spdiags((Dum*z(imask)-p(imask)).^2,0,npix,npix); 63 | Evp2_mat = spdiags((Dvp*z(imask)-q(imask)).^2,0,npix,npix); 64 | Evm2_mat = spdiags((Dvm*z(imask)-q(imask)).^2,0,npix,npix); 65 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.5*((norm(wup-1))/(4*epsilon)+(norm(wum-1))/(4*epsilon)+(norm(wvp-1))/(4*epsilon)+(norm(wvm-1))/(4*epsilon)+epsilon*norm(Dup*wup)+epsilon*norm(Dum*wum)+epsilon*norm(Dvp*wvp)+epsilon*norm(Dvm*wvp)+mu*norm(sqrt(Eup2_mat)*wup)+mu*norm(sqrt(Eum2_mat)*wum)+mu*norm(sqrt(Evp2_mat)*wvp)+mu*norm(sqrt(Evm2_mat)*wvm)); 66 | if(nargout>5) 67 | tab_nrj(cpt) = energie; 68 | cpt = cpt+1; 69 | end 70 | if(nargout>6) 71 | lambda = -mean(z(imask)-gt(imask)); 72 | zrmse = z+lambda; 73 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 74 | cpt_rmse = cpt_rmse+1; 75 | end 76 | 77 | % Alternating optimisation loops 78 | for k = 1:maxit 79 | 80 | 81 | % w update 82 | Eup2_mat = spdiags((Dup*z(imask)-p(imask)).^2,0,npix,npix); 83 | Eum2_mat = spdiags((Dum*z(imask)-p(imask)).^2,0,npix,npix); 84 | Evp2_mat = spdiags((Dvp*z(imask)-q(imask)).^2,0,npix,npix); 85 | Evm2_mat = spdiags((Dvm*z(imask)-q(imask)).^2,0,npix,npix); 86 | Aup = 0.5*(mu*Eup2_mat+epsilon*DuptDup+(1/(4*epsilon))*speye(npix)); 87 | Avp = 0.5*(mu*Evp2_mat+epsilon*DvptDvp+(1/(4*epsilon))*speye(npix)); 88 | Aum = 0.5*(mu*Eum2_mat+epsilon*DumtDum+(1/(4*epsilon))*speye(npix)); 89 | Avm = 0.5*(mu*Evm2_mat+epsilon*DvmtDvm+(1/(4*epsilon))*speye(npix)); 90 | [wup,fl] = pcg(Aup,bw,1e-4,100,[],[],wup); 91 | [wum,fl] = pcg(Aum,bw,1e-4,100,[],[],wum); 92 | [wvp,fl] = pcg(Avp,bw,1e-4,100,[],[],wvp); 93 | [wvm,fl] = pcg(Avm,bw,1e-4,100,[],[],wvm); 94 | Wup2_mat = spdiags(wup.^2,0,npix,npix); 95 | Wum2_mat = spdiags(wum.^2,0,npix,npix); 96 | Wvp2_mat = spdiags(wvp.^2,0,npix,npix); 97 | Wvm2_mat = spdiags(wvm.^2,0,npix,npix); 98 | 99 | % z update 100 | 101 | A = 0.5*mu*(Dup'*Wup2_mat*Dup+Dum'*Wum2_mat*Dum+Dvp'*Wvp2_mat*Dvp+Dvm'*Wvm2_mat*Dvm)+Lambda_two; % Matrix of the system 102 | b = 0.5*mu*(Dup'*Wup2_mat+Dum'*Wum2_mat)*p(imask)+0.5*mu*(Dvp'*Wvp2_mat+Dvm'*Wvm2_mat)*q(imask)+Lambda_two*z0(imask); 103 | [z(imask),fl] = pcg(A,b,1e-4,100,[],[],z(imask)); 104 | 105 | 106 | 107 | % Check CV 108 | energie_old = energie; 109 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.5*((norm(wup-1))/(4*epsilon)+(norm(wum-1))/(4*epsilon)+(norm(wvp-1))/(4*epsilon)+(norm(wvm-1))/(4*epsilon)+epsilon*norm(Dup*wup)+epsilon*norm(Dum*wum)+epsilon*norm(Dvp*wvp)+epsilon*norm(Dvm*wvp)+mu*norm(sqrt(Eup2_mat)*wup)+mu*norm(sqrt(Eum2_mat)*wum)+mu*norm(sqrt(Evp2_mat)*wvp)+mu*norm(sqrt(Evm2_mat)*wvm)); 110 | if(nargout>5) 111 | tab_nrj(cpt) = energie; 112 | cpt = cpt+1; 113 | end 114 | if(nargout>6) 115 | lambda = -mean(z(imask)-gt(imask)); 116 | zrmse = z+lambda; 117 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 118 | cpt_rmse = cpt_rmse+1; 119 | end 120 | 121 | if(nargout>5) 122 | if(mod(k,20)==1) 123 | figure(3) 124 | plot(tab_nrj(1:cpt-1)) 125 | %~ drawnow 126 | end 127 | end 128 | if(nargout>6) 129 | if(mod(k,20)==1) 130 | figure(4) 131 | plot(tab_rmse(1:cpt_rmse-1)) 132 | %~ drawnow 133 | end 134 | end 135 | %~ if(mod(k,5)==1) 136 | %~ figure(1) 137 | %~ surfl(z,[-135 30]); 138 | %~ view(-35,20) 139 | %~ shading flat; 140 | %~ colormap gray; 141 | %~ grid off 142 | %~ axis ij; 143 | %~ axis equal; 144 | %~ axis([1 size(p,1) 1 size(p,2) min(z(:)) max(z(:))]); 145 | %~ drawnow 146 | %~ end 147 | 148 | relative_residual = abs(energie-energie_old)./abs(energie_old); 149 | disp(sprintf('it %d - EAT = %.4f - res : %.6f',k,energie,relative_residual)) 150 | if(relative_residual < tol & k>5) 151 | break; 152 | end 153 | end 154 | if(k==maxit) 155 | disp('max number of iterations reached'); 156 | end 157 | 158 | % Put NaNs outside the mask 159 | z(mask==0) = NaN; 160 | wup(mask==0) = NaN; 161 | wum(mask==0) = NaN; 162 | wvp(mask==0) = NaN; 163 | wvm(mask==0) = NaN; 164 | 165 | if(nargout>5) 166 | tab_nrj = tab_nrj(1:k+1); 167 | end 168 | if(nargout>6) 169 | tab_rmse = tab_rmse(1:k+1); 170 | end 171 | end 172 | -------------------------------------------------------------------------------- /Toolbox/phi1_integration.m: -------------------------------------------------------------------------------- 1 | function [z,tab_nrj,tab_rmse] = phi1_integration(p,q,mask,lambda,z0,beta,gamma,maxit,tol,zinit,gt) 2 | 3 | % Check arguments 4 | if(nargin < 2) 5 | disp('Error: Not enough arguments'); 6 | return; 7 | end 8 | 9 | [nrows,ncols] = size(p); 10 | 11 | % Set default values for missing arguments 12 | if (~exist('mask','var')|isempty(mask)) mask=ones(nrows,ncols); end; 13 | if (~exist('lambda','var')|isempty(lambda)) lambda = 1e-9*mask; end; 14 | if (~exist('z0','var')|isempty(z0)) z0 = zeros(nrows,ncols); end; 15 | if (~exist('beta','var')|isempty(beta)) beta = 0.99; end; 16 | if (~exist('gamma','var')|isempty(gamma)) gamma = 0.01; end; 17 | if (~exist('maxit','var')|isempty(maxit)) maxit = 100; end; 18 | if (~exist('tol','var')|isempty(tol)) tol = 1e-4; end; 19 | if (~exist('zinit','var')|isempty(zinit)) zinit = z0; end; 20 | if (~exist('gt','var')|isempty(gt)) gt = z0; end; 21 | 22 | 23 | max_backtracking = 100; 24 | Linit = 0.5; 25 | L = Linit; 26 | c = 1e-2; 27 | eta = 1.2; 28 | 29 | % If lambda is a scalar, make it a matrix 30 | if(size(lambda,1)==1) 31 | lambda = lambda*mask; 32 | end 33 | 34 | % Make finite differences operators 35 | [Dup,Dum,Dvp,Dvm,imask,imaskup,imaskum,imaskvp,imaskvm] = make_gradient(mask); 36 | npix = length(imask); 37 | 38 | % Some stuff used later 39 | II = transpose(1:2*npix); 40 | JJ = repmat(1:npix,[2 1]); 41 | JJ = JJ(:); % 1 1 2 2 3 3 4 4.... 42 | Dt = sparse([],[],[],npix,2*npix,6*npix); 43 | Dt(:,1:2:end-1) = Dup'; 44 | Dt(:,2:2:end) = Dvp'; 45 | Dt2 = sparse([],[],[],npix,2*npix,6*npix); 46 | Dt2(:,1:2:end-1) = Dup'; 47 | Dt2(:,2:2:end) = Dvm'; 48 | Dt3 = sparse([],[],[],npix,2*npix,6*npix); 49 | Dt3(:,1:2:end-1) = Dum'; 50 | Dt3(:,2:2:end) = Dvp'; 51 | Dt4 = sparse([],[],[],npix,2*npix,6*npix); 52 | Dt4(:,1:2:end-1) = Dum'; 53 | Dt4(:,2:2:end) = Dvm'; 54 | Dz_minus_g = zeros(2,npix); 55 | Dz_minus_g2 = zeros(2,npix); 56 | Dz_minus_g3 = zeros(2,npix); 57 | Dz_minus_g4 = zeros(2,npix); 58 | 59 | Lambda_two = spdiags(lambda(imask),0,npix,npix); % Regularization 60 | 61 | if(nargout>1) 62 | tab_nrj = zeros(maxit+1,1); 63 | cpt = 1; 64 | end 65 | if(nargout>2) 66 | tab_rmse = zeros(maxit+1,1); 67 | cpt_rmse = 1; 68 | end 69 | 70 | % Initialization 71 | z = zinit; 72 | zprevious = z; 73 | 74 | 75 | % Compute gradient of f at current estimate 76 | % Residual 77 | Dz_minus_g(1,:) = Dup*z(imask)-p(imask); 78 | Dz_minus_g(2,:) = Dvp*z(imask)-q(imask); 79 | Dz_minus_g2(1,:) = Dup*z(imask)-p(imask); 80 | Dz_minus_g2(2,:) = Dvm*z(imask)-q(imask); 81 | Dz_minus_g3(1,:) = Dum*z(imask)-p(imask); 82 | Dz_minus_g3(2,:) = Dvp*z(imask)-q(imask); 83 | Dz_minus_g4(1,:) = Dum*z(imask)-p(imask); 84 | Dz_minus_g4(2,:) = Dvm*z(imask)-q(imask); 85 | % Normalized residual 86 | norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 87 | norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 88 | norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 89 | norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 90 | 91 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.25*(sum(log(norme_Dz_minus_g))+sum(log(norme_Dz_minus_g2))+sum(log(norme_Dz_minus_g3))+sum(log(norme_Dz_minus_g4))); 92 | 93 | if(nargout>1) 94 | tab_nrj(cpt) = energie; 95 | cpt = cpt+1; 96 | end 97 | if(nargout>2) 98 | lambda_rmse = -mean(z(imask)-gt(imask)); 99 | zrmse = z+lambda_rmse; 100 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 101 | cpt_rmse = cpt_rmse+1; 102 | end 103 | 104 | % Current energy 105 | f_curr = 0.25*(sum(log(norme_Dz_minus_g))+sum(log(norme_Dz_minus_g2))+sum(log(norme_Dz_minus_g3))+sum(log(norme_Dz_minus_g4))); 106 | 107 | % Alternating optimisation loops 108 | for k = 1:maxit 109 | 110 | Lcurr = L; 111 | 112 | % Current gradient 113 | Dz_minus_g = bsxfun(@rdivide,Dz_minus_g,norme_Dz_minus_g); 114 | Dz_minus_g2 = bsxfun(@rdivide,Dz_minus_g2,norme_Dz_minus_g2); 115 | Dz_minus_g3 = bsxfun(@rdivide,Dz_minus_g3,norme_Dz_minus_g3); 116 | Dz_minus_g4 = bsxfun(@rdivide,Dz_minus_g4,norme_Dz_minus_g4); 117 | % Make it a matrix 118 | Dz_minus_g_mat = sparse(II,JJ,Dz_minus_g(:)); 119 | Dz_minus_g_mat2 = sparse(II,JJ,Dz_minus_g2(:)); 120 | Dz_minus_g_mat3 = sparse(II,JJ,Dz_minus_g3(:)); 121 | Dz_minus_g_mat4 = sparse(II,JJ,Dz_minus_g4(:)); 122 | % Get all terms inside the sum 123 | grad_f_curr = sum(Dt*Dz_minus_g_mat+Dt2*Dz_minus_g_mat2+Dt3*Dz_minus_g_mat3+Dt4*Dz_minus_g_mat4,2); 124 | 125 | % Lazy backtracking to set stepsize 126 | lc = 0; % lc = 1 if Lipschitz constant L is big enough 127 | while(lc < max_backtracking) 128 | alpha = 1.99*(1-beta)/L; % Descent stepsize 129 | 130 | % Forward update, given the gradient 131 | zbar = z(imask) - alpha*grad_f_curr+beta*(z(imask)-zprevious(imask)); 132 | % Backward update (prox. update) 133 | znext = (zbar + 2*alpha*(lambda(imask)).*z0(imask))./(1+2*alpha*(lambda(imask))); 134 | 135 | z_dist = znext-z(imask); % Evaluate the difference between current and next estimate 136 | 137 | % Next energy 138 | Dz_minus_g(1,:) = Dup*znext(imask)-p(imask); 139 | Dz_minus_g(2,:) = Dvp*znext(imask)-q(imask); 140 | Dz_minus_g2(1,:) = Dup*znext(imask)-p(imask); 141 | Dz_minus_g2(2,:) = Dvm*znext(imask)-q(imask); 142 | Dz_minus_g3(1,:) = Dum*znext(imask)-p(imask); 143 | Dz_minus_g3(2,:) = Dvp*znext(imask)-q(imask); 144 | Dz_minus_g4(1,:) = Dum*znext(imask)-p(imask); 145 | Dz_minus_g4(2,:) = Dvm*znext(imask)-q(imask); 146 | norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 147 | norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 148 | norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 149 | norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 150 | 151 | f_next = 0.25*(sum(log(norme_Dz_minus_g))+sum(log(norme_Dz_minus_g2))+sum(log(norme_Dz_minus_g3))+sum(log(norme_Dz_minus_g4))); 152 | 153 | % Lipschitz test 154 | if(f_next <= f_curr+grad_f_curr'*z_dist+0.5*L*z_dist'*z_dist) 155 | L = L/1.05; 156 | break; % if Lipschitz => stepsize is small enough 157 | else 158 | lc = lc+1; % if not Lipschitz => try smaller stepsize 159 | L = eta*L; 160 | end 161 | end 162 | 163 | % Update auxiliary variables 164 | zprevious = z; 165 | z(imask) = znext; 166 | f_curr = f_next; 167 | 168 | % Current residual 169 | Dz_minus_g(1,:) = Dup*z(imask)-p(imask); 170 | Dz_minus_g(2,:) = Dvp*z(imask)-q(imask); 171 | Dz_minus_g2(1,:) = Dup*z(imask)-p(imask); 172 | Dz_minus_g2(2,:) = Dvm*z(imask)-q(imask); 173 | Dz_minus_g3(1,:) = Dum*z(imask)-p(imask); 174 | Dz_minus_g3(2,:) = Dvp*z(imask)-q(imask); 175 | Dz_minus_g4(1,:) = Dum*z(imask)-p(imask); 176 | Dz_minus_g4(2,:) = Dvm*z(imask)-q(imask); 177 | % Normalized residual 178 | norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 179 | norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 180 | norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 181 | norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 182 | 183 | %~ 184 | %~ % After a few iterations, decrease the Lipschitz constant for speedup 185 | %~ if(mod(k,50) == 1) 186 | %~ 187 | %~ % Current gradient 188 | %~ Dz_minus_g = bsxfun(@rdivide,Dz_minus_g,norme_Dz_minus_g); 189 | %~ Dz_minus_g2 = bsxfun(@rdivide,Dz_minus_g2,norme_Dz_minus_g2); 190 | %~ Dz_minus_g3 = bsxfun(@rdivide,Dz_minus_g3,norme_Dz_minus_g3); 191 | %~ Dz_minus_g4 = bsxfun(@rdivide,Dz_minus_g4,norme_Dz_minus_g4); 192 | %~ % Make it a matrix 193 | %~ Dz_minus_g_mat = sparse(II,JJ,Dz_minus_g(:)); 194 | %~ Dz_minus_g_mat2 = sparse(II,JJ,Dz_minus_g2(:)); 195 | %~ Dz_minus_g_mat3 = sparse(II,JJ,Dz_minus_g3(:)); 196 | %~ Dz_minus_g_mat4 = sparse(II,JJ,Dz_minus_g4(:)); 197 | %~ % Get all terms inside the sum 198 | %~ grad_f_curr = sum(Dt*Dz_minus_g_mat+Dt2*Dz_minus_g_mat2+Dt3*Dz_minus_g_mat3+Dt4*Dz_minus_g_mat4,2); 199 | %~ 200 | %~ % Lazy backtracking to set stepsize 201 | %~ lc = 0; % lc = 1 if Lipschitz constant L is big enough 202 | %~ while(lc < max_backtracking) 203 | %~ alpha = 2*(1-beta)/(c+L); % Descent stepsize 204 | %~ 205 | %~ % Forward update, given the gradient 206 | %~ zbar = z(imask) - alpha*grad_f_curr+beta*(z(imask)-zprevious(imask)); 207 | %~ % Backward update (prox. update) 208 | %~ znext = (zbar + 2*alpha*(lambda(imask)).*z0(imask))./(1+2*alpha*(lambda(imask))); 209 | %~ 210 | %~ z_dist = znext-z(imask); % Evaluate the difference between current and next estimate 211 | %~ 212 | %~ % Next energy 213 | %~ Dz_minus_g(1,:) = Dup*znext(imask)-p(imask); 214 | %~ Dz_minus_g(2,:) = Dvp*znext(imask)-q(imask); 215 | %~ Dz_minus_g2(1,:) = Dup*znext(imask)-p(imask); 216 | %~ Dz_minus_g2(2,:) = Dvm*znext(imask)-q(imask); 217 | %~ Dz_minus_g3(1,:) = Dum*znext(imask)-p(imask); 218 | %~ Dz_minus_g3(2,:) = Dvp*znext(imask)-q(imask); 219 | %~ Dz_minus_g4(1,:) = Dum*znext(imask)-p(imask); 220 | %~ Dz_minus_g4(2,:) = Dvm*znext(imask)-q(imask); 221 | %~ norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 222 | %~ norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 223 | %~ norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 224 | %~ norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 225 | %~ 226 | %~ f_next = 0.5*(sum(log(norme_Dz_minus_g))+sum(log(norme_Dz_minus_g2))+sum(log(norme_Dz_minus_g3))+sum(log(norme_Dz_minus_g4))); 227 | %~ 228 | %~ % Lipschitz test 229 | %~ if(f_next <= f_curr+grad_f_curr'*z_dist+0.5*L*z_dist'*z_dist) 230 | %~ L = L/eta; 231 | %~ lc = lc+1; % if not Lipschitz => try smaller stepsize 232 | %~ else 233 | %~ lc = max_backtracking; % if Lipschitz => stepsize is small enough 234 | %~ end 235 | %~ end 236 | %~ end 237 | 238 | 239 | % Check CV 240 | energie_old = energie; 241 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.25*(sum(log(norme_Dz_minus_g))+sum(log(norme_Dz_minus_g2))+sum(log(norme_Dz_minus_g3))+sum(log(norme_Dz_minus_g4))); 242 | 243 | if(nargout>1) 244 | tab_nrj(cpt) = energie; 245 | cpt = cpt+1; 246 | end 247 | if(nargout>2) 248 | lambda_rmse = -mean(z(imask)-gt(imask)); 249 | zrmse = z+lambda_rmse; 250 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 251 | cpt_rmse = cpt_rmse+1; 252 | end 253 | 254 | 255 | if(nargout>1) 256 | if(mod(k,100)==1) 257 | figure(3) 258 | plot(tab_nrj(1:cpt-1)) 259 | %~ drawnow 260 | end 261 | end 262 | if(nargout>2) 263 | if(mod(k,100)==1) 264 | figure(4) 265 | plot(tab_rmse(1:cpt_rmse-1)) 266 | %~ drawnow 267 | end 268 | end 269 | %~ if(mod(k,50)==1) 270 | %~ figure(473) 271 | %~ surfl(z,[-135 30]); 272 | %~ view(-35,20) 273 | %~ shading flat; 274 | %~ colormap gray; 275 | %~ grid off 276 | %~ axis ij; 277 | %~ axis equal; 278 | %~ axis([1 size(p,1) 1 size(p,2) min(z(:)) max(z(:))]); 279 | %~ drawnow 280 | %~ end 281 | 282 | relative_residual = abs(energie-energie_old)./abs(energie_old); 283 | disp(sprintf('it %d - EAT = %.4f - res : %.6f',k,energie,relative_residual)); 284 | if(relative_residual < tol & (energie 500) 285 | break; 286 | end 287 | end 288 | if(k == maxit) 289 | disp('Max number of iterations reached'); 290 | end 291 | 292 | %~ close(473) 293 | 294 | % Put NaNs outside the mask 295 | z(mask==0) = NaN; 296 | 297 | if(nargout>1) 298 | tab_nrj = tab_nrj(1:k+1); 299 | end 300 | if(nargout>2) 301 | tab_rmse = tab_rmse(1:k+1); 302 | end 303 | end 304 | -------------------------------------------------------------------------------- /Toolbox/phi2_integration.m: -------------------------------------------------------------------------------- 1 | function [z,tab_nrj,tab_rmse] = phi2_integration(p,q,mask,lambda,z0,beta,gamma,maxit,tol,zinit,gt) 2 | 3 | % Check arguments 4 | if(nargin < 2) 5 | disp('Error: Not enough arguments'); 6 | return; 7 | end 8 | 9 | [nrows,ncols] = size(p); 10 | 11 | % Set default values for missing arguments 12 | if (~exist('mask','var')|isempty(mask)) mask=ones(nrows,ncols); end; 13 | if (~exist('lambda','var')|isempty(lambda)) lambda = 1e-9*mask; end; 14 | if (~exist('z0','var')|isempty(z0)) z0 = zeros(nrows,ncols); end; 15 | if (~exist('beta','var')|isempty(beta)) beta = 0.99; end; 16 | if (~exist('gamma','var')|isempty(gamma)) gamma = 0.01; end; 17 | if (~exist('maxit','var')|isempty(maxit)) maxit = 100; end; 18 | if (~exist('tol','var')|isempty(tol)) tol = 1e-4; end; 19 | if (~exist('zinit','var')|isempty(zinit)) zinit = z0; end; 20 | if (~exist('gt','var')|isempty(gt)) gt = z0; end; 21 | 22 | 23 | max_backtracking = 100; 24 | Linit = 0.5; 25 | L = Linit; 26 | c = 1e-2; 27 | eta = 1.2; 28 | 29 | % If lambda is a scalar, make it a matrix 30 | if(size(lambda,1)==1) 31 | lambda = lambda*mask; 32 | end 33 | 34 | % Make finite differences operators 35 | [Dup,Dum,Dvp,Dvm,imask,imaskup,imaskum,imaskvp,imaskvm] = make_gradient(mask); 36 | npix = length(imask); 37 | 38 | % Some stuff used later 39 | II = transpose(1:2*npix); 40 | JJ = repmat(1:npix,[2 1]); 41 | JJ = JJ(:); % 1 1 2 2 3 3 4 4.... 42 | Dt = sparse([],[],[],npix,2*npix,6*npix); 43 | Dt(:,1:2:end-1) = Dup'; 44 | Dt(:,2:2:end) = Dvp'; 45 | Dt2 = sparse([],[],[],npix,2*npix,6*npix); 46 | Dt2(:,1:2:end-1) = Dup'; 47 | Dt2(:,2:2:end) = Dvm'; 48 | Dt3 = sparse([],[],[],npix,2*npix,6*npix); 49 | Dt3(:,1:2:end-1) = Dum'; 50 | Dt3(:,2:2:end) = Dvp'; 51 | Dt4 = sparse([],[],[],npix,2*npix,6*npix); 52 | Dt4(:,1:2:end-1) = Dum'; 53 | Dt4(:,2:2:end) = Dvm'; 54 | Dz_minus_g = zeros(2,npix); 55 | Dz_minus_g2 = zeros(2,npix); 56 | Dz_minus_g3 = zeros(2,npix); 57 | Dz_minus_g4 = zeros(2,npix); 58 | 59 | Lambda_two = spdiags(lambda(imask),0,npix,npix); % Regularization 60 | 61 | if(nargout>1) 62 | tab_nrj = zeros(maxit+1,1); 63 | cpt = 1; 64 | end 65 | if(nargout>2) 66 | tab_rmse = zeros(maxit+1,1); 67 | cpt_rmse = 1; 68 | end 69 | 70 | % Initialization 71 | z = zinit; 72 | zprevious = z; 73 | 74 | 75 | % Compute gradient of f at current estimate 76 | % Residual 77 | Dz_minus_g(1,:) = Dup*z(imask)-p(imask); 78 | Dz_minus_g(2,:) = Dvp*z(imask)-q(imask); 79 | Dz_minus_g2(1,:) = Dup*z(imask)-p(imask); 80 | Dz_minus_g2(2,:) = Dvm*z(imask)-q(imask); 81 | Dz_minus_g3(1,:) = Dum*z(imask)-p(imask); 82 | Dz_minus_g3(2,:) = Dvp*z(imask)-q(imask); 83 | Dz_minus_g4(1,:) = Dum*z(imask)-p(imask); 84 | Dz_minus_g4(2,:) = Dvm*z(imask)-q(imask); 85 | % Normalized residual 86 | norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 87 | norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 88 | norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 89 | norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 90 | 91 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.25*sum((norme_Dz_minus_g-gamma^2)./(norme_Dz_minus_g).^2)+0.25*sum((norme_Dz_minus_g2-gamma^2)./(norme_Dz_minus_g2).^2)+0.25*sum((norme_Dz_minus_g3-gamma^2)./(norme_Dz_minus_g3).^2)+0.25*sum((norme_Dz_minus_g4-gamma^2)./(norme_Dz_minus_g4).^2); 92 | 93 | if(nargout>1) 94 | tab_nrj(cpt) = energie; 95 | cpt = cpt+1; 96 | end 97 | if(nargout>2) 98 | lambda_rmse = -mean(z(imask)-gt(imask)); 99 | zrmse = z+lambda_rmse; 100 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 101 | cpt_rmse = cpt_rmse+1; 102 | end 103 | 104 | % Current energy 105 | f_curr = 0.25*sum((norme_Dz_minus_g-gamma^2)./(norme_Dz_minus_g).^2)+0.25*sum((norme_Dz_minus_g2-gamma^2)./(norme_Dz_minus_g2).^2)+0.25*sum((norme_Dz_minus_g3-gamma^2)./(norme_Dz_minus_g3).^2)+0.25*sum((norme_Dz_minus_g4-gamma^2)./(norme_Dz_minus_g4).^2); 106 | 107 | % Alternating optimisation loops 108 | for k = 1:maxit 109 | 110 | Lcurr = L; 111 | 112 | % Current gradient 113 | Dz_minus_g = bsxfun(@rdivide,Dz_minus_g,norme_Dz_minus_g.^2); 114 | Dz_minus_g2 = bsxfun(@rdivide,Dz_minus_g2,norme_Dz_minus_g2.^2); 115 | Dz_minus_g3 = bsxfun(@rdivide,Dz_minus_g3,norme_Dz_minus_g3.^2); 116 | Dz_minus_g4 = bsxfun(@rdivide,Dz_minus_g4,norme_Dz_minus_g4.^2); 117 | % Make it a matrix 118 | Dz_minus_g_mat = sparse(II,JJ,Dz_minus_g(:)); 119 | Dz_minus_g_mat2 = sparse(II,JJ,Dz_minus_g2(:)); 120 | Dz_minus_g_mat3 = sparse(II,JJ,Dz_minus_g3(:)); 121 | Dz_minus_g_mat4 = sparse(II,JJ,Dz_minus_g4(:)); 122 | % Get all terms inside the sum 123 | grad_f_curr = 0.5*sum(gamma^2*Dt*Dz_minus_g_mat+gamma^2*Dt2*Dz_minus_g_mat2+gamma^2*Dt3*Dz_minus_g_mat3+gamma^2*Dt4*Dz_minus_g_mat4,2); 124 | 125 | % Lazy backtracking to set stepsize 126 | lc = 0; % lc = 1 if Lipschitz constant L is big enough 127 | while(lc < max_backtracking) 128 | alpha = 1.99*(1-beta)/L; % Descent stepsize 129 | 130 | % Forward update, given the gradient 131 | zbar = z(imask) - alpha*grad_f_curr+beta*(z(imask)-zprevious(imask)); 132 | % Backward update (prox. update) 133 | znext = (zbar + 2*alpha*(lambda(imask)).*z0(imask))./(1+2*alpha*(lambda(imask))); 134 | 135 | z_dist = znext-z(imask); % Evaluate the difference between current and next estimate 136 | 137 | % Next energy 138 | Dz_minus_g(1,:) = Dup*znext(imask)-p(imask); 139 | Dz_minus_g(2,:) = Dvp*znext(imask)-q(imask); 140 | Dz_minus_g2(1,:) = Dup*znext(imask)-p(imask); 141 | Dz_minus_g2(2,:) = Dvm*znext(imask)-q(imask); 142 | Dz_minus_g3(1,:) = Dum*znext(imask)-p(imask); 143 | Dz_minus_g3(2,:) = Dvp*znext(imask)-q(imask); 144 | Dz_minus_g4(1,:) = Dum*znext(imask)-p(imask); 145 | Dz_minus_g4(2,:) = Dvm*znext(imask)-q(imask); 146 | norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 147 | norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 148 | norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 149 | norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 150 | 151 | f_next = 0.25*sum((norme_Dz_minus_g-gamma^2)./(norme_Dz_minus_g).^2)+0.25*sum((norme_Dz_minus_g2-gamma^2)./(norme_Dz_minus_g2).^2)+0.25*sum((norme_Dz_minus_g3-gamma^2)./(norme_Dz_minus_g3).^2)+0.25*sum((norme_Dz_minus_g4-gamma^2)./(norme_Dz_minus_g4).^2); 152 | 153 | 154 | % Lipschitz test 155 | if(f_next <= f_curr+grad_f_curr'*z_dist+0.5*L*z_dist'*z_dist) 156 | L = L/1.05; 157 | lc = max_backtracking; % if Lipschitz => stepsize is small enough 158 | else 159 | lc = lc+1; % if not Lipschitz => try smaller stepsize 160 | L = eta*L; 161 | end 162 | end 163 | 164 | % Update auxiliary variables 165 | zprevious = z; 166 | z(imask) = znext; 167 | f_curr = f_next; 168 | 169 | % Current residual 170 | Dz_minus_g(1,:) = Dup*z(imask)-p(imask); 171 | Dz_minus_g(2,:) = Dvp*z(imask)-q(imask); 172 | Dz_minus_g2(1,:) = Dup*z(imask)-p(imask); 173 | Dz_minus_g2(2,:) = Dvm*z(imask)-q(imask); 174 | Dz_minus_g3(1,:) = Dum*z(imask)-p(imask); 175 | Dz_minus_g3(2,:) = Dvp*z(imask)-q(imask); 176 | Dz_minus_g4(1,:) = Dum*z(imask)-p(imask); 177 | Dz_minus_g4(2,:) = Dvm*z(imask)-q(imask); 178 | % Normalized residual 179 | norme_Dz_minus_g = gamma^2+(Dz_minus_g(1,:).^2+Dz_minus_g(2,:).^2); 180 | norme_Dz_minus_g2 = gamma^2+(Dz_minus_g2(1,:).^2+Dz_minus_g2(2,:).^2); 181 | norme_Dz_minus_g3 = gamma^2+(Dz_minus_g3(1,:).^2+Dz_minus_g3(2,:).^2); 182 | norme_Dz_minus_g4 = gamma^2+(Dz_minus_g4(1,:).^2+Dz_minus_g4(2,:).^2); 183 | 184 | 185 | % Check CV 186 | energie_old = energie; 187 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.25*sum((norme_Dz_minus_g-gamma^2)./(norme_Dz_minus_g).^2)+0.25*sum((norme_Dz_minus_g2-gamma^2)./(norme_Dz_minus_g2).^2)+0.25*sum((norme_Dz_minus_g3-gamma^2)./(norme_Dz_minus_g3).^2)+0.25*sum((norme_Dz_minus_g4-gamma^2)./(norme_Dz_minus_g4).^2); 188 | 189 | if(nargout>1) 190 | tab_nrj(cpt) = energie; 191 | cpt = cpt+1; 192 | end 193 | if(nargout>2) 194 | lambda_rmse = -mean(z(imask)-gt(imask)); 195 | zrmse = z+lambda_rmse; 196 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 197 | cpt_rmse = cpt_rmse+1; 198 | end 199 | 200 | 201 | %~ if(mod(k,100)==1) 202 | %~ figure(1) 203 | %~ surfl(z,[-135 30]); 204 | %~ view(-35,20) 205 | %~ shading flat; 206 | %~ colormap gray; 207 | %~ grid off 208 | %~ axis ij; 209 | %~ axis equal; 210 | %~ axis([1 size(p,1) 1 size(p,2) min(z(:)) max(z(:))]); 211 | %~ drawnow 212 | %~ end 213 | 214 | if(nargout>1) 215 | if(mod(k,100)==1) 216 | figure(3) 217 | plot(tab_nrj(1:cpt-1)) 218 | drawnow 219 | end 220 | end 221 | if(nargout>2) 222 | if(mod(k,100)==1) 223 | figure(4) 224 | plot(tab_rmse(1:cpt_rmse-1)) 225 | drawnow 226 | end 227 | end 228 | 229 | relative_residual = abs(energie-energie_old)./abs(energie_old); 230 | disp(sprintf('it %d - EAT = %.4f - res : %.6f ',k,energie,relative_residual)); 231 | if(relative_residual < tol & (energie500) 232 | break; 233 | end 234 | end 235 | if(k == maxit) 236 | disp('Max number of iterations reached'); 237 | end 238 | 239 | 240 | % Put NaNs outside the mask 241 | z(mask==0) = NaN; 242 | 243 | if(nargout>1) 244 | tab_nrj = tab_nrj(1:k+1); 245 | end 246 | if(nargout>2) 247 | tab_rmse = tab_rmse(1:k+1); 248 | end 249 | end 250 | -------------------------------------------------------------------------------- /Toolbox/smooth_integration.m: -------------------------------------------------------------------------------- 1 | function z = smooth_integration(p,q,mask,lambda,z0,solver,precond) 2 | 3 | % Check arguments 4 | if(nargin < 2) 5 | disp('Error: Not enough arguments'); 6 | return; 7 | end 8 | 9 | [nrows,ncols] = size(p); 10 | 11 | % Set default values for missing arguments 12 | if (~exist('mask','var')|isempty(mask)) mask=ones(nrows,ncols); end; 13 | if (~exist('lambda','var')|isempty(lambda)) lambda = 1e-9*mask; end; 14 | if (~exist('z0','var')|isempty(z0)) z0 = zeros(nrows,ncols); end; 15 | if (~exist('solver','var')|isempty(solver)) solver = 'pcg'; end; 16 | if (~exist('precond','var')|isempty(precond)) precond = 'ichol'; end; 17 | 18 | % If lambda is a scalar, make it a matrix 19 | if(size(lambda,1)==1) 20 | lambda = lambda*mask; 21 | end 22 | 23 | % Make finite differences operators 24 | [Dup,Dum,Dvp,Dvm,imask] = make_gradient(mask); 25 | 26 | % Matrix of the system 27 | L = 0.5*(Dup'*Dup+Dum'*Dum+Dvp'*Dvp+Dvm'*Dvm); 28 | Lambda_two = spdiags(lambda(imask),0,length(imask),length(imask)); 29 | A = L + Lambda_two; 30 | 31 | % Second membre 32 | Du = 0.5*(Dup'+Dum'); 33 | Dv = 0.5*(Dvp'+Dvm'); 34 | b = Du*p(imask)+Dv*q(imask)+Lambda_two*z0(imask); 35 | 36 | % Preconditioning 37 | if(strcmp(precond,'none')) 38 | precondL = []; 39 | precondR = []; 40 | elseif(strcmp(precond,'CMG')) 41 | precondL = cmg_sdd(A); 42 | precondR = []; 43 | elseif(strcmp(precond,'ichol')) % Modified incomplete cholesky advised in [Bahr et al., CVM 2017] 44 | precondL = ichol(A,struct('type','ict','droptol',1e-03,'michol','on')); 45 | precondR = precondL'; 46 | end 47 | 48 | % Resolution 49 | z = z0; 50 | if(strcmp(solver,'direct')) % Calls cholesky 51 | z(imask) = A\b; 52 | elseif(strcmp(solver,'pcg')) % Calls CG 53 | z(imask) = pcg(A,b,1e-4,1000,precondL,precondR,z(imask)); 54 | end 55 | 56 | % Put NaNs outside the mask 57 | z(mask==0) = NaN; 58 | end 59 | -------------------------------------------------------------------------------- /Toolbox/tv_integration.m: -------------------------------------------------------------------------------- 1 | function [z,tab_nrj,tab_rmse] = tv_integration(p,q,mask,lambda,z0,alpha,maxit,tol,zinit,gt) 2 | 3 | % Check arguments 4 | if(nargin < 2) 5 | disp('Error: Not enough arguments'); 6 | return; 7 | end 8 | 9 | [nrows,ncols] = size(p); 10 | 11 | % Set default values for missing arguments 12 | if (~exist('mask','var')|isempty(mask)) mask=ones(nrows,ncols); end; 13 | if (~exist('lambda','var')|isempty(lambda)) lambda = 1e-9*mask; end; 14 | if (~exist('z0','var')|isempty(z0)) z0 = zeros(nrows,ncols); end; 15 | if (~exist('alpha','var')|isempty(alpha)) alpha = 0.01; end; 16 | if (~exist('maxit','var')|isempty(maxit)) maxit = 100; end; 17 | if (~exist('tol','var')|isempty(tol)) tol = 1e-4; end; 18 | if (~exist('zinit','var')|isempty(zinit)) zinit = z0; end; 19 | if (~exist('gt','var')|isempty(gt)) gt = z0; end; 20 | 21 | 22 | % If lambda is a scalar, make it a matrix 23 | if(size(lambda,1)==1) 24 | lambda = lambda*mask; 25 | end 26 | 27 | % Make finite differences operators 28 | [Dup,Dum,Dvp,Dvm,imask,imaskup,imaskum,imaskvp,imaskvm] = make_gradient(mask); 29 | npix = length(imask); 30 | 31 | % Some stuff used later 32 | Lambda_two = spdiags(lambda(imask),0,npix,npix); % Regularization 33 | A = Lambda_two+0.125*alpha*((Dup'*Dup+Dvp'*Dvp)+(Dup'*Dup+Dvm'*Dvm)+(Dum'*Dum+Dvp'*Dvp)+(Dum'*Dum+Dvm'*Dvm)); 34 | 35 | if(nargout>1) 36 | tab_nrj = zeros(maxit+1,1); 37 | cpt = 1; 38 | end 39 | if(nargout>2) 40 | tab_rmse = zeros(maxit+1,1); 41 | cpt_rmse = 1; 42 | end 43 | 44 | % Initialization 45 | z = zinit; 46 | 47 | rpp1 = zeros(npix,1); 48 | rpm1 = zeros(npix,1); 49 | rmp1 = zeros(npix,1); 50 | rmm1 = zeros(npix,1); 51 | rpp2 = zeros(npix,1); 52 | rpm2 = zeros(npix,1); 53 | rmp2 = zeros(npix,1); 54 | rmm2 = zeros(npix,1); 55 | bpp1 = zeros(npix,1); 56 | bpm1 = zeros(npix,1); 57 | bmp1 = zeros(npix,1); 58 | bmm1 = zeros(npix,1); 59 | bpp2 = zeros(npix,1); 60 | bpm2 = zeros(npix,1); 61 | bmp2 = zeros(npix,1); 62 | bmm2 = zeros(npix,1); 63 | 64 | % r update 65 | spp1 = Dup*z(imask)-p(imask)+bpp1; 66 | spm1 = Dup*z(imask)-p(imask)+bpm1; 67 | smp1 = Dum*z(imask)-p(imask)+bmp1; 68 | smm1 = Dum*z(imask)-p(imask)+bmm1; 69 | spp2 = Dvp*z(imask)-q(imask)+bpp2; 70 | spm2 = Dvm*z(imask)-q(imask)+bpm2; 71 | smp2 = Dvp*z(imask)-q(imask)+bmp2; 72 | smm2 = Dvm*z(imask)-q(imask)+bmm2; 73 | rpp1 = max(sqrt(spp1.^2+spp2.^2)-4/alpha,0).*spp1./sqrt(spp1.^2+spp2.^2); 74 | rpm1 = max(sqrt(spm1.^2+spm2.^2)-4/alpha,0).*spm1./sqrt(spm1.^2+spm2.^2); 75 | rmp1 = max(sqrt(smp1.^2+smp2.^2)-4/alpha,0).*smp1./sqrt(smp1.^2+smp2.^2); 76 | rmm1 = max(sqrt(smm1.^2+smm2.^2)-4/alpha,0).*smm1./sqrt(smm1.^2+smm2.^2); 77 | rpp2 = max(sqrt(spp1.^2+spp2.^2)-4/alpha,0).*spp2./sqrt(spp1.^2+spp2.^2); 78 | rpm2 = max(sqrt(spm1.^2+spm2.^2)-4/alpha,0).*spm2./sqrt(spm1.^2+spm2.^2); 79 | rmp2 = max(sqrt(smp1.^2+smp2.^2)-4/alpha,0).*smp2./sqrt(smp1.^2+smp2.^2); 80 | rmm2 = max(sqrt(smm1.^2+smm2.^2)-4/alpha,0).*smm2./sqrt(smm1.^2+smm2.^2); 81 | 82 | % b update 83 | bpp1 = bpp1+Dup*z(imask)-p(imask)-rpp1; 84 | bpm1 = bpm1+Dup*z(imask)-p(imask)-rpm1; 85 | bmp1 = bmp1+Dum*z(imask)-p(imask)-rmp1; 86 | bmm1 = bmm1+Dum*z(imask)-p(imask)-rmm1; 87 | bpp2 = bpp2+Dvp*z(imask)-q(imask)-rpp2; 88 | bpm2 = bpm2+Dvm*z(imask)-q(imask)-rpm2; 89 | bmp2 = bmp2+Dvp*z(imask)-q(imask)-rmp2; 90 | bmm2 = bmm2+Dvm*z(imask)-q(imask)-rmm2; 91 | 92 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+sum(sqrt((Dup*z(imask)-p(imask)).^2)+(Dvp*z(imask)-q(imask)).^2)+sum(sqrt((Dup*z(imask)-p(imask)).^2)+(Dvm*z(imask)-q(imask)).^2)+sum(sqrt((Dum*z(imask)-p(imask)).^2)+(Dvp*z(imask)-q(imask)).^2)+sum(sqrt((Dum*z(imask)-p(imask)).^2)+(Dvm*z(imask)-q(imask)).^2); 93 | 94 | if(nargout>1) 95 | tab_nrj(cpt) = energie; 96 | cpt = cpt+1; 97 | end 98 | if(nargout>2) 99 | lambda = -mean(z(imask)-gt(imask)); 100 | zrmse = z+lambda; 101 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 102 | cpt_rmse = cpt_rmse+1; 103 | end 104 | 105 | % Alternating optimisation loops 106 | for k = 1:maxit 107 | 108 | % z update 109 | ppp = p(imask)+rpp1-bpp1; 110 | ppm = p(imask)+rpm1-bpm1; 111 | pmp = p(imask)+rmp1-bmp1; 112 | pmm = p(imask)+rmm1-bmm1; 113 | qpp = q(imask)+rpp2-bpp2; 114 | qpm = q(imask)+rpm2-bpm2; 115 | qmp = q(imask)+rmp2-bmp2; 116 | qmm = q(imask)+rmm2-bmm2; 117 | b = Lambda_two*z0(imask)+0.125*alpha*((Dup'*ppp+Dvp'*qpp)+(Dup'*ppm+Dvm'*qpm)+(Dum'*pmp+Dvp'*qmp)+(Dum'*pmm+Dvm'*qmm)); 118 | [z(imask),fl] = pcg(A,b,1e-9,10,[],[],z(imask)); 119 | 120 | % r update 121 | spp1 = Dup*z(imask)-p(imask)+bpp1; 122 | spm1 = Dup*z(imask)-p(imask)+bpm1; 123 | smp1 = Dum*z(imask)-p(imask)+bmp1; 124 | smm1 = Dum*z(imask)-p(imask)+bmm1; 125 | spp2 = Dvp*z(imask)-q(imask)+bpp2; 126 | spm2 = Dvm*z(imask)-q(imask)+bpm2; 127 | smp2 = Dvp*z(imask)-q(imask)+bmp2; 128 | smm2 = Dvm*z(imask)-q(imask)+bmm2; 129 | rpp1 = max(sqrt(spp1.^2+spp2.^2)-4/alpha,0).*spp1./sqrt(spp1.^2+spp2.^2); 130 | rpm1 = max(sqrt(spm1.^2+spm2.^2)-4/alpha,0).*spm1./sqrt(spm1.^2+spm2.^2); 131 | rmp1 = max(sqrt(smp1.^2+smp2.^2)-4/alpha,0).*smp1./sqrt(smp1.^2+smp2.^2); 132 | rmm1 = max(sqrt(smm1.^2+smm2.^2)-4/alpha,0).*smm1./sqrt(smm1.^2+smm2.^2); 133 | rpp2 = max(sqrt(spp1.^2+spp2.^2)-4/alpha,0).*spp2./sqrt(spp1.^2+spp2.^2); 134 | rpm2 = max(sqrt(spm1.^2+spm2.^2)-4/alpha,0).*spm2./sqrt(spm1.^2+spm2.^2); 135 | rmp2 = max(sqrt(smp1.^2+smp2.^2)-4/alpha,0).*smp2./sqrt(smp1.^2+smp2.^2); 136 | rmm2 = max(sqrt(smm1.^2+smm2.^2)-4/alpha,0).*smm2./sqrt(smm1.^2+smm2.^2); 137 | 138 | % b update 139 | bpp1 = bpp1+Dup*z(imask)-p(imask)-rpp1; 140 | bpm1 = bpm1+Dup*z(imask)-p(imask)-rpm1; 141 | bmp1 = bmp1+Dum*z(imask)-p(imask)-rmp1; 142 | bmm1 = bmm1+Dum*z(imask)-p(imask)-rmm1; 143 | bpp2 = bpp2+Dvp*z(imask)-q(imask)-rpp2; 144 | bpm2 = bpm2+Dvm*z(imask)-q(imask)-rpm2; 145 | bmp2 = bmp2+Dvp*z(imask)-q(imask)-rmp2; 146 | bmm2 = bmm2+Dvm*z(imask)-q(imask)-rmm2; 147 | 148 | 149 | % Check CV 150 | energie_old = energie; 151 | energie = norm(Lambda_two*(z(imask)-z0(imask)))+0.25*(sum(sqrt((Dup*z(imask)-p(imask)).^2)+(Dvp*z(imask)-q(imask)).^2)+sum(sqrt((Dup*z(imask)-p(imask)).^2)+(Dvm*z(imask)-q(imask)).^2)+sum(sqrt((Dum*z(imask)-p(imask)).^2)+(Dvp*z(imask)-q(imask)).^2)+sum(sqrt((Dum*z(imask)-p(imask)).^2)+(Dvm*z(imask)-q(imask)).^2)); 152 | 153 | if(nargout>1) 154 | tab_nrj(cpt) = energie; 155 | cpt = cpt+1; 156 | end 157 | if(nargout>2) 158 | lambda = -mean(z(imask)-gt(imask)); 159 | zrmse = z+lambda; 160 | tab_rmse(cpt_rmse) = sqrt(mean((zrmse(imask)-gt(imask)).^2)); 161 | cpt_rmse = cpt_rmse+1; 162 | end 163 | %~ 164 | %~ figure(1) 165 | %~ surfl(z,[-135 30]); 166 | %~ view(-35,20) 167 | %~ axis ij; 168 | %~ axis equal; 169 | %~ axis([1 320 1 320 -20 70]); 170 | %~ shading flat; 171 | %~ colormap gray; 172 | %~ 173 | %~ grid off 174 | %~ drawnow 175 | %~ 176 | %~ figure(2) 177 | %~ wupdisp = NaN*ones(nrows,ncols); 178 | %~ wupdisp(imask) = bpp1; 179 | %~ imagesc(wupdisp); 180 | %~ colormap gray 181 | %~ colorbar 182 | %~ 183 | %~ if(nargout>1) 184 | %~ figure(3) 185 | %~ plot(tab_nrj(1:cpt-1)) 186 | %~ drawnow 187 | %~ end 188 | %~ if(nargout>2) 189 | %~ figure(4) 190 | %~ plot(tab_rmse(1:cpt_rmse-1)) 191 | %~ drawnow 192 | %~ end 193 | 194 | relative_residual = abs(energie-energie_old)./abs(energie_old); 195 | disp(sprintf('it %d - EAT = %.4f - res : %.6f',k,energie,relative_residual)); 196 | if(relative_residual < tol & k>20) 197 | break; 198 | end 199 | end 200 | 201 | if(k==maxit) 202 | disp('Maximum number of iterations reached'); 203 | end 204 | 205 | % Put NaNs outside the mask 206 | z(mask==0) = NaN; 207 | wup(mask==0) = NaN; 208 | wum(mask==0) = NaN; 209 | wvp(mask==0) = NaN; 210 | wvm(mask==0) = NaN; 211 | 212 | if(nargout>1) 213 | tab_nrj = tab_nrj(1:k+1); 214 | end 215 | if(nargout>2) 216 | tab_rmse = tab_rmse(1:k+1); 217 | end 218 | end 219 | -------------------------------------------------------------------------------- /demo_1_survey.m: -------------------------------------------------------------------------------- 1 | clear 2 | close all 3 | 4 | addpath('Toolbox/'); 5 | 6 | % Tested methods 7 | test_FFT = 1; % FFT integrator (periodic BC) 8 | test_DST = 1; % DST integrator (Dirichlet BC) 9 | test_DCT = 1; % DCT integrator (natural BC) 10 | test_HB = 1; % Modified Horn and Brook's scheme 11 | 12 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 | % Load a dataset containing: 14 | % -- p (nrows x ncols) : gradient in the u- (bottom) direction 15 | % -- q (nrows x ncols) : gradient in the v- (right) direction 16 | % -- u (nrows x ncols) : ground truth depth map 17 | % -- mask (nrows x ncols) : mask of the pixels on the vase (binary) 18 | load Datasets/vase 19 | 20 | % To emphasize the problem of boundaries, we crop the domain t 21 | p = p(83:310,90:180); 22 | q = q(83:310,90:180); 23 | u = u(83:310,90:180); 24 | mask = mask(83:310,90:180); 25 | %~ p = p(83:260,150:220); 26 | %~ q = q(83:260,150:220); 27 | %~ u = u(83:260,150:220); 28 | %~ mask = mask(83:260,150:220); 29 | indices_mask = find(mask>0); 30 | 31 | % Add zero-mean, Gaussian noise 32 | std_noise = 0.005*max(sqrt(p(indices_mask).^2+q(indices_mask).^2)); 33 | p(indices_mask) = p(indices_mask)+std_noise*randn(size((indices_mask))); 34 | q(indices_mask) = q(indices_mask)+std_noise*randn(size((indices_mask))); 35 | 36 | if(test_FFT) 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 38 | % FFT Integration 39 | disp('Doing FFT integration'); 40 | 41 | 42 | t_1 = tic; 43 | z_1 = FFT_Poisson(p,q); 44 | t_1 = toc(t_1); 45 | 46 | % Find the integration constant which minimizes RMSE 47 | lambda_1 = -mean(z_1(indices_mask)-u(indices_mask)); 48 | z_1 = z_1+lambda_1; 49 | % Calculate RMSE 50 | RMSE_1 = sqrt(mean((z_1(indices_mask)-u(indices_mask)).^2)); 51 | % Display evaluation results in terminal 52 | disp('============================='); 53 | disp('FFT integration:'); 54 | disp(sprintf('CPU: %.4f',t_1)); 55 | disp(sprintf('RMSE: %.2f',RMSE_1)); 56 | disp(' '); 57 | end 58 | 59 | if(test_DST) 60 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | % DST Integration 62 | disp('Doing DST integration'); 63 | 64 | u_b = zeros(size(p)); % Homogeneous Dirichlet BC 65 | % u_b(1,:) = 100; % For a more funny boundary, uncomment this ;) 66 | t_2 = tic; 67 | z_2 = DST_Poisson(p,q,u_b); 68 | t_2 = toc(t_2); 69 | 70 | % Find the integration constant which minimizes RMSE 71 | lambda_2 = -mean(z_2(indices_mask)-u(indices_mask)); 72 | z_2 = z_2+lambda_2; 73 | % Calculate RMSE 74 | RMSE_2 = sqrt(mean((z_2(indices_mask)-u(indices_mask)).^2)); 75 | % Display evaluation results in terminal 76 | disp('============================='); 77 | disp('DST integration:'); 78 | disp(sprintf('CPU: %.4f',t_2)); 79 | disp(sprintf('RMSE: %.2f',RMSE_2)); 80 | disp(' '); 81 | end 82 | 83 | if(test_DCT) 84 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 85 | % DCT Integration 86 | disp('Doing DCT integration'); 87 | 88 | t_3 = tic; 89 | z_3 = DCT_Poisson(p,q); % Natural Neumann BC 90 | t_3 = toc(t_3); 91 | 92 | % Find the integration constant which minimizes RMSE 93 | lambda_3 = -mean(z_3(indices_mask)-u(indices_mask)); 94 | z_3 = z_3+lambda_3; 95 | % Calculate RMSE 96 | RMSE_3 = sqrt(mean((z_3(indices_mask)-u(indices_mask)).^2)); 97 | % Display evaluation results in terminal 98 | disp('============================='); 99 | disp('DCT integration:'); 100 | disp(sprintf('CPU: %.4f',t_3)); 101 | disp(sprintf('RMSE: %.2f',RMSE_3)); 102 | disp(' '); 103 | end 104 | 105 | if(test_HB) 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 107 | % Anisotropic diffusion ontegration 108 | disp('Doing Horn Brooks integration'); 109 | 110 | zinit = z_1; % least-squares initialization 111 | maxit = 50000; % Stopping criterion 112 | tol = 1e-6; % Stopping criterion 113 | trace = 0; % To display or not the current estimate 114 | 115 | t_4 = tic; 116 | z_4 = horn_brooks(p,q,mask,maxit,tol,trace); 117 | t_4 = toc(t_4); 118 | 119 | % Find the integration constant which minimizes RMSE 120 | lambda_4 = -mean(z_4(indices_mask)-u(indices_mask)); 121 | z_4 = z_4+lambda_4; 122 | % Calculate RMSE 123 | RMSE_4 = sqrt(mean((z_4(indices_mask)-u(indices_mask)).^2)); 124 | % Display evaluation results in terminal 125 | disp('============================='); 126 | disp('Horn-Brook integration:'); 127 | disp(sprintf('CPU: %.4f',t_4)); 128 | disp(sprintf('RMSE: %.2f',RMSE_4)); 129 | disp(' '); 130 | end 131 | 132 | 133 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 134 | % Summarize results 135 | disp(' '); 136 | disp(' '); 137 | disp(' '); 138 | disp('============================='); 139 | disp('============================='); 140 | disp('Summary of the evaluation:'); 141 | disp('============================='); 142 | disp('============================='); 143 | 144 | if(test_FFT) 145 | disp('============================='); 146 | disp('FFT integration:'); 147 | disp(sprintf('CPU: %.4f',t_1)); 148 | disp(sprintf('RMSE: %.2f',RMSE_1)); 149 | disp(' '); 150 | end 151 | if(test_DST) 152 | % Display evaluation results in terminal 153 | disp('============================='); 154 | disp('DST integration:'); 155 | disp(sprintf('CPU: %.4f',t_2)); 156 | disp(sprintf('RMSE: %.2f',RMSE_2)); 157 | disp(' '); 158 | end 159 | if(test_DCT) 160 | % Display evaluation results in terminal 161 | disp('============================='); 162 | disp('DCT integration:'); 163 | disp(sprintf('CPU: %.4f',t_3)); 164 | disp(sprintf('RMSE: %.2f',RMSE_3)); 165 | disp(' '); 166 | end 167 | if(test_HB) 168 | % Display evaluation results in terminal 169 | disp('============================='); 170 | disp('Horn Brooks integration:'); 171 | disp(sprintf('CPU: %.4f',t_4)); 172 | disp(sprintf('RMSE: %.2f',RMSE_4)); 173 | disp(' '); 174 | end 175 | 176 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 177 | % Display a few things 178 | 179 | figure('units','normalized','outerposition',[0 0 1 1]) 180 | 181 | % Input data: p, q and mask 182 | subplot(4,4,1) 183 | imagesc(p); 184 | axis image 185 | axis off 186 | title('$$p$$','Interpreter','Latex','Fontsize',14) 187 | subplot(4,4,2) 188 | imagesc(q); 189 | axis image 190 | axis off 191 | title('$$q$$','Interpreter','Latex','Fontsize',14) 192 | subplot(4,4,3) 193 | imagesc(mask); 194 | axis image 195 | axis off 196 | colormap gray 197 | title('$$\Omega$$','Interpreter','Latex','Fontsize',14) 198 | subplot(4,4,4) 199 | surfl(u,[-135 30]); 200 | view(-60,20) 201 | axis ij; 202 | shading flat; 203 | colormap gray; 204 | axis equal; 205 | grid off 206 | axis([1 size(p,2) 1 size(p,1) min(u(:)) max(u(:))]); 207 | axis off 208 | title('Ground truth depth','Interpreter','Latex','Fontsize',14) 209 | 210 | 211 | if(test_FFT) 212 | subplot(4,4,5) 213 | surfl(z_1,[-135 30]); 214 | view(-60,20) 215 | axis ij; 216 | shading flat; 217 | colormap gray; 218 | axis equal; 219 | grid off 220 | axis([1 size(p,2) 1 size(p,1) min(u(:)) max(u(:))]); 221 | axis off 222 | title('FFT integration','Interpreter','Latex','Fontsize',14) 223 | 224 | error_map_1 = abs(u-z_1); 225 | error_map_1(mask==0) = NaN; 226 | 227 | subplot(4,4,6) 228 | imagesc(error_map_1,[0 10]); 229 | axis image 230 | axis off 231 | colormap gray 232 | title('Absolute error (FFT integration)','Interpreter','Latex','Fontsize',14) 233 | end 234 | 235 | if(test_DST) 236 | subplot(4,4,7) 237 | surfl(z_2,[-135 30]); 238 | view(-60,20) 239 | axis ij; 240 | shading flat; 241 | colormap gray; 242 | axis equal; 243 | grid off 244 | axis([1 size(p,2) 1 size(p,1) min(u(:)) max(u(:))]); 245 | axis off 246 | title('DST integration','Interpreter','Latex','Fontsize',14) 247 | 248 | error_map_2 = abs(u-z_2); 249 | error_map_2(mask==0) = NaN; 250 | 251 | subplot(4,4,8) 252 | imagesc(error_map_2,[0 10]); 253 | axis image 254 | axis off 255 | colormap gray 256 | title('Absolute error (DST integration)','Interpreter','Latex','Fontsize',14) 257 | end 258 | 259 | 260 | if(test_DCT) 261 | subplot(4,4,9) 262 | surfl(z_3,[-135 30]); 263 | view(-60,20) 264 | axis ij; 265 | shading flat; 266 | colormap gray; 267 | axis equal; 268 | grid off 269 | axis([1 size(p,2) 1 size(p,1) min(u(:)) max(u(:))]); 270 | axis off 271 | title('DCT integration','Interpreter','Latex','Fontsize',14) 272 | 273 | error_map_3 = abs(u-z_3); 274 | error_map_3(mask==0) = NaN; 275 | 276 | subplot(4,4,10) 277 | imagesc(error_map_3,[0 10]); 278 | axis image 279 | axis off 280 | colormap gray 281 | title('Absolute error (DCT integration)','Interpreter','Latex','Fontsize',14) 282 | end 283 | 284 | if(test_HB) 285 | subplot(4,4,11) 286 | surfl(z_4,[-135 30]); 287 | view(-60,20) 288 | axis ij; 289 | shading flat; 290 | colormap gray; 291 | axis equal; 292 | grid off 293 | axis off 294 | axis([1 size(p,2) 1 size(p,1) min(u(:)) max(u(:))]); 295 | title('Horn Brooks integration','Interpreter','Latex','Fontsize',14) 296 | 297 | error_map_4 = abs(u-z_4); 298 | error_map_4(mask==0) = NaN; 299 | 300 | subplot(4,4,12) 301 | imagesc(error_map_4,[0 10]); 302 | axis image 303 | axis off 304 | colormap gray 305 | title('Absolute error (Horn Brooks integration)','Interpreter','Latex','Fontsize',14) 306 | end 307 | 308 | -------------------------------------------------------------------------------- /demo_2_quadratic.m: -------------------------------------------------------------------------------- 1 | clear 2 | close all 3 | 4 | addpath('Toolbox/'); 5 | 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | % Load a dataset containing: 8 | % -- p (nrows x ncols) : gradient in the u- (bottom) direction 9 | % -- q (nrows x ncols) : gradient in the v- (right) direction 10 | % -- u (nrows x ncols) : ground truth depth map 11 | % -- mask (nrows x ncols) : mask of the pixels on the vase (binary) 12 | load Datasets/vase 13 | indices_mask = find(mask>0); % Indices of the pixel inside the mask 14 | 15 | % Add zero-mean, Gaussian noise inside the mask 16 | std_noise = 0.02*max(sqrt(p(indices_mask).^2+q(indices_mask).^2)); 17 | p = p+std_noise*randn(size(p)); 18 | q = q+std_noise*randn(size(q)); 19 | 20 | % Fill the gradient with 0 to test rectangular integration 21 | p(mask==0) = 0; 22 | q(mask==0) = 0; 23 | % Remove the ground truth depth values outside the mask 24 | u(mask==0) = NaN; 25 | 26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 27 | % Set optimization parameters 28 | lambda = 1e-6*ones(size(p)); % Uniform field of weights (nrows x ncols) 29 | z0 = zeros(size(p)); % Null depth prior (nrows x ncols) 30 | solver = 'pcg'; % Solver ('pcg' means conjugate gradient, 'direct' means backslash i.e. sparse Cholesky) 31 | precond = 'CMG'; % Preconditioner ('none' means no preconditioning, 'ichol' means incomplete Cholesky, 'CMG' means conjugate combinatorial multigrid -- the latter is fastest, but it need being installed, see README) 32 | 33 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | % Integrate without mask (full rectangular domain) 35 | Omega_1 = ones(size(p)); 36 | t_1 = tic; 37 | z_1 = smooth_integration(p,q,Omega_1,lambda,z0,solver,precond); 38 | t_1 = toc(t_1); 39 | 40 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 41 | % Integrate with mask (reduced non-rectangular domain) 42 | Omega_2 = mask; 43 | t_2 = tic; 44 | z_2 = smooth_integration(p,q,Omega_2,lambda,z0,solver,precond); 45 | t_2 = toc(t_2); 46 | 47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 48 | % Evaluate both integration methods over the mask 49 | 50 | % Find the constant of integration which minimizes RMSE wrt ground truth 51 | lambda_1 = -mean(z_1(indices_mask)-u(indices_mask)); 52 | z_1 = z_1+lambda_1; 53 | 54 | lambda_2 = -mean(z_2(indices_mask)-u(indices_mask)); 55 | z_2 = z_2+lambda_2; 56 | 57 | % Calculate RMSEs 58 | RMSE_1 = sqrt(mean((z_1(indices_mask)-u(indices_mask)).^2)); 59 | RMSE_2 = sqrt(mean((z_2(indices_mask)-u(indices_mask)).^2)); 60 | 61 | % Display evaluation results in terminal 62 | disp('============================='); 63 | disp('Integration without mask:'); 64 | disp(sprintf('CPU: %.4f',t_1)); 65 | disp(sprintf('RMSE over mask: %.2f',RMSE_1)); 66 | disp(''); 67 | disp('================================'); 68 | disp('Integration with mask:'); 69 | disp(sprintf('CPU: %.4f',t_2)); 70 | disp(sprintf('RMSE over mask: %.2f',RMSE_2)); 71 | disp(''); 72 | 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | % Display a few things 75 | 76 | figure('units','normalized','outerposition',[0 0 1 1]) 77 | 78 | % Input data: p, q and mask 79 | subplot(3,3,1) 80 | imagesc(p); 81 | axis image 82 | axis off 83 | title('$$p$$','Interpreter','Latex','Fontsize',14) 84 | subplot(3,3,2) 85 | imagesc(q); 86 | axis image 87 | axis off 88 | title('$$q$$','Interpreter','Latex','Fontsize',14) 89 | subplot(3,3,3) 90 | imagesc(mask); 91 | axis image 92 | axis off 93 | colormap gray 94 | title('$$\Omega$$','Interpreter','Latex','Fontsize',14) 95 | 96 | subplot(3,3,4) 97 | surfl(u,[-135 30]); 98 | view(-35,20) 99 | axis ij; 100 | shading flat; 101 | colormap gray; 102 | axis equal; 103 | grid off 104 | axis off 105 | title('Ground truth depth','Interpreter','Latex','Fontsize',14) 106 | 107 | 108 | subplot(3,3,5) 109 | surfl(z_1,[-135 30]); 110 | view(-35,20) 111 | axis ij; 112 | shading flat; 113 | colormap gray; 114 | axis equal; 115 | grid off 116 | axis off 117 | title('Reconstruction without mask','Interpreter','Latex','Fontsize',14) 118 | 119 | subplot(3,3,6) 120 | surfl(z_2,[-135 30]); 121 | view(-35,20) 122 | axis ij; 123 | shading flat; 124 | colormap gray; 125 | axis equal; 126 | grid off 127 | axis off 128 | title('Reconstruction with mask','Interpreter','Latex','Fontsize',14) 129 | 130 | error_map_1 = abs(u-z_1); 131 | error_map_1(mask==0) = NaN; 132 | 133 | error_map_2 = abs(u-z_2); 134 | error_map_2(mask==0) = NaN; 135 | 136 | subplot(3,3,8) 137 | imagesc(error_map_1,[0 5]); 138 | axis image 139 | axis off 140 | colormap gray 141 | title('Absolute error without mask','Interpreter','Latex','Fontsize',14) 142 | 143 | subplot(3,3,9) 144 | imagesc(error_map_2,[0 5]); 145 | axis image 146 | axis off 147 | colormap gray 148 | title('Absolute error with mask','Interpreter','Latex','Fontsize',14) 149 | -------------------------------------------------------------------------------- /demo_3_discontinuities.m: -------------------------------------------------------------------------------- 1 | clear 2 | close all 3 | 4 | addpath('Toolbox/'); 5 | 6 | % Tested methods 7 | test_TV = 1; % Total variation 8 | test_NC = 1; % Non-convex 9 | test_AD = 1; % Anisotropic diffusion 10 | test_MS = 1; % Mumford-Shah 11 | 12 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 | % Load a dataset containing: 14 | % -- p (nrows x ncols) : gradient in the u- (bottom) direction 15 | % -- q (nrows x ncols) : gradient in the v- (right) direction 16 | % -- u (nrows x ncols) : ground truth depth map 17 | % -- mask (nrows x ncols) : mask of the pixels on the vase (binary) 18 | load Datasets/vase 19 | 20 | % In this test we assume no mask is given, so discontinuities around the border should be recovered automatically 21 | mask = ones(size(p)); 22 | indices_mask = find(mask>0); 23 | 24 | % Add zero-mean, Gaussian noise 25 | std_noise = 0.005*max(sqrt(p(indices_mask).^2+q(indices_mask).^2)); 26 | p = p+std_noise*randn(size(p)); 27 | q = q+std_noise*randn(size(q)); 28 | 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 30 | % Quadratic Integration 31 | disp('Doing quadratic integration'); 32 | 33 | lambda = 1e-6*ones(size(p)); % Uniform field of weights (nrows x ncols) 34 | z0 = zeros(size(p)); % Null depth prior (nrows x ncols) 35 | solver = 'pcg'; % Solver ('pcg' means conjugate gradient, 'direct' means backslash i.e. sparse Cholesky) 36 | precond = 'CMG'; % Preconditioner for smooth integration ('none' means no preconditioning, 'ichol' means incomplete Cholesky, 'CMG' means conjugate combinatorial multigrid -- the latter is fastest, but it need being installed, see README) 37 | 38 | t_1 = tic; 39 | z_1 = smooth_integration(p,q,mask,lambda,z0,solver,precond); 40 | t_1 = toc(t_1); 41 | 42 | % Find the integration constant which minimizes RMSE 43 | lambda_1 = -mean(z_1(indices_mask)-u(indices_mask)); 44 | z_1 = z_1+lambda_1; 45 | % Calculate RMSE 46 | RMSE_1 = sqrt(mean((z_1(indices_mask)-u(indices_mask)).^2)); 47 | % Display evaluation results in terminal 48 | disp('============================='); 49 | disp('Quadratic integration:'); 50 | disp(sprintf('CPU: %.4f',t_1)); 51 | disp(sprintf('RMSE: %.2f',RMSE_1)); 52 | disp(' '); 53 | 54 | if(test_TV) 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 56 | % TV Integration 57 | disp('Doing TV integration'); 58 | 59 | zinit = z_1; % least-squares initialization 60 | alpha = 0.1; % Descent stepsize (influences speed) 61 | tol = 1e-5; % Stopping criterion 62 | maxit = 1000; % Stopping criterion 63 | 64 | t_2 = tic; 65 | z_2 = tv_integration(p,q,mask,lambda,z0,alpha,maxit,tol,zinit); 66 | t_2 = toc(t_2); 67 | 68 | % Find the integration constant which minimizes RMSE 69 | lambda_2 = -mean(z_2(indices_mask)-u(indices_mask)); 70 | z_2 = z_2+lambda_2; 71 | % Calculate RMSE 72 | RMSE_2 = sqrt(mean((z_2(indices_mask)-u(indices_mask)).^2)); 73 | % Display evaluation results in terminal 74 | disp('============================='); 75 | disp('TV integration:'); 76 | disp(sprintf('CPU: %.4f',t_2)); 77 | disp(sprintf('RMSE: %.2f',RMSE_2)); 78 | disp(' '); 79 | end 80 | 81 | if(test_NC) 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | % Nonconvex Integration 84 | disp('Doing nonconvex integration'); 85 | 86 | zinit = z_1; % least-squares initialization 87 | gamma = 0.5; % Nonconvex estimator parameter (to be tuned: e.g. 0.5 for phi1, 1 for phi2 in our tests) 88 | beta = 0.8; % Lischitz reduction constant (must be in (0,1), see iPiano paper, 0.8 seems to always work) 89 | maxit = 1000; % Stopping criterion 90 | tol = 1e-5; % Stopping criterion 91 | 92 | t_3 = tic; 93 | z_3 = phi1_integration(p,q,mask,lambda,z0,beta,gamma,maxit,tol,zinit,u); % Phi_1 estimator 94 | % z_3 = phi2_integration(p,q,mask,lambda,z0,beta,gamma,maxit,tol,zinit,u);% Phi_2 estimator 95 | t_3 = toc(t_3); 96 | 97 | % Find the integration constant which minimizes RMSE 98 | lambda_3 = -mean(z_3(indices_mask)-u(indices_mask)); 99 | z_3 = z_3+lambda_3; 100 | % Calculate RMSE 101 | RMSE_3 = sqrt(mean((z_3(indices_mask)-u(indices_mask)).^2)); 102 | % Display evaluation results in terminal 103 | disp('============================='); 104 | disp('Nonconvex integration:'); 105 | disp(sprintf('CPU: %.4f',t_3)); 106 | disp(sprintf('RMSE: %.2f',RMSE_3)); 107 | disp(' '); 108 | end 109 | 110 | if(test_AD) 111 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 112 | % Anisotropic diffusion ontegration 113 | disp('Doing anis diff integration'); 114 | 115 | zinit = z_1; % least-squares initialization 116 | mu = 0.2; % anis diff (to be tuned) 117 | nu = 10; % anis diff param (10 should work) 118 | maxit = 20; % Stopping criterion 119 | tol = 1e-5; % Stopping criterion 120 | 121 | t_4 = tic; 122 | z_4 = anisotropic_diffusion_integration(p,q,mask,lambda,z0,mu,nu,maxit,tol,zinit); 123 | t_4 = toc(t_4); 124 | 125 | % Find the integration constant which minimizes RMSE 126 | lambda_4 = -mean(z_4(indices_mask)-u(indices_mask)); 127 | z_4 = z_4+lambda_4; 128 | % Calculate RMSE 129 | RMSE_4 = sqrt(mean((z_4(indices_mask)-u(indices_mask)).^2)); 130 | % Display evaluation results in terminal 131 | disp('============================='); 132 | disp('Anis diff integration:'); 133 | disp(sprintf('CPU: %.4f',t_4)); 134 | disp(sprintf('RMSE: %.2f',RMSE_4)); 135 | disp(' '); 136 | end 137 | 138 | if(test_MS) 139 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 140 | % Mumford-Shah ontegration 141 | disp('Doing Mumford-Shah integration'); 142 | 143 | zinit = z_1; % least-squares initialization 144 | mu = 45; % Regularization weight for discontinuity set 145 | epsilon = 0.01; % Should be close to 0 146 | tol = 1e-5; % Stopping criterion 147 | maxit = 1000; % Stopping criterion 148 | 149 | t_5 = tic; 150 | z_5 = mumford_shah_integration(p,q,mask,lambda,z0,mu,epsilon,maxit,tol,zinit); 151 | t_5 = toc(t_5); 152 | 153 | % Find the integration constant which minimizes RMSE 154 | lambda_5 = -mean(z_5(indices_mask)-u(indices_mask)); 155 | z_5 = z_5+lambda_5; 156 | % Calculate RMSE 157 | RMSE_5 = sqrt(mean((z_5(indices_mask)-u(indices_mask)).^2)); 158 | % Display evaluation results in terminal 159 | disp('============================='); 160 | disp('Mumford-Shah integration:'); 161 | disp(sprintf('CPU: %.4f',t_5)); 162 | disp(sprintf('RMSE: %.2f',RMSE_5)); 163 | disp(' '); 164 | end 165 | 166 | 167 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 168 | % Summarize results 169 | disp(' '); 170 | disp(' '); 171 | disp(' '); 172 | disp('============================='); 173 | disp('============================='); 174 | disp('Summary of the evaluation:'); 175 | disp('============================='); 176 | disp('============================='); 177 | disp('Quadratic integration:'); 178 | disp(sprintf('CPU: %.4f',t_1)); 179 | disp(sprintf('RMSE: %.2f',RMSE_1)); 180 | disp(' '); 181 | if(test_TV) 182 | % Display evaluation results in terminal 183 | disp('============================='); 184 | disp('TV integration:'); 185 | disp(sprintf('CPU: %.4f',t_2)); 186 | disp(sprintf('RMSE: %.2f',RMSE_2)); 187 | disp(' '); 188 | end 189 | if(test_NC) 190 | % Display evaluation results in terminal 191 | disp('============================='); 192 | disp('Nonconvex integration:'); 193 | disp(sprintf('CPU: %.4f',t_3)); 194 | disp(sprintf('RMSE: %.2f',RMSE_3)); 195 | disp(' '); 196 | end 197 | if(test_AD) 198 | % Display evaluation results in terminal 199 | disp('============================='); 200 | disp('Anis diff integration:'); 201 | disp(sprintf('CPU: %.4f',t_4)); 202 | disp(sprintf('RMSE: %.2f',RMSE_4)); 203 | disp(' '); 204 | end 205 | if(test_MS) 206 | % Display evaluation results in terminal 207 | disp('============================='); 208 | disp('Mumford-Shah integration:'); 209 | disp(sprintf('CPU: %.4f',t_5)); 210 | disp(sprintf('RMSE: %.2f',RMSE_5)); 211 | disp(' '); 212 | end 213 | 214 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 215 | % Display a few things 216 | 217 | figure('units','normalized','outerposition',[0 0 1 1]) 218 | 219 | % Input data: p, q and mask 220 | subplot(4,4,1) 221 | imagesc(p); 222 | axis image 223 | axis off 224 | title('$$p$$','Interpreter','Latex','Fontsize',14) 225 | subplot(4,4,2) 226 | imagesc(q); 227 | axis image 228 | axis off 229 | title('$$q$$','Interpreter','Latex','Fontsize',14) 230 | subplot(4,4,3) 231 | imagesc(mask); 232 | axis image 233 | axis off 234 | colormap gray 235 | title('$$\Omega$$','Interpreter','Latex','Fontsize',14) 236 | subplot(4,4,4) 237 | surfl(u,[-135 30]); 238 | view(-35,20) 239 | axis ij; 240 | shading flat; 241 | colormap gray; 242 | axis equal; 243 | grid off 244 | axis([1 size(p,1) 1 size(p,2) min(u(:)) max(u(:))]); 245 | axis off 246 | title('Ground truth depth','Interpreter','Latex','Fontsize',14) 247 | 248 | 249 | subplot(4,4,5) 250 | surfl(z_1,[-135 30]); 251 | view(-35,20) 252 | axis ij; 253 | shading flat; 254 | colormap gray; 255 | axis equal; 256 | grid off 257 | axis([1 size(p,1) 1 size(p,2) min(u(:)) max(u(:))]); 258 | axis off 259 | title('Quadratic integration','Interpreter','Latex','Fontsize',14) 260 | 261 | error_map_1 = abs(u-z_1); 262 | error_map_1(mask==0) = NaN; 263 | 264 | subplot(4,4,6) 265 | imagesc(error_map_1,[0 10]); 266 | axis image 267 | axis off 268 | colormap gray 269 | title('Absolute error (quadratic integration)','Interpreter','Latex','Fontsize',14) 270 | 271 | if(test_TV) 272 | subplot(4,4,7) 273 | surfl(z_2,[-135 30]); 274 | view(-35,20) 275 | axis ij; 276 | shading flat; 277 | colormap gray; 278 | axis equal; 279 | grid off 280 | axis([1 size(p,1) 1 size(p,2) min(u(:)) max(u(:))]); 281 | axis off 282 | title('TV integration','Interpreter','Latex','Fontsize',14) 283 | 284 | error_map_2 = abs(u-z_2); 285 | error_map_2(mask==0) = NaN; 286 | 287 | subplot(4,4,8) 288 | imagesc(error_map_2,[0 10]); 289 | axis image 290 | axis off 291 | colormap gray 292 | title('Absolute error (TV integration)','Interpreter','Latex','Fontsize',14) 293 | end 294 | 295 | 296 | if(test_NC) 297 | subplot(4,4,9) 298 | surfl(z_3,[-135 30]); 299 | view(-35,20) 300 | axis ij; 301 | shading flat; 302 | colormap gray; 303 | axis equal; 304 | grid off 305 | axis([1 size(p,1) 1 size(p,2) min(u(:)) max(u(:))]); 306 | axis off 307 | title('Nonconvex integration','Interpreter','Latex','Fontsize',14) 308 | 309 | error_map_3 = abs(u-z_3); 310 | error_map_3(mask==0) = NaN; 311 | 312 | subplot(4,4,10) 313 | imagesc(error_map_3,[0 10]); 314 | axis image 315 | axis off 316 | colormap gray 317 | title('Absolute error (nonconvex integration)','Interpreter','Latex','Fontsize',14) 318 | end 319 | 320 | if(test_AD) 321 | subplot(4,4,11) 322 | surfl(z_4,[-135 30]); 323 | view(-35,20) 324 | axis ij; 325 | shading flat; 326 | colormap gray; 327 | axis equal; 328 | grid off 329 | axis off 330 | axis([1 size(p,1) 1 size(p,2) min(u(:)) max(u(:))]); 331 | title('Anis diff integration','Interpreter','Latex','Fontsize',14) 332 | 333 | error_map_4 = abs(u-z_4); 334 | error_map_4(mask==0) = NaN; 335 | 336 | subplot(4,4,12) 337 | imagesc(error_map_4,[0 10]); 338 | axis image 339 | axis off 340 | colormap gray 341 | title('Absolute error (anis diff integration)','Interpreter','Latex','Fontsize',14) 342 | end 343 | 344 | 345 | if(test_MS) 346 | subplot(4,4,13) 347 | surfl(z_5,[-135 30]); 348 | view(-35,20) 349 | axis ij; 350 | shading flat; 351 | colormap gray; 352 | axis equal; 353 | grid off 354 | axis([1 size(p,1) 1 size(p,2) min(u(:)) max(u(:))]); 355 | axis off 356 | title('Mumford-Shah integration','Interpreter','Latex','Fontsize',14) 357 | 358 | error_map_5 = abs(u-z_5); 359 | error_map_5(mask==0) = NaN; 360 | 361 | subplot(4,4,14) 362 | imagesc(error_map_5,[0 10]); 363 | axis image 364 | axis off 365 | colormap gray 366 | title('Absolute error (Mumford-Shah integration)','Interpreter','Latex','Fontsize',14) 367 | end 368 | --------------------------------------------------------------------------------