forked from YoYo000/fusibile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
displayUtils.h
144 lines (122 loc) · 4.9 KB
/
displayUtils.h
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
/*
* utility functions for visualization of results (disparity in color, warped output, ...)
*/
#pragma once
#include <sstream>
#include <fstream>
#if (CV_MAJOR_VERSION ==2)
#include <opencv2/contrib/contrib.hpp> // needed for applyColorMap!
#endif
#include "point_cloud.h"
#include "point_cloud_list.h"
/* compute gamma correction (just for display purposes to see more details in farther away areas of disparity image)
* Input: img - image
* gamma - gamma value
* Output: gamma corrected image
*/
Mat correctGamma( Mat& img, double gamma ) {
double inverse_gamma = 1.0 / gamma;
Mat lut_matrix(1, 256, CV_8UC1 );
uchar * ptr = lut_matrix.ptr();
for( int i = 0; i < 256; i++ )
ptr[i] = (int)( pow( (double) i / 255.0, inverse_gamma ) * 255.0 );
Mat result;
LUT( img, lut_matrix, result );
return result;
}
static void getDisparityForDisplay(const Mat_<float> &disp, Mat &dispGray, Mat &dispColor, float numDisparities, float minDisp = 0.0f){
float gamma = 2.0f; // to get higher contrast for lower disparity range (just for color visualization)
disp.convertTo(dispGray,CV_16U,65535.f/(numDisparities-minDisp),-minDisp*65535.f/(numDisparities-minDisp));
Mat disp8;
disp.convertTo(disp8,CV_8U,255.f/(numDisparities-minDisp),-minDisp*255.f/(numDisparities-minDisp));
if(minDisp == 0.0f)
disp8 = correctGamma(disp8,gamma);
applyColorMap(disp8, dispColor, COLORMAP_JET);
for(int y = 0; y < dispColor.rows; y++){
for(int x = 0; x < dispColor.cols; x++){
if(disp(y,x) <= 0.0f)
dispColor.at<Vec3b>(y,x) = Vec3b(0,0,0);
}
}
}
static void convertDisparityDepthImage(const Mat_<float> &dispL, Mat_<float> &d, float f, float baseline){
d = Mat::zeros(dispL.rows, dispL.cols, CV_32F);
for(int y = 0; y < dispL.rows; y++){
for(int x = 0; x < dispL.cols; x++){
d(y,x) = disparityDepthConversion(f,baseline,dispL(y,x));
}
}
}
static string getColorString(uint8_t color){
stringstream ss;
ss << (int)color << " " << (int)color << " " << (int)color;
return ss.str();
}
static string getColorString(Vec3b color){
stringstream ss;
ss << (int)color(2) << " " << (int)color(1) << " " << (int)color(0);
return ss.str();
}
static string getColorString(Vec3i color){
stringstream ss;
ss << (int)((float)color(2)/256.f) << " " << (int)((float)color(1)/256.f) << " " << (int)((float)color(0)/256.f);
return ss.str();
}
static void storePlyFileBinaryPointCloud (char* plyFilePath, PointCloudList &pc, Mat_<float> &distImg) {
cout << "store 3D points to ply file" << endl;
FILE *outputPly;
outputPly=fopen(plyFilePath,"wb");
/*write header*/
fprintf(outputPly, "ply\n");
fprintf(outputPly, "format binary_little_endian 1.0\n");
fprintf(outputPly, "element vertex %d\n",pc.size);
fprintf(outputPly, "property float x\n");
fprintf(outputPly, "property float y\n");
fprintf(outputPly, "property float z\n");
// fprintf(outputPly, "property float nx\n");
// fprintf(outputPly, "property float ny\n");
// fprintf(outputPly, "property float nz\n");
fprintf(outputPly, "property uchar red\n");
fprintf(outputPly, "property uchar green\n");
fprintf(outputPly, "property uchar blue\n");
fprintf(outputPly, "end_header\n");
distImg = Mat::zeros(pc.rows,pc.cols,CV_32F);
//write data
#pragma omp parallel for
for(size_t i = 0; i < pc.size; i++) {
const Point_li &p = pc.points[i];
// const float4 normal = p.normal;
float4 X = p.coord;
const char color_r = (int)p.texture4[2];
const char color_g = (int)p.texture4[1];
const char color_b = (int)p.texture4[0];
/*const int color = 127.0f;*/
/*printf("Writing point %f %f %f\n", X.x, X.y, X.z);*/
if(!(X.x < FLT_MAX && X.x > -FLT_MAX) || !(X.y < FLT_MAX && X.y > -FLT_MAX) || !(X.z < FLT_MAX && X.z >= -FLT_MAX)){
X.x = 0.0f;
X.y = 0.0f;
X.z = 0.0f;
}
#pragma omp critical
{
/*myfile << X.x << " " << X.y << " " << X.z << " " << normal.x << " " << normal.y << " " << normal.z << " " << color << " " << color << " " << color << endl;*/
fwrite(&X.x, sizeof(X.x), 1, outputPly);
fwrite(&X.y, sizeof(X.y), 1, outputPly);
fwrite(&X.z, sizeof(X.z), 1, outputPly);
// fwrite(&normal.x, sizeof(normal.x), 1, outputPly);
// fwrite(&normal.y, sizeof(normal.y), 1, outputPly);
// fwrite(&normal.z, sizeof(normal.z), 1, outputPly);
fwrite(&color_r, sizeof(char), 1, outputPly);
fwrite(&color_g, sizeof(char), 1, outputPly);
fwrite(&color_b, sizeof(char), 1, outputPly);
}
}
fclose(outputPly);
}
static void getNormalsForDisplay(const Mat &normals, Mat &normals_display, int rtype = CV_16U){
if(rtype == CV_8U)
normals.convertTo(normals_display,CV_8U,128,128);
else
normals.convertTo(normals_display,CV_16U,32767,32767);
cvtColor(normals_display,normals_display,COLOR_RGB2BGR);
}