Openholo  v4.2
Open Source Digital Holographic Library
examDepthMap.cpp
Go to the documentation of this file.
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, copy or use the software.
7 //
8 //
9 // License Agreement
10 // For Open Source Digital Holographic Library
11 //
12 // Openholo library is free software;
13 // you can redistribute it and/or modify it under the terms of the BSD 2-Clause license.
14 //
15 // Copyright (C) 2017-2024, Korea Electronics Technology Institute. All rights reserved.
16 // E-mail : contact.openholo@gmail.com
17 // Web : http://www.openholo.org
18 //
19 // Redistribution and use in source and binary forms, with or without modification,
20 // are permitted provided that the following conditions are met:
21 //
22 // 1. Redistribution's of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistribution's in binary form must reproduce the above copyright notice,
26 // this list of conditions and the following disclaimer in the documentation
27 // and/or other materials provided with the distribution.
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 copyright holder 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 // This software contains opensource software released under GNU Generic Public License,
41 // NVDIA Software License Agreement, or CUDA supplement to Software License Agreement.
42 // Check whether software you use contains licensed software.
43 //
44 //M*/
45 
46 #include <iostream>
47 #include <string>
48 #include <filesystem>
49 #include <ctime>
50 
51 #include "ophDepthMap.h"
52 
53 namespace fs = std::experimental::filesystem;
54 
55 int main() {
56  // Create ophDepthMap instance
57  ophDepthMap* Hologram = new ophDepthMap();
58 
59  std::string proj_dir = fs::current_path().generic_string();
60  std::string conf_file = proj_dir + "/../dataset/Dice/DepthMap/Dice_DepthMap.xml";
61 
62  // Read Config Parameters for Depth Map CGH
63  if (!Hologram->readConfig(conf_file.c_str())) {
64  std::cerr << "Fail to load Config file : " << conf_file << std::endl;
65  exit(1);
66  }
67 
68  // Load Depth and RGB image
69  std::string dm_path = proj_dir + "/../dataset/Dice/DepthMap";
70  if (!Hologram->readImageDepth(dm_path.c_str(), "dice_rgb", "dice_depth")) {
71  std::cerr << "Fail to load DepthMap file : " << dm_path << std::endl;
72  exit(1);
73  }
74 
75  // Select CPU or GPU Processing
76  // param = MODE_CPU or MODE_GPU
77  Hologram->SetMode(MODE_GPU);
78 
79  // CGH by Depth Map
80  Hologram->generateHologram();
81 
82  // Save to ohc(Openholo complex field file format)
83  std::string res_path = "./result/";
84  if (!fs::exists(res_path)) {
85  fs::create_directories(res_path);
86  }
87 
88  //
89  time_t cur_time = time(nullptr);
90 
91  struct tm local;
92 #ifdef _MSC_VER
93  localtime_s(&local, &cur_time);
94 #else
95  localtime_r(&cur_time, &local);
96 #endif
97 
98  char buff[32];
99 #ifdef _MSC_VER
100  sprintf_s(buff, "%4d%02d%02d-%02d%02d%02d",
101  local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
102  local.tm_hour, local.tm_min, local.tm_sec);
103 #else
104  sprintf(buff, "%4d%02d%02d-%02d%02d%02d",
105  local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
106  local.tm_hour, local.tm_min, local.tm_sec);
107 #endif
108 
109  std::string ohc_file = res_path + "dm_sample_" + std::string(buff) + ".ohc";
110  if (!Hologram->saveAsOhc(ohc_file.c_str())) {
111  std::cerr << "Fail to save OHC file : " << ohc_file << std::endl;
112  exit(1);
113  }
114 
115  // Encode Complex Field to Real Field
116  Hologram->encoding(Hologram->ENCODE_PHASE);
117 
118  // Normalize Real Field to unsigned char(0~255) for save to image(*.BMP)
119  Hologram->normalize();
120 
121  // Save to bmp
122  std::string bmp_file = res_path + "dm_sample_" + std::string(buff) + ".bmp";
123  if (!Hologram->save(bmp_file.c_str())) {
124  std::cerr << "Fail to save BMP file : " << bmp_file << std::endl;
125  exit(1);
126  }
127 
128  // Release memory used to Generate Depth Map
129  Hologram->release();
130 
131  return 0;
132 }
int main()
bool readImageDepth(const char *source_folder, const char *img_prefix, const char *depth_img_prefix)
Read image and depth map.
#define MODE_GPU
Definition: define.h:156
void normalize()
void SetMode(unsigned int mode)
Definition: ophGen.h:538
bool save(const char *fname, uint8_t bitsperpixel=8, uchar *src=nullptr, uint px=0, uint py=0)
Function for saving image files.
bool readConfig(const char *fname)
Read parameters from a config file. (*.xml)
Definition: ophDepthMap.cpp:91
This class generates CGH based on depth map.
Definition: ophDepthMap.h:108
virtual void encoding(unsigned int ENCODE_FLAG)
virtual bool saveAsOhc(const char *fname)
Function to write OHC file
Definition: Openholo.cpp:252
Real generateHologram(void)
Generate a hologram, main funtion. When the calculation is finished, the angular spectrum is performe...
unsigned long release(void)
Call release() when reference is finished.
Definition: Base.h:88