|
| 1 | +/* -*-c++-*- Copyright (C) 2020. All rights reserved |
| 2 | +* Author : Ethan Li <ethan.li.whu@gmail.com> |
| 3 | +* github£º https://github.com/ethan-li-coding |
| 4 | +* blog: https://ethanli.blog.csdn.net/ |
| 5 | +* zhihu: https://www.zhihu.com/people/yingsongli |
| 6 | +* bilibili: https://space.bilibili.com/328453794 |
| 7 | +* Describe : implement of FasterStereoCuda |
| 8 | +*/ |
| 9 | + |
| 10 | +#include <io.h> |
| 11 | +#include <chrono> |
| 12 | +using namespace std::chrono; |
| 13 | + |
| 14 | +#include <opencv.hpp> |
| 15 | +using namespace cv; |
| 16 | + |
| 17 | +#include <vector> |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +#include "FasterStereoCuda.h" |
| 21 | +#ifdef _DEBUG |
| 22 | +#pragma comment(lib,"opencv_world320d.lib") |
| 23 | +#pragma comment(lib, "FasterStereoCudad.lib") |
| 24 | +#else |
| 25 | +#pragma comment(lib,"opencv_world320.lib") |
| 26 | +#pragma comment(lib, "FasterStereoCuda.lib") |
| 27 | +#endif |
| 28 | + |
| 29 | +#include "option_manager.h" |
| 30 | + |
| 31 | +// using page locked memory |
| 32 | +#define USING_PAGE_LOCKED_MEMORY |
| 33 | + |
| 34 | +void LoadDatas(const string& path_left,const string& path_right,const string& file_type, vector<Mat>& left_mats,vector<Mat>& right_mats) |
| 35 | +{ |
| 36 | + //Load all image pairs in specified folder |
| 37 | + for (int i = 0; i < 2; i++) { |
| 38 | + auto& path = (i == 0) ? path_left : path_right; |
| 39 | + auto& mats = (i == 0) ? left_mats : right_mats; |
| 40 | + vector<string> str_paths; |
| 41 | + _finddata_t file; |
| 42 | + std::string str_root = path; |
| 43 | + if (str_root[str_root.length() - 1] != '\\') |
| 44 | + { |
| 45 | + str_root.append("\\"); |
| 46 | + } |
| 47 | + std::string search_name = str_root; |
| 48 | + search_name.append("*." + file_type); |
| 49 | + intptr_t k; |
| 50 | + intptr_t HANDLE; |
| 51 | + k = HANDLE = _findfirst(search_name.c_str(), &file); |
| 52 | + while (k != -1) |
| 53 | + { |
| 54 | + str_paths.push_back(str_root + file.name); |
| 55 | + k = _findnext(HANDLE, &file); |
| 56 | + } |
| 57 | + for (int n = 0; n < str_paths.size(); n++) { |
| 58 | + mats.emplace_back(imread(str_paths[n], IMREAD_GRAYSCALE)); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void LoadDatas(const string& path_left, const string& path_right, vector<Mat>& left_mats, vector<Mat>& right_mats) |
| 64 | +{ |
| 65 | + //Load single image pairs |
| 66 | + left_mats.emplace_back(imread(path_left, IMREAD_GRAYSCALE)); |
| 67 | + right_mats.emplace_back(imread(path_right, IMREAD_GRAYSCALE)); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * \brief mode1(single pairs): im2.png im6.png option.xml |
| 72 | + * mode2(multiple pairs): folder1 folder2 png option.xml |
| 73 | + * mode3: opt |
| 74 | + */ |
| 75 | +int main(int argc, char** argv) |
| 76 | +{ |
| 77 | + if (argc < 2){ |
| 78 | + cout << "insufficient parameters!" << endl; |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + string data_type; |
| 83 | + string path_left, |
| 84 | + path_right; |
| 85 | + string opt_path; |
| 86 | + string file_type; |
| 87 | + |
| 88 | + if (argc < 4) { |
| 89 | + cout << "insufficient parameters!" << endl; |
| 90 | + return 0; |
| 91 | + } |
| 92 | + |
| 93 | + if (argc == 4) { |
| 94 | + // mode 1 : single pairs |
| 95 | + path_left = argv[1]; |
| 96 | + path_right = argv[2]; |
| 97 | + opt_path = argv[3]; |
| 98 | + data_type = "S"; |
| 99 | + } |
| 100 | + else if(argc == 5) { |
| 101 | + // mode 2 : multiple pairs |
| 102 | + path_left = argv[1]; |
| 103 | + path_right = argv[2]; |
| 104 | + file_type = argv[3]; |
| 105 | + opt_path = argv[4]; |
| 106 | + data_type = "M"; |
| 107 | + } |
| 108 | + else if(argc == 2 && argv[1] == "opt"){ |
| 109 | + // mode 3 :initialize parameter file for algorithm |
| 110 | + FasterStereoCuda::StereoOption1 opt1; |
| 111 | + FasterStereoCuda::StereoOption2 opt2; |
| 112 | + OptionManager opt_mag; |
| 113 | + opt_mag.Load("option.xml", opt1); |
| 114 | + opt_mag.Load("option2.xml", opt2); |
| 115 | + cout << "initialize parameter file of algorithm done!" << endl; |
| 116 | + return 0; |
| 117 | + } |
| 118 | + else { |
| 119 | + cout << "parameter type error!" << endl; |
| 120 | + return 0; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /********************************************************/ |
| 125 | + /*loading parameters of algorithm from xml file*/ |
| 126 | + FasterStereoCuda::StereoOption1* ste_opt1 = nullptr; |
| 127 | + FasterStereoCuda::StereoOption2* ste_opt2 = nullptr; |
| 128 | + OptionManager opt_mag; |
| 129 | + OptionManager::OptionType opt_type = opt_mag.GaussType(opt_path); |
| 130 | + if(opt_type == OptionManager::OPTION_1) { |
| 131 | + ste_opt1 = new FasterStereoCuda::StereoOption1; |
| 132 | + if(!opt_mag.Load(opt_path, *ste_opt1)) { |
| 133 | + cout << "loading parameters of algorithm succeed!" << endl; |
| 134 | + delete ste_opt1; ste_opt1 = nullptr; |
| 135 | + return 0; |
| 136 | + } |
| 137 | + } |
| 138 | + else if(opt_type == OptionManager::OPTION_2) { |
| 139 | + ste_opt2 = new FasterStereoCuda::StereoOption2; |
| 140 | + if(!opt_mag.Load(opt_path, *ste_opt2)) { |
| 141 | + cout << "loading parameters of algorithm failed!" << endl; |
| 142 | + delete ste_opt2; ste_opt2 = nullptr; |
| 143 | + return 0; |
| 144 | + } |
| 145 | + } |
| 146 | + else { |
| 147 | + cout << "loading parameters of algorithm failed!" << endl; |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + /********************************************************/ |
| 152 | + /*data*/ |
| 153 | + vector<Mat> left_mats; |
| 154 | + vector<Mat> right_mats; |
| 155 | + if (data_type == "S") { |
| 156 | + // loading single image pair |
| 157 | + LoadDatas(path_left, path_right, left_mats, right_mats); |
| 158 | + } |
| 159 | + else if (data_type == "M") { |
| 160 | + // loading multiple image pairs from folder |
| 161 | + LoadDatas(path_left, path_right, file_type, left_mats, right_mats); |
| 162 | + } |
| 163 | + if(left_mats.empty()||right_mats.empty()||(left_mats.size()!=right_mats.size())) { |
| 164 | + cout << "data error!" << endl; |
| 165 | + return 0; |
| 166 | + } |
| 167 | + |
| 168 | + /********************************************************/ |
| 169 | + /*initialize algorithm*/ |
| 170 | + const int width = left_mats[0].cols; |
| 171 | + const int height = right_mats[0].rows; |
| 172 | + FasterStereoCuda stereo; |
| 173 | + if (ste_opt1 != nullptr) { |
| 174 | + // interface 1 : for generating disparity map |
| 175 | + if (!stereo.Init(*ste_opt1)) { |
| 176 | + return 0; |
| 177 | + } |
| 178 | + } |
| 179 | + else if(ste_opt2 != nullptr) { |
| 180 | + // interface 1 : for generating depth map |
| 181 | + if (!stereo.Init2(*ste_opt2)) { |
| 182 | + return 0; |
| 183 | + } |
| 184 | + } |
| 185 | + cout << "initialize algorithm done!" << endl; |
| 186 | + |
| 187 | + /********************************************************/ |
| 188 | + /*allocate memory of data*/ |
| 189 | +#ifdef USING_PAGE_LOCKED_MEMORY |
| 190 | + // using page locked memory |
| 191 | + unsigned char* img_left = nullptr; |
| 192 | + unsigned char* img_right = nullptr; |
| 193 | + float* d_data = nullptr; |
| 194 | + FasterStereoCuda::MallocPageLockedPtr(reinterpret_cast<void**>(&img_left), width * height * sizeof(unsigned char)); |
| 195 | + FasterStereoCuda::MallocPageLockedPtr(reinterpret_cast<void**>(&img_right), width * height * sizeof(unsigned char)); |
| 196 | + FasterStereoCuda::MallocPageLockedPtr(reinterpret_cast<void**>(&d_data), width * height * sizeof(float)); |
| 197 | +#else |
| 198 | + unsigned char* img_left = new unsigned char[width*height]; |
| 199 | + unsigned char* img_right = new unsigned char[width*height]; |
| 200 | + float* d_data = new float[width*height]; |
| 201 | +#endif |
| 202 | + |
| 203 | + /********************************************************/ |
| 204 | + /*set windows of results*/ |
| 205 | + namedWindow("left", WINDOW_NORMAL); |
| 206 | + namedWindow("right", WINDOW_NORMAL); |
| 207 | + namedWindow("d_map", WINDOW_NORMAL); |
| 208 | + namedWindow("d_map_color", WINDOW_NORMAL); |
| 209 | + double scale = width / 640.0; |
| 210 | + resizeWindow("left", int(width / scale), int(height / scale)); |
| 211 | + resizeWindow("right", int(width / scale), int(height / scale)); |
| 212 | + resizeWindow("d_map", int(width / scale), int(height / scale)); |
| 213 | + resizeWindow("d_map_color", int(width / scale), int(height / scale)); |
| 214 | + |
| 215 | + /********************************************************/ |
| 216 | + /*matching(for loop)*/ |
| 217 | + int time_count = 0; |
| 218 | + double time_all = 0.0, time_avg = 0.0, time_first = 0.0; |
| 219 | + for (int i = 0; i < left_mats.size(); ) { |
| 220 | + auto& mat_left = left_mats[i]; |
| 221 | + auto& mat_right = right_mats[i]; |
| 222 | + |
| 223 | + for (int i = 0; i < height;i++) { |
| 224 | + for (int j = 0; j < width;j++) { |
| 225 | + img_left[i*width + j] = mat_left.at<unsigned char>(i, j); |
| 226 | + img_right[i*width + j] = mat_right.at<unsigned char>(i, j); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + int validCount = 0; |
| 231 | + auto start = std::chrono::steady_clock::now(); |
| 232 | + |
| 233 | + /********************************************************/ |
| 234 | + /*do matching*/ |
| 235 | + if (ste_opt1 != nullptr) { |
| 236 | + // interface 1 : generating disparity map |
| 237 | + stereo.Match(img_left, img_right, d_data); |
| 238 | + } |
| 239 | + else if(ste_opt2 != nullptr) { |
| 240 | + // interface 2 : generating depth map |
| 241 | + stereo.Match2(img_left, img_right, d_data); |
| 242 | + } |
| 243 | + |
| 244 | + /********************************************************/ |
| 245 | + /*compute time consuming and frame*/ |
| 246 | + auto end = std::chrono::steady_clock::now(); |
| 247 | + auto tt = duration_cast<std::chrono::microseconds>(end - start); |
| 248 | + double time_level = tt.count()/1000.0; |
| 249 | + time_all += time_level; |
| 250 | + if (time_count==0) |
| 251 | + time_first = time_level; |
| 252 | + time_count++; |
| 253 | + if (time_count > 1) { |
| 254 | + time_avg = (time_all - time_first) / (time_count - 1); |
| 255 | + } |
| 256 | + else { |
| 257 | + time_avg = time_level; |
| 258 | + } |
| 259 | + printf("frame %d, timing: %6.4lf ms",i, time_level); |
| 260 | + |
| 261 | + /********************************************************/ |
| 262 | + /*results visualization*/ |
| 263 | + Mat disp_mat = Mat(height, width, CV_8UC1); |
| 264 | + Mat disp_color; |
| 265 | + float min_d = 999999.0f, max_d = -999999.0f; |
| 266 | + for (int i = 0; i < height; i++) { |
| 267 | + for (int j = 0; j < width; j++) { |
| 268 | + const float d = d_data[i * width + j]; |
| 269 | + if (d != -999999.0f) { |
| 270 | + min_d = min(min_d, d); |
| 271 | + max_d = max(max_d, d); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + for (int i = 0; i < height; i++) { |
| 276 | + for (int j = 0; j < width; j++) { |
| 277 | + const float d = d_data[i * width + j]; |
| 278 | + if (d == -999999.0f) { |
| 279 | + disp_mat.data[i * width + j] = 0; |
| 280 | + } |
| 281 | + else { |
| 282 | + disp_mat.data[i * width + j] = static_cast<uchar>((d - min_d) / (max_d - min_d) * 255); |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + double fps = int(1000.0 / time_avg * 10 + 0.5) / 10.0; |
| 287 | + printf(" fps = %6.4lf\n", fps); |
| 288 | + applyColorMap(disp_mat, disp_color, COLORMAP_JET); |
| 289 | + imshow("left", mat_left); |
| 290 | + imshow("right", mat_right); |
| 291 | + imshow("d_map", disp_mat); |
| 292 | + imshow("d_map_color", disp_color); |
| 293 | + cvWaitKey(1); |
| 294 | + |
| 295 | +#if 0 |
| 296 | + // save the disparity(depth) map if necessary |
| 297 | + imwrite("d.bmp", disp_mat); |
| 298 | +#endif |
| 299 | + |
| 300 | + // loop |
| 301 | + i = ++i % left_mats.size(); |
| 302 | + } |
| 303 | + |
| 304 | + /********************************************************/ |
| 305 | + /*release the memory*/ |
| 306 | + stereo.Release(); |
| 307 | + if(ste_opt1) { |
| 308 | + delete ste_opt1; ste_opt1 = nullptr; |
| 309 | + } |
| 310 | + if(ste_opt2) { |
| 311 | + delete ste_opt2; ste_opt2 = nullptr; |
| 312 | + } |
| 313 | +#ifdef USING_PAGE_LOCKED_MEMORY |
| 314 | + FasterStereoCuda::FreePageLockedPtr(img_left); |
| 315 | + FasterStereoCuda::FreePageLockedPtr(img_right); |
| 316 | + FasterStereoCuda::FreePageLockedPtr(d_data); |
| 317 | +#else |
| 318 | + delete[] d_data; d_data = nullptr; |
| 319 | + delete[] img_left; img_left = nullptr; |
| 320 | + delete[] img_right; img_right = nullptr; |
| 321 | +#endif |
| 322 | + |
| 323 | + system("pause"); |
| 324 | + return 0; |
| 325 | +} |
| 326 | + |
0 commit comments