From 9c3034e7ef2844ef4b2608ea5e768dd50c8ff063 Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Thu, 2 Aug 2018 14:14:38 +0300 Subject: [PATCH] Save to png via opencv. --- cppan.yml | 1 + src/common/mat.h | 34 +++++++++++++++++++++++++++-- src/mmp_extractor/mmp.cpp | 14 +++++++----- src/mmp_extractor/mmp_extractor.cpp | 1 + 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/cppan.yml b/cppan.yml index 1eca10a..fe5101b 100644 --- a/cppan.yml +++ b/cppan.yml @@ -91,6 +91,7 @@ projects: root_dir: src/mmp_extractor dependencies: - common + - pvt.cppan.demo.intel.opencv.highgui: "*" model: type: lib diff --git a/src/common/mat.h b/src/common/mat.h index f3c43f1..1685301 100644 --- a/src/common/mat.h +++ b/src/common/mat.h @@ -79,7 +79,7 @@ public: auto end() const { return data.end(); } // left/right - mat mirror() + mat mirror() const { int cols = width; int rows = height; @@ -97,7 +97,7 @@ public: } // up/down - mat flip() + mat flip() const { int cols = width; int rows = height; @@ -114,10 +114,40 @@ public: return m; } +#ifdef HAVE_OPENCV_IMGCODECS + operator cv::Mat() const + { + return flip().toCvMat(); + } +#endif + private: std::vector data; int width; int height; + +#ifdef HAVE_OPENCV_IMGCODECS + cv::Mat toCvMat() const + { + if constexpr (std::is_same_v) + { + int cols = width; + int rows = height; + cv::Mat m(height, width, CV_8UC3); + for (int row = 0; row < rows; row++) + { + for (int col = 0; col < cols; col++) + { + auto &o = operator()(row * cols + col); + m.ptr(row)[3 * col + 2] = (o >> 16) & 0xFF; + m.ptr(row)[3 * col + 1] = (o >> 8) & 0xFF; + m.ptr(row)[3 * col + 0] = (o >> 0) & 0xFF; + } + } + return m; + } + } +#endif }; inline void write_mat_bmp(const path &filename, int width, int height, int bits, const uint8_t *b, size_t s) diff --git a/src/mmp_extractor/mmp.cpp b/src/mmp_extractor/mmp.cpp index 092d49d..d2e26a0 100644 --- a/src/mmp_extractor/mmp.cpp +++ b/src/mmp_extractor/mmp.cpp @@ -18,15 +18,17 @@ #define NOMINMAX +#include #include "mmp.h" +#include + #include #include #include +#include #include -#include - void water_segment::load(const buffer &b) { wg.load(b); @@ -355,9 +357,10 @@ void mmp::writeColorMap() void mmp::writeSplitColormap() const { - std::unordered_set colors; + std::set colors; for (auto &pixel : colormap) colors.insert(pixel); + int i = 0; for (auto &color : colors) { auto m = colormap; @@ -369,8 +372,9 @@ void mmp::writeSplitColormap() const ss.fill('0'); ss.width(8); ss << std::hex << std::uppercase << color; - fn += ".colormap." + ss.str() + ".bmp"; - write_mat_bmp(fn, m); + fn += ".colormap." + ss.str() + ".png"; + std::cout << "\r[" << ++i << "/" << colors.size() << "] Processing color " << ss.str(); + cv::imwrite(fn.u8string(), cv::Mat(m)); } } diff --git a/src/mmp_extractor/mmp_extractor.cpp b/src/mmp_extractor/mmp_extractor.cpp index e212824..b08fa0b 100644 --- a/src/mmp_extractor/mmp_extractor.cpp +++ b/src/mmp_extractor/mmp_extractor.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #include "mmp.h" #include