Skip to content

Commit fb0bea5

Browse files
authored
Merge pull request weigao95#8 from Algomorph/master
Add a GenericFileFetch for more generic files that implements FetchInterface
2 parents e8ebf52 + 997d494 commit fb0bea5

14 files changed

+277
-28
lines changed

apps/crf_test/main.cu

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <iostream>
22
#include "common/ConfigParser.h"
33
#include "visualization/Visualizer.h"
4-
#include "imgproc/FileFetch.h"
4+
#include "imgproc/frameio/VolumeDeformFileFetch.h"
55
#include "imgproc/ImageProcessor.h"
66
#include "imgproc/segmentation/ForegroundSegmenterNaiveCRF.h"
77

apps/gpc_test/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "common/ConfigParser.h"
77
#include "common/data_transfer.h"
88
#include "visualization/Visualizer.h"
9-
#include "imgproc/FileFetch.h"
9+
#include "imgproc/frameio/VolumeDeformFileFetch.h"
1010
#include "imgproc/ImageProcessor.h"
1111
#include "imgproc/correspondence/ImagePairCorrespondence.h"
1212
#include "imgproc/correspondence/PatchColliderRGBCorrespondence.h"

apps/imgproc_app/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Created by wei on 5/29/18.
33
//
44
#include "common/ConfigParser.h"
5-
#include "imgproc/FileFetch.h"
5+
#include "imgproc/frameio/VolumeDeformFileFetch.h"
66
#include "imgproc/ImageProcessor.h"
77
#include "visualization/Visualizer.h"
88

apps/imgproc_test/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "common/ConfigParser.h"
44
#include "common/CameraObservation.h"
55
#include "visualization/Visualizer.h"
6-
#include "imgproc/FileFetch.h"
6+
#include "imgproc/frameio/VolumeDeformFileFetch.h"
77
#include "imgproc/ImageProcessor.h"
88
#include <iostream>
99
#include <opencv2/opencv.hpp>

apps/render_test/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "common/Constants.h"
66
#include "common/ConfigParser.h"
77
#include "imgproc/ImageProcessor.h"
8-
#include "imgproc/FileFetch.h"
8+
#include "imgproc/frameio/VolumeDeformFileFetch.h"
99
#include "core/SurfelGeometry.h"
1010
#include "core/WarpField.h"
1111
#include "core/render/Renderer.h"

core/SurfelWarpSerial.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
#include "core/warp_solver/WarpSolver.h"
99
#include "core/geometry/SurfelNodeDeformer.h"
1010
#include "visualization/Visualizer.h"
11-
11+
#include "imgproc/frameio/FetchInterface.h"
12+
#include "imgproc/frameio/GenericFileFetch.h"
1213

1314
#include <thread>
1415
#include <fstream>
1516

16-
1717
surfelwarp::SurfelWarpSerial::SurfelWarpSerial() {
1818
//The config is assumed to be updated
1919
const auto& config = ConfigParser::Instance();
2020

2121
//Construct the image processor
22-
FileFetch::Ptr fetcher = std::make_shared<FileFetch>(config.data_path());
22+
FetchInterface::Ptr fetcher = std::make_shared<GenericFileFetch>(config.data_path());
2323
m_image_processor = std::make_shared<ImageProcessor>(fetcher);
2424

2525
//Construct the holder for surfel geometry

core/SurfelWarpSerial.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "common/Constants.h"
88
#include "common/ConfigParser.h"
99
#include "imgproc/ImageProcessor.h"
10-
#include "imgproc/FileFetch.h"
10+
#include "imgproc/frameio/VolumeDeformFileFetch.h"
1111
#include "core/SurfelGeometry.h"
1212
#include "core/WarpField.h"
1313
#include "core/Camera.h"

imgproc/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
file(GLOB IMGPROC_SRC *.h *.hpp *.cpp *.cu)
2+
file(GLOB IMGPROC_FRAMEIO_SRC frameio/*.h frameio/*.hpp frameio/*.cpp frameio/*.cu)
3+
source_group("frameio" FILES ${IMGPROC_FRAMEIO_SRC})
24
source_group("src" FILES ${IMGPROC_SRC})
3-
cuda_add_library(imgproc_lib ${IMGPROC_SRC})
5+
cuda_add_library(imgproc_lib ${IMGPROC_SRC} ${IMGPROC_FRAMEIO_SRC})
46
target_link_libraries(imgproc_lib math_lib)
57
target_link_libraries(imgproc_lib common_lib)
68
target_link_libraries(imgproc_lib ${OpenCV_LIBS})

imgproc/ImageProcessor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "common/DeviceBufferArray.h"
1212
#include "common/CameraObservation.h"
1313
#include "math/device_mat.h"
14-
#include "imgproc/FetchInterface.h"
14+
#include "imgproc/frameio/FetchInterface.h"
1515
#include "imgproc/segmentation/ForegroundSegmenter.h"
1616
#include "imgproc/correspondence/ImagePairCorrespondence.h"
1717

File renamed without changes.

imgproc/frameio/GenericFileFetch.cpp

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// ================================================================
2+
// Created by Gregory Kramida on 9/23/19.
3+
// Copyright (c) 2019 Gregory Kramida
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// ================================================================
16+
17+
#include "GenericFileFetch.h"
18+
#include "common/OpenCV_CrossPlatform.h"
19+
20+
#include <opencv2/core.hpp>
21+
#include <boost/filesystem/operations.hpp>
22+
#include <exception>
23+
#include <regex>
24+
#include <iterator>
25+
#include <common/logging.h>
26+
27+
28+
namespace fs = boost::filesystem;
29+
30+
surfelwarp::GenericFileFetch::GenericFileFetch(const fs::path& data_path, std::string extension, bool force_no_masks) :
31+
m_mask_buffer_ix(SIZE_MAX), m_use_masks(false)
32+
{
33+
std::vector<path> sorted_paths;
34+
if (extension.length() > 0 && extension[0] != '.') {
35+
extension = "." + extension;
36+
}
37+
std::copy(fs::directory_iterator(data_path), fs::directory_iterator(), std::back_inserter(sorted_paths));
38+
std::sort(sorted_paths.begin(), sorted_paths.end());
39+
bool unexpected_mask_frame_number = false;
40+
for (auto& path : sorted_paths) {
41+
if (FilenameIndicatesDepthImage(path.filename().string(), extension)) {
42+
int frame_number = GetFrameNumber(path);
43+
if (frame_number != m_depth_image_paths.size()) {
44+
throw std::runtime_error("Unexpected depth frame number encountered");
45+
}
46+
m_depth_image_paths.push_back(path);
47+
} else if (FilenameIndicatesRGBImage(path.filename().string(), extension)) {
48+
int frame_number = GetFrameNumber(path);
49+
if (frame_number != m_rgb_image_paths.size()) {
50+
throw std::runtime_error("Unexpected RGB frame number encountered");
51+
}
52+
m_rgb_image_paths.push_back(path);
53+
} else if (!force_no_masks && FilenameIndicatesMaskImage(path.filename().string(), extension)) {
54+
int frame_number = GetFrameNumber(path);
55+
if (frame_number != m_mask_image_paths.size()) {
56+
unexpected_mask_frame_number = true;
57+
}
58+
m_mask_image_paths.push_back(path);
59+
}
60+
}
61+
62+
if (m_depth_image_paths.size() != m_rgb_image_paths.size()) {
63+
LOG(FATAL) << "Presumed depth image count (" << m_depth_image_paths.size() <<
64+
") doesn't equal presumed rgb image count." << m_rgb_image_paths.size();
65+
}
66+
67+
if (!force_no_masks) {
68+
if (unexpected_mask_frame_number) {
69+
LOG(WARNING)
70+
<< "Warning: inconsistent mask frame numbers encountered in the filenames. Proceeding without masks.";
71+
} else if (!m_mask_image_paths.empty()) {
72+
if (m_depth_image_paths.size() != m_mask_image_paths.size() && !m_mask_image_paths.empty()) {
73+
LOG(WARNING)
74+
<< "Warning: seems like there were some mask image files, but their number doesn't match the "
75+
"number of depth frames. Proceeding without masks.";
76+
} else {
77+
m_use_masks = true;
78+
}
79+
}
80+
}
81+
}
82+
83+
bool surfelwarp::GenericFileFetch::HasSubstringFromSet(const std::string& string, const std::string* set, int set_size)
84+
{
85+
bool found_indicator = false;
86+
for (int i_target_string = 0; i_target_string < set_size; i_target_string++) {
87+
if (string.find(set[i_target_string]) != std::string::npos) {
88+
return true;
89+
}
90+
}
91+
return false;
92+
}
93+
94+
bool surfelwarp::GenericFileFetch::FilenameIndicatesDepthImage(const path& filename, const std::string& valid_extension)
95+
{
96+
if (filename.extension() != valid_extension) return false;
97+
const std::array<std::string, 3> possible_depth_indicators = {"depth", "DEPTH", "Depth"};
98+
return HasSubstringFromSet(filename.string(), possible_depth_indicators.data(), possible_depth_indicators.size());
99+
}
100+
101+
bool surfelwarp::GenericFileFetch::FilenameIndicatesRGBImage(const path& filename, const std::string& valid_extension)
102+
{
103+
if (filename.extension() != valid_extension) return false;
104+
const std::array<std::string, 5> possible_depth_indicators = {"color", "COLOR", "Color", "rgb", "RGB"};
105+
return HasSubstringFromSet(filename.string(), possible_depth_indicators.data(), possible_depth_indicators.size());
106+
}
107+
108+
109+
bool surfelwarp::GenericFileFetch::FilenameIndicatesMaskImage(const surfelwarp::GenericFileFetch::path& filename,
110+
const std::string& valid_extension)
111+
{
112+
if (filename.extension() != valid_extension) return false;
113+
const std::array<std::string, 3> possible_depth_indicators = {"mask", "Mask", "MASK"};
114+
return HasSubstringFromSet(filename.string(), possible_depth_indicators.data(), possible_depth_indicators.size());
115+
}
116+
117+
int surfelwarp::GenericFileFetch::GetFrameNumber(const surfelwarp::GenericFileFetch::path& filename)
118+
{
119+
const std::regex digits_regex("\\d+");
120+
std::smatch match_result;
121+
const std::string filename_stem = filename.stem().string();
122+
if (!std::regex_search(filename_stem, match_result, digits_regex)) {
123+
throw std::runtime_error("Could not find frame number in filename.");
124+
};
125+
return std::stoi(match_result.str(0));
126+
}
127+
128+
void surfelwarp::GenericFileFetch::FetchDepthImage(size_t frame_idx, cv::Mat& depth_img)
129+
{
130+
path file_path = this->m_depth_image_paths[frame_idx];
131+
//Read the image
132+
depth_img = cv::imread(file_path.string(), CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
133+
134+
if (this->m_use_masks) {
135+
mask_mutex.lock();
136+
// Apply mask to image
137+
if (this->m_mask_buffer_ix != frame_idx) {
138+
m_mask_image_buffer = cv::imread(this->m_mask_image_paths[frame_idx].string(),
139+
CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
140+
m_mask_buffer_ix = frame_idx;
141+
}
142+
cv::Mat masked;
143+
depth_img.copyTo(masked, m_mask_image_buffer);
144+
mask_mutex.unlock();
145+
depth_img = masked;
146+
}
147+
}
148+
149+
void surfelwarp::GenericFileFetch::FetchDepthImage(size_t frame_idx, void* depth_img)
150+
{
151+
152+
}
153+
154+
void surfelwarp::GenericFileFetch::FetchRGBImage(size_t frame_idx, cv::Mat& rgb_img)
155+
{
156+
path file_path = this->m_rgb_image_paths[frame_idx];
157+
//Read the image
158+
rgb_img = cv::imread(file_path.string(), CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
159+
if (this->m_use_masks) {
160+
mask_mutex.lock();
161+
if (this->m_mask_buffer_ix != frame_idx) {
162+
m_mask_image_buffer = cv::imread(this->m_mask_image_paths[frame_idx].string(),
163+
CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
164+
m_mask_buffer_ix = frame_idx;
165+
}
166+
cv::Mat masked;
167+
rgb_img.copyTo(masked, m_mask_image_buffer);
168+
mask_mutex.unlock();
169+
rgb_img = masked;
170+
}
171+
}
172+
173+
void surfelwarp::GenericFileFetch::FetchRGBImage(size_t frame_idx, void* rgb_img)
174+
{
175+
176+
}

imgproc/frameio/GenericFileFetch.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// ================================================================
2+
// Created by Gregory Kramida on 9/23/19.
3+
// Copyright (c) 2019 Gregory Kramida
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// ================================================================
16+
#pragma once
17+
18+
#include "FetchInterface.h"
19+
20+
#include <boost/filesystem/path.hpp>
21+
#include <vector>
22+
#include <mutex>
23+
24+
namespace surfelwarp
25+
{
26+
/**
27+
* \brief Utility for fetching depth & RGB frames from images on disk, from the directory specified at construction.
28+
* \details The image filenames must include 0-based index, there must be the same number of RGB and depth frames,
29+
* RGB filenames must include one of {RGB, color, Color, COLOR, rgb} in the filename,
30+
* depth filenames must include one of {depth, DEPTH, Depth} in the filename.
31+
* If the source directory also contains a matching number of likewise-indexed mask images, with each filename
32+
* including one of {mask, Mask, MASK}, the masks are applied to both the RGB and depth images in memory as part
33+
* of the fetch operation.
34+
*/
35+
class GenericFileFetch : public FetchInterface
36+
{
37+
public:
38+
using Ptr = std::shared_ptr<GenericFileFetch>;
39+
using path = boost::filesystem::path;
40+
41+
explicit GenericFileFetch(const path& data_path, std::string file_extension = ".png", bool force_no_masks = false);
42+
~GenericFileFetch() override = default;
43+
44+
//Buffer may be maintained outside fetch object for thread safety
45+
void FetchDepthImage(size_t frame_idx, cv::Mat& depth_img) override;
46+
void FetchDepthImage(size_t frame_idx, void* depth_img) override;
47+
48+
//Should be rgb, in CV_8UC3 format
49+
void FetchRGBImage(size_t frame_idx, cv::Mat& rgb_img) override;
50+
void FetchRGBImage(size_t frame_idx, void* rgb_img) override;
51+
52+
private:
53+
static int GetFrameNumber(const path& filename);
54+
static bool HasSubstringFromSet(const std::string& string, const std::string* set, int set_size);
55+
static bool FilenameIndicatesDepthImage(const path& filename, const std::string& valid_extension);
56+
static bool FilenameIndicatesRGBImage(const path& filename, const std::string& valid_extension);
57+
static bool FilenameIndicatesMaskImage(const path& filename, const std::string& valid_extension);
58+
59+
std::vector<path> m_rgb_image_paths;
60+
std::vector<path> m_depth_image_paths;
61+
std::vector<path> m_mask_image_paths;
62+
63+
size_t m_mask_buffer_ix;
64+
cv::Mat m_mask_image_buffer;
65+
bool m_use_masks;
66+
std::mutex mask_mutex;
67+
};
68+
} // end namespace surfelwarp
69+

imgproc/FileFetch.cpp imgproc/frameio/VolumeDeformFileFetch.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#include <opencv2/opencv.hpp>
22
#include "common/OpenCV_CrossPlatform.h"
3-
#include "imgproc/FileFetch.h"
3+
#include "VolumeDeformFileFetch.h"
44

5-
void surfelwarp::FileFetch::FetchDepthImage(size_t frame_idx, cv::Mat & depth_img)
5+
void surfelwarp::VolumeDeformFileFetch::FetchDepthImage(size_t frame_idx, cv::Mat & depth_img)
66
{
77
path file_path = FileNameSurfelWarp(frame_idx, true);
88
//Read the image
99
depth_img = cv::imread(file_path.string(), CV_ANYCOLOR | CV_ANYDEPTH);
1010
}
1111

12-
void surfelwarp::FileFetch::FetchDepthImage(size_t frame_idx, void * depth_img)
12+
void surfelwarp::VolumeDeformFileFetch::FetchDepthImage(size_t frame_idx, void * depth_img)
1313
{
1414

1515
}
1616

17-
void surfelwarp::FileFetch::FetchRGBImage(size_t frame_idx, cv::Mat & rgb_img)
17+
void surfelwarp::VolumeDeformFileFetch::FetchRGBImage(size_t frame_idx, cv::Mat & rgb_img)
1818
{
1919
path file_path = FileNameSurfelWarp(frame_idx, false);
2020
//Read the image
2121
rgb_img = cv::imread(file_path.string(), CV_ANYCOLOR | CV_ANYDEPTH);
2222
}
2323

24-
void surfelwarp::FileFetch::FetchRGBImage(size_t frame_idx, void * rgb_img)
24+
void surfelwarp::VolumeDeformFileFetch::FetchRGBImage(size_t frame_idx, void * rgb_img)
2525
{
2626

2727
}
2828

29-
boost::filesystem::path surfelwarp::FileFetch::FileNameVolumeDeform(size_t frame_idx, bool is_depth_img) const
29+
boost::filesystem::path surfelwarp::VolumeDeformFileFetch::FileNameVolumeDeform(size_t frame_idx, bool is_depth_img) const
3030
{
3131
//Construct the file_name
3232
char frame_idx_str[20];
33-
sprintf(frame_idx_str, "%06d", frame_idx);
33+
sprintf(frame_idx_str, "%06d", static_cast<int>(frame_idx));
3434
std::string file_name = "frame-";
3535
file_name += std::string(frame_idx_str);
3636
if (is_depth_img) {
@@ -48,6 +48,6 @@ boost::filesystem::path surfelwarp::FileFetch::FileNameVolumeDeform(size_t frame
4848

4949

5050

51-
boost::filesystem::path surfelwarp::FileFetch::FileNameSurfelWarp(size_t frame_idx, bool is_depth_img) const {
51+
boost::filesystem::path surfelwarp::VolumeDeformFileFetch::FileNameSurfelWarp(size_t frame_idx, bool is_depth_img) const {
5252
return FileNameVolumeDeform(frame_idx, is_depth_img);
5353
}

0 commit comments

Comments
 (0)