unwrap.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. * //
  40. //M*/
  41. #include <opencv2/imgproc.hpp>
  42. #include <opencv2/highgui.hpp>
  43. #include <opencv2/phase_unwrapping.hpp>
  44. #include <iostream>
  45. #include <fstream>
  46. #include <stdio.h>
  47. using namespace cv;
  48. using namespace std;
  49. static const char* keys =
  50. {
  51. "{@inputPath | | Path of the wrapped phase map saved in a yaml file }"
  52. "{@outputUnwrappedName | | Path of the unwrapped phase map to be saved in a yaml file and as an 8 bit png}"
  53. };
  54. static void help()
  55. {
  56. cout << "\nThis example shows how to use the \"Phase unwrapping module\" to unwrap a phase map"
  57. " saved in a yaml file (see extra_data\\phase_unwrapping\\data\\wrappedpeaks.yml)."
  58. " The mat name in the file should be \"phaseValue\". The result is saved in a yaml file"
  59. " too. Two images (wrapped.png and output_name.png) are also created"
  60. " for visualization purpose."
  61. "\nTo call: ./example_phase_unwrapping_unwrap <input_path> <output_unwrapped_name> \n"
  62. << endl;
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. phase_unwrapping::HistogramPhaseUnwrapping::Params params;
  67. CommandLineParser parser(argc, argv, keys);
  68. String inputPath = parser.get<String>(0);
  69. String outputUnwrappedName = parser.get<String>(1);
  70. if( inputPath.empty() || outputUnwrappedName.empty() )
  71. {
  72. help();
  73. return -1;
  74. }
  75. FileStorage fsInput(inputPath, FileStorage::READ);
  76. FileStorage fsOutput(outputUnwrappedName + ".yml", FileStorage::WRITE);
  77. Mat wPhaseMap;
  78. Mat uPhaseMap;
  79. Mat reliabilities;
  80. fsInput["phaseValues"] >> wPhaseMap;
  81. fsInput.release();
  82. params.width = wPhaseMap.cols;
  83. params.height = wPhaseMap.rows;
  84. Ptr<phase_unwrapping::HistogramPhaseUnwrapping> phaseUnwrapping = phase_unwrapping::HistogramPhaseUnwrapping::create(params);
  85. phaseUnwrapping->unwrapPhaseMap(wPhaseMap, uPhaseMap);
  86. fsOutput << "phaseValues" << uPhaseMap;
  87. fsOutput.release();
  88. phaseUnwrapping->getInverseReliabilityMap(reliabilities);
  89. Mat uPhaseMap8, wPhaseMap8, reliabilities8;
  90. wPhaseMap.convertTo(wPhaseMap8, CV_8U, 255, 128);
  91. uPhaseMap.convertTo(uPhaseMap8, CV_8U, 1, 128);
  92. reliabilities.convertTo(reliabilities8, CV_8U, 255,128);
  93. imshow("reliabilities", reliabilities);
  94. imshow("wrapped phase map", wPhaseMap8);
  95. imshow("unwrapped phase map", uPhaseMap8);
  96. imwrite(outputUnwrappedName + ".png", uPhaseMap8);
  97. imwrite("reliabilities.png", reliabilities8);
  98. bool loop = true;
  99. while( loop )
  100. {
  101. char key = (char)waitKey(0);
  102. if( key == 27 )
  103. {
  104. loop = false;
  105. }
  106. }
  107. return 0;
  108. }