├── LICENSE ├── README.md ├── dispersion3D.cpp ├── dispersion3D.h ├── example ├── absurdephoton-depthmap.png ├── absurdephoton-image.png ├── logo-absurdephoton-depthmap-data.xml ├── logo-absurdephoton-depthmap-image.png ├── logo-absurdephoton-depthmap-mask.png ├── logo-absurdephoton-segmentation-data.xml ├── logo-absurdephoton-segmentation-grid.png ├── logo-absurdephoton-segmentation-image.png ├── logo-absurdephoton-segmentation-mask.png └── logo-absurdephoton.png ├── icons ├── 3d-depth.png ├── 3d-long.png ├── 3d-manual.png ├── 3d-quality-off.png ├── 3d-quality.png ├── 3d-realtime.png ├── 3d-strip-quads.png ├── 3d-strip-triangles.png ├── 3d-widget.png ├── 3d-window.png ├── 3d.png ├── absurdephoton.png ├── add.png ├── anaglyph.png ├── animation.png ├── arrow-left-right.png ├── arrow-left.png ├── arrow-right.png ├── arrow-up-down.png ├── axis-3d-off.png ├── axis-3d.png ├── blur.png ├── camera.png ├── color-balance.png ├── color-rgb.png ├── color.png ├── combobox-arrow.png ├── compute.png ├── config.png ├── contours.png ├── curve-cos2power2.png ├── curve-cos2sqrt.png ├── curve-cosinus.png ├── curve-cosinus2.png ├── curve-linear.png ├── curve-power2.png ├── curve-power3.png ├── curve-sigmoid.png ├── curve-undulate.png ├── curve-undulate2.png ├── curve-undulate3.png ├── delete.png ├── denoise.png ├── direction-down.png ├── direction-left.png ├── direction-right.png ├── direction-up.png ├── double-step.png ├── edit.png ├── equalize.png ├── gamma.png ├── glasses.png ├── gray.png ├── grid.png ├── hand.png ├── hide.png ├── image-off.png ├── image.png ├── infinite-off.png ├── infinite.png ├── label-join.png ├── label-new-add.png ├── label-new.png ├── label-undo.png ├── label.png ├── light-off.png ├── light-on.png ├── load.png ├── mask-off.png ├── mask.png ├── normalize.png ├── photoshop.png ├── pipette.png ├── quit.png ├── save.png ├── segmentation.png ├── session-load.png ├── session-save.png ├── stop.png ├── thick.png ├── tiff.png ├── undo-off.png ├── undo.png ├── view.png ├── wiggle.png ├── window.png ├── zoom-100.png ├── zoom-minus.png ├── zoom-plus.png └── zoom.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── mat-image-tools.cpp ├── mat-image-tools.h ├── openglwidget.cpp ├── openglwidget.h ├── resources.qrc ├── screenshots ├── screenshot-3d-options.jpg ├── screenshot-3d-rendering.jpg ├── screenshot-anaglyph.jpg ├── screenshot-animation.jpg ├── screenshot-depthmap.jpg ├── screenshot-gradients.jpg └── screenshot-gui.jpg └── segmentation-depthmap-3d-opencv.pro /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # segmentation-depthmap-3d-opencv 2 | ## Use an image segmentation to produce a RGB+D image (image + depthmap). Or use the GUI to view already-made RGB+D images in 3D, there's even an anaglyph mode to perceive depth with red+cyan glasses. Animate the 3D view and export to a series of images to build later an animated image. This nice GUI uses QT, OpenCV and OpenGL. 3 | ### v1.5 - 2019-07-08 4 | 5 | ![Screenshot - Global](screenshots/screenshot-gui.jpg?raw=true) 6 |
7 | 8 | ## HISTORY 9 | 10 | * v0: launch 11 | * v1: basic segmentation edition to obtain a depthmap + software 3D view using depthmap displacement of the base image 12 | * v1.1: created a formula-based system to draw gray gradients -> best control of transitions between the segmented areas and volume/depth in 3D 13 | * v1.2: hard work to understand how OpenGL works, applied it to obtain a real-time view of RGB+D combination - 3D animation and capture to PNG images - Anaglyph view 14 | * v1.3: successive optimizations of OpenGL viewing, at last about ten times faster than the first version - Many, many optimizations - several color renderings added to help reduce anaglyph discomforts like ghosting 15 | * v1.4: interoperability achieved with .ply 3D mesh export for use in other 3D tools like MeshLab or Blender + adapted to openCV 4.1 16 | * v1.5: code clean-up and fixes 17 |
18 |
19 | 20 | ## LICENSE 21 | 22 | The present code is under GPL v3 license, that means you can do almost whatever you want 23 | with it! 24 | 25 | I used bits of code from several sources, mainly from the openCV and openGL examples 26 |
27 |
28 | 29 | ## WHY? 30 | 31 | I didn't find any simple tool (understand: GUI) to produce depth maps from unique pictures. 32 | 33 | One solution to help produce depth maps is image segmentation: coloring zones with superpixel cells is so easy! That's what was achieved with my [superpixels-segmentation-gui-opencv tool](https://github.com/AbsurdePhoton/superpixels-segmentation-gui-opencv), also available on GitHub. But it wasn't enough. 34 | 35 | The segmentation-depthmap-3d-opencv tool helps creating depthmaps directly, with a 3D visual help (even with real perception of depth in the anaglyph mode), by "coloring" in gray the segmented areas. There are several gradient modes available, to obtain better transitions between the areas. 36 | 37 | segmentation-depthmap-3d-opencv can also be used as a 3D vizualisation tool for RGB+D images, you can even export the results to .ply files to use later in 3D editors like MeshLab or Blender. 38 | 39 | I'm not an ace of C++ and QT and OpenGL: if you don't find my code pretty never mind, because it WORKS, and that's all I'm asking of it :) 40 |
41 |
42 | 43 | ## WITH WHAT? 44 | 45 | Developed using: 46 | * Linux Ubuntu 16.04 47 | * QT Creator 3.5 48 | * Requires these libraries: 49 | * QT 5 50 | * openCV 4.1 compiled with openCV-contribs - should work with 3.x versions without much editing 51 | * OpenGL 1.2 functions minimum 52 | 53 | This software should also work under Microsoft Windows: if you tried it successfully please contact me, I'd like to offer compiled Windows executables! 54 |
55 |
56 | 57 | ## HOW? 58 | 59 | * Help is available in the GUI, with each element's tooltip. Just stay your mouse over an element 60 | * to create a depthmap, you need a segmented image produced by my other tool, [superpixels-segmentation-gui-opencv](https://github.com/AbsurdePhoton/superpixels-segmentation-gui-opencv) 61 | * segmentation-depthmap-3d-opencv is composed of 3 main areas: segmentation/depthmap, gradients and 3D vizualisation 62 | * A 3-button mouse is all you need 63 | * Sample files are in the "example" folder : load the segmentation or depthmap XML file 64 | 65 | ### BASIC DEPTHMAP 66 | 67 | ![Screenshot - Depthmap](screenshots/screenshot-depthmap.jpg?raw=true) 68 | 69 | * To begin, load a segmented image produced with [superpixels-segmentation-gui-opencv](https://github.com/AbsurdePhoton/superpixels-segmentation-gui-opencv), nothing is possible without it! The image appears in the main viewport and its thumbnail is shown on the left side. A basic depthmap is already available, but it is absolutely not reliable! 70 | 71 | * In the depthmap view, you can zoom with the mouse wheel or the zoom buttons at the bottom. To move the view, either use the left mouse button on the thumbnail view, or the right mouse button or spacebar to move the view when the image is zoomed-in 72 | 73 | * Click with the left mouse button on the segmented gray-colored areas, its border is now yellow. You can change its gray level with the left panel: use "beginning color" 74 | 75 | * Roughly change the areas' gray levels according to this simple principle: dark areas are far and lighter areas are near the viewer's eyes 76 | 77 | * You can immediatly see the result in the 3D rendering area, use you mouse to rotate (left button) or move (right button) the object 78 | 79 | ### ADVANCED DEPTHMAP 80 | 81 | ![Screenshot - Gradients](screenshots/screenshot-gradients.jpg?raw=true) 82 | 83 | * Now that you have a rough depthmap and already a not-so-bad 3D view of your image, let's correct the errors and refine the effects! 84 | 85 | * For each area, using the gradients panel you can modify your depthmap with gradients 86 | 87 | * You can use linear or radial gradients to obtain smooth transitions between areas. When linear or radial mode is selected, there is a white arrow in the depthmap view, one for each area. Use the handles to change the beginning and end of the arrow. Change also the beginning and end gray levels, and voilà a gradient is applied to the current area! Practice is good to understand what's going on 88 | 89 | * One step further, you can change the gradient curve, for example more of the beginning gray level and less ending gray tone. To achieve this, change the LINEAR mode to another one, and watch your 3D view change. You can for example simulate a sphere-like effect, or give an undulation effect to an entire area 90 | 91 | ### 3D rendering 92 | 93 | ![Screenshot - 3D rendering](screenshots/screenshot-3d-rendering.jpg?raw=true) 94 | ![Screenshot - 3D options](screenshots/screenshot-3d-options.jpg?raw=true) 95 | 96 | On the right is the 3D rendering view. There are many options to play with! 97 | 98 | * You can show or hide the axes, add a light, etc 99 | 100 | * Blur the depthmap to get better color transitions (you have to activate it first by pushing the right button) 101 | 102 | * You can change the z scale with the Depth slider, to add more depth to your image. Try it with the anaglyph view in real-time! 103 | 104 | * Speaking of anaglyph view, you can activate it and change the shift factor, but beware of eyes strain, set it to your liking. You must wear red-cyan glasses to get the depth perception 105 | 106 | * To help reduce bad effects in anaglyph mode like "ghosting", you can change the image's gamma, or use different color modes. The Dubois mode is the most optimized for red-cyan viewing, but it slightly changes the image's colors 107 | 108 | ### 3D ANIMATION 109 | 110 | ![Screenshot - 3D rendering](screenshots/screenshot-animation.jpg?raw=true) 111 | 112 | Animate your 3D object! 113 | 114 | * You can capture a unique view or animated steps, either visually (eye icon) or saving (toggle the eye to have the floppy icon) the result to PNG files 115 | 116 | * You can set the resolution of your choice. First try with low resolutions before capturing 8K images... 117 | 118 | * You can visualize in windowed or fullscreen mode 119 | 120 | * Change the number of frames to produce, for better animation/transition between each step 121 | 122 | * There are two modes for animation, for each x and y axes that you can combine. Set the horizontal and vertical angle variation, select the "wobble" or "rotating" effect. You can globally set the animation to infinite or single loop, and also have a come-and-go effect. 123 | 124 | * Push the "capture" button and watch the result. Press "Escape" on your keyboard in fullscreen mode, or "ESC" or click the "Capture" button again to stop the animation in windowed mode 125 | 126 | ### 3D export 127 | 128 | * Use the "Export mesh" button to save a .ply file that you can later use in MeshLab or Blender (or even AutoCAD if you like paying your software) 129 | 130 |
131 |
132 | 133 | ## Enjoy! 134 | 135 | ### AbsurdePhoton 136 | My photographer website ''Photongénique'': www.absurdephoton.fr 137 | 138 | -------------------------------------------------------------------------------- /dispersion3D.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | #include 3 | //#include 4 | 5 | using namespace cv; 6 | using namespace cv::ximgproc; 7 | using namespace std; 8 | //using namespace Magick; 9 | 10 | Mat Displace(const Mat &image, const Mat &depthmap, const int &coefficient, const int &reference) 11 | { 12 | Mat result; 13 | image.copyTo(result); 14 | //result = 0; 15 | 16 | int displacement; 17 | 18 | for (int i = 0; i < image.cols; i++) { 19 | for (int j = 0; j < image.rows; j++) { 20 | displacement = round(((double(depthmap.at(j,i)[0]) - (255 - reference)) / (255 - reference)) * (-coefficient)); 21 | 22 | if ((i + displacement < image.cols) & (i + displacement >= 0)) { 23 | result.at(j,i + displacement) = image.at(j,i); 24 | } 25 | } 26 | } 27 | 28 | return result; 29 | } 30 | 31 | Mat AnaglyphRedCyan(Mat image, const Mat &depthmap, const bool &gray, const int &coefficient, const int &reference) 32 | { 33 | if (gray) { 34 | Mat temp; 35 | cv::cvtColor(image, temp, CV_BGR2GRAY); 36 | cv::cvtColor(temp, image, CV_GRAY2BGR); 37 | } 38 | 39 | Mat result = Displace(image, depthmap, coefficient, reference); 40 | 41 | int from_to[] = { 2,2 }; // channel 2 = red, copy channel 2 from source to channel 2 in dest 42 | mixChannels( &image, 1, &result, 1, from_to, 1 ); 43 | 44 | return result; 45 | } 46 | 47 | Mat Project3D(const Mat &image, const Mat &depthmap, const int &d0, const int &angleX, const int &angleY, const int &radius) 48 | { 49 | // angle is in degrees from 0 to 360 - PI = 180° 50 | // d0 = reference depth (zero parallax) 51 | 52 | Mat result = Mat::zeros(image.rows, image.cols, CV_8UC4); 53 | 54 | double h = 400.0; // camera offset 55 | double D = 1000; // depth of image plane 56 | double L = 10000; // focal length of camera 57 | 58 | int W = image.cols; 59 | int H = image.rows; 60 | int x0 = round(W / 2); 61 | int y0 = round(H / 2); 62 | 63 | //loop over each frame 64 | //for ( var frame_id = 0 ; frame_id < nb_frames ; frame_id++ ) { 65 | double thetaX = 3.14159 * 2.0 * angleX / 360; // angle of rotation of camera 66 | double thetaY = 3.14159 * 2.0 * angleY / 360; // angle of rotation of camera 67 | 68 | double camera_x = cos(thetaX) * h; // camera position 69 | double camera_y = sin(thetaY) * h; 70 | 71 | Mat current_depth(image.rows, image.cols, CV_8UC1); 72 | current_depth = -d0 - 1; 73 | 74 | for ( int i= 0 ; i< H ; i++ ) { 75 | for ( int j= 0 ; j< W ; j++ ) { 76 | //var d = depthp[4*(i*W+j)+0] - d0; 77 | int d = depthmap.at(i,j)[0] - d0; // depth of current pixel 78 | 79 | //get x coordinate of corresponding pixel in target image 80 | double tangent_alpha = camera_x / D; 81 | double alpha = atan(tangent_alpha); 82 | double tangent_beta = (tangent_alpha * L + camera_x + x0 - j) / (D - d + L); 83 | double beta = atan(tangent_beta); 84 | double gamma = beta - alpha; 85 | double x_ref_dbl = L * tan(gamma) / cos(alpha); 86 | x_ref_dbl = x0 - x_ref_dbl; 87 | //get y coordinate of corresponding pixel in target image 88 | tangent_alpha = camera_y / D; 89 | alpha = atan(tangent_alpha); 90 | tangent_beta = (tangent_alpha * L + camera_y + y0 - i) / (D - d + L); 91 | beta = atan(tangent_beta); 92 | gamma = beta - alpha; 93 | double y_ref_dbl = L * tan(gamma) / cos(alpha); 94 | y_ref_dbl = y0 - y_ref_dbl; 95 | 96 | int x_ref = round(x_ref_dbl); 97 | int y_ref = round(y_ref_dbl); 98 | 99 | //var radius = 0; 100 | 101 | for ( int x_offset= -radius ; x_offset<= +radius ; x_offset++ ) { 102 | for ( int y_offset= -radius ; y_offset<= +radius ; y_offset++ ) { 103 | int x = x_ref+x_offset; 104 | int y = y_ref+y_offset; 105 | //we are only interested in the four pixels at the corners of the square the pixel is in 106 | //dist = Math.max(Math.abs(x-x_ref_dbl),Math.abs(y-y_ref_dbl)); 107 | //if ( dist > radius ) continue; 108 | if ( x >= 0 && x < W && y >= 0 && y < H ) { 109 | //check if frame pixel has already gotten a color 110 | //only update if new depth is closer than current depth 111 | if ( d > current_depth.at(y,x) ) { 112 | //update color 113 | //for ( var rgb_ind = 0 ; rgb_ind < 3 ; rgb_ind++ ) { 114 | // destp[4*(y*W+x)+rgb_ind] = imgp[4*(i*W+j)+rgb_ind]; 115 | //} 116 | Vec3b color = image.at(i,j); 117 | result.at(y,x) = Vec4b(color[0], color[1], color[2], 255); 118 | //destp[4*(y*W+x)+3] = 255; 119 | //update the current depth for the frame pixel 120 | //current_depth[y*W+x] = d; 121 | current_depth.at(y,x) = d; 122 | } 123 | } 124 | } 125 | } 126 | } 127 | } 128 | /* 129 | //check consistency 130 | for ( var i= 0 ; i< H ; i++ ) { 131 | for ( var j= 0 ; j< W ; j++ ) { 132 | if ( destp[4*(i*W+j)+3] == 255 ) { 133 | if ( current_depth[i*W+j] == -d0-1 ) { 134 | //should not happen unless something is really wrong 135 | console.log("i = ",i,"j = ", j,"current_depth = ",current_depth[i*W+j]); 136 | } 137 | } 138 | } 139 | } 140 | //do some inpainting for the occluded pixels that are now visible 141 | if ( inpainting_method == "method 1" ) { 142 | //that's what is in https://github.com/fulmicoton/wigglejs 143 | fill_with_average(destp, W, H); 144 | } 145 | else if ( inpainting_method == "method 2" ) { 146 | //that's my version 147 | fill_occlusions(destp,current_depth,W,H,d0); 148 | } 149 | else if ( inpainting_method == "none" ) { 150 | for ( var i= 0 ; i< H ; i++ ) { 151 | for ( var j= 0 ; j< W ; j++ ) { 152 | //see if occluded (still transparent) 153 | if ( destp[4*(i*W+j)+3] == 255 ) { 154 | //pixel is not transparent anymore (has been filled) 155 | continue; 156 | } 157 | //make pixel not transparent (should be black) 158 | destp[4*(i*W+j)+3] = 255; 159 | } 160 | } 161 | }*/ 162 | 163 | Mat convert; 164 | cv::cvtColor(result, convert, CV_BGRA2BGR); 165 | 166 | return convert; 167 | } 168 | 169 | Mat AnaglyphProject3D(Mat image, const Mat &depthmap, const bool &gray, const int &d0, const int &angle, const int &radius) 170 | { 171 | if (gray) { 172 | Mat temp; 173 | cv::cvtColor(image, temp, CV_BGR2GRAY); 174 | cv::cvtColor(temp, image, CV_GRAY2BGR); 175 | } 176 | 177 | Mat gauche = Project3D(image, depthmap, d0, angle, 0, radius); 178 | Mat droite = Project3D(image, depthmap, d0, 180-angle, 0, radius); 179 | 180 | int from_to[] = { 2,2 }; // channel 2 = red, copy channel 2 from source to channel 2 in dest 181 | mixChannels( &gauche, 1, &droite, 1, from_to, 1 ); 182 | 183 | return droite; 184 | } 185 | -------------------------------------------------------------------------------- /dispersion3D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenCV 3D dispersion tools library 3 | * Author: AbsurdePhoton 4 | * 5 | * v0 - 2018/09/23 6 | * 7 | * 3D projection RGB+Depth 8 | * 9 | */ 10 | 11 | #ifndef DISPERSION3D_H 12 | #define DISPERSION3D_H 13 | 14 | #include "opencv2/opencv.hpp" 15 | #include 16 | //#include 17 | 18 | using namespace cv; 19 | //using namespace Magick; 20 | 21 | Mat Displace(const Mat &image, const Mat &depthmap, const int &coefficient, const int &reference); 22 | Mat AnaglyphRedCyan(Mat image, const Mat &depthmap, const bool &gray, const int &coefficient, const int &reference); 23 | Mat Project3D(const Mat &image, const Mat &depthmap, const int &d0, const int &angleX, const int &angleY, const int &radius); 24 | Mat AnaglyphProject3D(Mat image, const Mat &depthmap, const bool &gray, const int &d0, const int &angle, const int &radius); 25 | 26 | #endif // DISPERSION3D_H 27 | 28 | -------------------------------------------------------------------------------- /example/absurdephoton-depthmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/absurdephoton-depthmap.png -------------------------------------------------------------------------------- /example/absurdephoton-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/absurdephoton-image.png -------------------------------------------------------------------------------- /example/logo-absurdephoton-depthmap-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/logo-absurdephoton-depthmap-image.png -------------------------------------------------------------------------------- /example/logo-absurdephoton-depthmap-mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/logo-absurdephoton-depthmap-mask.png -------------------------------------------------------------------------------- /example/logo-absurdephoton-segmentation-grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/logo-absurdephoton-segmentation-grid.png -------------------------------------------------------------------------------- /example/logo-absurdephoton-segmentation-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/logo-absurdephoton-segmentation-image.png -------------------------------------------------------------------------------- /example/logo-absurdephoton-segmentation-mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/logo-absurdephoton-segmentation-mask.png -------------------------------------------------------------------------------- /example/logo-absurdephoton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/example/logo-absurdephoton.png -------------------------------------------------------------------------------- /icons/3d-depth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-depth.png -------------------------------------------------------------------------------- /icons/3d-long.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-long.png -------------------------------------------------------------------------------- /icons/3d-manual.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-manual.png -------------------------------------------------------------------------------- /icons/3d-quality-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-quality-off.png -------------------------------------------------------------------------------- /icons/3d-quality.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-quality.png -------------------------------------------------------------------------------- /icons/3d-realtime.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-realtime.png -------------------------------------------------------------------------------- /icons/3d-strip-quads.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-strip-quads.png -------------------------------------------------------------------------------- /icons/3d-strip-triangles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-strip-triangles.png -------------------------------------------------------------------------------- /icons/3d-widget.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-widget.png -------------------------------------------------------------------------------- /icons/3d-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d-window.png -------------------------------------------------------------------------------- /icons/3d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/3d.png -------------------------------------------------------------------------------- /icons/absurdephoton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/absurdephoton.png -------------------------------------------------------------------------------- /icons/add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/add.png -------------------------------------------------------------------------------- /icons/anaglyph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/anaglyph.png -------------------------------------------------------------------------------- /icons/animation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/animation.png -------------------------------------------------------------------------------- /icons/arrow-left-right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/arrow-left-right.png -------------------------------------------------------------------------------- /icons/arrow-left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/arrow-left.png -------------------------------------------------------------------------------- /icons/arrow-right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/arrow-right.png -------------------------------------------------------------------------------- /icons/arrow-up-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/arrow-up-down.png -------------------------------------------------------------------------------- /icons/axis-3d-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/axis-3d-off.png -------------------------------------------------------------------------------- /icons/axis-3d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/axis-3d.png -------------------------------------------------------------------------------- /icons/blur.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/blur.png -------------------------------------------------------------------------------- /icons/camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/camera.png -------------------------------------------------------------------------------- /icons/color-balance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/color-balance.png -------------------------------------------------------------------------------- /icons/color-rgb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/color-rgb.png -------------------------------------------------------------------------------- /icons/color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/color.png -------------------------------------------------------------------------------- /icons/combobox-arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/combobox-arrow.png -------------------------------------------------------------------------------- /icons/compute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/compute.png -------------------------------------------------------------------------------- /icons/config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/config.png -------------------------------------------------------------------------------- /icons/contours.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/contours.png -------------------------------------------------------------------------------- /icons/curve-cos2power2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-cos2power2.png -------------------------------------------------------------------------------- /icons/curve-cos2sqrt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-cos2sqrt.png -------------------------------------------------------------------------------- /icons/curve-cosinus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-cosinus.png -------------------------------------------------------------------------------- /icons/curve-cosinus2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-cosinus2.png -------------------------------------------------------------------------------- /icons/curve-linear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-linear.png -------------------------------------------------------------------------------- /icons/curve-power2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-power2.png -------------------------------------------------------------------------------- /icons/curve-power3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-power3.png -------------------------------------------------------------------------------- /icons/curve-sigmoid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-sigmoid.png -------------------------------------------------------------------------------- /icons/curve-undulate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-undulate.png -------------------------------------------------------------------------------- /icons/curve-undulate2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-undulate2.png -------------------------------------------------------------------------------- /icons/curve-undulate3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/curve-undulate3.png -------------------------------------------------------------------------------- /icons/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/delete.png -------------------------------------------------------------------------------- /icons/denoise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/denoise.png -------------------------------------------------------------------------------- /icons/direction-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/direction-down.png -------------------------------------------------------------------------------- /icons/direction-left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/direction-left.png -------------------------------------------------------------------------------- /icons/direction-right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/direction-right.png -------------------------------------------------------------------------------- /icons/direction-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/direction-up.png -------------------------------------------------------------------------------- /icons/double-step.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/double-step.png -------------------------------------------------------------------------------- /icons/edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/edit.png -------------------------------------------------------------------------------- /icons/equalize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/equalize.png -------------------------------------------------------------------------------- /icons/gamma.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/gamma.png -------------------------------------------------------------------------------- /icons/glasses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/glasses.png -------------------------------------------------------------------------------- /icons/gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/gray.png -------------------------------------------------------------------------------- /icons/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/grid.png -------------------------------------------------------------------------------- /icons/hand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/hand.png -------------------------------------------------------------------------------- /icons/hide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/hide.png -------------------------------------------------------------------------------- /icons/image-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/image-off.png -------------------------------------------------------------------------------- /icons/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/image.png -------------------------------------------------------------------------------- /icons/infinite-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/infinite-off.png -------------------------------------------------------------------------------- /icons/infinite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/infinite.png -------------------------------------------------------------------------------- /icons/label-join.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/label-join.png -------------------------------------------------------------------------------- /icons/label-new-add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/label-new-add.png -------------------------------------------------------------------------------- /icons/label-new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/label-new.png -------------------------------------------------------------------------------- /icons/label-undo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/label-undo.png -------------------------------------------------------------------------------- /icons/label.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/label.png -------------------------------------------------------------------------------- /icons/light-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/light-off.png -------------------------------------------------------------------------------- /icons/light-on.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/light-on.png -------------------------------------------------------------------------------- /icons/load.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/load.png -------------------------------------------------------------------------------- /icons/mask-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/mask-off.png -------------------------------------------------------------------------------- /icons/mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/mask.png -------------------------------------------------------------------------------- /icons/normalize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/normalize.png -------------------------------------------------------------------------------- /icons/photoshop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/photoshop.png -------------------------------------------------------------------------------- /icons/pipette.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/pipette.png -------------------------------------------------------------------------------- /icons/quit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/quit.png -------------------------------------------------------------------------------- /icons/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/save.png -------------------------------------------------------------------------------- /icons/segmentation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/segmentation.png -------------------------------------------------------------------------------- /icons/session-load.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/session-load.png -------------------------------------------------------------------------------- /icons/session-save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/session-save.png -------------------------------------------------------------------------------- /icons/stop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/stop.png -------------------------------------------------------------------------------- /icons/thick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/thick.png -------------------------------------------------------------------------------- /icons/tiff.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/tiff.png -------------------------------------------------------------------------------- /icons/undo-off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/undo-off.png -------------------------------------------------------------------------------- /icons/undo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/undo.png -------------------------------------------------------------------------------- /icons/view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/view.png -------------------------------------------------------------------------------- /icons/wiggle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/wiggle.png -------------------------------------------------------------------------------- /icons/window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/window.png -------------------------------------------------------------------------------- /icons/zoom-100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/zoom-100.png -------------------------------------------------------------------------------- /icons/zoom-minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/zoom-minus.png -------------------------------------------------------------------------------- /icons/zoom-plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/zoom-plus.png -------------------------------------------------------------------------------- /icons/zoom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/icons/zoom.png -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /*#------------------------------------------------- 2 | # 3 | # Segmentation to Depthmap to 3D with openCV 4 | # 5 | # by AbsurdePhoton - www.absurdephoton.fr 6 | # 7 | # v1.5 - 2019/07/08 8 | # 9 | #-------------------------------------------------*/ 10 | 11 | #include "mainwindow.h" 12 | #include 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | QApplication a(argc, argv); 17 | MainWindow w; 18 | w.show(); 19 | 20 | return a.exec(); 21 | } 22 | -------------------------------------------------------------------------------- /mainwindow.h: -------------------------------------------------------------------------------- 1 | /*#------------------------------------------------- 2 | # 3 | # Segmentation to Depthmap to 3D with openCV 4 | # 5 | # by AbsurdePhoton - www.absurdephoton.fr 6 | # 7 | # v1.5 - 2019/07/08 8 | # 9 | #-------------------------------------------------*/ 10 | 11 | #ifndef MAINWINDOW_H 12 | #define MAINWINDOW_H 13 | 14 | #include 15 | 16 | #include "opencv2/highgui.hpp" 17 | #include "opencv2/imgproc.hpp" 18 | #include "opencv2/imgcodecs.hpp" 19 | #include 20 | #include "opencv2/opencv.hpp" 21 | #include "opencv2/core/utility.hpp" 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "mat-image-tools.h" 28 | 29 | namespace Ui { 30 | class MainWindow; 31 | } 32 | 33 | class MainWindow : public QMainWindow 34 | { 35 | Q_OBJECT 36 | 37 | public: 38 | explicit MainWindow(QWidget *parent = 0); 39 | ~MainWindow(); 40 | 41 | public slots: 42 | 43 | private slots: 44 | 45 | //// quit 46 | void on_button_quit_clicked(); 47 | 48 | //// init 49 | void AddCurveItem(const QString &title, const QColor &color, const QString &tip); 50 | 51 | //// labels 52 | void on_listWidget_labels_currentItemChanged(QListWidgetItem *currentItem); // show current label color when item change 53 | 54 | //// load & save 55 | void ChangeBaseDir(QString filename); // Set base dir and file 56 | void SaveDirBaseFile(); // just to keep the last open dir 57 | 58 | void DisableGUI(); 59 | void on_button_save_depthmap_clicked(); // save session files 60 | void on_button_load_depthmap_clicked(); // load session files 61 | void on_button_load_segmentation_clicked(); 62 | void on_button_load_rgbd_clicked(); 63 | void on_button_save_ply_clicked(); 64 | 65 | //// Display 66 | 67 | // Masks 68 | void on_checkBox_depthmap_clicked(); // toggle depthmap view 69 | void on_checkBox_image_clicked(); // toggle image view 70 | 71 | void on_horizontalSlider_blend_image_valueChanged(); // image transparency 72 | void on_horizontalSlider_blend_depthmap_valueChanged(); // depthmap transparency 73 | 74 | // Gradients 75 | void on_radioButton_flat_clicked(); 76 | void on_radioButton_linear_clicked(); 77 | void on_radioButton_double_linear_clicked(); 78 | void on_radioButton_radial_clicked(); 79 | void on_horizontalSlider_begin_valueChanged(int value); 80 | void on_horizontalSlider_end_valueChanged(int value); 81 | void on_listWidget_gradient_curve_currentItemChanged(QListWidgetItem *currentItem); 82 | 83 | // Viewport 84 | void on_pushButton_zoom_minus_clicked(); // levels of zoom 85 | void on_pushButton_zoom_plus_clicked(); 86 | void on_pushButton_zoom_fit_clicked(); 87 | void on_pushButton_zoom_100_clicked(); 88 | 89 | void on_horizontalScrollBar_viewport_valueChanged(); // scroll the viewport 90 | void on_verticalScrollBar_viewport_valueChanged(); 91 | 92 | // 3D view 93 | void on_checkBox_3d_realtime_clicked(); 94 | void on_button_3d_update_clicked(); 95 | 96 | void on_checkBox_3d_anaglyph_clicked(); 97 | void on_checkBox_3d_axes_clicked(); 98 | void on_checkBox_3d_blur_clicked(); 99 | void on_checkBox_3d_light_clicked(); 100 | void on_comboBox_3d_tint_currentIndexChanged(int index); 101 | void on_doubleSpinBox_gamma_valueChanged(double value); 102 | void on_horizontalSlider_depth3D_valueChanged(int value); 103 | void on_horizontalSlider_anaglyph_shift_valueChanged(int value); 104 | void on_horizontalSlider_blur_amount_valueChanged(int value); 105 | void on_button_3d_reset_clicked(); 106 | void on_checkBox_3d_fullscreen_clicked(); 107 | 108 | // 3D capture 109 | void on_spinBox_3d_frames_valueChanged(int value); 110 | void on_checkBox_3d_capture_clicked(); 111 | void on_checkBox_3d_capture_unique_clicked(); 112 | void on_checkBox_3d_infinite_cycle_clicked(); 113 | void on_checkBox_3d_double_cycle_clicked(); 114 | //void on_checkBox_3d_circular_clicked(); 115 | void on_checkBox_3d_capture_fullscreen_clicked(); 116 | 117 | private: 118 | //// UI 119 | void InitializeValues(); 120 | 121 | //// labels 122 | int GetCurrentLabelNumber(); // label value for use with label_mask 123 | void DeleteAllLabels(); // delete ALL labels and create one new if wanted 124 | 125 | //// Keyboard & mouse events 126 | void keyPressEvent(QKeyEvent *keyEvent); // for the create cell mode 127 | void keyReleaseEvent(QKeyEvent *keyEvent); 128 | 129 | void mouseReleaseEvent(QMouseEvent *eventRelease); // when the mouse button is released 130 | void mousePressEvent(QMouseEvent *eventPress); // mouse events = zoom, set cell color etc 131 | void mouseMoveEvent(QMouseEvent *eventPress); 132 | void wheelEvent(QWheelEvent *wheelEvent); 133 | 134 | //// Display 135 | void Render(); // display image in viewport with depthmap and selection 136 | void ShowThumbnail(); // display thumbnail view 137 | /*void ShowAnaglyph(); 138 | void ShowAnaglyphProject3D();*/ 139 | 140 | void ZoomPlus(); // zoom in 141 | void ZoomMinus(); // zoom out 142 | void ShowZoomValue(); // display current zoom level 143 | 144 | void BlockGradientsSignals(const bool &active); 145 | void ChangeLabelGradient(); 146 | void ShowGradient(); 147 | void SetViewportXY(const int &x, const int &y); // change the origin of the viewport 148 | void UpdateViewportDimensions(); // calculate width and height of the viewport 149 | cv::Point Viewport2Image(const cv::Point &p); // calculate coordinates in the image from the viewport 150 | 151 | // the UI object, to access the UI elements created with Qt Designer 152 | Ui::MainWindow *ui; 153 | 154 | cv::Mat labels; // Segmentation cells and labels 155 | int nbLabels; // max number of labels 156 | 157 | cv::Mat image, // main image 158 | thumbnail, // thumbnail of main image 159 | depthmap, // gray-painted cells 160 | selection, // selection mask 161 | currentLabelMask; // for 3D partial update 162 | 163 | cv::Rect selection_rect; // rectangle of current selection 164 | 165 | cv::Mat disp_color; // Processed image display with mask and depthmap 166 | cv::Rect viewport; // part of the segmentation image to display 167 | 168 | std::string basefile, basedir, basedirinifile; // main image filename: directory and filename without extension 169 | 170 | Qt::MouseButton mouseButton; // save mouse button value when holding down a mouse button 171 | QPoint mouse_origin; // save the mouse position when holding down a mouse button 172 | cv::Point pos_save; // save the mouse position in image coordinates 173 | 174 | int num_zooms = 22; // number of zoom values 175 | double zooms[23] = {0, 0.05, 0.0625, 0.0833, 0.125, 0.1667, 0.25, 0.3333, 0.5, 0.6667, // reduced view 176 | 1, 1.25, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10000}; // zoomed view 177 | double zoom, oldZoom; // zoom factor for image display 178 | QString zoom_type; // if zoom changes indicates where it came from: button clic or mouse wheel 179 | 180 | bool loaded, computeVertices3D, updateVertices3D, computeColors3D; // indicators: image loaded & segmentation computed 181 | bool moveBegin, moveEnd; 182 | bool abort_3d; 183 | int saveXOpenGL, saveYOpenGL, saveWidthOpenGL, saveHeightOpenGL; 184 | 185 | struct struct_gradient{ 186 | cv::Point beginPoint; 187 | cv::Point endPoint; 188 | int beginColor; 189 | int endColor; 190 | gradientType gradient; 191 | curveType curve; 192 | }; 193 | 194 | struct_gradient gradients[1000]; 195 | 196 | }; 197 | 198 | #endif // MAINWINDOW_H 199 | -------------------------------------------------------------------------------- /mat-image-tools.cpp: -------------------------------------------------------------------------------- 1 | /*#------------------------------------------------- 2 | * 3 | * OpenCV image tools library 4 | * Author: AbsurdePhoton 5 | * 6 | * v1.9 - 2019/07/08 7 | * 8 | #-------------------------------------------------*/ 9 | 10 | 11 | #include 12 | 13 | #include "opencv2/opencv.hpp" 14 | 15 | #include "mat-image-tools.h" 16 | 17 | using namespace std; 18 | using namespace cv; 19 | 20 | /////////////////////////////////////////////////////////// 21 | //// Utils 22 | /////////////////////////////////////////////////////////// 23 | 24 | bool IsRGBColorDark(int red, int green, int blue) // tell if a RGB color is dark or not 25 | { 26 | double brightness; 27 | brightness = (red * 299) + (green * 587) + (blue * 114); 28 | brightness = brightness / 255000; 29 | 30 | // values range from 0 to 1 : anything greater than 0.5 should be bright enough for dark text 31 | return (brightness <= 0.5); 32 | } 33 | 34 | /////////////////////////////////////////////////////////// 35 | //// Conversions from QImage and QPixmap to Mat 36 | /////////////////////////////////////////////////////////// 37 | 38 | Mat QImage2Mat(const QImage &source) // Convert QImage to Mat, every number of channels supported 39 | { 40 | switch (source.format()) { // which format ? 41 | 42 | case QImage::Format_ARGB32: // 8-bit, 4 channel 43 | case QImage::Format_ARGB32_Premultiplied: { 44 | Mat temp(source.height(), source.width(), CV_8UC4, 45 | const_cast(source.bits()), 46 | static_cast(source.bytesPerLine())); 47 | return temp; 48 | } 49 | 50 | case QImage::Format_RGB32: { // 8-bit, 3 channel 51 | Mat temp(source.height(), source.width(), CV_8UC4, 52 | const_cast(source.bits()), 53 | static_cast(source.bytesPerLine())); 54 | Mat tempNoAlpha; 55 | cvtColor(temp, tempNoAlpha, COLOR_BGRA2BGR); // drop the all-white alpha channel 56 | return tempNoAlpha; 57 | } 58 | 59 | case QImage::Format_RGB888: { // 8-bit, 3 channel 60 | QImage swapped = source.rgbSwapped(); 61 | return Mat(swapped.height(), swapped.width(), CV_8UC3, 62 | const_cast(swapped.bits()), 63 | static_cast(swapped.bytesPerLine())).clone(); 64 | } 65 | 66 | case QImage::Format_Indexed8: { // 8-bit, 1 channel 67 | Mat temp(source.height(), source.width(), CV_8UC1, 68 | const_cast(source.bits()), 69 | static_cast(source.bytesPerLine())); 70 | return temp; 71 | } 72 | 73 | default: 74 | break; 75 | } 76 | 77 | return Mat(); // return empty Mat if type not found 78 | } 79 | 80 | Mat QPixmap2Mat(const QPixmap &source) // Convert Pixmap to Mat 81 | { 82 | return QImage2Mat(source.toImage()); // simple ! 83 | } 84 | 85 | /////////////////////////////////////////////////////////// 86 | //// Mat conversions to QImage and QPixmap 87 | /////////////////////////////////////////////////////////// 88 | 89 | QImage Mat2QImage(const Mat &source) // convert BGR Mat to RGB QImage 90 | { 91 | Mat temp; 92 | cvtColor(source, temp, COLOR_BGR2RGB); // convert Mat BGR to QImage RGB 93 | QImage dest((const uchar *) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888); // conversion 94 | dest.bits(); // enforce deep copy of QImage::QImage (const uchar * data, int width, int height, Format format) 95 | 96 | return dest; 97 | } 98 | 99 | QPixmap Mat2QPixmap(const Mat &source) // convert Mat to QPixmap 100 | { 101 | QImage i = Mat2QImage(source); // first step: convert to QImage 102 | QPixmap p = QPixmap::fromImage(i, Qt::AutoColor); // then convert QImage to QPixmap 103 | 104 | return p; 105 | } 106 | 107 | QPixmap Mat2QPixmapResized(const Mat &source, const int &width, const int &height, const bool &smooth) // convert Mat to resized QPixmap 108 | { 109 | Qt::TransformationMode quality; // quality 110 | if (smooth) quality = Qt::SmoothTransformation; 111 | else quality = Qt::FastTransformation; 112 | 113 | QImage i = Mat2QImage(source); // first step: convert to QImage 114 | QPixmap p = QPixmap::fromImage(i, Qt::AutoColor); // then convert QImage to QPixmap 115 | 116 | return p.scaled(width, height, Qt::KeepAspectRatio, quality); // resize with high quality 117 | } 118 | 119 | QImage cvMatToQImage(const Mat &source) // another implementation BGR to RGB, every number of channels supported 120 | { 121 | switch (source.type()) { // which type ? 122 | 123 | case CV_8UC4: { // 8-bit, 4 channel 124 | QImage image( source.data, 125 | source.cols, source.rows, 126 | static_cast(source.step), 127 | QImage::Format_ARGB32); 128 | return image; 129 | } 130 | 131 | 132 | case CV_8UC3: { // 8-bit, 3 channel 133 | QImage image( source.data, 134 | source.cols, source.rows, 135 | static_cast(source.step), 136 | QImage::Format_RGB888); 137 | return image.rgbSwapped(); 138 | } 139 | 140 | case CV_8UC1: { // 8-bit, 1 channel 141 | QImage image(source.data, source.cols, source.rows, 142 | static_cast(source.step), 143 | QImage::Format_Grayscale8); 144 | } 145 | } 146 | 147 | return QImage(); // return empty Mat if type not found 148 | } 149 | 150 | /////////////////////////////////////////////////////////// 151 | //// Brightness, Contrast, Gamma, Equalize, Balance 152 | /////////////////////////////////////////////////////////// 153 | 154 | Mat BrightnessContrast(const Mat &source, const double &alpha, const int &beta) // set brightness and contrast 155 | { 156 | Mat image = Mat::zeros(source.size(), source.type()); 157 | 158 | // Do the operation new_image(i,j) = alpha*image(i,j) + beta 159 | for (int y = 0; y < source.rows; y++) // scan entire Mat 160 | for (int x = 0; x < source.cols; x++) 161 | for (int c = 0; c < 3; c++) 162 | image.at(y, x)[c] = saturate_cast(alpha*(source.at(y, x)[c]) + beta); // change individual values 163 | return image; 164 | } 165 | 166 | Mat GammaCorrection(const Mat &source, const double gamma) 167 | { 168 | CV_Assert(gamma >= 0); 169 | 170 | Mat lookUpTable(1, 256, CV_8U); 171 | uchar* p = lookUpTable.ptr(); 172 | for( int i = 0; i < 256; ++i) 173 | p[i] = saturate_cast(pow(i / 255.0, gamma) * 255.0); 174 | 175 | Mat res = source.clone(); 176 | LUT(source, lookUpTable, res); 177 | 178 | //Mat img_gamma_corrected; 179 | //hconcat(source, res, img_gamma_corrected); 180 | 181 | return res; 182 | } 183 | 184 | Mat EqualizeHistogram(const Mat &source) // histogram equalization 185 | { 186 | Mat hist_equalized_image; 187 | cvtColor(source, hist_equalized_image, COLOR_BGR2YCrCb); // Convert the image from BGR to YCrCb color space 188 | 189 | // Split image into 3 channels: Y, Cr and Cb channels respectively and store it in a std::vector 190 | vector vec_channels; 191 | split(hist_equalized_image, vec_channels); 192 | 193 | equalizeHist(vec_channels[0], vec_channels[0]); // Equalize the histogram of only the Y channel 194 | 195 | merge(vec_channels, hist_equalized_image); // Merge 3 channels in the vector to form the color image in YCrCB color space. 196 | 197 | cvtColor(hist_equalized_image, hist_equalized_image, COLOR_YCrCb2BGR); // Convert the histogram equalized image from YCrCb to BGR color space again 198 | 199 | return hist_equalized_image; 200 | } 201 | 202 | Mat SimplestColorBalance(const Mat &source, const float &percent) // color balance with histograms 203 | { 204 | Mat dest; 205 | float half_percent = percent / 200.0f; 206 | vector tmpsplit; 207 | 208 | split(source, tmpsplit); // split channels 209 | 210 | for (int i=0; i<3; i++) { // just the 3 first channels 211 | // find low and high precentile values (based on input percentile) 212 | Mat flat; 213 | tmpsplit[i].reshape(1,1).copyTo(flat); 214 | cv::sort(flat,flat,SORT_EVERY_ROW + SORT_ASCENDING); 215 | int lowval = flat.at(cvFloor(((float)flat.cols) * half_percent)); 216 | int highval = flat.at(cvCeil(((float)flat.cols) * (1.0 - half_percent))); 217 | 218 | // saturate below the low percentile and above the high percentile 219 | tmpsplit[i].setTo(lowval,tmpsplit[i] < lowval); 220 | tmpsplit[i].setTo(highval,tmpsplit[i] > highval); 221 | 222 | // scale channel 223 | normalize(tmpsplit[i], tmpsplit[i], 0, 255, NORM_MINMAX); 224 | } 225 | 226 | merge(tmpsplit, dest); // result 227 | 228 | return dest; 229 | } 230 | 231 | /////////////////////////////////////////////////////////// 232 | //// Dilation and Erosion 233 | /////////////////////////////////////////////////////////// 234 | 235 | Mat DilatePixels(const Mat &source, const int &dilation_size) // dilate pixels 236 | { 237 | if (dilation_size <= 0) 238 | return source; 239 | 240 | // Create structuring element 241 | Mat element = getStructuringElement(MORPH_RECT, // MORPH_CROSS MORPH_RECT & MORPH_ELLIPSE possible 242 | Size(2 * dilation_size + 1, 2 * dilation_size + 1), 243 | Point(-1,-1)); 244 | Mat dest; 245 | dilate(source, dest, element); // Apply dilation on the image 246 | 247 | return dest; 248 | } 249 | 250 | Mat ErodePixels(const Mat &source, const int &erosion_size) // erode pixels 251 | { 252 | if (erosion_size <= 0) 253 | return source; 254 | 255 | // Create structuring element 256 | Mat element = getStructuringElement( MORPH_RECT, // MORPH_CROSS MORPH_RECT & MORPH_ELLIPSE possible 257 | Size(2 * erosion_size + 1, 2 * erosion_size + 1), 258 | Point(-1,-1)); 259 | Mat dest; 260 | erode(source, dest, element); // Apply erosion on the image 261 | 262 | return dest; 263 | } 264 | 265 | /////////////////////////////////////////////////////////// 266 | //// Shift a frame in one direction 267 | /////////////////////////////////////////////////////////// 268 | 269 | Mat ShiftFrame(const Mat &source, const int &nb_pixels, const shift_direction &direction) // shift frame in one direction 270 | { 271 | Mat dest = Mat::zeros(source.size(), source.type()); 272 | 273 | switch (direction) { // just make a copy with Rect 274 | case(shift_up) : 275 | source(Rect(0, nb_pixels, source.cols, source.rows - nb_pixels)).copyTo(dest(Rect(0, 0, dest.cols, dest.rows - nb_pixels))); 276 | break; 277 | case(shift_right) : 278 | source(Rect(0, 0, source.cols - nb_pixels, source.rows)).copyTo(dest(Rect(nb_pixels, 0, source.cols - nb_pixels, source.rows))); 279 | break; 280 | case(shift_down) : 281 | source(Rect(0, 0, source.cols, source.rows - nb_pixels)).copyTo(dest(Rect(0, nb_pixels, source.cols, source.rows - nb_pixels))); 282 | break; 283 | case(shift_left) : 284 | source(Rect(nb_pixels, 0, source.cols - nb_pixels, source.rows)).copyTo(dest(Rect(0, 0, source.cols - nb_pixels, source.rows))); 285 | break; 286 | } 287 | 288 | return dest; 289 | } 290 | 291 | /////////////////////////////////////////////////////////// 292 | //// Clipping & Resize 293 | /////////////////////////////////////////////////////////// 294 | 295 | Mat CopyFromImage (Mat source, const Rect &frame) // copy part of an image 296 | { 297 | if (source.channels() == 1) 298 | cvtColor(source, source, COLOR_GRAY2BGR); 299 | return source(frame); // just use the Rect area 300 | } 301 | 302 | Mat ResizeImageAspectRatio(const Mat &source, const Size &frame) // Resize image keeping aspect ratio 303 | { 304 | double zoomX = double(frame.width) / source.cols; // try vertical and horizontal ratios 305 | double zoomY = double(frame.height) / source.rows; 306 | double zoom; 307 | if (zoomX < zoomY) zoom = zoomX; // the lowest fit the view 308 | else zoom = zoomY; 309 | 310 | Mat dest; 311 | resize(source, dest, Size(int(source.cols*zoom), int(source.rows*zoom)), 0, 0, INTER_AREA); // resize 312 | 313 | return dest; 314 | } 315 | 316 | /////////////////////////////////////////////////////////// 317 | //// Contours 318 | /////////////////////////////////////////////////////////// 319 | 320 | double MedianMat(Mat source) // median, used by DrawColoredContours 321 | { 322 | source = source.reshape(0,1);// spread source to single row 323 | std::vector vecFromMat; 324 | 325 | source.copyTo(vecFromMat); // copy source to vector vecFromMat 326 | std::nth_element(vecFromMat.begin(), vecFromMat.begin() + vecFromMat.size() / 2, vecFromMat.end()); 327 | 328 | return vecFromMat[vecFromMat.size() / 2]; 329 | } 330 | 331 | Mat DrawColoredContours(const Mat &source, const double &sigma, const int &aperture_size, const int &thickness) // Canny edge detection 332 | { 333 | RNG rng(123); // controled randomization 334 | Mat canny_output, gray, blur; 335 | 336 | GaussianBlur(source, blur, Size(3,3), 0, 0); // better with a gaussian blur first 337 | cvtColor(blur, gray, COLOR_BGR2GRAY ); // convert to gray 338 | 339 | double median = MedianMat(gray); // get median value of matrix 340 | int lower = (int)std::max(0.0, (1.0-sigma) * median); // generate thresholds 341 | int upper = (int)std::min(255.0, (1.0+sigma) * median); 342 | 343 | vector> contours; // create structure to compute 344 | vector hierarchy; 345 | Canny(gray, canny_output, lower, upper, aperture_size, true ); // apply Canny 346 | findContours(canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); // find the edges 347 | Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3); 348 | 349 | for (size_t i = 0; i< contours.size(); i++) { // scan the image 350 | Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255)); // apply colors 351 | drawContours(drawing, contours, (int)i, color, thickness, 8, hierarchy, 0, Point()); // draw the lines 352 | } 353 | 354 | return drawing; 355 | } 356 | 357 | /////////////////////////////////////////////////////////// 358 | //// Noise filters utils 359 | /////////////////////////////////////////////////////////// 360 | 361 | double PSNR(const Mat &source1, const Mat &source2) // noise difference between 2 images 362 | { 363 | Mat s1; 364 | absdiff(source1, source2, s1); // |source1 - source2| 365 | s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits 366 | s1 = s1.mul(s1); // |source1 - source2|^2 367 | 368 | Scalar s = sum(s1); // sum elements per channel 369 | 370 | double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels 371 | 372 | if( sse <= 1e-10) // for small values return zero 373 | return 0; 374 | else // compute PSNR 375 | { 376 | double mse =sse /(double)(source1.channels() * source1.total()); 377 | double psnr = 10.0*log10((255*255)/mse); 378 | 379 | return psnr; 380 | } 381 | } 382 | 383 | /////////////////////////////////////////////////////////// 384 | //// Gray gradients 385 | /////////////////////////////////////////////////////////// 386 | 387 | double GrayCurve(const int &color, const int &type, const int &begin, const int &range) // return a value transformed by a function 388 | { 389 | // all functions must have continuous results in [0..1] range and have this shape : f(x) * range + begin 390 | 391 | double x = double(color-begin) / range; // x of f(x) 392 | 393 | if (range == 0) return color; // faster this way 394 | 395 | switch (type) { 396 | // good spread 397 | case curve_linear: return color; // linear -> the same color ! 398 | // S-shaped 399 | case curve_cosinus2: return pow(cos(Pi / 2 - x * Pi/2), 2) * range + begin; // cosinus² (better color gradient): f(x)=cos(pi/2-x*pi/2)² 400 | case curve_sigmoid: return 1.0 / (1 + exp(-5 * (2* (x) - 1))) * range + begin; // sigmoid (S-shaped): f(x)=1/(1 + e(-5*(2x -1)) 401 | // fast beginning 402 | case curve_cosinus: return cos(Pi / 2 - x * Pi/2) * range + begin; // cosinus (more of end color): f(x)=cos(pi/2-x*pi/2) 403 | case curve_cos2sqrt: return pow(cos(Pi/2 - sqrt(x) * Pi/2), 2) * range + begin; // cos²sqrt: f(x)=cos(pi/2−sqrt(x)*pi/2)² 404 | // fast ending 405 | case curve_power2: return pow(x, 2) * range + begin; // power2 (more of begin color): f(x)=x² 406 | case curve_cos2power2: return pow(cos(Pi/2 - pow(x, 2) * Pi/2), 2) * range + begin; // cos²power2: f(x)=cos(pi/2−x²∙pi/2)² 407 | case curve_power3: return pow(x, 3) * range + begin; // power3 (even more of begin color): f(x)=x³ 408 | // undulate 409 | case curve_undulate: return cos(double(color-begin) / 4 * Pi) * range + begin; // undulate: f(x)=cos(x/4*pi) 410 | case curve_undulate2: return cos(pow(double(color-begin) * 2 * Pi/2 + 0.5, 2)) * range + begin; // undulate²: f(x)=cos((x*2*pi)²) / 2 + 0.5 411 | case curve_undulate3: return (cos(Pi*Pi*pow(x+2.085,2))/(pow(x+2.085,3)+8)+(x+2.085)-2.11) * range + begin; // undulate3: f(x) = cos(pi²∙(x+2.085)²) / ((x+2.085)³+10) + (x+2.085) − 2.11 412 | 413 | //case 5: return sqrt(double(color-begin) / range) * range + begin; 414 | } 415 | 416 | return color; 417 | } 418 | 419 | float EuclideanDistance(Point center, Point point, int radius){ // return distance between 2 points 420 | float distance = sqrt(std::pow(center.x - point.x, 2) + std::pow(center.y - point.y, 2)); 421 | 422 | if (distance > radius) return radius; // no value beyond radius 423 | else return distance; 424 | } 425 | 426 | void GradientFillGray(const int &gradient_type, Mat &img, const Mat &msk, const Point &beginPoint, 427 | const Point &endPoint, const int &beginColor, const int &endColor, 428 | const int &curve, Rect area) // fill a 1-channel image with the mask converted to gray gradients 429 | // img must be 1-channel and msk 1-channel 430 | // img is input-output 431 | // area = rectangle containing the mask (speeds up the image scan) - no area given = entire image scanned 432 | { 433 | if (area == Rect(0, 0, 0, 0)) // default area = 0 434 | area = Rect(0,0, img.cols, img.rows); // set it to image dimensions 435 | 436 | switch (gradient_type) { 437 | case (gradient_flat): { // flat = same color everywhere 438 | img.setTo(beginColor, msk); // fill the mask with this color 439 | return; 440 | } 441 | case (gradient_linear): { // grayscale is spread along the line 442 | int A = (endPoint.x - beginPoint.x); // horizontal difference 443 | int B = (endPoint.y - beginPoint.y); // vertical difference 444 | int C1 = A * beginPoint.x + B * beginPoint.y; // sort of euclidian distance, but no need to compute sqrt values 445 | int C2 = A * endPoint.x + B * endPoint.y; 446 | 447 | float CO; // will contain color values 448 | 449 | for (int row = area.y; row < area.y + area.height; row++) // scan mask 450 | for (int col = area.x; col < area.x + area.width; col++) 451 | if (msk.at(row, col) != 0) { // non-zero pixel in mask 452 | int C = A * col + B * row; // "distance" for this pixel 453 | 454 | if (C <= C1) CO = beginColor; // before begin point : begin color 455 | else if (C >= C2) CO = endColor; // after end point : end color 456 | else CO = round(GrayCurve(float(beginColor * (C2 - C) + endColor * (C - C1))/(C2 - C1), 457 | curve, beginColor, endColor - beginColor)); // C0 = percentage between begin and end colors, "shaped" by gray curve 458 | img.at(row, col) = CO; // set grayscale to image 459 | } 460 | return; // done ! -> exit 461 | } 462 | case (gradient_doubleLinear): { // double linear = 2 times linear, just invert the vector the second time 463 | int A = (endPoint.x - beginPoint.x); // same comments as linear 464 | int B = (endPoint.y - beginPoint.y); 465 | int C1 = A * beginPoint.x + B * beginPoint.y; 466 | int C2 = A * endPoint.x + B * endPoint.y; 467 | 468 | float CO; 469 | 470 | for (int row = area.y; row < area.y + area.height; row++) 471 | for (int col = area.x; col < area.x + area.width; col++) 472 | if (msk.at(row, col) != 0) { 473 | int C = A * col + B * row; 474 | if (((C > C1) & (C < C2)) | (C >= C2) | (C == C1)) { // the only difference is we don't fill "before" the begin point 475 | if (C == C1) CO = beginColor; 476 | else if (C >= C2) CO = endColor; 477 | else CO = round(GrayCurve(float(beginColor * (C2 - C) + endColor * (C - C1))/(C2 - C1), 478 | curve, beginColor, endColor - beginColor)); 479 | img.at(row, col) = CO; 480 | } 481 | } 482 | 483 | Point newEndPoint; // invert the vector 484 | newEndPoint.x = 2* beginPoint.x - endPoint.x; 485 | newEndPoint.y = 2* beginPoint.y - endPoint.y; 486 | 487 | A = (newEndPoint.x - beginPoint.x); // same as before, but with new inverted vector 488 | B = (newEndPoint.y - beginPoint.y); 489 | C1 = A * beginPoint.x + B * beginPoint.y; 490 | C2 = A * newEndPoint.x + B * newEndPoint.y; 491 | 492 | for (int row = area.y; row < area.y + area.height; row++) 493 | for (int col = area.x; col < area.x + area.width; col++) 494 | if (msk.at(row, col) != 0) { 495 | int C = A * col + B * row; 496 | if (((C > C1) & (C < C2)) | (C >= C2) | (C == C1)) { // once again don't fill "before" begin point 497 | if (C == C1) CO = beginColor; 498 | else if (C >= C2) CO = endColor; 499 | else CO = round(GrayCurve(float(beginColor * (C2 - C) + endColor * (C - C1))/(C2 - C1), 500 | curve, beginColor, endColor - beginColor)); 501 | img.at(row, col) = CO; 502 | } 503 | } 504 | return; 505 | } 506 | case (gradient_radial): { // radial = concentric circles 507 | float radius = std::sqrt(std::pow(beginPoint.x - endPoint.x, 2) + std::pow(beginPoint.y - endPoint.y, 2)); // maximum euclidian distance = vector length 508 | 509 | int CO; // will contain color values 510 | 511 | for (int row = area.y; row < area.y + area.height; row++) // scan entire mask 512 | for (int col = area.x; col < area.x + area.width; col++) 513 | if (msk.at(row, col) != 0) { // non-zero pixel in mask 514 | CO = round(GrayCurve(beginColor + EuclideanDistance(beginPoint, Point(col, row), radius) / radius * (endColor - beginColor), 515 | curve, beginColor, endColor - beginColor)); // pixel in temp gradient mask = distance percentage, "shaped" by gray curve 516 | img.at(row, col) = CO; 517 | } 518 | return; 519 | } 520 | } 521 | } 522 | 523 | //// Color tints 524 | 525 | Mat AnaglyphTint(const Mat & source, const int &tint) // change tint of image to avoid disturbing colors in red-cyan anaglyph mode 526 | { 527 | Mat dest = Mat::zeros(source.rows, source.cols, CV_8UC3); 528 | Mat splt[3]; //destination array 529 | split(source, splt); // split source to 3 channels (RGB) 530 | 531 | for (int row = 0; row < source.rows; row++) // scan entire image 532 | for (int col = 0; col < source.cols; col++) { 533 | int R, G, B; 534 | Vec3b color = source.at(row, col); // get pixel color 535 | 536 | switch (tint) { // apply tint formula 537 | case tint_color: 538 | default: { // original colors 539 | R = round( 1.000 * color[2] + 0.000 * color[1] + 0.000 * color[0]); 540 | G = round( 0.000 * color[2] + 1.000 * color[1] + 0.000 * color[0]); 541 | B = round( 0.000 * color[2] + 0.000 * color[1] + 1.000 * color[0]); 542 | break; 543 | } 544 | case tint_gray: { // grayscale 545 | R = round( 0.299 * color[2] + 0.587 * color[1] + 0.114 * color[0]); 546 | G = round( 0.299 * color[2] + 0.587 * color[1] + 0.114 * color[0]); 547 | B = round( 0.299 * color[2] + 0.587 * color[1] + 0.114 * color[0]); 548 | break; 549 | } 550 | case tint_true: { // only red and cyan values 551 | R = round( 0.299 * color[2] + 0.587 * color[1] + 0.114 * color[0]); 552 | G = round( 0.000 * color[2] + 0.000 * color[1] + 0.000 * color[0]); 553 | B = round( 0.299 * color[2] + 0.587 * color[1] + 0.114 * color[0]); 554 | break; 555 | } 556 | case tint_half: { // half-colors 557 | R = round( 0.299 * color[2] + 0.587 * color[1] + 0.114 * color[0]); 558 | G = round( 0.000 * color[2] + 1.000 * color[1] + 0.000 * color[0]); 559 | B = round( 0.000 * color[2] + 0.000 * color[1] + 1.000 * color[0]); 560 | break; 561 | } 562 | case tint_optimized: { // optimized colors 563 | R = round( 0.000 * color[2] + 0.700 * color[1] + 0.300 * color[0]); 564 | G = round( 0.000 * color[2] + 1.000 * color[1] + 0.000 * color[0]); 565 | B = round( 0.000 * color[2] + 0.000 * color[1] + 1.000 * color[0]); 566 | break; 567 | } 568 | case tint_dubois: { // Dubois algorithm 569 | R = round( 0.4045 * color[2] + 0.4346 * color[1] + 0.1609 * color[0]); 570 | G = round( 0.3298 * color[2] + 0.6849 * color[1] - 0.0146 * color[0]); 571 | B = round(-0.1162 * color[2] - 0.1902 * color[1] + 1.3099 * color[0]); 572 | break; 573 | } 574 | } 575 | 576 | if (R < 0) R = 0; // check if values are out of range 577 | if (G < 0) G = 0; 578 | if (B < 0) B = 0; 579 | if (R > 255) R = 255; 580 | if (G > 255) G = 255; 581 | if (B > 255) B = 255; 582 | 583 | dest.at(row, col) = Vec3b(B, G, R); // new pixel RGB values 584 | } 585 | 586 | return dest; 587 | } 588 | -------------------------------------------------------------------------------- /mat-image-tools.h: -------------------------------------------------------------------------------- 1 | /*#------------------------------------------------- 2 | * 3 | * OpenCV image tools library 4 | * Author: AbsurdePhoton 5 | * 6 | * v1.9 - 2019/07/08 7 | * 8 | * Convert mat images to QPixmap or QImage and vice-versa 9 | * Brightness, Contrast, Gamma, Equalize, Color Balance 10 | * Erosion / dilation of pixels 11 | * Copy part of image 12 | * Resize image keeping aspect ration 13 | * Contours using Canny algorithm with auto min and max threshold 14 | * Noise reduction quality 15 | * Gray gradients 16 | * Red-cyan anaglyph tints 17 | * 18 | #-------------------------------------------------*/ 19 | 20 | #ifndef MAT2IMAGE_H 21 | #define MAT2IMAGE_H 22 | 23 | #include "opencv2/opencv.hpp" 24 | #include 25 | 26 | const double Pi = atan(1)*4; 27 | enum shift_direction{shift_up=1, shift_right, shift_down, shift_left}; // directions for shift function 28 | enum gradientType {gradient_flat, gradient_linear, gradient_doubleLinear, gradient_radial}; // gradient types 29 | enum curveType {curve_linear, curve_cosinus2, curve_sigmoid, curve_cosinus, curve_cos2sqrt, 30 | curve_power2, curve_cos2power2, curve_power3, curve_undulate, curve_undulate2, curve_undulate3}; // gray curve types 31 | enum anaglyphTint {tint_color, tint_gray, tint_true, tint_half, tint_optimized, tint_dubois}; // red/cyan anaglyph tints 32 | 33 | bool IsRGBColorDark(int red, int green, int blue); // is the RGB value given dark or not ? 34 | 35 | cv::Mat QImage2Mat(const QImage &source); // convert QImage to Mat 36 | cv::Mat QPixmap2Mat(const QPixmap &source); // convert QPixmap to Mat 37 | 38 | QImage Mat2QImage(const cv::Mat &source); // convert Mat to QImage 39 | QPixmap Mat2QPixmap(const cv::Mat &source); // convert Mat to QPixmap 40 | QPixmap Mat2QPixmapResized(const cv::Mat &source, const int &width, const int &height, const bool &smooth); // convert Mat to resized QPixmap 41 | QImage cvMatToQImage(const cv::Mat &source); // another implementation Mat type wise 42 | 43 | cv::Mat BrightnessContrast(const cv::Mat &source, const double &alpha, const int &beta); // brightness and contrast 44 | cv::Mat GammaCorrection(const cv::Mat &source, const double gamma); // gamma correction 45 | cv::Mat EqualizeHistogram(const cv::Mat &source); // histogram equalization 46 | cv::Mat SimplestColorBalance(const cv::Mat &source, const float &percent); // color balance with histograms 47 | 48 | cv::Mat DilatePixels(const cv::Mat &source, const int &dilation_size); // dilate pixels 49 | cv::Mat ErodePixels(const cv::Mat &source, const int &erosion_size); // erode pixels 50 | 51 | cv::Mat ShiftFrame(const cv::Mat &source, const int &nb_pixels, const shift_direction &direction); // shift frame in one direction 52 | 53 | cv::Mat CopyFromImage (cv::Mat source, const cv::Rect &frame); // copy part of an image 54 | cv::Mat ResizeImageAspectRatio(const cv::Mat &source, const cv::Size &frame); // Resize image keeping aspect ratio 55 | 56 | cv::Mat DrawColoredContours(const cv::Mat &source, const double &sigma, const int &apertureSize, const int &thickness); // draw colored contours of an image 57 | 58 | double PSNR(const cv::Mat &source1, const cv::Mat &source2); // noise difference between 2 images 59 | 60 | void GradientFillGray(const int &gradient_type, cv::Mat &img, const cv::Mat &msk, 61 | const cv::Point &beginPoint, const cv::Point &endPoint, 62 | const int &beginColor, const int &endColor, 63 | const int &curve, cv::Rect area = cv::Rect(0, 0, 0, 0)); // fill a 1-channel image with the mask converted to gray gradients 64 | 65 | cv::Mat AnaglyphTint(const cv::Mat & source, const int &tint); // change tint of image to avoid disturbing colors in red-cyan anaglyph mode 66 | 67 | #endif // MAT2IMAGE_H 68 | -------------------------------------------------------------------------------- /openglwidget.cpp: -------------------------------------------------------------------------------- 1 | /*#------------------------------------------------- 2 | # 3 | # openGL Widget 4 | # 5 | # by AbsurdePhoton - www.absurdephoton.fr 6 | # 7 | # v1.5 - 2019/07/08 8 | # 9 | # * .ply mesh export 10 | # 11 | # * Render using openGL VBO (i.e. in GPU memory) 12 | # 13 | # * Options : 14 | # - Lights 15 | # - Axes drawing 16 | # - Anaglyph red/cyan with adjustable shift 17 | # - Mouse control : 18 | # . zoom with wheel 19 | # . move view with left mouse button 20 | # . rotate view with right mouse button 21 | # - Keyboard controls : 22 | # . move view with arrows 23 | # . rotate with pg_up, pg_down, home, end 24 | # 25 | # * QT signals sent when zoomed, moved or rotated 26 | # 27 | # * Public access to zoom, position and rotation 28 | # 29 | #-------------------------------------------------*/ 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #include "opencv2/opencv.hpp" 38 | #include "openglwidget.h" 39 | #include "mat-image-tools.h" 40 | 41 | using namespace cv; 42 | 43 | /////////////////////////////////////////////// 44 | //// Widget 45 | /////////////////////////////////////////////// 46 | 47 | openGLWidget::openGLWidget(QWidget *parent) 48 | : QOpenGLWidget(parent), 49 | vertexbuffer(QOpenGLBuffer::VertexBuffer), 50 | indexbuffer(QOpenGLBuffer::IndexBuffer), 51 | colorbuffer(QOpenGLBuffer::VertexBuffer) 52 | { 53 | 54 | } 55 | 56 | openGLWidget::~openGLWidget() 57 | { 58 | 59 | } 60 | 61 | /////////////////////////////////////////////// 62 | //// Redefine widget main functions : 63 | //// init, repaint, resize 64 | /////////////////////////////////////////////// 65 | 66 | void openGLWidget::initializeGL() // launched when the widget is initialized 67 | { 68 | glClear(Qt::black); // clear the screen with black color 69 | 70 | glEnable(GL_DEPTH_TEST); // z-sorting 71 | //glEnable(GL_DEPTH_CLAMP); // no clipping - it's a bit slower 72 | glEnable(GL_COLOR_MATERIAL); // to see the two faces of a triangle, needed by lights when not shading 73 | glShadeModel(GL_SMOOTH); // blend colors - GL_FLAT chooses one color in the polygon 74 | 75 | glDisable(GL_CULL_FACE); // facet culling 76 | glEnable(GL_BLEND); // prefer using GLBlendFunc, used by the anaglyphic view 77 | 78 | xRot = 0; // initial values of rotation 79 | yRot = 0; 80 | zRot = 0; 81 | xShift = 0; // initial (x,y) position 82 | yShift = 0; 83 | angleLight = 5000; 84 | computeVertices3D = true; // compute vertices 85 | computeIndexes3D = true; // compute indexes 86 | computeColors3D = true; // compute colors 87 | updateVertices3D = false; // not a partial update 88 | updateAllVertices3D = false; // used by updateVertices3D 89 | zoom3D = 8; // zoom coefficient 90 | axesEnabled = true; // draw 3D origin axes enabled 91 | anaglyphEnabled = false; // anaglypgh red / cyan rendering disabled 92 | anaglyphShift = -1.5; // shift of cyan vs red image (angle in degrees on y axis) 93 | lightEnabled = false; // light disabled 94 | qualityEnabled = true; // antialiasing enabled 95 | 96 | //// Lights 97 | GLfloat light_position[] = { 5000, 0, 5000, 1.0 }; 98 | GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 1.0}; 99 | GLfloat light_diffuse[] = { 3, 3, 3, 0.5}; 100 | glEnable(GL_LIGHTING); // enable lighting 101 | glEnable(GL_LIGHT0); // define the first light 102 | glLightfv(GL_LIGHT0, GL_POSITION, light_position); 103 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); // ambient 104 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); // diffuse 105 | } 106 | 107 | int openGLWidget::NumberOfIndexes(const int &rows, const int &cols) // number of expected indexes for an image 108 | { 109 | if (rows <= 0) // special case 110 | return 0; 111 | else // a bit complicated, see ComputeIndexes() 112 | return double(double(cols) * double(rows)) + double((double(cols) * 2 - 1) * (double(rows) / 2 - 1)); 113 | } 114 | 115 | int openGLWidget::NumberOfVertices(const int &rows, const int &cols) // number of expected vertices for an image 116 | { 117 | return rows * cols; 118 | } 119 | 120 | GLuint openGLWidget::VertexIndex(const int &y, const int &x) // pixel's index in a image 121 | { 122 | return y * image3D.cols + x; // just like an array 123 | } 124 | 125 | void openGLWidget::ComputeVertices() // (re)create vertices array and buffer 126 | { 127 | vertexarray.clear(); // destroy buffers and arrays 128 | vertexbuffer.destroy(); 129 | vertexarray.reserve(NumberOfVertices(depthmap3D.rows, depthmap3D.cols)); // reserve memory space in advance 130 | 131 | int halfX = -depthmap3D.cols / 2; // to center the axes in middle of image 132 | int halfY = depthmap3D.rows / 2; 133 | 134 | for (float row = 0; row < depthmap3D.rows; row++) { // for each row of the image 135 | for (float col = 0; col < depthmap3D.cols; col++) { // for each pixel in the row from left to right 136 | vertexarray.push_back(QVector3D(col+halfX, -row+halfY, 137 | (depthmap3D.at(row, col) - 127) * depth3D)); // vertex in buffer 138 | } 139 | } 140 | 141 | vertexbuffer.create(); // create VBO vertices buffer 142 | vertexbuffer.bind(); // bind it 143 | vertexbuffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); // vertex buffer will be often modified 144 | vertexbuffer.allocate(vertexarray.constData(), vertexarray.size()*sizeof(QVector3D)); // allocate and populate in GPU RAM 145 | vertexbuffer.release(); // done 146 | 147 | computeVertices3D = false; // done recomputing 148 | } 149 | 150 | void openGLWidget::UpdateVertices() // update vertices z 151 | { 152 | Rect area_save = area3D; // save values for later 153 | if (updateAllVertices3D) { // ... precisely in case of this 154 | area3D = Rect(0, 0, image3D.cols, image3D.rows); // area is the whole image 155 | } 156 | 157 | vertexbuffer.bind(); // use current VBO 158 | GLfloat* posBuffer = (GLfloat*) (vertexbuffer.map(QOpenGLBuffer::WriteOnly)); // map a pointer on it 159 | 160 | if (posBuffer != (GLfloat*) NULL) { // pointer not valid ? 161 | 162 | int index; // index of current vertex 163 | 164 | for (int row = area3D.y; row < area3D.y + area3D.height; row++) { // for each row of area 165 | for (int col = area3D.x; col < area3D.x + area3D.width; col++) { // for each pixel in the row from left to right 166 | if ((updateAllVertices3D) | (mask3D.at(row, col) != 0)) { // current pixel set in mask ? 167 | index = VertexIndex(row, col); // use index of this pixel 168 | posBuffer[3 * index + 2] = (depthmap3D.at(row, col) - 127) * depth3D; // rewrite directly into VBO 169 | } 170 | } 171 | } 172 | 173 | vertexbuffer.unmap(); // update done 174 | } 175 | 176 | vertexbuffer.release(); // release VBO 177 | 178 | updateVertices3D = false; // done recomputing 179 | 180 | if (updateAllVertices3D) { // restore old values 181 | area3D = area_save; 182 | updateAllVertices3D = false; 183 | } 184 | } 185 | 186 | void openGLWidget::ComputeIndexes() // (re)create index array and buffer 187 | { 188 | indexarray.clear(); // destroy buffers and arrays 189 | indexbuffer.destroy(); 190 | int arraySize = NumberOfIndexes(image3D.rows, image3D.cols); // how many indexes ? 191 | //indexarray.reserve(arraySize); // reserve memory space in advance 192 | 193 | for (int row = 0; row < depthmap3D.rows -1; row++) { // for each row of the images 194 | if (int(row)%2 == 0) { // row number is even 195 | for (int col = 0; col < depthmap3D.cols; col++) { // for each pixel in the row from left to right 196 | indexarray.push_back(VertexIndex(row, col)); // index in buffer 197 | indexarray.push_back(VertexIndex(row+1, col)); 198 | } 199 | } 200 | else { // row number is odd 201 | for (int col = depthmap3D.cols - 1; col > 0 ; col--) { // for each pixel in the row from right to left except the first one 202 | indexarray.push_back(VertexIndex(row+1, col)); // the first time it creates a "degenerate triangle" 203 | indexarray.push_back(VertexIndex(row, col-1)); 204 | } 205 | int col = 1; 206 | indexarray.push_back(VertexIndex(row+1, col-1)); // add one more vertex to finish the column 207 | } 208 | } 209 | 210 | indexbuffer.create(); // create VBO vertices buffer 211 | indexbuffer.bind(); // bind it 212 | indexbuffer.setUsagePattern(QOpenGLBuffer::StaticDraw); // vertex buffer will be often modified 213 | indexbuffer.allocate(indexarray.constData(), indexarray.size()*sizeof(GLuint)); // allocate and populate in GPU RAM 214 | indexbuffer.release(); // done 215 | 216 | computeIndexes3D = false; // done recomputing 217 | 218 | emit verticesChanged(arraySize); // emit signal for number of vertices 219 | } 220 | 221 | void openGLWidget::ComputeColors() // (re)create colors array and buffer 222 | { 223 | colorarray.clear(); // destroy buffers and arrays 224 | colorbuffer.destroy(); 225 | colorarray.reserve(NumberOfVertices(image3D.rows, image3D.cols)); // reserve memory space in advance 226 | Vec3b CO; // pixel color of reference image 227 | 228 | for (float row = 0; row < image3D.rows; row++) { // for each row of the images 229 | for (float col = 0; col < image3D.cols; col++) { // for each pixel in the row from left to right 230 | CO = Vec3b(image3D.at(row, col)); // // pixel color of reference image 231 | colorarray.push_back(QVector3D(CO[2] / 255.0, CO[1] / 255.0, CO[0] / 255.0)); // color in buffer, RGB values between 0 and 1 232 | } 233 | } 234 | 235 | computeColors3D = false; // done recomputing 236 | 237 | colorbuffer.create(); // create colors buffer 238 | colorbuffer.bind(); // bind it 239 | colorbuffer.allocate(colorarray.constData(), colorarray.size()*sizeof(QVector3D)); // copy data to VBO 240 | colorbuffer.release(); // release VBO 241 | } 242 | 243 | void openGLWidget::SaveToObj(const QString &filename) // Save current 3D arrays to WaveFront .obj file 244 | { 245 | //open ascii text file for writing 246 | QFile file(filename); 247 | if (file.open(QIODevice::ReadWrite)) 248 | { 249 | QTextStream stream(&file); 250 | //stream << "example " << 49 << "\n"; 251 | //file.close(); 252 | 253 | // save vertices 254 | QVector3D vertex, color; 255 | int max = NumberOfVertices(image3D.rows, image3D.cols); 256 | 257 | for (int index = 0; index < max; index++) { 258 | vertex = vertexarray[index]; 259 | color = colorarray[index]; 260 | stream << "v " << vertex.x() << " " << vertex.y() << " " << vertex.z() 261 | << " " << color[0] << " " << color[1] << " " << color[2] 262 | << "\n"; 263 | } 264 | 265 | // save indexes 266 | max = NumberOfIndexes(image3D.rows, image3D.cols) - 2; 267 | 268 | for (int index = 0; index < max; index++) { 269 | stream << "f " << indexarray[index]+1 << " " << indexarray[index+1]+1 << " " << indexarray[index+2]+1 << "\n"; 270 | } 271 | 272 | file.close(); 273 | } 274 | } 275 | 276 | void openGLWidget::SaveToPly(const QString &filename) // Save current 3D arrays to Polygon File Format .ply file 277 | { 278 | //open ascii text file for writing 279 | QFile file(filename); 280 | if (file.open(QIODevice::ReadWrite)) 281 | { 282 | QTextStream stream(&file); 283 | //stream << "example " << 49 << "\n"; 284 | //file.close(); 285 | 286 | //// header 287 | stream << "ply" << "\n"; 288 | stream << "format ascii 1.0" << "\n"; 289 | stream << "comment Produced by a tool from AbsurdePhoton" << "\n"; 290 | stream << "comment GitHub: https://github.com/AbsurdePhoton" << "\n"; 291 | stream << "comment My photography site: absurdephoton.fr" << "\n"; 292 | 293 | // quantities 294 | // vertices 295 | int maxVertices = NumberOfVertices(image3D.rows, image3D.cols); 296 | stream << "element vertex " << maxVertices << "\n"; 297 | stream << "property float x" << "\n"; // define vertex x y and z 298 | stream << "property float y" << "\n"; 299 | stream << "property float z" << "\n"; 300 | stream << "property uchar red" << "\n"; // and vertex colors 301 | stream << "property uchar green" << "\n"; 302 | stream << "property uchar blue" << "\n"; 303 | 304 | // triangles 305 | int maxTriangles = NumberOfIndexes(image3D.rows, image3D.cols) - 2; 306 | stream << "element face " << maxTriangles << "\n"; 307 | stream << "property list uchar int vertex_index" << "\n"; 308 | 309 | // end of header 310 | stream << "end_header" << "\n"; 311 | 312 | //// save vertices 313 | QVector3D vertex, color; 314 | 315 | int index; // index of current vertex 316 | std::string st; // used to get a comma separator for floats, streams use locales ! 317 | 318 | for (int row = 0; row < image3D.rows; row++) { // for each row of area 319 | for (int col = 0; col < image3D.cols; col++) { // for each pixel in the row from left to right 320 | index = VertexIndex(row, col); // use index of this pixel 321 | color = colorarray[index]; 322 | /*stream << col << " " << -row << " " << qSetRealNumberPrecision(5) << (depthmap3D.at(row, col) - 127) * depth3D 323 | << " " << int(round(color[0]*255)) << " " << int(round(color[1]*255)) << " " << int(round(color[2]*255)) 324 | << "\n";*/ 325 | st = std::to_string(col) + " " + std::to_string(-row) + " " + std::to_string(float((depthmap3D.at(row, col) - 127) * depth3D)) 326 | + " " + std::to_string(int(round(color[0]*255))) + " " + std::to_string(int(round(color[1]*255))) + " " + std::to_string(int(round(color[2]*255))) 327 | + "\n"; 328 | stream << QString::fromStdString(st); 329 | } 330 | } 331 | 332 | // save faces + indexes 333 | int maxIndexes = NumberOfIndexes(image3D.rows, image3D.cols) - 2; 334 | 335 | for (int index = 0; index < maxIndexes; index++) { 336 | stream << "3 " << indexarray[index] << " " << indexarray[index+1] << " " << indexarray[index+2] << "\n"; 337 | } 338 | 339 | // Close the file 340 | stream << "\n"; 341 | file.close(); 342 | } 343 | } 344 | 345 | void openGLWidget::paintGL() // 3D rendering 346 | { 347 | if (lightEnabled) { 348 | glEnable(GL_LIGHTING); // turn on the lights... 349 | } else 350 | glDisable(GL_LIGHTING); // ... or not 351 | 352 | 353 | if (qualityEnabled) { // antialiasing 354 | glShadeModel(GL_SMOOTH); // blend colors 355 | glEnable(GL_POINT_SMOOTH); // draw points with anti-aliasing 356 | glEnable(GL_LINE_SMOOTH); // draw lines with anti-aliasing 357 | //glEnable(GL_POLYGON_SMOOTH); // draw polygons with anti-aliasing // can result in noise 358 | glEnable(GL_DITHER); //dither color components 359 | glEnable(GL_MULTISAMPLE); // use multiple fragment samples in computing the final color of a pixel 360 | } 361 | else { // no antialiasing 362 | glShadeModel(GL_FLAT); // don't blend colors 363 | glDisable(GL_POINT_SMOOTH); // draw aliased points 364 | glDisable(GL_LINE_SMOOTH); // draw aliased lines 365 | //glDisable(GL_POLYGON_SMOOTH); // draw aliased polygons 366 | glDisable(GL_DITHER); //dither color components 367 | glDisable(GL_MULTISAMPLE); // use multiple fragment samples in computing the final color of a pixel 368 | } 369 | 370 | //// init 3D view 371 | 372 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers 373 | 374 | if (anaglyphEnabled) glRotatef(-anaglyphShift, 0.0, 1.0, 0.0); // rotate a bit the first image on y axis 375 | 376 | glLoadIdentity(); // replace current matrix with identity matrix (reset) 377 | 378 | glTranslatef(xShift, yShift, 0); // translation matrix for all objects - no z used 379 | 380 | glScaled(zoom3D, zoom3D, zoom3D); // scale objects with zoom factor 381 | 382 | glRotatef(xRot, 1.0, 0.0, 0.0); // rotate all objects 383 | glRotatef(yRot, 0.0, 1.0, 0.0); 384 | glRotatef(zRot, 0.0, 0.0, 1.0); 385 | 386 | //// draw 3D origin axes 387 | 388 | if (axesEnabled) { // yes draw origin axes 389 | glLineWidth(2); // bigger width of the lines to really see them 390 | 391 | if (anaglyphEnabled) glColor3d(1,1,1); 392 | else glColor3d(1,0,0); // x axis color : red 393 | glBegin(GL_LINES); // draw several lines 394 | glVertex3f(0.0f, 0.0f, 0.0f); 395 | glVertex3f(1000.0f, 0.0f, 0.0f); 396 | glEnd(); 397 | glBegin(GL_TRIANGLES); // triangle at the end of the line = arrow 398 | glVertex3f( 925.0f, -50.0f, 0.0f ); 399 | glVertex3f( 925.0f, 50.0f, 0.0f ); 400 | glVertex3f( 1000.0f, 0.0f, 0.0f ); 401 | glEnd(); 402 | 403 | if (anaglyphEnabled) glColor3d(0.85,0.85,0.85); 404 | else glColor3d(0,0,1); // y axis color : blue 405 | glBegin(GL_LINES); 406 | glVertex3f( 0.0f, 0.0f, 0.0f ); 407 | glVertex3f( 0.0f, -1000.0f, 0.0f ); 408 | glEnd(); 409 | glBegin(GL_TRIANGLES); 410 | glVertex3f( -50.0f, -925.0f, 0.0f ); 411 | glVertex3f( 50.0f, -925.0f, 0.0f ); 412 | glVertex3f( 0.0f, -1000.0f, 0.0f ); 413 | glEnd(); 414 | 415 | if (anaglyphEnabled) glColor3d(0.75,0.75,0.75); 416 | else glColor3d(0,1,0); // z axis color : green 417 | glBegin(GL_LINES); 418 | glVertex3f( 0.0f, 0.0f, 0.0f); 419 | glVertex3f( 0.0f, 0.0f, 1000.0f); 420 | glEnd(); 421 | glBegin(GL_TRIANGLES); 422 | glVertex3f(0.0f, -50.0f, 925.0f); 423 | glVertex3f(0.0f, 50.0f, 925.0f); 424 | glVertex3f(0.0f, 0.0f, 1000.0f); 425 | glEnd(); 426 | } 427 | 428 | if ((depthmap3D.empty()) | (image3D.empty())) // nothing more to render => exit 429 | return; 430 | 431 | if (computeVertices3D) { // totally recompute vertices 432 | ComputeVertices(); 433 | updateVertices3D = false; 434 | } 435 | 436 | if (updateVertices3D) // partially recompute vertices 437 | UpdateVertices(); 438 | 439 | if (computeIndexes3D) { // totally recompute vertices 440 | ComputeIndexes(); 441 | } 442 | 443 | if (computeColors3D) // totally recompute vertices colors 444 | ComputeColors(); 445 | 446 | if (anaglyphEnabled) { 447 | glRotatef(-anaglyphShift, 0.0, 1.0, 0.0); 448 | glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE); // if anaglyph this is the left (red) image 449 | } 450 | 451 | glEnableClientState(GL_VERTEX_ARRAY); // vertices drawing mode 452 | glEnableClientState(GL_COLOR_ARRAY); // with colors 453 | colorbuffer.bind(); // VBO color buffer used 454 | glColorPointer(3, GL_FLOAT, 0, NULL); // use color buffer 455 | colorbuffer.release(); // done for colors 456 | vertexbuffer.bind(); // the same for vertices 457 | glVertexPointer(3, GL_FLOAT, 0, NULL); 458 | vertexbuffer.release(); 459 | indexbuffer.bind(); // the same for indexes 460 | glDrawElements(GL_TRIANGLE_STRIP, indexarray.size(), GL_UNSIGNED_INT, NULL); // draw triangles 461 | indexbuffer.release(); 462 | glDisableClientState(GL_VERTEX_ARRAY); // finished defining vertices and colors 463 | glDisableClientState(GL_COLOR_ARRAY); 464 | 465 | if (anaglyphEnabled) { // if anaglyph draw the second (cyan) image 466 | 467 | glClear(GL_DEPTH_BUFFER_BIT); // only reset the depth not the colors this time 468 | //glTranslatef(anaglyphShift * 2048, 0, 0); // bad method ! 469 | glRotatef(anaglyphShift, 0.0, 1.0, 0.0); // rotate a bit the second image on y axis 470 | glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // blend the right image with the already drawn left image 471 | glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE); // draw only in the cyan channels (blue + green) 472 | 473 | glEnableClientState(GL_VERTEX_ARRAY); // exactly as the left image, using the same buffers 474 | glEnableClientState(GL_COLOR_ARRAY); 475 | colorbuffer.bind(); 476 | glColorPointer(3, GL_FLOAT, 0, NULL); 477 | colorbuffer.release(); 478 | vertexbuffer.bind(); 479 | glVertexPointer(3, GL_FLOAT, 0, NULL); 480 | vertexbuffer.release(); 481 | indexbuffer.bind(); 482 | glDrawElements(GL_TRIANGLE_STRIP, indexarray.size(), GL_UNSIGNED_INT, NULL); 483 | indexbuffer.release(); 484 | glDisableClientState(GL_VERTEX_ARRAY); 485 | glDisableClientState(GL_COLOR_ARRAY); 486 | 487 | glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE); // red, green and blue channels enabled again 488 | } 489 | } 490 | 491 | void openGLWidget::resizeGL(int width, int height) // called when the widget is resized 492 | { 493 | double ratio = double(width) / height; // ratio of the widget : width / height 494 | glViewport(0, 0, width, height); // resize openGL viewport 495 | 496 | glMatrixMode(GL_PROJECTION); // openGL projection mode 497 | glLoadIdentity(); 498 | #ifdef QT_OPENGL_ES_1 // older versions of openGL 499 | glOrthof(-4 * 2048, +4 * 2048, -4 * 2048 / ratio, +4 * 2048 / ratio, -5000*2048, 5000*2048); // define view rectangle and clipping 500 | #else 501 | glOrtho(-4 * 2048, +4 * 2048, -4 * 2048 / ratio, +4 * 2048 / ratio, -5000*2048, 5000*2048); 502 | #endif 503 | glMatrixMode(GL_MODELVIEW); // now openGL model mode 504 | } 505 | 506 | /////////////////////////////////////////////// 507 | //// Drag with mouse + zoom with mouse wheel 508 | //// + emit signals to get new values 509 | /////////////////////////////////////////////// 510 | 511 | static void NormalizeAngle(int &angle) // angle must be between 0 and 359° 512 | { 513 | while (angle < 0) angle = 360 + angle; // no negative values 514 | angle = angle % 360; // 0 -> 359° 515 | } 516 | 517 | void openGLWidget::SetXRotation(int angle) // set X rotation and emit a signal 518 | { 519 | NormalizeAngle(angle); // angle must be between 0 and 359° 520 | if (angle != xRot) { // really changed ? 521 | xRot = angle; 522 | emit xRotationChanged(angle); // emit signal 523 | update(); // update 3D rendering 524 | } 525 | } 526 | 527 | void openGLWidget::SetYRotation(int angle) // set Y rotation and emit a signal 528 | { 529 | NormalizeAngle(angle); // angle must be between 0 and 359° 530 | if (angle != yRot) { // really changed ? 531 | yRot = angle; 532 | emit yRotationChanged(angle); // emit signal 533 | update(); // update 3D rendering 534 | } 535 | } 536 | 537 | void openGLWidget::SetZRotation(int angle) // set Z rotation and emit a signal 538 | { 539 | NormalizeAngle(angle); // angle must be between 0 and 359° 540 | if (angle != zRot) { // really changed ? 541 | zRot = angle; 542 | emit zRotationChanged(angle); // emit signal 543 | update(); // update 3D rendering 544 | } 545 | } 546 | 547 | void openGLWidget::SetAngleUp() // for keyboard control of x and y angles 548 | { 549 | SetXRotation(xRot - 10); 550 | } 551 | 552 | void openGLWidget::SetAngleDown() 553 | { 554 | SetXRotation(xRot + 10); 555 | } 556 | 557 | void openGLWidget::SetAngleLeft() 558 | { 559 | SetYRotation(yRot - 10); 560 | } 561 | 562 | void openGLWidget::SetAngleRight() 563 | { 564 | SetYRotation(yRot + 10); 565 | } 566 | 567 | void openGLWidget::SetXShift(int value) // move view (x) 568 | { 569 | xShift = value; 570 | emit xShiftChanged(xShift); // emit signal 571 | update(); // update 3D rendering 572 | } 573 | 574 | void openGLWidget::SetYShift(int value) // move view (y) 575 | { 576 | yShift = value; 577 | emit yShiftChanged(yShift); // emit signal 578 | update(); // update 3D rendering 579 | } 580 | 581 | void openGLWidget::SetShiftUp() // for keyboard control of x and y positions 582 | { 583 | SetYShift(yShift + 10 * zoom3D); 584 | } 585 | 586 | void openGLWidget::SetShiftDown() 587 | { 588 | SetYShift(yShift - 10 * zoom3D); 589 | } 590 | 591 | void openGLWidget::SetShiftLeft() 592 | { 593 | SetXShift(xShift - 10 * zoom3D); 594 | } 595 | 596 | void openGLWidget::SetShiftRight() 597 | { 598 | SetXShift(xShift + 10 * zoom3D); 599 | } 600 | 601 | void openGLWidget::mousePressEvent(QMouseEvent *event) // save initial mouse position for move and rotate 602 | { 603 | lastPos = event->pos(); // save initial position of the mouse 604 | } 605 | 606 | void openGLWidget::mouseMoveEvent(QMouseEvent *event) // move and rotate view with mouse buttons 607 | { 608 | int dx = event->x() - lastPos.x(); // how much the mouse has moved in pixels 609 | int dy = event->y() - lastPos.y(); 610 | 611 | if (event->buttons() & Qt::LeftButton) { // left button = rotate on x and y axes 612 | SetXRotation(xRot + dy); // x = vertical 613 | SetYRotation(yRot + dx); // y = horizontal 614 | } else if (event->buttons() & Qt::RightButton) { // right button = move the view on x and y axes 615 | //setXRotation(xRot + 8 * dy); 616 | //setZRotation(zRot + 8 * dx); 617 | SetXShift(xShift + dx * 48); 618 | SetYShift(yShift - dy * 48); 619 | 620 | update(); // redraw 3d scene 621 | } 622 | 623 | lastPos = event->pos(); // save mouse position again 624 | } 625 | 626 | void openGLWidget::wheelEvent(QWheelEvent *event) // zoom 627 | { 628 | int n = event->delta(); // amount of wheel turn 629 | //zoom3D += n / 120 / 2; // should work with this (standard) value 630 | 631 | if (n < 0) // zoom out 632 | zoom3D = zoom3D / 1.25; 633 | else // zoom in 634 | zoom3D = zoom3D * 1.25; 635 | 636 | emit zoomChanged(zoom3D); // emit signal 637 | 638 | update(); // redraw 3d scene 639 | } 640 | 641 | /////////////////////////////////////////////// 642 | //// Capture 3D scene to QImage 643 | /////////////////////////////////////////////// 644 | 645 | void openGLWidget::Capture() // take a snapshot of rendered 3D scene 646 | { 647 | capture3D = grabFramebuffer(); // slow because it relies on glReadPixels() to read back the pixels 648 | } 649 | -------------------------------------------------------------------------------- /openglwidget.h: -------------------------------------------------------------------------------- 1 | /*#------------------------------------------------- 2 | # 3 | # openGL Widget 4 | # 5 | # by AbsurdePhoton - www.absurdephoton.fr 6 | # 7 | # v1.5 - 2019/07/08 8 | # 9 | # * .ply mesh export 10 | # 11 | # * Render using openGL VBO (i.e. in GPU memory) 12 | # 13 | # * Options : 14 | # - Lights 15 | # - Axes drawing 16 | # - Anaglyph red/cyan with adjustable shift 17 | # - Mouse control : 18 | # . zoom with wheel 19 | # . move view with left mouse button 20 | # . rotate view with right mouse button 21 | # - Keyboard controls : 22 | # . move view with arrows 23 | # . rotate with pg_up, pg_down, home, end 24 | # 25 | # * QT signals sent when zoomed, moved or rotated 26 | # 27 | # * Public access to zoom, position and rotation 28 | # 29 | #-------------------------------------------------*/ 30 | 31 | #ifndef OPENGLWIDGET_H 32 | #define OPENGLWIDGET_H 33 | 34 | #include 35 | #include 36 | #include 37 | #include "opencv2/opencv.hpp" 38 | 39 | class openGLWidget : public QOpenGLWidget 40 | { 41 | Q_OBJECT 42 | 43 | 44 | public: 45 | explicit openGLWidget(QWidget *parent = 0); 46 | ~openGLWidget(); 47 | 48 | void Capture(); // take a snapshot of rendered 3D scene 49 | 50 | bool computeVertices3D, // recompute all vertices and create a new buffer 51 | computeIndexes3D, // recompute all indexes and create a new buffer 52 | computeColors3D, // recompute all colors and create a new buffer 53 | updateVertices3D, // recompute only vertices using the mask "mask3D", directly in GPU's RAM 54 | updateAllVertices3D; // when only updating vertices, indicate that the whole image is concerned 55 | 56 | QOpenGLBuffer vertexbuffer; // VBO for vertices 57 | QOpenGLBuffer indexbuffer; // VBO for indexes 58 | QOpenGLBuffer colorbuffer; // VBO for vertex colors 59 | 60 | QVector vertexarray; // vertex coordinates 61 | QVector indexarray; // vertex indexes 62 | QVector colorarray; // vertex colors 63 | 64 | double xRot, yRot, zRot; // rotation values 65 | int xShift, yShift; // position values 66 | double angleLight; // x position 67 | 68 | cv::Mat image3D; // reference image 69 | cv::Mat depthmap3D; // depthmap image 70 | cv::Mat mask3D; // mask for partial update 71 | cv::Rect area3D; // used for partial update 72 | 73 | double zoom3D; // zoom coefficient 74 | double depth3D; // used for depthmap rendering 75 | 76 | bool anaglyphEnabled; // anaglypgh red / cyan rendering (angle in degrees on y axis) 77 | double anaglyphShift; // shift of cyan vs red image 78 | bool axesEnabled; // draw 3D origin axes 79 | bool lightEnabled; // light 80 | bool qualityEnabled; // antialiasing 81 | 82 | QImage capture3D; // image of captured 3D scene 83 | 84 | 85 | protected: 86 | 87 | void initializeGL(); // launched when the widget is initialized 88 | void paintGL(); // 3D rendering 89 | void resizeGL(int width, int height); // called when the widget is resized 90 | void mousePressEvent(QMouseEvent *event); // save initial mouse position for move and rotate 91 | void mouseMoveEvent(QMouseEvent *event); // move and rotate view with mouse buttons 92 | void wheelEvent(QWheelEvent *event); // zoom 93 | void ComputeVertices(); // create vertices 94 | void ComputeIndexes(); // create indexes 95 | void UpdateVertices(); // update vertices z 96 | void ComputeColors(); // recompute colors 97 | int NumberOfVertices(const int &rows, const int &cols); // number of expected vertices for an image 98 | int NumberOfIndexes(const int &rows, const int &cols); // number of expected indexes for an image 99 | GLuint VertexIndex(const int &y, const int &x); // pixel's index in a image 100 | 101 | public slots: 102 | 103 | void SetXRotation(int angle); // rotate view 104 | void SetYRotation(int angle); 105 | void SetZRotation(int angle); 106 | void SetAngleUp(); 107 | void SetAngleDown(); 108 | void SetAngleLeft(); 109 | void SetAngleRight(); 110 | 111 | void SetXShift(int value); // move view 112 | void SetYShift(int value); 113 | void SetShiftUp(); 114 | void SetShiftDown(); 115 | void SetShiftLeft(); 116 | void SetShiftRight(); 117 | 118 | void SaveToObj(const QString &filename); 119 | void SaveToPly(const QString &filename); 120 | 121 | 122 | signals: 123 | 124 | void xRotationChanged(int angle); // rotation signals 125 | void yRotationChanged(int angle); 126 | void zRotationChanged(int angle); 127 | 128 | void xShiftChanged(int dec); // move signals 129 | void yShiftChanged(int dec); 130 | 131 | void zoomChanged(double zoom); // zoom signal 132 | 133 | void verticesChanged(int nb_Vertices); // number of vertices signal 134 | 135 | 136 | private: 137 | 138 | QPoint lastPos; // save mouse position 139 | 140 | }; 141 | 142 | #endif // OPENGLWIDGET_H 143 | 144 | -------------------------------------------------------------------------------- /resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | icons/3d.png 4 | icons/3d-depth.png 5 | icons/3d-manual.png 6 | icons/3d-realtime.png 7 | icons/3d-quality.png 8 | icons/3d-quality-off.png 9 | icons/3d-strip-quads.png 10 | icons/3d-strip-triangles.png 11 | icons/3d-widget.png 12 | icons/3d-window.png 13 | icons/absurdephoton.png 14 | icons/anaglyph.png 15 | icons/animation.png 16 | icons/arrow-left-right.png 17 | icons/arrow-up-down.png 18 | icons/axis-3d.png 19 | icons/axis-3d-off.png 20 | icons/blur.png 21 | icons/camera.png 22 | icons/color.png 23 | icons/color-balance.png 24 | icons/color-rgb.png 25 | icons/combobox-arrow.png 26 | icons/compute.png 27 | icons/contours.png 28 | icons/gamma.png 29 | icons/glasses.png 30 | icons/gray.png 31 | icons/infinite.png 32 | icons/infinite-off.png 33 | icons/image.png 34 | icons/image-off.png 35 | icons/label.png 36 | icons/label-new.png 37 | icons/light-off.png 38 | icons/light-on.png 39 | icons/mask.png 40 | icons/mask-off.png 41 | icons/pipette.png 42 | icons/quit.png 43 | icons/save.png 44 | icons/stop.png 45 | icons/undo.png 46 | icons/undo-off.png 47 | icons/zoom.png 48 | icons/zoom-100.png 49 | icons/zoom-minus.png 50 | icons/zoom-plus.png 51 | icons/view.png 52 | icons/wiggle.png 53 | icons/curve-linear.png 54 | icons/curve-cosinus.png 55 | icons/curve-cosinus2.png 56 | icons/curve-cos2power2.png 57 | icons/curve-cos2sqrt.png 58 | icons/curve-power2.png 59 | icons/curve-power3.png 60 | icons/curve-sigmoid.png 61 | icons/curve-undulate.png 62 | icons/curve-undulate2.png 63 | icons/curve-undulate3.png 64 | example/absurdephoton-image.png 65 | example/absurdephoton-depthmap.png 66 | 67 | 68 | -------------------------------------------------------------------------------- /screenshots/screenshot-3d-options.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-3d-options.jpg -------------------------------------------------------------------------------- /screenshots/screenshot-3d-rendering.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-3d-rendering.jpg -------------------------------------------------------------------------------- /screenshots/screenshot-anaglyph.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-anaglyph.jpg -------------------------------------------------------------------------------- /screenshots/screenshot-animation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-animation.jpg -------------------------------------------------------------------------------- /screenshots/screenshot-depthmap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-depthmap.jpg -------------------------------------------------------------------------------- /screenshots/screenshot-gradients.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-gradients.jpg -------------------------------------------------------------------------------- /screenshots/screenshot-gui.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbsurdePhoton/segmentation-depthmap-3d-opencv/4e7920464e2bb9690e42dec6fdce66634b9390f9/screenshots/screenshot-gui.jpg -------------------------------------------------------------------------------- /segmentation-depthmap-3d-opencv.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Segmentation to Depthmap to 3D with openCV 4 | # 5 | # by AbsurdePhoton - www.absurdephoton.fr 6 | # 7 | # v1.5 - 2019/07/08 8 | # 9 | #------------------------------------------------- 10 | 11 | QT += core gui opengl 12 | 13 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 14 | 15 | TARGET = segmentation-depthmap-3d-opencv 16 | TEMPLATE = app 17 | 18 | INCLUDEPATH += /usr/local/include/opencv2 19 | 20 | LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui 21 | 22 | SOURCES += main.cpp\ 23 | mainwindow.cpp \ 24 | mat-image-tools.cpp \ 25 | openglwidget.cpp 26 | 27 | HEADERS += mainwindow.h \ 28 | mat-image-tools.h \ 29 | openglwidget.h 30 | 31 | FORMS += mainwindow.ui 32 | 33 | # we add the package opencv to pkg-config 34 | CONFIG += link_pkgconfig 35 | PKGCONFIG += opencv4 36 | 37 | QMAKE_CXXFLAGS += -std=c++11 38 | 39 | # icons 40 | RESOURCES += resources.qrc 41 | --------------------------------------------------------------------------------