Skip to content

Commit 0216d73

Browse files
committed
fix bug: cv::imread rotates images
1 parent 5cd8a17 commit 0216d73

19 files changed

+106
-105
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ If you use the covisibility-based matching method for your research, please cite
5858

5959
3.Run the following script to automatically reconstruction:
6060
```
61-
python3 ./scripts/auto_reconstruction.py --workspace_path ${workspace_path}$
61+
python3 ./scripts/run_test_data.py --workspace_path ${workspace_path}$
6262
```
6363
Refer to [tutorial.md](docs/en/tutorial.md) for more details.
6464

README_CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ XRSfM 是一个开源的运动恢复结构代码仓库,它是[OpenXRLab](https
5555

5656
3.运行以下脚本进行重建:
5757
```
58-
python3 ./scripts/auto_reconstruction.py --workspace_path ${workspace_path}$
58+
python3 ./scripts/run_test_data.py --workspace_path ${workspace_path}$
5959
```
6060
更多细节请查看[tutorial.md](docs/zh/tutorial.md)
6161

docs/en/tutorial.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ workspace
126126
```
127127
Then run the following command line to reconstruct
128128
```
129-
python3 ./scripts/auto_reconstruction.py --workspace_path ${workspace_path}$ --estimate_scale
129+
python3 ./scripts/run_test_data.py --workspace_path ${workspace_path}$ --estimate_scale
130130
```

docs/zh/tutorial.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,5 @@ workspace
130130
```
131131
然后通过以下脚本达到相同的效果。
132132
```
133-
python3 ./scripts/auto_reconstruction.py --workspace_path ${workspace_path}$ --estimate_scale
133+
python3 ./scripts/run_test_data.py --workspace_path ${workspace_path}$ --estimate_scale
134134
```

scripts/auto_reconstruction.py scripts/run_test_data.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ def get_opts():
66
parser = ArgumentParser()
77
parser.add_argument('--workspace_path', type=str, required=True,
88
help='workspace_path')
9-
# parser.add_argument('--start_from_ios_binary',action='store_true',
10-
# help='estimate_scale')
119
parser.add_argument('--estimate_scale', action='store_true',
1210
help='estimate_scale')
11+
# parser.add_argument('--start_from_ios_binary',action='store_true',
12+
# help='estimate_scale')
1313
return parser.parse_args()
1414

1515

16+
def change_camera_txt_format(data_path):
17+
with open(data_path+'/camera.txt', 'r') as file:
18+
line = file.readline()
19+
20+
space_index1 = line.find(' ')
21+
space_index2 = line.find(' ', space_index1 + 1)
22+
23+
with open(data_path+'/camera_colmap.txt', 'w') as file:
24+
file.write(f'0 RADIAL 1440 1920 {line[space_index2+1:]}')
25+
26+
1627
if __name__ == '__main__':
1728
args = get_opts()
1829

@@ -22,18 +33,21 @@ def get_opts():
2233
data_path = data_path+'/'
2334

2435
images_path = data_path+'images/'
25-
camera_path = data_path+'camera.txt'
36+
camera_path = data_path+'camera_colmap.txt'
2637
retrieval_path = data_path+'retrieval.txt'
2738

2839
output_path = data_path
2940

3041
# if(args.start_from_ios_binary):
3142
# ios_file = data_path+'Data.txt'
3243
# os.system('./bin/unpack_collect_data '+ios_file+' '+data_path)
44+
change_camera_txt_format(data_path)
45+
3346
os.system('./bin/run_matching ' + images_path + ' ' + retrieval_path
3447
+ ' sequential ' + output_path)
3548
os.system('./bin/run_reconstruction ' + output_path + ' '
3649
+ camera_path + ' ' + output_path)
50+
3751
if (args.estimate_scale):
3852
output_refined_path = data_path+'refined/'
3953
if not os.path.exists(output_refined_path):

src/base/camera.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ namespace xrsfm {
1010
class Camera {
1111
public:
1212
Camera() {}
13+
Camera(int _id, std::string mode_name, int w, int h) {
14+
id_ = _id;
15+
width_ = w;
16+
height_ = h;
17+
if (mode_name == "SIMPLE_PINHOLE") {
18+
model_id_ = 0;
19+
} else if (mode_name == "PINHOLE") {
20+
model_id_ = 1;
21+
} else if (mode_name == "SIMPLE_RADIAL") {
22+
model_id_ = 2;
23+
} else if (mode_name == "RADIAL") {
24+
model_id_ = 3;
25+
} else if (mode_name == "OPENCV") {
26+
model_id_ = 4;
27+
}
28+
params_.resize(camera_model_param_size(model_id_));
29+
is_valid = true;
30+
}
1331
Camera(int _id, int _model_id) {
1432
id_ = _id;
1533
model_id_ = _model_id;
@@ -26,6 +44,7 @@ class Camera {
2644

2745
uint32_t id_ = -1;
2846
uint32_t model_id_ = -1;
47+
int width_, height_;
2948
std::vector<double> params_;
3049
bool is_valid;
3150

src/base/camera_model.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,6 @@ struct OpenCVCameraModel {
219219

220220
#endif
221221

222-
inline int camera_model_param_size(int model_id) {
223-
#define CAMERA_MODEL_CASE(CameraModel) \
224-
if (model_id == CameraModel::kModelId) \
225-
return CameraModel::kNumParams;
226-
227-
CAMERA_MODEL_CASES
228-
#undef CAMERA_MODEL_CASE
229-
assert(false);
230-
return -1;
231-
}
232-
233222
inline int index_fx(int model_id) {
234223
#define CAMERA_MODEL_CASE(CameraModel) \
235224
if (model_id == CameraModel::kModelId) \
@@ -285,5 +274,16 @@ inline int index_distort(int model_id) {
285274
return -1;
286275
};
287276

277+
inline int camera_model_param_size(int model_id) {
278+
#define CAMERA_MODEL_CASE(CameraModel) \
279+
if (model_id == CameraModel::kModelId) \
280+
return CameraModel::kNumParams;
281+
282+
CAMERA_MODEL_CASES
283+
#undef CAMERA_MODEL_CASE
284+
assert(false);
285+
return -1;
286+
}
287+
288288
} // namespace xrsfm
289289
#endif

src/base/map.h

-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ class CorrespondenceGraph {
115115

116116
class Map {
117117
public:
118-
std::vector<cv::Mat> images_; // for debug
119-
120-
// std::vector<Camera> cameras_;
121118
std::vector<Track> tracks_;
122119
std::vector<Frame> frames_;
123120
std::vector<FramePair> frame_pairs_;

src/feature/feature_extraction.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "optim/loransac.h"
1515
#include "optim/ransac.h"
1616
#include "sift_extractor.h"
17-
#include "utility/io_ecim.hpp"
1817
#include "utility/timer.h"
1918

2019
namespace xrsfm {
@@ -38,7 +37,8 @@ void FeatureExtract(const std::string &image_dir_path,
3837
for (int i = 0; i < frames.size(); i++) {
3938
Frame &frame = frames[i];
4039
frame.keypoints_.clear();
41-
const cv::Mat image = cv::imread(image_dir_path + frame.name);
40+
const cv::Mat image = cv::imread(image_dir_path + frame.name,
41+
cv::IMREAD_IGNORE_ORIENTATION);
4242
if (image.rows == 0) {
4343
std::cout << "Can't read " << image_dir_path + frame.name
4444
<< std::endl;

src/feature/feature_processing.cc

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "optim/loransac.h"
1717
#include "optim/ransac.h"
1818
#include "sift_extractor.h"
19-
#include "utility/io_ecim.hpp"
2019
#include "utility/timer.h"
2120
#include "match_expansion.h"
2221

src/geometry/error_corrector.h

-10
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,11 @@ inline double ToDeg(double theta) { return theta * 180 / M_PI; }
1212

1313
class ErrorDetector {
1414
public:
15-
Init(std::string image_dir, bool debug) {
16-
debug_ = debug;
17-
image_dir_ = image_dir;
18-
};
19-
2015
bool CheckAllRelativePose(Map &map, int frame_id,
2116
std::set<int> &bad_matched_frame_ids);
2217
bool IsGoodRelativePose(const Map &map, const FramePair &fp,
2318
std::vector<char> &inlier_mask);
24-
void IsGoodRelativePose_Debug(Map &map, FramePair &fp,
25-
const std::vector<char> &inlier_mask);
26-
// void StoreRelativePose(Map &map, int frame_id, std::ofstream &file);
2719

28-
bool debug_;
29-
// ViewerThread *viewerTh_;
3020
std::string image_dir_;
3121
};
3222

src/geometry/error_detector.cc

-41
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,6 @@ bool ErrorDetector::IsGoodRelativePose(const Map &map, const FramePair &fp,
100100
return true;
101101
}
102102

103-
void ErrorDetector::IsGoodRelativePose_Debug(
104-
Map &map, FramePair &fp, const std::vector<char> &inlier_mask) {
105-
auto &frame1 = map.frames_[fp.id1];
106-
auto &frame2 = map.frames_[fp.id2];
107-
cv::Mat image1 = cv::imread(image_dir_ + frame1.name);
108-
cv::Mat image2 = cv::imread(image_dir_ + frame2.name);
109-
if (image1.empty() || image2.empty()) {
110-
printf("%s\n", (image_dir_ + frame1.name).c_str());
111-
} else {
112-
// DrawFeatureMatches(image1, image2, frame1.points, frame2.points,
113-
// fp.matches, inlier_mask);
114-
// DrawFeatureMatches1(image1, frame1.points, frame2.points, fp.matches,
115-
// inlier_mask);
116-
}
117-
// frame1.flag_for_view = frame2.flag_for_view = true;
118-
// viewer_->Draw(map, true);
119-
// frame1.flag_for_view = frame2.flag_for_view = false;
120-
frame1.flag_for_view = frame2.flag_for_view = true;
121-
// viewerTh_->update_map(map);
122-
frame1.flag_for_view = frame2.flag_for_view = false;
123-
cv::waitKey();
124-
}
125-
126103
bool ErrorDetector::CheckAllRelativePose(Map &map, int frame_id,
127104
std::set<int> &bad_matched_frame_ids) {
128105
bad_matched_frame_ids.clear();
@@ -170,7 +147,6 @@ bool ErrorDetector::CheckAllRelativePose(Map &map, int frame_id,
170147
} else {
171148
int bad_neighbor_id = fp.id1 == frame_id ? fp.id2 : fp.id1;
172149
bad_matched_frame_ids.insert(bad_neighbor_id);
173-
// if (debug_) IsGoodRelativePose_Debug(map, fp, inlier_mask);
174150
}
175151
}
176152
}
@@ -182,21 +158,4 @@ bool ErrorDetector::CheckAllRelativePose(Map &map, int frame_id,
182158
return true;
183159
}
184160

185-
// void ErrorDetector::StoreRelativePose(Map &map, int frame_id,
186-
// std::ofstream &file) {
187-
// for (const auto id : map.frameid2framepairids_[frame_id]) {
188-
// auto &fp = map.frame_pairs_[id];
189-
// const auto &frame1 = map.frames_[fp.id1];
190-
// const auto &frame2 = map.frames_[fp.id2];
191-
// if (frame1.registered && frame2.registered && frame1.is_keyframe &&
192-
// frame2.is_keyframe) {
193-
// file << id << std::endl;
194-
// file << frame1.Tcw.q.coeffs().transpose() << " "
195-
// << frame1.Tcw.t.transpose() << std::endl;
196-
// file << frame2.Tcw.q.coeffs().transpose() << " "
197-
// << frame2.Tcw.t.transpose() << std::endl;
198-
// }
199-
// }
200-
// file << "-1" << std::endl;
201-
// }
202161
} // namespace xrsfm

src/optimization/ba_solver.cc

-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ void BASolver::KGBA(Map &map, const std::vector<int> fix_key_frame_ids,
672672
ceres::Solve(solver_options, &problem, &summary);
673673
// std::cout << summary.BriefReport() << "\n";
674674
PrintSolverSummary(summary);
675-
map.Camera(0).log();
676675

677676
printf("kf: %d/%d\n", num_kf, num_rf);
678677
UpdateByRefFrame(map);

src/run_matching.cc

+17-10
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212

1313
using namespace xrsfm;
1414

15-
void GetFeatures(const std::string &images_path, const std::string &ftr_path,
16-
std::vector<Frame> &frames) {
15+
void GetFeatures(const std::string &images_path,
16+
const std::vector<std::string> &image_names,
17+
const std::string &ftr_path, std::vector<Frame> &frames) {
18+
const int num_image = image_names.size();
19+
frames.resize(num_image);
20+
for (int i = 0; i < num_image; ++i) {
21+
frames[i].id = i;
22+
frames[i].name = image_names[i];
23+
}
24+
1725
std::ifstream ftr_bin(ftr_path);
1826
if (ftr_bin.good()) {
1927
ReadFeatures(ftr_path, frames);
@@ -33,7 +41,8 @@ void GetImageSizeVec(const std::string &images_path,
3341
LoadImageSize(path, image_size);
3442
} else {
3543
for (const auto &image_name : image_names) {
36-
const cv::Mat image = cv::imread(images_path + image_name);
44+
const cv::Mat image = cv::imread(images_path + image_name,
45+
cv::IMREAD_IGNORE_ORIENTATION);
3746
image_size.push_back(ImageSize(image.cols, image.rows));
3847
}
3948
SaveImageSize(path, image_size);
@@ -173,27 +182,25 @@ int main(int argc, const char *argv[]) {
173182
const std::string fp_init_path = output_path + "fp_init.bin";
174183
const std::string fp_path = output_path + "fp.bin";
175184

176-
// 1.read imagesstd::filesystem
185+
// 1.read images
177186
if (!std::experimental::filesystem::exists(images_path)) {
178187
std::cout << "image path not exists :" << images_path << "\n";
179188
exit(-1);
180189
}
181190
std::vector<std::string> image_names;
182191
LoadImageNames(images_path, image_names);
183-
std::vector<ImageSize> image_size_vec;
184-
GetImageSizeVec(images_path, image_names, size_path, image_size_vec);
185192
std::map<std::string, int> name2id;
186193
const int num_image = image_names.size();
187-
std::vector<Frame> frames(num_image);
188194
for (int i = 0; i < num_image; ++i) {
189-
frames[i].id = i;
190-
frames[i].name = image_names[i];
191195
name2id[image_names[i]] = i;
192196
}
193197
std::cout << "Load Image Info Done.\n";
194198

195199
// 2.feature extraction
196-
GetFeatures(images_path, ftr_path, frames);
200+
std::vector<Frame> frames;
201+
std::vector<ImageSize> image_size_vec;
202+
GetFeatures(images_path, image_names, ftr_path, frames);
203+
GetImageSizeVec(images_path, image_names, size_path, image_size_vec);
197204
std::cout << "Extract Features Done.\n";
198205

199206
// 5.image matching

src/run_reconstruction.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ void PreProcess(const std::string dir_path, const std::string camera_path,
1818
std::cout << "ReadFramePairs\n";
1919

2020
// set cameras & image name
21-
Camera seq = ReadCameraIOSRecord(camera_path);
21+
std::map<int, Camera> cameras = ReadCamerasText(camera_path);
22+
CHECK_EQ(cameras.size(), 1);
23+
const int camera_id = cameras.begin()->first;
2224

2325
for (auto &frame : frames) {
24-
frame.camera_id = seq.id_;
26+
frame.camera_id = camera_id;
2527
}
2628

2729
// convert keypoint to points(for reconstruction)
@@ -36,7 +38,7 @@ void PreProcess(const std::string dir_path, const std::string camera_path,
3638
}
3739
}
3840

39-
map.camera_map_[seq.id_] = seq;
41+
map.camera_map_ = cameras;
4042
map.frames_ = frames;
4143
map.frame_pairs_ = frame_pairs;
4244

src/tag/tag_extract.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ extern "C" {
2626
#include "optimization/cost_factor_ceres.h"
2727
#include "utility/io_ecim.hpp"
2828
#include "utility/timer.h"
29-
// #include "utility/viewer.h"
3029

3130
namespace xrsfm {
3231

@@ -48,7 +47,7 @@ tag_extract(std::string image_dir) {
4847
for (auto &image_name : image_vec) {
4948
std::string img_path = image_dir + image_name;
5049
cv::Mat frame, gray;
51-
frame = cv::imread(img_path.c_str());
50+
frame = cv::imread(img_path.c_str(), cv::IMREAD_IGNORE_ORIENTATION);
5251
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
5352
image_u8_t im = {.width = gray.cols,
5453
.height = gray.rows,
@@ -90,7 +89,7 @@ tag_extract(const std::string &image_dir,
9089
for (auto &image_name : image_vec) {
9190
std::string img_path = image_dir + image_name;
9291
cv::Mat frame, gray;
93-
frame = cv::imread(img_path.c_str());
92+
frame = cv::imread(img_path.c_str(), cv::IMREAD_IGNORE_ORIENTATION);
9493
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
9594
image_u8_t im = {.width = gray.cols,
9695
.height = gray.rows,

src/utility/io_ecim.cc

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ void WritePoints3DBinary(const std::string &path,
225225
}
226226

227227
void WriteColMapDataBinary(const std::string &output_path, const Map &map) {
228+
namespace fs = std::experimental::filesystem;
229+
if (!fs::is_directory(output_path)) {
230+
fs::create_directories(output_path);
231+
}
228232
WriteCamerasBinary(output_path + "cameras.bin", map.camera_map_);
229233
WriteImagesBinary(output_path + "images.bin", map.frames_);
230234
WritePoints3DBinary(output_path + "points3D.bin", map.tracks_);

0 commit comments

Comments
 (0)