-
Notifications
You must be signed in to change notification settings - Fork 4
/
cvx_io.cpp
233 lines (202 loc) · 6.27 KB
/
cvx_io.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// cvx_io.cpp
// RGB_RF
//
// Created by jimmy on 2016-05-26.
// Copyright © 2016 jimmy. All rights reserved.
//
#include "cvx_io.hpp"
#include <iostream>
#include <dirent.h>
using cv::Mat;
using std::cout;
using std::endl;
bool CvxIO::imread_depth_16bit_to_32f(const char *file, cv::Mat & depth_img)
{
depth_img = cv::imread(file, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if (depth_img.empty()) {
printf("Error: can not read image from %s\n", file);
return false;
}
assert(depth_img.type() == CV_16UC1);
depth_img.convertTo(depth_img, CV_32F);
return true;
}
bool CvxIO::imread_depth_16bit_to_64f(const char *filename, cv::Mat & depth_img)
{
depth_img = cv::imread(filename, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if (depth_img.empty()) {
printf("Error: can not read image from %s\n", filename);
return false;
}
assert(depth_img.type() == CV_16UC1);
depth_img.convertTo(depth_img, CV_64F);
return true;
}
bool CvxIO::imread_rgb_8u(const char *file_name, cv::Mat & rgb_img)
{
rgb_img = cv::imread(file_name, CV_LOAD_IMAGE_COLOR);
if (rgb_img.empty()) {
printf("Error: can not read image from %s\n", file_name);
return false;
}
assert(rgb_img.type() == CV_8UC3);
return true;
}
bool CvxIO::imread_gray_8u(const char *file_name, cv::Mat & grey_img)
{
grey_img = cv::imread(file_name, CV_LOAD_IMAGE_GRAYSCALE);
if (grey_img.empty()) {
printf("Error: can not read image from %s\n", file_name);
return false;
}
assert(grey_img.type() == CV_8UC1);
return true;
}
void CvxIO::imwrite_depth_8u(const char *file, const cv::Mat & depth_img)
{
assert(depth_img.type() == CV_32F || depth_img.type() == CV_64F);
assert(depth_img.channels() == 1);
double minv = 0.0;
double maxv = 0.0;
cv::minMaxLoc(depth_img, &minv, &maxv);
printf("min, max values are: %lf %lf\n", minv, maxv);
cv::Mat shifted_depth_map;
depth_img.convertTo(shifted_depth_map, CV_32F, 1.0, -minv);
cv::Mat depth_8u;
shifted_depth_map.convertTo(depth_8u, CV_8UC1, 255/(maxv - minv));
cv::imwrite(file, depth_8u);
printf("save to: %s\n", file);
}
void CvxIO::imwrite_xyz_to_8urgb(const char *file, const cv::Mat & xyz_img)
{
assert(xyz_img.type() == CV_32FC3 || xyz_img.type() == CV_64FC3);
cv::Mat single_channel = xyz_img.reshape(1);
double minv = 0.0;
double maxv = 0.0;
cv::minMaxLoc(single_channel, &minv, &maxv);
printf("min, max values are: %lf %lf\n", minv, maxv);
cv::Mat shifted_image;
single_channel.convertTo(shifted_image, CV_32F, 1.0, -minv);
cv::Mat single_channel_8u;
shifted_image.convertTo(single_channel_8u, CV_8UC1, 255/(maxv - minv));
cv::Mat bgr_image = single_channel_8u.reshape(3, xyz_img.rows);
cv::Mat rgb_img;
cv::cvtColor(bgr_image, rgb_img, CV_BGR2RGB);
cv::imwrite(file, rgb_img);
printf("save to: %s\n", file);
}
bool CvxIO::save_mat(const char *txtfile, const cv::Mat & mat)
{
assert(mat.type() == CV_64FC1);
FILE * pf = fopen(txtfile, "w");
if (!pf) {
printf("Error: can not write to %s \n", txtfile);
return false;
}
fprintf(pf, "%d %d\n", mat.rows, mat.cols);
for (int y = 0; y < mat.rows; y++) {
for (int x = 0; x< mat.cols; x++) {
fprintf(pf, "%lf ", mat.at<double>(y, x));
}
fprintf(pf, "\n");
}
fclose(pf);
printf("save to %s\n", txtfile);
return true;
}
bool CvxIO::load_mat(const char *txtfile, cv::Mat & mat)
{
FILE *pf = fopen(txtfile, "r");
if (!pf) {
printf("Error: can not read from %s \n", txtfile);
return false;
}
int h = 0;
int w = 0;
int num = fscanf(pf, "%d %d", &h, &w);
assert(num == 2);
mat = cv::Mat::zeros(h, w, CV_64FC1);
for (int y = 0; y<h; y++) {
for (int x = 0; x<w; x++) {
double val = 0;
num = fscanf(pf, "%lf", &val);
assert(num ==1);
mat.at<double>(y, x) = val;
}
}
fclose(pf);
return true;
}
bool CvxIO::write_mat(FILE *pf, const cv::Mat & mat)
{
assert(pf);
fprintf(pf, "%d %d\n", mat.rows, mat.cols);
for (int y = 0; y < mat.rows; y++) {
for (int x = 0; x< mat.cols; x++) {
fprintf(pf, "%lf ", mat.at<double>(y, x));
}
fprintf(pf, "\n");
}
return true;
}
bool CvxIO::read_mat(FILE *pf, cv::Mat & mat)
{
int h = 0;
int w = 0;
int num = fscanf(pf, "%d %d", &h, &w);
assert(num == 2);
mat = cv::Mat::zeros(h, w, CV_64FC1);
for (int y = 0; y<h; y++) {
for (int x = 0; x<w; x++) {
double val = 0;
num = fscanf(pf, "%lf", &val);
assert(num ==1);
mat.at<double>(y, x) = val;
}
}
return true;
}
vector<string> CvxIO::read_files(const char *dir_name)
{
const char *post_fix = strrchr(dir_name, '.');
string pre_str(dir_name);
pre_str = pre_str.substr(0, pre_str.rfind('/') + 1);
//printf("pre_str is %s\n", pre_str.c_str());
assert(post_fix);
vector<string> file_names;
DIR *dir = NULL;
struct dirent *ent = NULL;
if ((dir = opendir (pre_str.c_str())) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
const char *cur_post_fix = strrchr( ent->d_name, '.');
//printf("cur post_fix is %s %s\n", post_fix, cur_post_fix);
if (!strcmp(post_fix, cur_post_fix)) {
file_names.push_back(pre_str + string(ent->d_name));
// cout<<file_names.back()<<endl;
}
//printf ("%s\n", ent->d_name);
}
closedir (dir);
}
printf("read %lu files\n", file_names.size());
return file_names;
}
vector<string> CvxIO::read_file_names(const char *file_name)
{
vector<string> file_names;
FILE *pf = fopen(file_name, "r");
assert(pf);
while (1) {
char line[1024] = {NULL};
int ret = fscanf(pf, "%s", line);
if (ret != 1) {
break;
}
file_names.push_back(string(line));
}
printf("read %lu lines\n", file_names.size());
fclose(pf);
return file_names;
}