1#include "opencvReading.h"
57int cvdepth_from_bit_depth(
int bit_depth)
59 if (bit_depth >= 1 && bit_depth <= 8)
61 else if (bit_depth >= 9 && bit_depth <= 16)
63 else if (bit_depth == 32)
65 else throw std::invalid_argument(
"invalid raw image bit depth");
74void read_raw(std::ifstream& stream, cv::Mat image)
76 CV_Assert(stream.good() && !image.empty() && image.isContinuous());
77 stream.read(
reinterpret_cast<char*
>(image.data), image.size().area() * image.elemSize());
88cv::Mat read_color_YUV(std::string filepath,
int frame,
rvs::Parameters const& parameters) {
91 auto type = CV_MAKETYPE(cvdepth_from_bit_depth(bit_depth), 1);
92 cv::Mat y_channel(size, type);
93 cv::Mat cb_channel(size / 2, type);
94 cv::Mat cr_channel(size / 2, type);
96 std::ifstream stream(filepath, std::ios::binary);
98 std::ostringstream what;
99 what <<
"Failed to read raw YUV color file \"" << filepath <<
"\"";
100 throw std::runtime_error(what.str());
102 stream.seekg(size.area() * y_channel.elemSize() * 3 / 2 * frame);
103 read_raw(stream, y_channel);
104 read_raw(stream, cb_channel);
105 read_raw(stream, cr_channel);
107 cv::resize(cb_channel, cb_channel, size, 0, 0, cv::INTER_CUBIC);
108 cv::resize(cr_channel, cr_channel, size, 0, 0, cv::INTER_CUBIC);
110 cv::Mat image(size, CV_MAKETYPE(cvdepth_from_bit_depth(bit_depth), 3));
111 cv::Mat src[] = { y_channel, cr_channel, cb_channel };
112 cv::merge(src, 3, image);
123cv::Mat read_color_RGB(std::string filepath,
rvs::Parameters const& parameters) {
124 cv::Mat image = cv::imread(filepath, cv::IMREAD_UNCHANGED);
127 throw std::runtime_error(
"Failed to read color file");
129 throw std::runtime_error(
"Color file does not have the expected size");
131 throw std::runtime_error(
"Color file has wrong bit depth");
132 if (image.channels() != 3)
133 throw std::runtime_error(
"Color file has wrong number of channels");
144unsigned max_level(
int bit_depth)
146 assert(bit_depth > 0 && bit_depth <= 16);
147 return (1u << bit_depth) - 1u;
157vk::Format selectFormat(cv::Mat& image, rvs::detail::ColorSpace colorSpace) {
159 vk::Format form = vk::Format::eUndefined;
160 if (colorSpace == rvs::detail::ColorSpace::RGB) {
161 if (image.depth() == 8) {
162 if (image.channels() == 4) {
163 form = vk::Format::eR8G8B8A8Unorm;
164 }
else if (image.channels() == 3) {
165 form = vk::Format::eR8G8B8Unorm;
167 else if (image.channels() == 2) {
168 form = vk::Format::eR8G8Unorm;
170 else if (image.channels() == 1) {
171 form = vk::Format::eR8Unorm;
174 else if (image.depth() == 16 || image.depth() == 10) {
176 if (image.channels() == 4) {
177 form = vk::Format::eR16G16B16A16Unorm;
179 else if (image.channels() == 3) {
180 form = vk::Format::eR16G16B16Unorm;
182 else if (image.channels() == 2) {
183 form = vk::Format::eR16G16Unorm;
185 else if (image.channels() == 1) {
186 form = vk::Format::eR16Unorm;
189 else if (image.depth() == 32) {
190 if (image.channels() == 4) {
191 form = vk::Format::eR32G32B32A32Sfloat;
193 else if (image.channels() == 3) {
194 form = vk::Format::eR32G32B32Sfloat;
196 else if (image.channels() == 2) {
197 form = vk::Format::eR32G32Sfloat;
199 else if (image.channels() == 1) {
200 form = vk::Format::eR32Sfloat;
215cv::Mat read_color(std::string filepath,
int frame,
rvs::Parameters const& parameters)
220 rvs::detail::ColorSpace color_space;
221 if (filepath.substr(filepath.size() - 4, 4) ==
".yuv") {
222 image = read_color_YUV(filepath, frame, parameters);
223 color_space = rvs::detail::ColorSpace::YUV;
225 else if (frame == 0) {
226 image = read_color_RGB(filepath, parameters);
227 color_space = rvs::detail::ColorSpace::RGB;
230 throw std::runtime_error(
"Readig multiple frames not (yet) supported for image files");
248 cv::Mat color = image;
251 if (color_space == rvs::detail::ColorSpace::YUV && rvs::detail::g_color_space == rvs::detail::ColorSpace::RGB) {
253 image.convertTo(color, CV_16U, 64);
255 cv::cvtColor(color, color, cv::COLOR_YCrCb2RGB);
256 cv::cvtColor(color, color, cv::COLOR_RGB2RGBA);
259 else if (color_space == rvs::detail::ColorSpace::RGB && rvs::detail::g_color_space == rvs::detail::ColorSpace::YUV) {
260 cv::cvtColor(color, color, cv::COLOR_BGR2YCrCb);
274cv::Mat read_depth_RGB(std::string filepath,
rvs::Parameters const& parameters) {
275 cv::Mat image = cv::imread(filepath, cv::IMREAD_UNCHANGED);
278 throw std::runtime_error(
"Failed to read depth file");
280 throw std::runtime_error(
"Depth file does not have the expected size");
282 throw std::runtime_error(
"Depth file has the wrong bit depth");
283 if (image.channels() != 1)
284 throw std::runtime_error(
"Depth file has the wrong number of channels");
297cv::Mat read_depth_YUV(std::string filepath,
int frame,
rvs::Parameters const& parameters) {
300 cv::Mat image(size, CV_MAKETYPE(cvdepth_from_bit_depth(bit_depth), 1));
301 std::ifstream stream(filepath, std::ios_base::binary);
302 if (!stream.good()) {
303 std::ostringstream what;
304 what <<
"Failed to read raw YUV depth file \"" << filepath <<
"\"";
305 throw std::runtime_error(what.str());
309 case rvs::ColorFormat::YUV420:
310 stream.seekg(size.area() * image.elemSize() * 3 / 2 * frame);
312 case rvs::ColorFormat::YUV400:
313 stream.seekg(size.area() * image.elemSize() * frame);
316 throw std::logic_error(
"Unknown depth map color format");
319 read_raw(stream, image);
331cv::Mat read_depth(std::string filepath,
int frame,
rvs::Parameters const& parameters)
336 if (filepath.substr(filepath.size() - 4, 4) ==
".yuv") {
337 image = read_depth_YUV(filepath, frame, parameters);
339 else if (frame == 0) {
340 image = read_depth_RGB(filepath, parameters);
343 throw std::runtime_error(
"Readig multiple frames not (yet) supported for image files");
375 image.convertTo(image, CV_16U, 64);
382 auto const NaN = std::numeric_limits<float>::quiet_NaN();
383 image.setTo(NaN, image == 0);
cv::Rect getCropRegion() const
int getColorBitDepth() const
bool hasInvalidDepth() const
cv::Size getPaddedSize() const
ColorFormat getDepthColorFormat() const
int getDepthBitDepth() const