-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (126 loc) · 5.88 KB
/
main.cpp
File metadata and controls
151 lines (126 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "PatchMatchWrapper.cuh"
#include <filesystem>
using namespace std;
using namespace std::filesystem;
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <algorithm>
#include "stb_image_write.h"
// int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
void write_plane(std::string name, DisparityPlane* planes, int width, int height) {
std::ofstream myfile;
myfile.open(name);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
myfile << std::to_string(i) << " " << std::to_string(j) << " ";
myfile << std::setprecision(8)<<planes[i * width + j].p.x <<" ";
myfile << std::setprecision(8)<<planes[i * width + j].p.y <<" ";
myfile << std::setprecision(8)<<planes[i * width + j].p.z <<" ";
myfile << std::endl;
}
}
}
void write_disparity(std::string filename, float* disps, int _height, int _width, int min_disparity, int max_disparity)
{
uint8_t* vis_data = new uint8_t[size_t(_height) * _width];
float min_disp = float(_width), max_disp = -float(_width);
for (int i = 0; i < _height; i++) {
for (int j = 0; j < _width; j++) {
float disp = abs(disps[i * _width + j]);
if (disp != INVALID_FLOAT) {
min_disp = std::fmin(min_disp, disp);
max_disp = std::fmax(max_disp, disp);
}
}
}
std::cout << "write out disparity, min disp is:" << min_disp << " max disp is" << max_disp << std::endl;
for (int i = 0; i < _height; i++) {
for (int j = 0; j < _width; j++) {
const float disp = abs(disps[i * _width + j]);
if (disp == INVALID_FLOAT) {
vis_data[i * _width + j] = 0;
}
else {
vis_data[i * _width + j] = static_cast<uint8_t>((disp - min_disp) / (max_disp - min_disp) * 255);
}
}
}
//write w,h,c
int result = stbi_write_png(filename.c_str(), _width, _height, 1, (void*)vis_data,0);
delete[] vis_data;
}
std::tuple<std::string, std::string, std::string> retrieve_middlebury_old(int idx) {
std::vector<std::string> name = { "Aloe", "Baby1", "Baby2", "Baby3", "Bowling1",
"Bowling2", "Cloth1", "Cloth2", "Cloth3", "Cloth4",
"Flowerpots","Lampshade1", "Lampshade2", "Midd1", "Midd2",
"Monopoly","Plastic", "Rocks1", "Rocks2", "Wood1",
"Wood2" };
auto imgdir_path = current_path() / "dataset";
auto left_img_path = imgdir_path / name[idx] / "view1.png"; //disp1,disp5 in directory are ground true
auto right_img_path = imgdir_path / name[idx] / "view5.png";
return std::make_tuple(left_img_path.string(), right_img_path.string(), name[idx]);
}
std::tuple<std::string, std::string,std::string> retrieve_whu(std::string dir_index,std::string index) {
auto imgdir_path = current_path() / "WHU_stereo_dataset"/"WHU_stereo_dataset";
auto left_img_path = imgdir_path / "train" / dir_index / "Left" / index; //disp1,disp5 in directory are ground true
auto right_img_path = imgdir_path /"train" /dir_index/ "Right"/index;
return std::make_tuple(left_img_path.string(), right_img_path.string(),index);
}
int main()
{
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
}
cudaError_t err = cudaDeviceSetLimit(cudaLimitMallocHeapSize, 1048576ULL * 1024); //1GB
if (err != cudaSuccess) {
fprintf(stderr, "cuda set heap size failed!");
return 1;
}
auto name_tuple = retrieve_middlebury_old(0);
//auto name_tuple = retrieve_whu("012_98", "012008.png");
std::string left_img_path_str = std::get<0>(name_tuple);
std::string right_img_path_str = std::get<1>(name_tuple);
std::string img_name= std::get<2>(name_tuple);
std::replace(left_img_path_str.begin(), left_img_path_str.end(), '\\', '\/');
std::replace(right_img_path_str.begin(), right_img_path_str.end(), '\\', '\/');
std::cout << left_img_path_str << std::endl;
int l_width, l_height, l_bpp;
int r_width, r_height, r_bpp;
uint8_t* image_left = stbi_load(left_img_path_str.c_str(), &l_width, &l_height, &l_bpp, 3);
std::cout << "image width: " << l_width << "image height: " << l_height << "bpp: " << l_bpp << std::endl;
uint8_t* image_right = stbi_load(right_img_path_str.c_str(), &r_width, &r_height, &r_bpp, 3);
std::cout << "image width: " << r_width << "image height: " << r_height << "bpp: " << r_bpp << std::endl;
PMOption option;
option.width = l_width;
option.height = l_height;
option.mode = PM_MODE::ROW_SWEEP;
PatchMatchWrapper* compute_wrapper=new PatchMatchWrapper(option);
compute_wrapper->Init();
compute_wrapper->SetSourceImgs(image_left, image_right);
compute_wrapper->Compute(8);
float* image_disp_left = compute_wrapper->RetrieveLeft();
auto save_name = img_name +std::to_string(option.mode)+ "pm_disp_left.png";
write_disparity(save_name, image_disp_left, option.height, option.width, option.disp_min, option.disp_max);
delete compute_wrapper;
// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
return 0;
}